; 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"

@heap = external hidden global ptr, align 8

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

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

; Function Attrs: nounwind uwtable
define dso_local zeroext i16 @MEM_getLargestFreeBlock() #1 {
  %1 = alloca ptr, align 8
  %2 = alloca i16, align 2
  %3 = alloca i16, align 2
  %4 = alloca i16, align 2
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #2
  %5 = load ptr, ptr @heap, align 8, !tbaa !5
  store ptr %5, ptr %1, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr %2) #2
  store i16 0, ptr %2, align 2, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %3) #2
  store i16 0, ptr %3, align 2, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %4) #2
  br label %6

6:                                                ; preds = %32, %0
  %7 = load ptr, ptr %1, align 8, !tbaa !5
  %8 = load i16, ptr %7, align 2, !tbaa !9
  store i16 %8, ptr %4, align 2, !tbaa !9
  %9 = zext i16 %8 to i32
  %10 = icmp ne i32 %9, 0
  br i1 %10, label %11, label %39

11:                                               ; preds = %6
  %12 = load i16, ptr %4, align 2, !tbaa !9
  %13 = zext i16 %12 to i32
  %14 = and i32 %13, 1
  %15 = icmp ne i32 %14, 0
  br i1 %15, label %16, label %17

16:                                               ; preds = %11
  store i16 0, ptr %3, align 2, !tbaa !9
  br label %32

17:                                               ; preds = %11
  %18 = load i16, ptr %4, align 2, !tbaa !9
  %19 = zext i16 %18 to i32
  %20 = load i16, ptr %3, align 2, !tbaa !9
  %21 = zext i16 %20 to i32
  %22 = add nsw i32 %21, %19
  %23 = trunc i32 %22 to i16
  store i16 %23, ptr %3, align 2, !tbaa !9
  %24 = load i16, ptr %3, align 2, !tbaa !9
  %25 = zext i16 %24 to i32
  %26 = load i16, ptr %2, align 2, !tbaa !9
  %27 = zext i16 %26 to i32
  %28 = icmp sgt i32 %25, %27
  br i1 %28, label %29, label %31

29:                                               ; preds = %17
  %30 = load i16, ptr %3, align 2, !tbaa !9
  store i16 %30, ptr %2, align 2, !tbaa !9
  br label %31

31:                                               ; preds = %29, %17
  br label %32

32:                                               ; preds = %31, %16
  %33 = load i16, ptr %4, align 2, !tbaa !9
  %34 = zext i16 %33 to i32
  %35 = ashr i32 %34, 1
  %36 = load ptr, ptr %1, align 8, !tbaa !5
  %37 = sext i32 %35 to i64
  %38 = getelementptr inbounds i16, ptr %36, i64 %37
  store ptr %38, ptr %1, align 8, !tbaa !5
  br label %6, !llvm.loop !11

39:                                               ; preds = %6
  %40 = load i16, ptr %2, align 2, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 2, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %3) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %2) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #2
  ret i16 %40
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = !{!"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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; 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"

@heap = external hidden local_unnamed_addr global ptr, align 8

; Function Attrs: nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) uwtable
define dso_local zeroext i16 @MEM_getLargestFreeBlock() local_unnamed_addr #0 {
  %1 = load ptr, ptr @heap, align 8, !tbaa !5
  %2 = load i16, ptr %1, align 2, !tbaa !9
  %.not14 = icmp eq i16 %2, 0
  br i1 %.not14, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %0, %11
  %.in = phi i16 [ %15, %11 ], [ %2, %0 ]
  %.017 = phi ptr [ %14, %11 ], [ %1, %0 ]
  %.0916 = phi i32 [ %.1, %11 ], [ 0, %0 ]
  %.01015 = phi i16 [ %.111, %11 ], [ 0, %0 ]
  %3 = zext i16 %.in to i32
  %4 = and i32 %3, 1
  %.not13 = icmp eq i32 %4, 0
  br i1 %.not13, label %5, label %11

5:                                                ; preds = %.lr.ph
  %6 = add i32 %.0916, %3
  %7 = and i32 %6, 65535
  %8 = zext i16 %.01015 to i32
  %9 = icmp ugt i32 %7, %8
  %10 = trunc i32 %6 to i16
  %spec.select = select i1 %9, i16 %10, i16 %.01015
  br label %11

11:                                               ; preds = %5, %.lr.ph
  %.111 = phi i16 [ %.01015, %.lr.ph ], [ %spec.select, %5 ]
  %.1 = phi i32 [ 0, %.lr.ph ], [ %6, %5 ]
  %12 = lshr i32 %3, 1
  %13 = zext nneg i32 %12 to i64
  %14 = getelementptr inbounds i16, ptr %.017, i64 %13
  %15 = load i16, ptr %14, align 2, !tbaa !9
  %.not = icmp eq i16 %15, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !11

._crit_edge:                                      ; preds = %11, %0
  %.010.lcssa = phi i16 [ 0, %0 ], [ %.111, %11 ]
  ret i16 %.010.lcssa
}

attributes #0 = { nofree norecurse nosync nounwind memory(read, 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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass