#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* memcpy */
#include <math.h>
#include <stdint.h>

void *cuda_upload_var(void *host_var, int size)
{
	void *cuda_var;
	cudaMalloc(&cuda_var, 4);
	cudaMemcpy(cuda_var, host_var, size, cudaMemcpyHostToDevice);
	return cuda_var;
}
void cuda_download_var(void *cuda_var, void *host_var, int size)
{
	cudaMemcpy(host_var, cuda_var, size, cudaMemcpyDeviceToHost);
	cudaFree(cuda_var);
}

/* Extended C syntax test */

/**********************/

/* BLOCK COMMENT TEST */

/**********************/

int aaaaa; /* Comment explaining 'aaaaa' */ /* See how comments are preserved in the output */

char bbbbbb;

typedef struct Aggregate2
{

    int c;
} Aggregate2;

typedef struct Aggregate
{

    int a;

    int b;

    Aggregate2 c;
} Aggregate;

/* These will be in global scope in the output code */

typedef struct LocalType
{

    int foo;
} LocalType;

void local_func(int p)
{

    LocalType bug_test; /* Just to test that identifier lookup works */
    bug_test.foo = 123;
    aaaaa = bug_test.foo + p;
}
int main(int argc, const char **argv)
{

    int temp_var;

    int i;

    Aggregate foo1 = {
        0,
        1,
        { 2 }
    };
    temp_var = 1 + 2*3;
    local_func(10);

    {

        int test;
        test = 5;
        aaaaa = test;
    }

    if (1) {

        /* Foo */

        for (i = 0; i < 10; i = i + 1) {
            temp_var = temp_var + 1;
        }
    } else if (2) {

        for (i = 0; i < 10; i = i + 1) 
            ;
    } else {

        /* Bar */

        if (1) {
            i = 2;
        }

        while (i) {
            i = i - 1;
        }
    }

    return foo1.a;
}


	.file	"tmpxft_0028fd73_00000000-6_syntax.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2033:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2033:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z15cuda_upload_varPvi
	.type	_Z15cuda_upload_varPvi, @function
_Z15cuda_upload_varPvi:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rdi, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	movl	%esi, %ebx
	movl	$4, %esi
	subq	$24, %rsp
	.cfi_def_cfa_offset 48
	movq	%fs:40, %rax
	movq	%rax, 8(%rsp)
	xorl	%eax, %eax
	movq	%rsp, %rdi
	call	cudaMalloc@PLT
	movq	(%rsp), %rdi
	movslq	%ebx, %rdx
	movq	%rbp, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movq	(%rsp), %rax
	movq	8(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L3
	call	__stack_chk_fail@PLT
.L3:
	addq	$24, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z15cuda_upload_varPvi, .-_Z15cuda_upload_varPvi
	.globl	_Z17cuda_download_varPvS_i
	.type	_Z17cuda_download_varPvS_i, @function
_Z17cuda_download_varPvS_i:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rdi, %rbx
	movslq	%edx, %rdx
	movq	%rsi, %rdi
	movl	$2, %ecx
	movq	%rbx, %rsi
	call	cudaMemcpy@PLT
	movq	%rbx, %rdi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	cudaFree@PLT
	.cfi_endproc
.LFE2028:
	.size	_Z17cuda_download_varPvS_i, .-_Z17cuda_download_varPvS_i
	.globl	_Z10local_funci
	.type	_Z10local_funci, @function
_Z10local_funci:
.LFB2029:
	.cfi_startproc
	endbr64
	addl	$123, %edi
	movl	%edi, aaaaa(%rip)
	ret
	.cfi_endproc
.LFE2029:
	.size	_Z10local_funci, .-_Z10local_funci
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2030:
	.cfi_startproc
	endbr64
	movl	$5, aaaaa(%rip)
	xorl	%eax, %eax
	ret
	.cfi_endproc
.LFE2030:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2056:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	movq	%rax, %rdi
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2056:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.globl	bbbbbb
	.bss
	.type	bbbbbb, @object
	.size	bbbbbb, 1
bbbbbb:
	.zero	1
	.globl	aaaaa
	.align 4
	.type	aaaaa, @object
	.size	aaaaa, 4
aaaaa:
	.zero	4
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:


#include <hip/hip_runtime.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* memcpy */
#include <math.h>
#include <stdint.h>

void *cuda_upload_var(void *host_var, int size)
{
	void *cuda_var;
	hipMalloc(&cuda_var, 4);
	hipMemcpy(cuda_var, host_var, size, hipMemcpyHostToDevice);
	return cuda_var;
}
void cuda_download_var(void *cuda_var, void *host_var, int size)
{
	hipMemcpy(host_var, cuda_var, size, hipMemcpyDeviceToHost);
	hipFree(cuda_var);
}

/* Extended C syntax test */

/**********************/

/* BLOCK COMMENT TEST */

/**********************/

int aaaaa; /* Comment explaining 'aaaaa' */ /* See how comments are preserved in the output */

char bbbbbb;

typedef struct Aggregate2
{

    int c;
} Aggregate2;

typedef struct Aggregate
{

    int a;

    int b;

    Aggregate2 c;
} Aggregate;

/* These will be in global scope in the output code */

typedef struct LocalType
{

    int foo;
} LocalType;

void local_func(int p)
{

    LocalType bug_test; /* Just to test that identifier lookup works */
    bug_test.foo = 123;
    aaaaa = bug_test.foo + p;
}
int main(int argc, const char **argv)
{

    int temp_var;

    int i;

    Aggregate foo1 = {
        0,
        1,
        { 2 }
    };
    temp_var = 1 + 2*3;
    local_func(10);

    {

        int test;
        test = 5;
        aaaaa = test;
    }

    if (1) {

        /* Foo */

        for (i = 0; i < 10; i = i + 1) {
            temp_var = temp_var + 1;
        }
    } else if (2) {

        for (i = 0; i < 10; i = i + 1) 
            ;
    } else {

        /* Bar */

        if (1) {
            i = 2;
        }

        while (i) {
            i = i - 1;
        }
    }

    return foo1.a;
}


	.text
	.file	"syntax.hip"
	.globl	_Z15cuda_upload_varPvi          # -- Begin function _Z15cuda_upload_varPvi
	.type	_Z15cuda_upload_varPvi,@function
_Z15cuda_upload_varPvi:                 # @_Z15cuda_upload_varPvi
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	subq	$16, %rsp
	.cfi_def_cfa_offset 48
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movl	%esi, %ebx
	movq	%rdi, %r14
	leaq	8(%rsp), %r15
	movl	$4, %esi
	movq	%r15, %rdi
	callq	hipMalloc
	movq	(%r15), %rdi
	movslq	%ebx, %rdx
	movq	%r14, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movq	(%r15), %rax
	addq	$16, %rsp
	.cfi_def_cfa_offset 32
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z15cuda_upload_varPvi, .Lfunc_end0-_Z15cuda_upload_varPvi
	.cfi_endproc
                                        # -- End function
	.globl	_Z17cuda_download_varPvS_i      # -- Begin function _Z17cuda_download_varPvS_i
	.type	_Z17cuda_download_varPvS_i,@function
_Z17cuda_download_varPvS_i:             # @_Z17cuda_download_varPvS_i
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movq	%rdi, %rbx
	movslq	%edx, %rdx
	movq	%rsi, %rdi
	movq	%rbx, %rsi
	movl	$2, %ecx
	callq	hipMemcpy
	movq	%rbx, %rdi
	popq	%rbx
	.cfi_def_cfa_offset 8
	jmp	hipFree                         # TAILCALL
.Lfunc_end1:
	.size	_Z17cuda_download_varPvS_i, .Lfunc_end1-_Z17cuda_download_varPvS_i
	.cfi_endproc
                                        # -- End function
	.globl	_Z10local_funci                 # -- Begin function _Z10local_funci
	.type	_Z10local_funci,@function
_Z10local_funci:                        # @_Z10local_funci
	.cfi_startproc
# %bb.0:
	addl	$123, %edi
	movl	%edi, aaaaa(%rip)
	retq
.Lfunc_end2:
	.size	_Z10local_funci, .Lfunc_end2-_Z10local_funci
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	movl	$5, aaaaa(%rip)
	xorl	%eax, %eax
	retq
.Lfunc_end3:
	.size	main, .Lfunc_end3-main
	.cfi_endproc
                                        # -- End function
	.type	aaaaa,@object                   # @aaaaa
	.bss
	.globl	aaaaa
	.p2align	2, 0x0
aaaaa:
	.long	0                               # 0x0
	.size	aaaaa, 4

	.type	bbbbbb,@object                  # @bbbbbb
	.globl	bbbbbb
bbbbbb:
	.byte	0                               # 0x0
	.size	bbbbbb, 1

	.type	__hip_cuid_,@object             # @__hip_cuid_
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_

	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.amdgpu_metadata
---
amdhsa.kernels:  []
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
