; 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
@.str = external hidden unnamed_addr constant [43 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [14 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [15 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [20 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [37 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [15 x i8], align 1
@.str.8 = external hidden unnamed_addr constant [6 x i8], align 1
@.str.9 = external hidden unnamed_addr constant [38 x i8], align 1

; 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
declare dso_local zeroext i16 @MEM_getFree() #1

; Function Attrs: nounwind uwtable
declare dso_local zeroext i16 @MEM_getAllocated() #1

; Function Attrs: noinline nounwind uwtable
define dso_local zeroext i8 @MEM_checkIntegrity() #2 {
  %1 = alloca i8, align 1
  %2 = alloca i64, align 8
  %3 = alloca i16, align 2
  %4 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 1, ptr %1) #4
  store i8 1, ptr %1, align 1, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #4
  store i64 ptrtoint (ptr @_bend to i64), ptr %2, align 8, !tbaa !8
  %5 = load i64, ptr %2, align 8, !tbaa !8
  %6 = add i64 %5, 1
  store i64 %6, ptr %2, align 8, !tbaa !8
  %7 = load i64, ptr %2, align 8, !tbaa !8
  %8 = lshr i64 %7, 1
  store i64 %8, ptr %2, align 8, !tbaa !8
  %9 = load i64, ptr %2, align 8, !tbaa !8
  %10 = shl i64 %9, 1
  store i64 %10, ptr %2, align 8, !tbaa !8
  call void @llvm.lifetime.start.p0(i64 2, ptr %3) #4
  %11 = load i64, ptr %2, align 8, !tbaa !8
  %12 = add i64 %11, 2
  %13 = sub i64 3774871040, %12
  %14 = trunc i64 %13 to i16
  store i16 %14, ptr %3, align 2, !tbaa !10
  %15 = call zeroext i16 @MEM_getFree()
  %16 = zext i16 %15 to i32
  %17 = call zeroext i16 @MEM_getAllocated()
  %18 = zext i16 %17 to i32
  %19 = add nsw i32 %16, %18
  %20 = load i16, ptr %3, align 2, !tbaa !10
  %21 = zext i16 %20 to i32
  %22 = icmp ne i32 %19, %21
  br i1 %22, label %23, label %33

23:                                               ; preds = %0
  call void @KDebug_Alert(ptr noundef @.str)
  call void @KDebug_Alert(ptr noundef @.str.1)
  %24 = load i16, ptr %3, align 2, !tbaa !10
  %25 = zext i16 %24 to i32
  %26 = call i32 (ptr, ...) @kprintf(ptr noundef @.str.2, i32 noundef %25)
  call void @KDebug_Alert(ptr noundef @.str.3)
  %27 = call zeroext i16 @MEM_getFree()
  %28 = zext i16 %27 to i32
  %29 = call i32 (ptr, ...) @kprintf(ptr noundef @.str.4, i32 noundef %28)
  call void @KDebug_Alert(ptr noundef @.str.5)
  %30 = call zeroext i16 @MEM_getAllocated()
  %31 = zext i16 %30 to i32
  %32 = call i32 (ptr, ...) @kprintf(ptr noundef @.str.4, i32 noundef %31)
  store i8 0, ptr %1, align 1, !tbaa !5
  br label %33

33:                                               ; preds = %23, %0
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %34 = call i64 (...) @SYS_getStackPointer()
  store i64 %34, ptr %4, align 8, !tbaa !8
  %35 = load i64, ptr %4, align 8, !tbaa !8
  %36 = icmp ult i64 %35, 3774871040
  br i1 %36, label %37, label %40

37:                                               ; preds = %33
  call void @KDebug_Alert(ptr noundef @.str.6)
  call void @KDebug_Alert(ptr noundef @.str.7)
  %38 = load i64, ptr %4, align 8, !tbaa !8
  %39 = call i32 (ptr, ...) @kprintf(ptr noundef @.str.8, i64 noundef %38)
  store i8 0, ptr %1, align 1, !tbaa !5
  br label %47

40:                                               ; preds = %33
  %41 = load i64, ptr %4, align 8, !tbaa !8
  %42 = icmp uge i64 %41, 3774873600
  br i1 %42, label %43, label %46

43:                                               ; preds = %40
  call void @KDebug_Alert(ptr noundef @.str.9)
  call void @KDebug_Alert(ptr noundef @.str.7)
  %44 = load i64, ptr %4, align 8, !tbaa !8
  %45 = call i32 (ptr, ...) @kprintf(ptr noundef @.str.8, i64 noundef %44)
  store i8 0, ptr %1, align 1, !tbaa !5
  br label %46

46:                                               ; preds = %43, %40
  br label %47

47:                                               ; preds = %46, %37
  %48 = load i8, ptr %1, align 1, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 2, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %1) #4
  ret i8 %48
}

declare void @KDebug_Alert(ptr noundef) #3

declare i32 @kprintf(ptr noundef, ...) #3

declare i64 @SYS_getStackPointer(...) #3

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 = { noinline 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 #3 = { "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 #4 = { 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"long", !6, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"short", !6, 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
@.str = external hidden unnamed_addr constant [43 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [14 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [15 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [20 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [37 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [15 x i8], align 1
@.str.8 = external hidden unnamed_addr constant [6 x i8], align 1
@.str.9 = external hidden unnamed_addr constant [38 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local zeroext i16 @MEM_getFree() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local zeroext i16 @MEM_getAllocated() local_unnamed_addr #0

; Function Attrs: noinline nounwind uwtable
define dso_local zeroext range(i8 0, 2) i8 @MEM_checkIntegrity() local_unnamed_addr #1 {
  %1 = and i16 trunc (i64 add (i64 ptrtoint (ptr @_bend to i64), i64 1) to i16), -8
  %2 = sub i16 -2562, %1
  %3 = tail call zeroext i16 @MEM_getFree()
  %4 = zext i16 %3 to i32
  %5 = tail call zeroext i16 @MEM_getAllocated()
  %6 = zext i16 %5 to i32
  %7 = add nuw nsw i32 %6, %4
  %8 = zext i16 %2 to i32
  %.not = icmp eq i32 %7, %8
  br i1 %.not, label %17, label %9

9:                                                ; preds = %0
  tail call void @KDebug_Alert(ptr noundef nonnull @.str) #3
  tail call void @KDebug_Alert(ptr noundef nonnull @.str.1) #3
  %10 = tail call i32 (ptr, ...) @kprintf(ptr noundef nonnull @.str.2, i32 noundef %8) #3
  tail call void @KDebug_Alert(ptr noundef nonnull @.str.3) #3
  %11 = tail call zeroext i16 @MEM_getFree()
  %12 = zext i16 %11 to i32
  %13 = tail call i32 (ptr, ...) @kprintf(ptr noundef nonnull @.str.4, i32 noundef %12) #3
  tail call void @KDebug_Alert(ptr noundef nonnull @.str.5) #3
  %14 = tail call zeroext i16 @MEM_getAllocated()
  %15 = zext i16 %14 to i32
  %16 = tail call i32 (ptr, ...) @kprintf(ptr noundef nonnull @.str.4, i32 noundef %15) #3
  br label %17

17:                                               ; preds = %9, %0
  %.0 = phi i8 [ 0, %9 ], [ 1, %0 ]
  %18 = tail call i64 (...) @SYS_getStackPointer() #3
  %19 = icmp ult i64 %18, 3774871040
  br i1 %19, label %.sink.split, label %20

20:                                               ; preds = %17
  %21 = icmp ugt i64 %18, 3774873599
  br i1 %21, label %.sink.split, label %23

.sink.split:                                      ; preds = %20, %17
  %.str.9.sink = phi ptr [ @.str.6, %17 ], [ @.str.9, %20 ]
  tail call void @KDebug_Alert(ptr noundef nonnull %.str.9.sink) #3
  tail call void @KDebug_Alert(ptr noundef nonnull @.str.7) #3
  %22 = tail call i32 (ptr, ...) @kprintf(ptr noundef nonnull @.str.8, i64 noundef %18) #3
  br label %23

23:                                               ; preds = %.sink.split, %20
  %.1 = phi i8 [ %.0, %20 ], [ 0, %.sink.split ]
  ret i8 %.1
}

declare void @KDebug_Alert(ptr noundef) local_unnamed_addr #2

declare i32 @kprintf(ptr noundef, ...) local_unnamed_addr #2

declare i64 @SYS_getStackPointer(...) local_unnamed_addr #2

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 = { noinline 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 = { "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 #3 = { 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)"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass