; LLVM IR from Stephane-D/SGDK :: src/ext/flash-save (saveman)

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

%struct.save_manager = type { i8, [2 x %struct.sector_metadata], i64, [0 x i64] }
%struct.sector_metadata = type { %union.anon, i64 }
%union.anon = type { ptr }
%struct.sector_hdr = type { i64, i8, i8 }
%struct.save_blob = type { i8, %union.anon.0, i16, [0 x i16] }
%union.anon.0 = type { i8 }

@sm = 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 signext i16 @sm_load(i8 noundef zeroext %0, ptr noundef %1, i16 noundef zeroext %2) #1 {
  %4 = alloca i16, align 2
  %5 = alloca i8, align 1
  %6 = alloca ptr, align 8
  %7 = alloca i16, align 2
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca i16, align 2
  %12 = alloca i64, align 8
  %13 = alloca [2 x i8], align 1
  store i8 %0, ptr %5, align 1, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !8
  store i16 %2, ptr %7, align 2, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %14 = load ptr, ptr %6, align 8, !tbaa !8
  store ptr %14, ptr %8, align 8, !tbaa !8
  %15 = load i8, ptr %5, align 1, !tbaa !5
  %16 = zext i8 %15 to i32
  %17 = load ptr, ptr @sm, align 8, !tbaa !8
  %18 = getelementptr inbounds %struct.save_manager, ptr %17, i32 0, i32 1
  %19 = load ptr, ptr @sm, align 8, !tbaa !8
  %20 = getelementptr inbounds %struct.save_manager, ptr %19, i32 0, i32 0
  %21 = load i8, ptr %20, align 8, !tbaa !5
  %22 = zext i8 %21 to i64
  %23 = getelementptr inbounds [2 x %struct.sector_metadata], ptr %18, i64 0, i64 %22
  %24 = getelementptr inbounds %struct.sector_metadata, ptr %23, i32 0, i32 0
  %25 = load ptr, ptr %24, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.sector_hdr, ptr %25, i32 0, i32 1
  %27 = load volatile i8, ptr %26, align 8, !tbaa !12
  %28 = zext i8 %27 to i32
  %29 = icmp sgt i32 %16, %28
  br i1 %29, label %30, label %31

30:                                               ; preds = %3
  store i16 -3, ptr %4, align 2
  store i32 1, ptr %9, align 4
  br label %103

31:                                               ; preds = %3
  %32 = load ptr, ptr %6, align 8, !tbaa !8
  %33 = ptrtoint ptr %32 to i64
  %34 = and i64 %33, 1
  %35 = icmp ne i64 %34, 0
  br i1 %35, label %36, label %37

36:                                               ; preds = %31
  store i16 -3, ptr %4, align 2
  store i32 1, ptr %9, align 4
  br label %103

37:                                               ; preds = %31
  %38 = load ptr, ptr @sm, align 8, !tbaa !8
  %39 = getelementptr inbounds %struct.save_manager, ptr %38, i32 0, i32 3
  %40 = load i8, ptr %5, align 1, !tbaa !5
  %41 = zext i8 %40 to i64
  %42 = getelementptr inbounds [0 x i64], ptr %39, i64 0, i64 %41
  %43 = load i64, ptr %42, align 8, !tbaa !15
  %44 = icmp ne i64 %43, 0
  br i1 %44, label %46, label %45

45:                                               ; preds = %37
  store i16 -1, ptr %4, align 2
  store i32 1, ptr %9, align 4
  br label %103

46:                                               ; preds = %37
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %47 = load ptr, ptr @sm, align 8, !tbaa !8
  %48 = getelementptr inbounds %struct.save_manager, ptr %47, i32 0, i32 3
  %49 = load i8, ptr %5, align 1, !tbaa !5
  %50 = zext i8 %49 to i64
  %51 = getelementptr inbounds [0 x i64], ptr %48, i64 0, i64 %50
  %52 = load i64, ptr %51, align 8, !tbaa !15
  %53 = inttoptr i64 %52 to ptr
  store ptr %53, ptr %10, align 8, !tbaa !8
  call void @llvm.lifetime.start.p0(i64 2, ptr %11) #3
  %54 = load ptr, ptr %10, align 8, !tbaa !8
  %55 = getelementptr inbounds %struct.save_blob, ptr %54, i32 0, i32 2
  %56 = load volatile i16, ptr %55, align 2, !tbaa !10
  %57 = zext i16 %56 to i32
  %58 = load i16, ptr %7, align 2, !tbaa !10
  %59 = zext i16 %58 to i32
  %60 = icmp slt i32 %57, %59
  br i1 %60, label %61, label %66

61:                                               ; preds = %46
  %62 = load ptr, ptr %10, align 8, !tbaa !8
  %63 = getelementptr inbounds %struct.save_blob, ptr %62, i32 0, i32 2
  %64 = load volatile i16, ptr %63, align 2, !tbaa !10
  %65 = zext i16 %64 to i32
  br label %69

66:                                               ; preds = %46
  %67 = load i16, ptr %7, align 2, !tbaa !10
  %68 = zext i16 %67 to i32
  br label %69

69:                                               ; preds = %66, %61
  %70 = phi i32 [ %65, %61 ], [ %68, %66 ]
  %71 = trunc i32 %70 to i16
  store i16 %71, ptr %11, align 2, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %72 = load ptr, ptr %10, align 8, !tbaa !8
  %73 = getelementptr inbounds %struct.save_blob, ptr %72, i32 0, i32 3
  %74 = getelementptr inbounds [0 x i16], ptr %73, i64 0, i64 0
  %75 = ptrtoint ptr %74 to i64
  store i64 %75, ptr %12, align 8, !tbaa !15
  %76 = load i64, ptr %12, align 8, !tbaa !15
  %77 = load ptr, ptr %6, align 8, !tbaa !8
  %78 = load i16, ptr %11, align 2, !tbaa !10
  %79 = call signext i16 @flash_read(i64 noundef %76, ptr noundef %77, i16 noundef zeroext %78)
  %80 = load i16, ptr %11, align 2, !tbaa !10
  %81 = zext i16 %80 to i32
  %82 = and i32 %81, 1
  %83 = icmp ne i32 %82, 0
  br i1 %83, label %84, label %101

84:                                               ; preds = %69
  call void @llvm.lifetime.start.p0(i64 2, ptr %13) #3
  %85 = load i64, ptr %12, align 8, !tbaa !15
  %86 = load i16, ptr %11, align 2, !tbaa !10
  %87 = zext i16 %86 to i32
  %88 = and i32 %87, -2
  %89 = sext i32 %88 to i64
  %90 = add i64 %85, %89
  %91 = getelementptr inbounds [2 x i8], ptr %13, i64 0, i64 0
  %92 = call signext i16 @flash_read(i64 noundef %90, ptr noundef %91, i16 noundef zeroext 2)
  %93 = getelementptr inbounds [2 x i8], ptr %13, i64 0, i64 0
  %94 = load i8, ptr %93, align 1, !tbaa !5
  %95 = load ptr, ptr %8, align 8, !tbaa !8
  %96 = load i16, ptr %11, align 2, !tbaa !10
  %97 = zext i16 %96 to i32
  %98 = sub nsw i32 %97, 1
  %99 = sext i32 %98 to i64
  %100 = getelementptr inbounds i8, ptr %95, i64 %99
  store i8 %94, ptr %100, align 1, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 2, ptr %13) #3
  br label %101

101:                                              ; preds = %84, %69
  %102 = load i16, ptr %11, align 2, !tbaa !10
  store i16 %102, ptr %4, align 2
  store i32 1, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  br label %103

103:                                              ; preds = %101, %45, %36, %30
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %104 = load i16, ptr %4, align 2
  ret i16 %104
}

declare signext i16 @flash_read(i64 noundef, ptr noundef, i16 noundef zeroext) #2

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 = { "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)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"any pointer", !6, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"short", !6, i64 0}
!12 = !{!13, !6, i64 8}
!13 = !{!"sector_hdr", !14, i64 0, !6, i64 8, !6, i64 9}
!14 = !{!"long", !6, i64 0}
!15 = !{!14, !14, 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"

%struct.sector_metadata = type { %union.anon, i64 }
%union.anon = type { ptr }

@sm = external hidden local_unnamed_addr global ptr, align 8

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

; Function Attrs: mustprogress 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 noundef signext i16 @sm_load(i8 noundef zeroext %0, ptr noundef %1, i16 noundef zeroext %2) local_unnamed_addr #1 {
  %4 = alloca [2 x i8], align 1
  %5 = load ptr, ptr @sm, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %5, i64 8
  %7 = load i8, ptr %5, align 8, !tbaa !9
  %8 = zext i8 %7 to i64
  %9 = getelementptr inbounds [2 x %struct.sector_metadata], ptr %6, i64 0, i64 %8
  %10 = load ptr, ptr %9, align 8, !tbaa !9
  %11 = getelementptr inbounds i8, ptr %10, i64 8
  %12 = load volatile i8, ptr %11, align 8, !tbaa !10
  %13 = icmp uge i8 %12, %0
  %14 = ptrtoint ptr %1 to i64
  %15 = and i64 %14, 1
  %.not = icmp eq i64 %15, 0
  %or.cond = and i1 %.not, %13
  br i1 %or.cond, label %16, label %43

16:                                               ; preds = %3
  %17 = getelementptr inbounds i8, ptr %5, i64 48
  %18 = zext i8 %0 to i64
  %19 = getelementptr inbounds [0 x i64], ptr %17, i64 0, i64 %18
  %20 = load i64, ptr %19, align 8, !tbaa !13
  %.not21 = icmp eq i64 %20, 0
  br i1 %.not21, label %43, label %21

21:                                               ; preds = %16
  %22 = inttoptr i64 %20 to ptr
  %23 = getelementptr inbounds i8, ptr %22, i64 2
  %24 = load volatile i16, ptr %23, align 2, !tbaa !14
  %25 = icmp ult i16 %24, %2
  br i1 %25, label %26, label %28

26:                                               ; preds = %21
  %27 = load volatile i16, ptr %23, align 2, !tbaa !14
  br label %28

28:                                               ; preds = %21, %26
  %.in = phi i16 [ %27, %26 ], [ %2, %21 ]
  %29 = getelementptr inbounds i8, ptr %22, i64 4
  %30 = ptrtoint ptr %29 to i64
  %31 = tail call signext i16 @flash_read(i64 noundef %30, ptr noundef %1, i16 noundef zeroext %.in) #3
  %32 = zext i16 %.in to i32
  %33 = and i32 %32, 1
  %.not22 = icmp eq i32 %33, 0
  br i1 %.not22, label %43, label %34

34:                                               ; preds = %28
  call void @llvm.lifetime.start.p0(i64 2, ptr nonnull %4) #3
  %35 = and i32 %32, 65534
  %36 = zext nneg i32 %35 to i64
  %37 = add i64 %36, %30
  %38 = call signext i16 @flash_read(i64 noundef %37, ptr noundef nonnull %4, i16 noundef zeroext 2) #3
  %39 = load i8, ptr %4, align 1, !tbaa !9
  %40 = zext i16 %.in to i64
  %41 = getelementptr i8, ptr %1, i64 %40
  %42 = getelementptr i8, ptr %41, i64 -1
  store i8 %39, ptr %42, align 1, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 2, ptr nonnull %4) #3
  br label %43

43:                                               ; preds = %28, %34, %16, %3
  %.0 = phi i16 [ -3, %3 ], [ -1, %16 ], [ %.in, %34 ], [ %.in, %28 ]
  ret i16 %.0
}

declare signext i16 @flash_read(i64 noundef, ptr noundef, i16 noundef zeroext) local_unnamed_addr #2

attributes #0 = { mustprogress 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 = { "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)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!11, !7, i64 8}
!11 = !{!"sector_hdr", !12, i64 0, !7, i64 8, !7, i64 9}
!12 = !{!"long", !7, i64 0}
!13 = !{!12, !12, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"short", !7, i64 0}


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