; LLVM IR from Stephane-D/SGDK :: src (memory)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@_bend = external global i64, align 8
@heap = external hidden global ptr, align 8
@free = external hidden global ptr, align 8
@heapEnd = external hidden global ptr, align 8
@needPack = external hidden global i8, align 1

; Function Attrs: nounwind uwtable
define dso_local void @MEM_init() #0 {
  %1 = alloca i64, align 8
  %2 = alloca i16, align 2
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #2
  store i64 ptrtoint (ptr @_bend to i64), ptr %1, align 8, !tbaa !5
  %3 = load i64, ptr %1, align 8, !tbaa !5
  %4 = add i64 %3, 1
  store i64 %4, ptr %1, align 8, !tbaa !5
  %5 = load i64, ptr %1, align 8, !tbaa !5
  %6 = lshr i64 %5, 1
  store i64 %6, ptr %1, align 8, !tbaa !5
  %7 = load i64, ptr %1, align 8, !tbaa !5
  %8 = shl i64 %7, 1
  store i64 %8, ptr %1, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr %2) #2
  %9 = load i64, ptr %1, align 8, !tbaa !5
  %10 = add i64 %9, 2
  %11 = sub i64 3774871040, %10
  %12 = trunc i64 %11 to i16
  store i16 %12, ptr %2, align 2, !tbaa !9
  %13 = load i64, ptr %1, align 8, !tbaa !5
  %14 = inttoptr i64 %13 to ptr
  store ptr %14, ptr @heap, align 8, !tbaa !11
  %15 = load i16, ptr %2, align 2, !tbaa !9
  %16 = load ptr, ptr @heap, align 8, !tbaa !11
  store i16 %15, ptr %16, align 2, !tbaa !9
  %17 = load ptr, ptr @heap, align 8, !tbaa !11
  store ptr %17, ptr @free, align 8, !tbaa !11
  %18 = load ptr, ptr @heap, align 8, !tbaa !11
  %19 = load i16, ptr %2, align 2, !tbaa !9
  %20 = zext i16 %19 to i32
  %21 = ashr i32 %20, 1
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds i16, ptr %18, i64 %22
  store ptr %23, ptr @heapEnd, align 8, !tbaa !11
  %24 = load ptr, ptr @heapEnd, align 8, !tbaa !11
  store i16 0, ptr %24, align 2, !tbaa !9
  store i8 0, ptr @needPack, align 1, !tbaa !13
  call void @llvm.lifetime.end.p0(i64 2, ptr %2) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #2
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

attributes #0 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nounwind }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"short", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!7, !7, i64 0}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@_bend = external global i64, align 8
@heap = external hidden local_unnamed_addr global ptr, align 8
@free = external hidden local_unnamed_addr global ptr, align 8
@heapEnd = external hidden local_unnamed_addr global ptr, align 8
@needPack = external hidden local_unnamed_addr global i8, align 1

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(write, inaccessiblemem: none) uwtable
define dso_local void @MEM_init() local_unnamed_addr #0 {
  %1 = and i64 add (i64 ptrtoint (ptr @_bend to i64), i64 1), -8
  %2 = trunc i64 %1 to i16
  %3 = or disjoint i16 %2, 2
  %4 = sub i16 -2560, %3
  %5 = inttoptr i64 %1 to ptr
  store ptr %5, ptr @heap, align 8, !tbaa !5
  store i16 %4, ptr %5, align 8, !tbaa !9
  store ptr %5, ptr @free, align 8, !tbaa !5
  %6 = lshr exact i16 %4, 1
  %7 = zext nneg i16 %6 to i64
  %8 = getelementptr inbounds i16, ptr %5, i64 %7
  store ptr %8, ptr @heapEnd, align 8, !tbaa !5
  store i16 0, ptr %8, align 2, !tbaa !9
  store i8 0, ptr @needPack, align 1, !tbaa !11
  ret void
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(write, inaccessiblemem: none) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"short", !7, i64 0}
!11 = !{!7, !7, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass