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

@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_save(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 i16, align 2
  %9 = alloca i32, align 4
  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 2, ptr %8) #2
  store i16 0, ptr %8, align 2, !tbaa !10
  %10 = load i8, ptr %5, align 1, !tbaa !5
  %11 = zext i8 %10 to i32
  %12 = load ptr, ptr @sm, align 8, !tbaa !8
  %13 = getelementptr inbounds %struct.save_manager, ptr %12, i32 0, i32 1
  %14 = load ptr, ptr @sm, align 8, !tbaa !8
  %15 = getelementptr inbounds %struct.save_manager, ptr %14, i32 0, i32 0
  %16 = load i8, ptr %15, align 8, !tbaa !5
  %17 = zext i8 %16 to i64
  %18 = getelementptr inbounds [2 x %struct.sector_metadata], ptr %13, i64 0, i64 %17
  %19 = getelementptr inbounds %struct.sector_metadata, ptr %18, i32 0, i32 0
  %20 = load ptr, ptr %19, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.sector_hdr, ptr %20, i32 0, i32 1
  %22 = load volatile i8, ptr %21, align 8, !tbaa !12
  %23 = zext i8 %22 to i32
  %24 = icmp sge i32 %11, %23
  br i1 %24, label %25, label %26

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

26:                                               ; preds = %3
  %27 = load ptr, ptr %6, align 8, !tbaa !8
  %28 = ptrtoint ptr %27 to i64
  %29 = and i64 %28, 1
  %30 = icmp ne i64 %29, 0
  br i1 %30, label %31, label %32

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

32:                                               ; preds = %26
  %33 = load ptr, ptr @sm, align 8, !tbaa !8
  %34 = getelementptr inbounds %struct.save_manager, ptr %33, i32 0, i32 2
  %35 = load i64, ptr %34, align 8, !tbaa !15
  %36 = load i16, ptr %7, align 2, !tbaa !10
  %37 = call zeroext i16 @blob_len(i16 noundef zeroext %36)
  %38 = zext i16 %37 to i64
  %39 = add i64 %35, %38
  %40 = load ptr, ptr @sm, align 8, !tbaa !8
  %41 = getelementptr inbounds %struct.save_manager, ptr %40, i32 0, i32 1
  %42 = load ptr, ptr @sm, align 8, !tbaa !8
  %43 = getelementptr inbounds %struct.save_manager, ptr %42, i32 0, i32 0
  %44 = load i8, ptr %43, align 8, !tbaa !5
  %45 = zext i8 %44 to i64
  %46 = getelementptr inbounds [2 x %struct.sector_metadata], ptr %41, i64 0, i64 %45
  %47 = getelementptr inbounds %struct.sector_metadata, ptr %46, i32 0, i32 1
  %48 = load i64, ptr %47, align 8, !tbaa !16
  %49 = icmp uge i64 %39, %48
  br i1 %49, label %50, label %55

50:                                               ; preds = %32
  %51 = load i8, ptr %5, align 1, !tbaa !5
  %52 = load ptr, ptr %6, align 8, !tbaa !8
  %53 = load i16, ptr %7, align 2, !tbaa !10
  %54 = call signext i16 @sector_cross(i8 noundef zeroext %51, ptr noundef %52, i16 noundef zeroext %53)
  store i16 %54, ptr %8, align 2, !tbaa !10
  br label %60

55:                                               ; preds = %32
  %56 = load i8, ptr %5, align 1, !tbaa !5
  %57 = load ptr, ptr %6, align 8, !tbaa !8
  %58 = load i16, ptr %7, align 2, !tbaa !10
  %59 = call signext i16 @blob_save(i8 noundef zeroext %56, ptr noundef %57, i16 noundef zeroext %58)
  store i16 %59, ptr %8, align 2, !tbaa !10
  br label %60

60:                                               ; preds = %55, %50
  %61 = load i16, ptr %8, align 2, !tbaa !10
  store i16 %61, ptr %4, align 2
  store i32 1, ptr %9, align 4
  br label %62

62:                                               ; preds = %60, %31, %25
  call void @llvm.lifetime.end.p0(i64 2, ptr %8) #2
  %63 = load i16, ptr %4, align 2
  ret i16 %63
}

; Function Attrs: nounwind uwtable
declare hidden zeroext i16 @blob_len(i16 noundef zeroext) #1

; Function Attrs: nounwind uwtable
declare hidden signext i16 @sector_cross(i8 noundef zeroext, ptr noundef, i16 noundef zeroext) #1

; Function Attrs: nounwind uwtable
declare hidden signext i16 @blob_save(i8 noundef zeroext, ptr noundef, i16 noundef zeroext) #1

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 = !{!"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}
!16 = !{!17, !14, i64 8}
!17 = !{!"sector_metadata", !6, i64 0, !14, i64 8}


; 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: nounwind uwtable
define dso_local signext i16 @sm_save(i8 noundef zeroext %0, ptr noundef %1, i16 noundef zeroext %2) local_unnamed_addr #0 {
  %4 = load ptr, ptr @sm, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %4, i64 8
  %6 = load i8, ptr %4, align 8, !tbaa !9
  %7 = zext i8 %6 to i64
  %8 = getelementptr inbounds [2 x %struct.sector_metadata], ptr %5, i64 0, i64 %7
  %9 = load ptr, ptr %8, align 8, !tbaa !9
  %10 = getelementptr inbounds i8, ptr %9, i64 8
  %11 = load volatile i8, ptr %10, align 8, !tbaa !10
  %.not = icmp ugt i8 %11, %0
  %12 = ptrtoint ptr %1 to i64
  %13 = and i64 %12, 1
  %.not11 = icmp eq i64 %13, 0
  %or.cond = and i1 %.not11, %.not
  br i1 %or.cond, label %14, label %30

14:                                               ; preds = %3
  %15 = getelementptr inbounds i8, ptr %4, i64 40
  %16 = load i64, ptr %15, align 8, !tbaa !13
  %17 = tail call zeroext i16 @blob_len(i16 noundef zeroext %2)
  %18 = zext i16 %17 to i64
  %19 = add i64 %16, %18
  %20 = load ptr, ptr @sm, align 8, !tbaa !5
  %21 = getelementptr inbounds i8, ptr %20, i64 8
  %22 = load i8, ptr %20, align 8, !tbaa !9
  %23 = zext i8 %22 to i64
  %24 = getelementptr inbounds [2 x %struct.sector_metadata], ptr %21, i64 0, i64 %23, i32 1
  %25 = load i64, ptr %24, align 8, !tbaa !14
  %.not12 = icmp ult i64 %19, %25
  br i1 %.not12, label %28, label %26

26:                                               ; preds = %14
  %27 = tail call signext i16 @sector_cross(i8 noundef zeroext %0, ptr noundef %1, i16 noundef zeroext %2)
  br label %30

28:                                               ; preds = %14
  %29 = tail call signext i16 @blob_save(i8 noundef zeroext %0, ptr noundef %1, i16 noundef zeroext %2)
  br label %30

30:                                               ; preds = %26, %28, %3
  %.010 = phi i16 [ -3, %3 ], [ %27, %26 ], [ %29, %28 ]
  ret i16 %.010
}

; Function Attrs: nounwind uwtable
declare hidden zeroext i16 @blob_len(i16 noundef zeroext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden signext i16 @sector_cross(i8 noundef zeroext, ptr noundef, i16 noundef zeroext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden signext i16 @blob_save(i8 noundef zeroext, ptr noundef, i16 noundef zeroext) local_unnamed_addr #0

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

!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, !12, i64 8}
!15 = !{!"sector_metadata", !7, i64 0, !12, i64 8}


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