; 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_blob = type { i8, %union.anon.0, i16, [0 x i16] }
%union.anon.0 = type { i8 }
%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_delete(i8 noundef zeroext %0) #1 {
  %2 = alloca i16, align 2
  %3 = alloca i8, align 1
  %4 = alloca ptr, align 8
  %5 = alloca %struct.save_blob, align 2
  %6 = alloca i16, align 2
  store i8 %0, ptr %3, align 1, !tbaa !5
  %7 = load i8, ptr %3, align 1, !tbaa !5
  %8 = zext i8 %7 to i32
  %9 = load ptr, ptr @sm, align 8, !tbaa !8
  %10 = getelementptr inbounds %struct.save_manager, ptr %9, i32 0, i32 1
  %11 = load ptr, ptr @sm, align 8, !tbaa !8
  %12 = getelementptr inbounds %struct.save_manager, ptr %11, i32 0, i32 0
  %13 = load i8, ptr %12, align 8, !tbaa !5
  %14 = zext i8 %13 to i64
  %15 = getelementptr inbounds [2 x %struct.sector_metadata], ptr %10, i64 0, i64 %14
  %16 = getelementptr inbounds %struct.sector_metadata, ptr %15, i32 0, i32 0
  %17 = load ptr, ptr %16, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.sector_hdr, ptr %17, i32 0, i32 1
  %19 = load volatile i8, ptr %18, align 8, !tbaa !10
  %20 = zext i8 %19 to i32
  %21 = icmp sge i32 %8, %20
  br i1 %21, label %22, label %23

22:                                               ; preds = %1
  store i16 -3, ptr %2, align 2
  br label %67

23:                                               ; preds = %1
  %24 = load ptr, ptr @sm, align 8, !tbaa !8
  %25 = getelementptr inbounds %struct.save_manager, ptr %24, i32 0, i32 3
  %26 = load i8, ptr %3, align 1, !tbaa !5
  %27 = zext i8 %26 to i64
  %28 = getelementptr inbounds [0 x i64], ptr %25, i64 0, i64 %27
  %29 = load i64, ptr %28, align 8, !tbaa !13
  %30 = icmp ne i64 %29, 0
  br i1 %30, label %32, label %31

31:                                               ; preds = %23
  store i16 0, ptr %2, align 2
  br label %67

32:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %33 = load ptr, ptr @sm, align 8, !tbaa !8
  %34 = getelementptr inbounds %struct.save_manager, ptr %33, i32 0, i32 3
  %35 = load i8, ptr %3, align 1, !tbaa !5
  %36 = zext i8 %35 to i64
  %37 = getelementptr inbounds [0 x i64], ptr %34, i64 0, i64 %36
  %38 = load i64, ptr %37, align 8, !tbaa !13
  %39 = inttoptr i64 %38 to ptr
  store ptr %39, ptr %4, align 8, !tbaa !8
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  %40 = load ptr, ptr %4, align 8, !tbaa !8
  call void @llvm.memcpy.p0.p0.i64(ptr align 2 %5, ptr align 2 %40, i64 4, i1 true)
  %41 = getelementptr inbounds %struct.save_blob, ptr %5, i32 0, i32 1
  %42 = load i8, ptr %41, align 1
  %43 = and i8 %42, -5
  %44 = or i8 %43, 0
  store i8 %44, ptr %41, align 1
  call void @llvm.lifetime.start.p0(i64 2, ptr %6) #4
  %45 = load ptr, ptr %4, align 8, !tbaa !8
  %46 = ptrtoint ptr %45 to i64
  %47 = call zeroext i16 @flash_program(i64 noundef %46, ptr noundef %5, i16 noundef zeroext 2)
  %48 = zext i16 %47 to i32
  %49 = icmp ne i32 2, %48
  %50 = zext i1 %49 to i32
  %51 = trunc i32 %50 to i16
  store i16 %51, ptr %6, align 2, !tbaa !14
  %52 = load i16, ptr %6, align 2, !tbaa !14
  %53 = icmp ne i16 %52, 0
  br i1 %53, label %60, label %54

54:                                               ; preds = %32
  %55 = load ptr, ptr @sm, align 8, !tbaa !8
  %56 = getelementptr inbounds %struct.save_manager, ptr %55, i32 0, i32 3
  %57 = load i8, ptr %3, align 1, !tbaa !5
  %58 = zext i8 %57 to i64
  %59 = getelementptr inbounds [0 x i64], ptr %56, i64 0, i64 %58
  store i64 0, ptr %59, align 8, !tbaa !13
  br label %60

60:                                               ; preds = %54, %32
  %61 = load i16, ptr %6, align 2, !tbaa !14
  %62 = sext i16 %61 to i32
  %63 = icmp ne i32 %62, 0
  %64 = zext i1 %63 to i64
  %65 = select i1 %63, i32 -2, i32 0
  %66 = trunc i32 %65 to i16
  store i16 %66, ptr %2, align 2
  call void @llvm.lifetime.end.p0(i64 2, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  br label %67

67:                                               ; preds = %60, %31, %22
  %68 = load i16, ptr %2, align 2
  ret i16 %68
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

declare zeroext i16 @flash_program(i64 noundef, ptr noundef, i16 noundef zeroext) #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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!"any pointer", !6, i64 0}
!10 = !{!11, !6, i64 8}
!11 = !{!"sector_hdr", !12, i64 0, !6, i64 8, !6, i64 9}
!12 = !{!"long", !6, i64 0}
!13 = !{!12, !12, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"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"

%struct.save_blob = type { i8, %union.anon.0, i16, [0 x i16] }
%union.anon.0 = type { i8 }
%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 signext range(i16 -3, 1) i16 @sm_delete(i8 noundef zeroext %0) local_unnamed_addr #1 {
  %2 = alloca %struct.save_blob, align 2
  %3 = load ptr, ptr @sm, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %3, i64 8
  %5 = load i8, ptr %3, align 8, !tbaa !9
  %6 = zext i8 %5 to i64
  %7 = getelementptr inbounds [2 x %struct.sector_metadata], ptr %4, i64 0, i64 %6
  %8 = load ptr, ptr %7, align 8, !tbaa !9
  %9 = getelementptr inbounds i8, ptr %8, i64 8
  %10 = load volatile i8, ptr %9, align 8, !tbaa !10
  %.not = icmp ugt i8 %10, %0
  br i1 %.not, label %11, label %28

11:                                               ; preds = %1
  %12 = getelementptr inbounds i8, ptr %3, i64 48
  %13 = zext i8 %0 to i64
  %14 = getelementptr inbounds [0 x i64], ptr %12, i64 0, i64 %13
  %15 = load i64, ptr %14, align 8, !tbaa !13
  %.not9 = icmp eq i64 %15, 0
  br i1 %.not9, label %28, label %16

16:                                               ; preds = %11
  %17 = inttoptr i64 %15 to ptr
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %2) #4
  call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 2 %2, ptr align 2 %17, i64 4, i1 true)
  %18 = getelementptr inbounds i8, ptr %2, i64 1
  %19 = load i8, ptr %18, align 1
  %20 = and i8 %19, -5
  store i8 %20, ptr %18, align 1
  %21 = call zeroext i16 @flash_program(i64 noundef %15, ptr noundef nonnull %2, i16 noundef zeroext 2) #4
  %.not10 = icmp eq i16 %21, 2
  br i1 %.not10, label %22, label %26

22:                                               ; preds = %16
  %23 = load ptr, ptr @sm, align 8, !tbaa !5
  %24 = getelementptr inbounds i8, ptr %23, i64 48
  %25 = getelementptr inbounds [0 x i64], ptr %24, i64 0, i64 %13
  store i64 0, ptr %25, align 8, !tbaa !13
  br label %26

26:                                               ; preds = %22, %16
  %27 = phi i16 [ 0, %22 ], [ -2, %16 ]
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %2) #4
  br label %28

28:                                               ; preds = %11, %1, %26
  %.0 = phi i16 [ %27, %26 ], [ -3, %1 ], [ 0, %11 ]
  ret i16 %.0
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

declare zeroext i16 @flash_program(i64 noundef, ptr noundef, i16 noundef zeroext) local_unnamed_addr #3

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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!"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}


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