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

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

@flash_init.cfi_check = external hidden constant [5 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local signext i16 @flash_init() #0 {
  %1 = alloca i16, align 2
  %2 = alloca [45 x i8], align 16
  %3 = alloca i16, align 2
  %4 = alloca i32, align 4
  %5 = alloca i8, align 1
  call void @llvm.lifetime.start.p0(i64 45, ptr %2) #3
  %6 = getelementptr inbounds [45 x i8], ptr %2, i64 0, i64 0
  call void @cfi_read(ptr noundef %6)
  call void @llvm.lifetime.start.p0(i64 2, ptr %3) #3
  store i16 0, ptr %3, align 2, !tbaa !5
  br label %7

7:                                                ; preds = %26, %0
  %8 = load i16, ptr %3, align 2, !tbaa !5
  %9 = zext i16 %8 to i64
  %10 = icmp ult i64 %9, 5
  br i1 %10, label %12, label %11

11:                                               ; preds = %7
  store i32 2, ptr %4, align 4
  br label %29

12:                                               ; preds = %7
  %13 = load i16, ptr %3, align 2, !tbaa !5
  %14 = zext i16 %13 to i64
  %15 = getelementptr inbounds [45 x i8], ptr %2, i64 0, i64 %14
  %16 = load i8, ptr %15, align 1, !tbaa !9
  %17 = zext i8 %16 to i32
  %18 = load i16, ptr %3, align 2, !tbaa !5
  %19 = zext i16 %18 to i64
  %20 = getelementptr inbounds [5 x i8], ptr @flash_init.cfi_check, i64 0, i64 %19
  %21 = load i8, ptr %20, align 1, !tbaa !9
  %22 = zext i8 %21 to i32
  %23 = icmp ne i32 %17, %22
  br i1 %23, label %24, label %25

24:                                               ; preds = %12
  store i16 -1, ptr %1, align 2
  store i32 1, ptr %4, align 4
  br label %29

25:                                               ; preds = %12
  br label %26

26:                                               ; preds = %25
  %27 = load i16, ptr %3, align 2, !tbaa !5
  %28 = add i16 %27, 1
  store i16 %28, ptr %3, align 2, !tbaa !5
  br label %7, !llvm.loop !10

29:                                               ; preds = %24, %11
  call void @llvm.lifetime.end.p0(i64 2, ptr %3) #3
  %30 = load i32, ptr %4, align 4
  switch i32 %30, label %45 [
    i32 2, label %31
  ]

31:                                               ; preds = %29
  call void @llvm.lifetime.start.p0(i64 1, ptr %5) #3
  %32 = getelementptr inbounds [45 x i8], ptr %2, i64 0, i64 28
  %33 = load i8, ptr %32, align 4, !tbaa !9
  store i8 %33, ptr %5, align 1, !tbaa !9
  %34 = load i8, ptr %5, align 1, !tbaa !9
  %35 = icmp ne i8 %34, 0
  br i1 %35, label %36, label %40

36:                                               ; preds = %31
  %37 = load i8, ptr %5, align 1, !tbaa !9
  %38 = zext i8 %37 to i32
  %39 = icmp sgt i32 %38, 4
  br i1 %39, label %40, label %41

40:                                               ; preds = %36, %31
  store i16 -1, ptr %1, align 2
  store i32 1, ptr %4, align 4
  br label %44

41:                                               ; preds = %36
  %42 = getelementptr inbounds [45 x i8], ptr %2, i64 0, i64 0
  %43 = call signext i16 @metadata_populate(ptr noundef %42)
  store i16 %43, ptr %1, align 2
  store i32 1, ptr %4, align 4
  br label %44

44:                                               ; preds = %41, %40
  call void @llvm.lifetime.end.p0(i64 1, ptr %5) #3
  br label %45

45:                                               ; preds = %44, %29
  call void @llvm.lifetime.end.p0(i64 45, ptr %2) #3
  %46 = load i16, ptr %1, align 2
  ret i16 %46
}

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

; Function Attrs: noinline nounwind uwtable
declare hidden void @cfi_read(ptr noundef) #2 section ".ramprog"

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

; Function Attrs: nounwind uwtable
declare hidden signext i16 @metadata_populate(ptr noundef) #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" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"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"

@flash_init.cfi_check = external hidden local_unnamed_addr constant [5 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local signext i16 @flash_init() local_unnamed_addr #0 {
  %1 = alloca [45 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 45, ptr nonnull %1) #3
  call void @cfi_read(ptr noundef nonnull %1)
  %2 = load i8, ptr %1, align 16, !tbaa !5
  %3 = load i8, ptr @flash_init.cfi_check, align 1, !tbaa !5
  %.not = icmp eq i8 %2, %3
  br i1 %.not, label %4, label %.loopexit

4:                                                ; preds = %0
  %5 = getelementptr inbounds i8, ptr %1, i64 1
  %6 = load i8, ptr %5, align 1, !tbaa !5
  %7 = load i8, ptr getelementptr inbounds (i8, ptr @flash_init.cfi_check, i64 1), align 1, !tbaa !5
  %.not.1 = icmp eq i8 %6, %7
  br i1 %.not.1, label %8, label %.loopexit

8:                                                ; preds = %4
  %9 = getelementptr inbounds i8, ptr %1, i64 2
  %10 = load i8, ptr %9, align 2, !tbaa !5
  %11 = load i8, ptr getelementptr inbounds (i8, ptr @flash_init.cfi_check, i64 2), align 1, !tbaa !5
  %.not.2 = icmp eq i8 %10, %11
  br i1 %.not.2, label %12, label %.loopexit

12:                                               ; preds = %8
  %13 = getelementptr inbounds i8, ptr %1, i64 3
  %14 = load i8, ptr %13, align 1, !tbaa !5
  %15 = load i8, ptr getelementptr inbounds (i8, ptr @flash_init.cfi_check, i64 3), align 1, !tbaa !5
  %.not.3 = icmp eq i8 %14, %15
  br i1 %.not.3, label %16, label %.loopexit

16:                                               ; preds = %12
  %17 = getelementptr inbounds i8, ptr %1, i64 4
  %18 = load i8, ptr %17, align 4, !tbaa !5
  %19 = load i8, ptr getelementptr inbounds (i8, ptr @flash_init.cfi_check, i64 4), align 1, !tbaa !5
  %.not.4 = icmp eq i8 %18, %19
  br i1 %.not.4, label %.critedge, label %.loopexit

.critedge:                                        ; preds = %16
  %20 = getelementptr inbounds i8, ptr %1, i64 28
  %21 = load i8, ptr %20, align 4, !tbaa !5
  %22 = add i8 %21, -5
  %or.cond = icmp ult i8 %22, -4
  br i1 %or.cond, label %.loopexit, label %23

23:                                               ; preds = %.critedge
  %24 = call signext i16 @metadata_populate(ptr noundef nonnull %1)
  br label %.loopexit

.loopexit:                                        ; preds = %0, %4, %8, %12, %16, %23, %.critedge
  %.2 = phi i16 [ %24, %23 ], [ -1, %.critedge ], [ -1, %16 ], [ -1, %12 ], [ -1, %8 ], [ -1, %4 ], [ -1, %0 ]
  call void @llvm.lifetime.end.p0(i64 45, ptr nonnull %1) #3
  ret i16 %.2
}

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

; Function Attrs: noinline nounwind uwtable
declare hidden void @cfi_read(ptr noundef) local_unnamed_addr #2 section ".ramprog"

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

; Function Attrs: nounwind uwtable
declare hidden signext i16 @metadata_populate(ptr noundef) 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" }
attributes #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,SROAPass,SimplifyCFGPass