/*
 *
 *  test.c
 *       tim.burgess@noaa.gov
 *
 *  A place for trying out various code
 */

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <cuda.h>

#define NPIXELS 100

static char daytab[2][13] = {
  {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
  {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};

int day_of_year(int year, int month, int day);

unsigned char *landmask_d;

int main() {
  // report on free mem
//  checkCUDAMemory();

  // allocate device memory for landmask
  size_t landMemSize = NPIXELS * sizeof(char);
  cudaMalloc((void **)&landmask_d, landMemSize);
  printf("allocating %ld device Kbytes for landmask\n", landMemSize/1024);
//  checkCUDAError("memory allocation");

  cudaFree(landmask_d);
  

  //printf("day of year:%d\n", day_of_year(2010, 8, 9) );

  return 0;
}


int day_of_year(int year, int month, int day) {

  int i, leap;


  leap = year%4 == 0 && (year%100 != 0 || year%400 == 0);

  for (i = 1; i < month; i++) 
    day += daytab[leap][i];

  return day;
}

	.file	"tmpxft_00386b88_00000000-6_hack.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2031:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2031:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"allocating %ld device Kbytes for landmask\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	movl	$100, %esi
	leaq	landmask_d(%rip), %rdi
	call	cudaMalloc@PLT
	xorl	%edx, %edx
	leaq	.LC0(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movq	landmask_d(%rip), %rdi
	call	cudaFree@PLT
	xorl	%eax, %eax
	popq	%rdx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	main, .-main
	.text
	.globl	_Z11day_of_yeariii
	.type	_Z11day_of_yeariii, @function
_Z11day_of_yeariii:
.LFB2028:
	.cfi_startproc
	endbr64
	movl	%esi, %r8d
	xorl	%ecx, %ecx
	movl	%edx, %esi
	testl	$3, %edi
	jne	.L5
	movl	%edi, %eax
	movl	$100, %ecx
	cltd
	idivl	%ecx
	movb	$1, %cl
	testl	%edx, %edx
	jne	.L5
	movl	%edi, %eax
	movl	$400, %ecx
	cltd
	idivl	%ecx
	testl	%edx, %edx
	sete	%cl
.L5:
	movzbl	%cl, %ecx
	leaq	_ZL6daytab(%rip), %rax
	xorl	%edx, %edx
	negq	%rcx
	andl	$13, %ecx
	addq	%rax, %rcx
	movl	%esi, %eax
.L6:
	incq	%rdx
	cmpl	%edx, %r8d
	jle	.L11
	movsbl	(%rcx,%rdx), %esi
	addl	%esi, %eax
	jmp	.L6
.L11:
	ret
	.cfi_endproc
.LFE2028:
	.size	_Z11day_of_yeariii, .-_Z11day_of_yeariii
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2054:
	.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
.LFE2054:
	.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	landmask_d
	.bss
	.align 8
	.type	landmask_d, @object
	.size	landmask_d, 8
landmask_d:
	.zero	8
	.section	.rodata
	.align 16
	.type	_ZL6daytab, @object
	.size	_ZL6daytab, 26
_ZL6daytab:
	.string	""
	.ascii	"\037\034\037\036\037\036\037\037\036\037\036\037"
	.string	""
	.ascii	"\037\035\037\036\037\036\037\037\036\037\036\037"
	.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:

/*
 *
 *  test.c
 *       tim.burgess@noaa.gov
 *
 *  A place for trying out various code
 */

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <hip/hip_runtime.h>

#define NPIXELS 100

static char daytab[2][13] = {
  {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
  {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};

int day_of_year(int year, int month, int day);

unsigned char *landmask_d;

int main() {
  // report on free mem
//  checkCUDAMemory();

  // allocate device memory for landmask
  size_t landMemSize = NPIXELS * sizeof(char);
  hipMalloc((void **)&landmask_d, landMemSize);
  printf("allocating %ld device Kbytes for landmask\n", landMemSize/1024);
//  checkCUDAError("memory allocation");

  hipFree(landmask_d);
  

  //printf("day of year:%d\n", day_of_year(2010, 8, 9) );

  return 0;
}


int day_of_year(int year, int month, int day) {

  int i, leap;


  leap = year%4 == 0 && (year%100 != 0 || year%400 == 0);

  for (i = 1; i < month; i++) 
    day += daytab[leap][i];

  return day;
}

	.text
	.file	"hack.hip"
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	movl	$landmask_d, %edi
	movl	$100, %esi
	callq	hipMalloc
	movl	$.L.str, %edi
	xorl	%esi, %esi
	xorl	%eax, %eax
	callq	printf
	movq	landmask_d(%rip), %rdi
	callq	hipFree
	xorl	%eax, %eax
	popq	%rcx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	main, .Lfunc_end0-main
	.cfi_endproc
                                        # -- End function
	.globl	_Z11day_of_yeariii              # -- Begin function _Z11day_of_yeariii
	.type	_Z11day_of_yeariii,@function
_Z11day_of_yeariii:                     # @_Z11day_of_yeariii
	.cfi_startproc
# %bb.0:
	movl	%edx, %eax
	testb	$3, %dil
	je	.LBB1_2
# %bb.1:
	xorl	%ecx, %ecx
	jmp	.LBB1_4
.LBB1_2:
	imull	$-1030792151, %edi, %edx        # imm = 0xC28F5C29
	addl	$85899344, %edx                 # imm = 0x51EB850
	movl	%edx, %edi
	rorl	$2, %edi
	movl	$1, %ecx
	cmpl	$42949672, %edi                 # imm = 0x28F5C28
	ja	.LBB1_4
# %bb.3:
	rorl	$4, %edx
	xorl	%ecx, %ecx
	cmpl	$10737419, %edx                 # imm = 0xA3D70B
	setb	%cl
.LBB1_4:
	cmpl	$2, %esi
	jl	.LBB1_7
# %bb.5:                                # %.lr.ph.preheader
	movl	%esi, %edx
	leaq	(%rcx,%rcx,2), %rsi
	leaq	(%rcx,%rsi,4), %rcx
	movl	$1, %esi
.LBB1_6:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movsbl	_ZL6daytab(%rcx,%rsi), %edi
	addl	%edi, %eax
	incq	%rsi
	cmpq	%rsi, %rdx
	jne	.LBB1_6
.LBB1_7:                                # %._crit_edge
	retq
.Lfunc_end1:
	.size	_Z11day_of_yeariii, .Lfunc_end1-_Z11day_of_yeariii
	.cfi_endproc
                                        # -- End function
	.type	landmask_d,@object              # @landmask_d
	.bss
	.globl	landmask_d
	.p2align	3, 0x0
landmask_d:
	.quad	0
	.size	landmask_d, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"allocating %ld device Kbytes for landmask\n"
	.size	.L.str, 43

	.type	_ZL6daytab,@object              # @_ZL6daytab
	.section	.rodata,"a",@progbits
	.p2align	4, 0x0
_ZL6daytab:
	.ascii	"\000\037\034\037\036\037\036\037\037\036\037\036\037"
	.ascii	"\000\037\035\037\036\037\036\037\037\036\037\036\037"
	.size	_ZL6daytab, 26

	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.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 landmask_d
	.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
