#include <iostream>
#include <math.h>
#include <stdio.h>
#include<stdlib.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <unistd.h>
#include <assert.h>

// #define GB 8
// #define MB 1
// #define KB 1

int main(int argc, char *argv[])
{
    unsigned GB,MB,KB;
    char *endptr;

    fprintf(stdout, "Input %d argument\n",argc);
    assert(argc == 3);
    // float size_char_format = atoi(argv[1]);
    float size = strtof(argv[1], &endptr);
    if (*endptr != '\0')
        printf("%s is not convertible to float\n", argv[1]);
    else
        printf("size is  %f\n", size ); //0.000000    
    int over_sub_char_format = atoi(argv[2]);
    // float size = (float) size_char_format;
    unsigned over_sub = (unsigned) over_sub_char_format;

    if (over_sub == 1)
    {
        // printf("size is %f\n", size);
        fprintf(stdout, "It is 110%% oversubscription mode\n");
        float occupy_mem = 11  - size   +  size * 1.0/10.0 -0.1 ;
        GB = (unsigned) occupy_mem;
        float temp_MB = (occupy_mem -floor(occupy_mem)) *1024.0;
        MB = (unsigned) temp_MB;
        float temp_KB = (temp_MB - floor(temp_MB)) *1024.0;
        KB = (unsigned) temp_KB;
    }
    else if (over_sub == 2)
    {
        printf("size is %f\n", size);
        fprintf(stdout, "It is 125%% oversubscription mode\n");
        float occupy_mem = 11  - size   +  size * 1.0/4.0 - 0.1  ; 
        GB = (unsigned) occupy_mem;
        float temp_MB = (occupy_mem -floor(occupy_mem)) *1024.0;
        MB = (unsigned) temp_MB;
        float temp_KB = (temp_MB - floor(temp_MB)) *1024.0;
        KB = (unsigned) temp_KB;
    }else{
        assert(0);
    }
    
    if (GB == 0)
    {
        assert(0);
    }
    if(MB == 0)
    {
        assert(0);
    }
    if(KB == 0)
    {
        assert(0);
    }
    
    fprintf(stdout, "the calculated size is %u GB, %u MB,%u KB \n",GB,MB,KB);
    

    float *GB_ptr;
    float *MB_ptr;
    float *KB_ptr;
    
    cudaMalloc((void **) &GB_ptr, sizeof(float) * 1024*1024*256 * GB );
    cudaMalloc((void **) &MB_ptr, sizeof(float) * 1024*256 * MB);
    cudaMalloc((void **) &KB_ptr, sizeof(float) * 256 * KB);
    
    sleep(900);
    sleep(900);
    sleep(900);
    sleep(900);
    sleep(900);
    

    cudaFree(GB_ptr);
    cudaFree(MB_ptr);
    cudaFree(KB_ptr);
    return 0;
}
	.file	"tmpxft_0039338c_00000000-6_memory_occ.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB3651:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE3651:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"Input %d argument\n"
.LC1:
	.string	"%s is not convertible to float\n"
.LC2:
	.string	"size is  %f\n"
.LC3:
	.string	"It is 110%% oversubscription mode\n"
.LC8:
	.string	"size is %f\n"
.LC9:
	.string	"It is 125%% oversubscription mode\n"
.LC11:
	.string	"the calculated size is %u GB, %u MB,%u KB \n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB3648:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	movl	%edi, %ecx
	movq	%rsi, %r13
	leaq	.LC0(%rip), %rdx
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	movl	$2, %esi
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	subq	$72, %rsp
	.cfi_def_cfa_offset 112
	movq	stdout(%rip), %rdi
	movq	%fs:40, %rax
	movq	%rax, 56(%rsp)
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movq	8(%r13), %rdi
	leaq	24(%rsp), %rsi
	call	strtof@PLT
	movq	24(%rsp), %rax
	movss	%xmm0, 4(%rsp)
	cmpb	$0, (%rax)
	je	.L3
	movq	8(%r13), %rdx
	leaq	.LC1(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	jmp	.L4
.L3:
	leaq	.LC2(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	4(%rsp), %xmm0
	call	__printf_chk@PLT
.L4:
	movq	16(%r13), %rdi
	call	atoi@PLT
	cmpl	$1, %eax
	jne	.L5
	movq	stdout(%rip), %rdi
	leaq	.LC3(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movss	.LC4(%rip), %xmm1
	subss	4(%rsp), %xmm1
	cvtss2sd	4(%rsp), %xmm0
	divsd	.LC5(%rip), %xmm0
	cvtss2sd	%xmm1, %xmm1
	jmp	.L9
.L5:
	cmpl	$2, %eax
	jne	.L6
	leaq	.LC8(%rip), %rsi
	movl	$2, %edi
	movb	$1, %al
	cvtss2sd	4(%rsp), %xmm0
	movsd	%xmm0, 8(%rsp)
	call	__printf_chk@PLT
	movq	stdout(%rip), %rdi
	leaq	.LC9(%rip), %rdx
	xorl	%eax, %eax
	movl	$2, %esi
	call	__fprintf_chk@PLT
	movsd	8(%rsp), %xmm0
	mulsd	.LC10(%rip), %xmm0
	movss	.LC4(%rip), %xmm1
	subss	4(%rsp), %xmm1
	cvtss2sd	%xmm1, %xmm1
.L9:
	addsd	%xmm0, %xmm1
	subsd	.LC6(%rip), %xmm1
	cvtsd2ss	%xmm1, %xmm1
	movaps	%xmm1, %xmm0
	cvttss2siq	%xmm1, %rbx
	movss	%xmm1, 4(%rsp)
	call	floorf@PLT
	movss	4(%rsp), %xmm1
	subss	%xmm0, %xmm1
	mulss	.LC7(%rip), %xmm1
	movaps	%xmm1, %xmm0
	cvttss2siq	%xmm1, %rbp
	movss	%xmm1, 4(%rsp)
	call	floorf@PLT
	movss	4(%rsp), %xmm1
	subss	%xmm0, %xmm1
	mulss	.LC7(%rip), %xmm1
	cvttss2siq	%xmm1, %r12
.L6:
	movq	stdout(%rip), %rdi
	movl	%r12d, %r9d
	movl	%ebp, %r8d
	movl	%ebx, %ecx
	leaq	.LC11(%rip), %rdx
	movl	$2, %esi
	xorl	%eax, %eax
	call	__fprintf_chk@PLT
	movl	%ebx, %esi
	leaq	32(%rsp), %rdi
	salq	$30, %rsi
	call	cudaMalloc@PLT
	movl	%ebp, %esi
	leaq	40(%rsp), %rdi
	salq	$20, %rsi
	call	cudaMalloc@PLT
	movl	%r12d, %esi
	leaq	48(%rsp), %rdi
	salq	$10, %rsi
	call	cudaMalloc@PLT
	movl	$900, %edi
	call	sleep@PLT
	movl	$900, %edi
	call	sleep@PLT
	movl	$900, %edi
	call	sleep@PLT
	movl	$900, %edi
	call	sleep@PLT
	movl	$900, %edi
	call	sleep@PLT
	movq	32(%rsp), %rdi
	call	cudaFree@PLT
	movq	40(%rsp), %rdi
	call	cudaFree@PLT
	movq	48(%rsp), %rdi
	call	cudaFree@PLT
	movq	56(%rsp), %rax
	subq	%fs:40, %rax
	je	.L7
	call	__stack_chk_fail@PLT
.L7:
	addq	$72, %rsp
	.cfi_def_cfa_offset 40
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE3648:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB3674:
	.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
.LFE3674:
	.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
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC4:
	.long	1093664768
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC5:
	.long	0
	.long	1076101120
	.align 8
.LC6:
	.long	-1717986918
	.long	1069128089
	.section	.rodata.cst4
	.align 4
.LC7:
	.long	1149239296
	.section	.rodata.cst8
	.align 8
.LC10:
	.long	0
	.long	1070596096
	.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 <iostream>
#include <math.h>
#include <stdio.h>
#include<stdlib.h>
#include <hip/hip_runtime.h>

#include <unistd.h>
#include <assert.h>

// #define GB 8
// #define MB 1
// #define KB 1

int main(int argc, char *argv[])
{
    unsigned GB,MB,KB;
    char *endptr;

    fprintf(stdout, "Input %d argument\n",argc);
    assert(argc == 3);
    // float size_char_format = atoi(argv[1]);
    float size = strtof(argv[1], &endptr);
    if (*endptr != '\0')
        printf("%s is not convertible to float\n", argv[1]);
    else
        printf("size is  %f\n", size ); //0.000000    
    int over_sub_char_format = atoi(argv[2]);
    // float size = (float) size_char_format;
    unsigned over_sub = (unsigned) over_sub_char_format;

    if (over_sub == 1)
    {
        // printf("size is %f\n", size);
        fprintf(stdout, "It is 110%% oversubscription mode\n");
        float occupy_mem = 11  - size   +  size * 1.0/10.0 -0.1 ;
        GB = (unsigned) occupy_mem;
        float temp_MB = (occupy_mem -floor(occupy_mem)) *1024.0;
        MB = (unsigned) temp_MB;
        float temp_KB = (temp_MB - floor(temp_MB)) *1024.0;
        KB = (unsigned) temp_KB;
    }
    else if (over_sub == 2)
    {
        printf("size is %f\n", size);
        fprintf(stdout, "It is 125%% oversubscription mode\n");
        float occupy_mem = 11  - size   +  size * 1.0/4.0 - 0.1  ; 
        GB = (unsigned) occupy_mem;
        float temp_MB = (occupy_mem -floor(occupy_mem)) *1024.0;
        MB = (unsigned) temp_MB;
        float temp_KB = (temp_MB - floor(temp_MB)) *1024.0;
        KB = (unsigned) temp_KB;
    }else{
        assert(0);
    }
    
    if (GB == 0)
    {
        assert(0);
    }
    if(MB == 0)
    {
        assert(0);
    }
    if(KB == 0)
    {
        assert(0);
    }
    
    fprintf(stdout, "the calculated size is %u GB, %u MB,%u KB \n",GB,MB,KB);
    

    float *GB_ptr;
    float *MB_ptr;
    float *KB_ptr;
    
    hipMalloc((void **) &GB_ptr, sizeof(float) * 1024*1024*256 * GB );
    hipMalloc((void **) &MB_ptr, sizeof(float) * 1024*256 * MB);
    hipMalloc((void **) &KB_ptr, sizeof(float) * 256 * KB);
    
    sleep(900);
    sleep(900);
    sleep(900);
    sleep(900);
    sleep(900);
    

    hipFree(GB_ptr);
    hipFree(MB_ptr);
    hipFree(KB_ptr);
    return 0;
}
	.text
	.file	"memory_occ.hip"
                                        # Start of file scope inline assembly
	.globl	_ZSt21ios_base_library_initv

                                        # End of file scope inline assembly
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function main
.LCPI0_0:
	.long	0x41300000                      # float 11
.LCPI0_3:
	.long	0x44800000                      # float 1024
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0
.LCPI0_1:
	.quad	0x3fd0000000000000              # double 0.25
.LCPI0_2:
	.quad	0xbfb999999999999a              # double -0.10000000000000001
.LCPI0_4:
	.quad	0x4024000000000000              # double 10
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.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	$48, %rsp
	.cfi_def_cfa_offset 80
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movq	%rsi, %rbx
	movl	%edi, %edx
	movq	stdout(%rip), %rdi
	movl	$.L.str, %esi
	xorl	%eax, %eax
	callq	fprintf
	movq	8(%rbx), %rdi
	leaq	40(%rsp), %r14
	movq	%r14, %rsi
	callq	strtof
	movq	(%r14), %rax
	cmpb	$0, (%rax)
	movss	%xmm0, 4(%rsp)                  # 4-byte Spill
	je	.LBB0_2
# %bb.1:
	movq	8(%rbx), %rsi
	movl	$.L.str.1, %edi
	xorl	%eax, %eax
	callq	printf
	jmp	.LBB0_3
.LBB0_2:
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str.2, %edi
	movb	$1, %al
	callq	printf
.LBB0_3:
	movq	16(%rbx), %rdi
	callq	atoi
	cmpl	$2, %eax
	je	.LBB0_6
# %bb.4:
                                        # implicit-def: $r15d
                                        # implicit-def: $r14d
                                        # implicit-def: $ebx
	cmpl	$1, %eax
	jne	.LBB0_8
# %bb.5:
	movq	stdout(%rip), %rdi
	movl	$.L.str.3, %esi
	xorl	%eax, %eax
	callq	fprintf
	movss	.LCPI0_0(%rip), %xmm0           # xmm0 = mem[0],zero,zero,zero
	movss	4(%rsp), %xmm1                  # 4-byte Reload
                                        # xmm1 = mem[0],zero,zero,zero
	subss	%xmm1, %xmm0
	cvtss2sd	%xmm1, %xmm1
	divsd	.LCPI0_4(%rip), %xmm1
	jmp	.LBB0_7
.LBB0_6:
	xorps	%xmm0, %xmm0
	cvtss2sd	4(%rsp), %xmm0                  # 4-byte Folded Reload
	movsd	%xmm0, 8(%rsp)                  # 8-byte Spill
	movl	$.L.str.4, %edi
	movb	$1, %al
	callq	printf
	movq	stdout(%rip), %rdi
	movl	$.L.str.5, %esi
	xorl	%eax, %eax
	callq	fprintf
	movss	.LCPI0_0(%rip), %xmm0           # xmm0 = mem[0],zero,zero,zero
	subss	4(%rsp), %xmm0                  # 4-byte Folded Reload
	movsd	8(%rsp), %xmm1                  # 8-byte Reload
                                        # xmm1 = mem[0],zero
	mulsd	.LCPI0_1(%rip), %xmm1
.LBB0_7:
	cvtss2sd	%xmm0, %xmm0
	addsd	%xmm0, %xmm1
	addsd	.LCPI0_2(%rip), %xmm1
	xorps	%xmm0, %xmm0
	cvtsd2ss	%xmm1, %xmm0
	movss	%xmm0, 4(%rsp)                  # 4-byte Spill
	cvttss2si	%xmm0, %rbx
	callq	floorf@PLT
	movss	4(%rsp), %xmm1                  # 4-byte Reload
                                        # xmm1 = mem[0],zero,zero,zero
	subss	%xmm0, %xmm1
	mulss	.LCPI0_3(%rip), %xmm1
	movss	%xmm1, 4(%rsp)                  # 4-byte Spill
	cvttss2si	%xmm1, %r14
	movaps	%xmm1, %xmm0
	callq	floorf@PLT
	movss	4(%rsp), %xmm1                  # 4-byte Reload
                                        # xmm1 = mem[0],zero,zero,zero
	subss	%xmm0, %xmm1
	mulss	.LCPI0_3(%rip), %xmm1
	cvttss2si	%xmm1, %r15
.LBB0_8:
	movq	stdout(%rip), %rdi
	movl	$.L.str.6, %esi
	movl	%ebx, %edx
	movl	%r14d, %ecx
	movl	%r15d, %r8d
	xorl	%eax, %eax
	callq	fprintf
	movl	%ebx, %esi
	shlq	$30, %rsi
	leaq	32(%rsp), %rbx
	movq	%rbx, %rdi
	callq	hipMalloc
	movl	%r14d, %esi
	shlq	$20, %rsi
	leaq	24(%rsp), %r14
	movq	%r14, %rdi
	callq	hipMalloc
	movl	%r15d, %esi
	shlq	$10, %rsi
	leaq	16(%rsp), %r15
	movq	%r15, %rdi
	callq	hipMalloc
	movl	$900, %edi                      # imm = 0x384
	callq	sleep
	movl	$900, %edi                      # imm = 0x384
	callq	sleep
	movl	$900, %edi                      # imm = 0x384
	callq	sleep
	movl	$900, %edi                      # imm = 0x384
	callq	sleep
	movl	$900, %edi                      # imm = 0x384
	callq	sleep
	movq	(%rbx), %rdi
	callq	hipFree
	movq	(%r14), %rdi
	callq	hipFree
	movq	(%r15), %rdi
	callq	hipFree
	xorl	%eax, %eax
	addq	$48, %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	main, .Lfunc_end0-main
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Input %d argument\n"
	.size	.L.str, 19

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"%s is not convertible to float\n"
	.size	.L.str.1, 32

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"size is  %f\n"
	.size	.L.str.2, 13

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"It is 110%% oversubscription mode\n"
	.size	.L.str.3, 35

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"size is %f\n"
	.size	.L.str.4, 12

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"It is 125%% oversubscription mode\n"
	.size	.L.str.5, 35

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"the calculated size is %u GB, %u MB,%u KB \n"
	.size	.L.str.6, 44

	.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 __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
