// Scott Gordon and Steven Kundert
// CMPS 5433 - Colmenares
// Two Loops Project - Collatz Conjecture Verification
// Sequential implementation
#include <stdio.h>							//standard IO
#include <stdint.h>							//limits file
FILE *f = fopen("VerifyConjecture.txt", "w");//file for writing output
static const int NUM = 1024;

void hailStoneArray(long * a, long size)	//our Sequential code 
{
	long i;
	long iter = 0;							//init values 
	unsigned long long n;					//n can grow very large 
	
	for(i = 2; i < size; i++)				//starting at two, loop for the problem size
	{
		iter = 0;							//set count to zero
		n = i;								//which value are we computing?
		while(n != 1)						//while not converging to 1
		{
			iter++;							//add one more interation
			if((n % 2) == 0)				//if even
			{
				n = n / 2;					// n / 2
			}
			else							//if odd
			{								//
				n = (3 * n + 1) / 2;		//(3n+1)/2. since (3n+1) % 2 == 0, divide by 2
				iter++;						//add one iteration for shortcutted step
			}
		}	
		a[i] = iter;						//store the count in the correct place in the array. 
	}
}

int main()									//our main
{
	const long Asize = (sizeof(long) *NUM);				//size of array (also largest N) 
	long * a_h;							//pointer for array
	long * a_d;
	a_h = (long *)malloc(Asize);//allocate array with zeros, make it correct size
	hailStoneArray(a_h, NUM);			//call our function 
	for (int x = 2; x < NUM; x++)			//loop for output
	{
		fprintf(f,"It takes %d iterations for %d to reach 1 using the Collatz Conjecture\n", a_h[x], x);
	}										//print output
	
	free(a_h); 							//free resources 
	return 0;  								//return 
}

	.file	"tmpxft_0025732f_00000000-6_TwoLoopsSequential.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
	.globl	_Z14hailStoneArrayPll
	.type	_Z14hailStoneArrayPll, @function
_Z14hailStoneArrayPll:
.LFB2027:
	.cfi_startproc
	endbr64
	movl	$2, %edx
.L3:
	cmpq	%rsi, %rdx
	jge	.L10
	movq	%rdx, %rax
	xorl	%ecx, %ecx
.L6:
	testb	$1, %al
	jne	.L4
	incq	%rcx
	shrq	%rax
	jmp	.L5
.L4:
	leaq	(%rax,%rax,2), %rax
	addq	$2, %rcx
	incq	%rax
	shrq	%rax
.L5:
	cmpq	$1, %rax
	jne	.L6
	movq	%rcx, (%rdi,%rdx,8)
	incq	%rdx
	jmp	.L3
.L10:
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z14hailStoneArrayPll, .-_Z14hailStoneArrayPll
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"It takes %d iterations for %d to reach 1 using the Collatz Conjecture\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2028:
	.cfi_startproc
	endbr64
	pushq	%r12
	.cfi_def_cfa_offset 16
	.cfi_offset 12, -16
	movl	$8192, %edi
	leaq	.LC0(%rip), %r12
	pushq	%rbp
	.cfi_def_cfa_offset 24
	.cfi_offset 6, -24
	movl	$2, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 32
	.cfi_offset 3, -32
	call	malloc@PLT
	movl	$1024, %esi
	movq	%rax, %rdi
	movq	%rax, %rbx
	call	_Z14hailStoneArrayPll
.L12:
	movq	(%rbx,%rbp,8), %rcx
	movq	f(%rip), %rdi
	movl	%ebp, %r8d
	xorl	%eax, %eax
	movq	%r12, %rdx
	movl	$2, %esi
	incq	%rbp
	call	__fprintf_chk@PLT
	cmpq	$1024, %rbp
	jne	.L12
	movq	%rbx, %rdi
	call	free@PLT
	popq	%rbx
	.cfi_def_cfa_offset 24
	xorl	%eax, %eax
	popq	%rbp
	.cfi_def_cfa_offset 16
	popq	%r12
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2028:
	.size	main, .-main
	.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	.rodata.str1.1
.LC1:
	.string	"w"
.LC2:
	.string	"VerifyConjecture.txt"
	.section	.text.startup
	.type	_GLOBAL__sub_I_f, @function
_GLOBAL__sub_I_f:
.LFB2170:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	.LC1(%rip), %rsi
	leaq	.LC2(%rip), %rdi
	call	fopen@PLT
	movq	%rax, f(%rip)
	popq	%rdx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2170:
	.size	_GLOBAL__sub_I_f, .-_GLOBAL__sub_I_f
	.section	.init_array
	.align 8
	.quad	_GLOBAL__sub_I_f
	.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	f
	.bss
	.align 8
	.type	f, @object
	.size	f, 8
f:
	.zero	8
	.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:

// Scott Gordon and Steven Kundert
// CMPS 5433 - Colmenares
// Two Loops Project - Collatz Conjecture Verification
// Sequential implementation

#include <hip/hip_runtime.h>
#include <stdio.h>							//standard IO
#include <stdint.h>							//limits file
FILE *f = fopen("VerifyConjecture.txt", "w");//file for writing output
static const int NUM = 1024;

void hailStoneArray(long * a, long size)	//our Sequential code 
{
	long i;
	long iter = 0;							//init values 
	unsigned long long n;					//n can grow very large 
	
	for(i = 2; i < size; i++)				//starting at two, loop for the problem size
	{
		iter = 0;							//set count to zero
		n = i;								//which value are we computing?
		while(n != 1)						//while not converging to 1
		{
			iter++;							//add one more interation
			if((n % 2) == 0)				//if even
			{
				n = n / 2;					// n / 2
			}
			else							//if odd
			{								//
				n = (3 * n + 1) / 2;		//(3n+1)/2. since (3n+1) % 2 == 0, divide by 2
				iter++;						//add one iteration for shortcutted step
			}
		}	
		a[i] = iter;						//store the count in the correct place in the array. 
	}
}

int main()									//our main
{
	const long Asize = (sizeof(long) *NUM);				//size of array (also largest N) 
	long * a_h;							//pointer for array
	long * a_d;
	a_h = (long *)malloc(Asize);//allocate array with zeros, make it correct size
	hailStoneArray(a_h, NUM);			//call our function 
	for (int x = 2; x < NUM; x++)			//loop for output
	{
		fprintf(f,"It takes %d iterations for %d to reach 1 using the Collatz Conjecture\n", a_h[x], x);
	}										//print output
	
	free(a_h); 							//free resources 
	return 0;  								//return 
}

	.text
	.file	"TwoLoopsSequential.hip"
	.globl	_Z14hailStoneArrayPll           # -- Begin function _Z14hailStoneArrayPll
	.type	_Z14hailStoneArrayPll,@function
_Z14hailStoneArrayPll:                  # @_Z14hailStoneArrayPll
	.cfi_startproc
# %bb.0:
	cmpq	$3, %rsi
	jl	.LBB0_5
# %bb.1:                                # %.preheader.preheader
	movl	$2, %eax
.LBB0_2:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_3 Depth 2
	movq	%rax, %rdx
	xorl	%ecx, %ecx
.LBB0_3:                                #   Parent Loop BB0_2 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	%edx, %r8d
	andl	$1, %r8d
	leaq	1(%rdx,%rdx,2), %r9
	cmoveq	%rdx, %r9
	addq	%r8, %rcx
	incq	%rcx
	movq	%r9, %rdx
	shrq	%rdx
	cmpq	$2, %r9
	jne	.LBB0_3
# %bb.4:                                #   in Loop: Header=BB0_2 Depth=1
	movq	%rcx, (%rdi,%rax,8)
	incq	%rax
	cmpq	%rsi, %rax
	jne	.LBB0_2
.LBB0_5:                                # %._crit_edge
	retq
.Lfunc_end0:
	.size	_Z14hailStoneArrayPll, .Lfunc_end0-_Z14hailStoneArrayPll
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%r14
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	pushq	%rax
	.cfi_def_cfa_offset 32
	.cfi_offset %rbx, -24
	.cfi_offset %r14, -16
	movl	$8192, %edi                     # imm = 0x2000
	callq	malloc
	movq	%rax, %rbx
	movl	$2, %eax
.LBB1_1:                                # %.preheader.i
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB1_2 Depth 2
	movq	%rax, %rdx
	xorl	%ecx, %ecx
.LBB1_2:                                #   Parent Loop BB1_1 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	%edx, %esi
	andl	$1, %esi
	leaq	1(%rdx,%rdx,2), %rdi
	cmoveq	%rdx, %rdi
	addq	%rsi, %rcx
	incq	%rcx
	movq	%rdi, %rdx
	shrq	%rdx
	cmpq	$2, %rdi
	jne	.LBB1_2
# %bb.3:                                #   in Loop: Header=BB1_1 Depth=1
	movq	%rcx, (%rbx,%rax,8)
	incq	%rax
	cmpq	$1024, %rax                     # imm = 0x400
	jne	.LBB1_1
# %bb.4:                                # %_Z14hailStoneArrayPll.exit.preheader
	movl	$2, %r14d
.LBB1_5:                                # %_Z14hailStoneArrayPll.exit
                                        # =>This Inner Loop Header: Depth=1
	movq	f(%rip), %rdi
	movq	(%rbx,%r14,8), %rdx
	movl	$.L.str.2, %esi
	movl	%r14d, %ecx
	xorl	%eax, %eax
	callq	fprintf
	incq	%r14
	cmpq	$1024, %r14                     # imm = 0x400
	jne	.LBB1_5
# %bb.6:
	movq	%rbx, %rdi
	callq	free
	xorl	%eax, %eax
	addq	$8, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	main, .Lfunc_end1-main
	.cfi_endproc
                                        # -- End function
	.section	.text.startup,"ax",@progbits
	.type	_GLOBAL__sub_I_TwoLoopsSequential.hip,@function # -- Begin function _GLOBAL__sub_I_TwoLoopsSequential.hip
_GLOBAL__sub_I_TwoLoopsSequential.hip:  # @_GLOBAL__sub_I_TwoLoopsSequential.hip
	.cfi_startproc
# %bb.0:
	pushq	%rax
	.cfi_def_cfa_offset 16
	movl	$.L.str, %edi
	movl	$.L.str.1, %esi
	callq	fopen
	movq	%rax, f(%rip)
	popq	%rax
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end2:
	.size	_GLOBAL__sub_I_TwoLoopsSequential.hip, .Lfunc_end2-_GLOBAL__sub_I_TwoLoopsSequential.hip
	.cfi_endproc
                                        # -- End function
	.type	f,@object                       # @f
	.bss
	.globl	f
	.p2align	3, 0x0
f:
	.quad	0
	.size	f, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"VerifyConjecture.txt"
	.size	.L.str, 21

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"w"
	.size	.L.str.1, 2

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"It takes %d iterations for %d to reach 1 using the Collatz Conjecture\n"
	.size	.L.str.2, 71

	.section	.init_array,"aw",@init_array
	.p2align	3, 0x0
	.quad	_GLOBAL__sub_I_TwoLoopsSequential.hip
	.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 _GLOBAL__sub_I_TwoLoopsSequential.hip
	.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
