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

%struct.flash_chip = type { i64, i16, [4 x %struct.flash_region] }
%struct.flash_region = type { i64, i16, i16 }

@flash = external hidden global %struct.flash_chip, 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 @flash_sector_limits(i64 noundef %0, ptr noundef %1, ptr noundef %2) #1 {
  %4 = alloca i16, align 2
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i16, align 2
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  %12 = alloca i64, align 8
  store i64 %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !9
  %13 = load i64, ptr %5, align 8, !tbaa !5
  %14 = load i64, ptr @flash, align 8, !tbaa !11
  %15 = icmp uge i64 %13, %14
  br i1 %15, label %16, label %17

16:                                               ; preds = %3
  store i16 -1, ptr %4, align 2
  br label %70

17:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 2, ptr %8) #2
  %18 = load i16, ptr getelementptr inbounds (%struct.flash_chip, ptr @flash, i32 0, i32 1), align 8, !tbaa !14
  %19 = zext i16 %18 to i32
  %20 = sub nsw i32 %19, 1
  %21 = trunc i32 %20 to i16
  store i16 %21, ptr %8, align 2, !tbaa !15
  br label %22

22:                                               ; preds = %30, %17
  %23 = load i64, ptr %5, align 8, !tbaa !5
  %24 = load i16, ptr %8, align 2, !tbaa !15
  %25 = zext i16 %24 to i64
  %26 = getelementptr inbounds [4 x %struct.flash_region], ptr getelementptr inbounds (%struct.flash_chip, ptr @flash, i32 0, i32 2), i64 0, i64 %25
  %27 = getelementptr inbounds %struct.flash_region, ptr %26, i32 0, i32 0
  %28 = load i64, ptr %27, align 8, !tbaa !16
  %29 = icmp ult i64 %23, %28
  br i1 %29, label %30, label %33

30:                                               ; preds = %22
  %31 = load i16, ptr %8, align 2, !tbaa !15
  %32 = add i16 %31, -1
  store i16 %32, ptr %8, align 2, !tbaa !15
  br label %22, !llvm.loop !18

33:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %34 = load i16, ptr %8, align 2, !tbaa !15
  %35 = zext i16 %34 to i64
  %36 = getelementptr inbounds [4 x %struct.flash_region], ptr getelementptr inbounds (%struct.flash_chip, ptr @flash, i32 0, i32 2), i64 0, i64 %35
  store ptr %36, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  %37 = load ptr, ptr %9, align 8, !tbaa !9
  %38 = getelementptr inbounds %struct.flash_region, ptr %37, i32 0, i32 2
  %39 = load i16, ptr %38, align 2, !tbaa !20
  %40 = zext i16 %39 to i64
  %41 = mul i64 %40, 256
  store i64 %41, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #2
  %42 = load i64, ptr %5, align 8, !tbaa !5
  %43 = load ptr, ptr %9, align 8, !tbaa !9
  %44 = getelementptr inbounds %struct.flash_region, ptr %43, i32 0, i32 0
  %45 = load i64, ptr %44, align 8, !tbaa !16
  %46 = sub i64 %42, %45
  store i64 %46, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #2
  %47 = load i64, ptr %11, align 8, !tbaa !5
  %48 = load i64, ptr %10, align 8, !tbaa !5
  %49 = sub i64 %48, 1
  %50 = xor i64 %49, -1
  %51 = and i64 %47, %50
  %52 = load ptr, ptr %9, align 8, !tbaa !9
  %53 = getelementptr inbounds %struct.flash_region, ptr %52, i32 0, i32 0
  %54 = load i64, ptr %53, align 8, !tbaa !16
  %55 = add i64 %51, %54
  store i64 %55, ptr %12, align 8, !tbaa !5
  %56 = load ptr, ptr %6, align 8, !tbaa !9
  %57 = icmp ne ptr %56, null
  br i1 %57, label %58, label %61

58:                                               ; preds = %33
  %59 = load i64, ptr %12, align 8, !tbaa !5
  %60 = load ptr, ptr %6, align 8, !tbaa !9
  store i64 %59, ptr %60, align 8, !tbaa !5
  br label %61

61:                                               ; preds = %58, %33
  %62 = load ptr, ptr %7, align 8, !tbaa !9
  %63 = icmp ne ptr %62, null
  br i1 %63, label %64, label %69

64:                                               ; preds = %61
  %65 = load i64, ptr %12, align 8, !tbaa !5
  %66 = load i64, ptr %10, align 8, !tbaa !5
  %67 = add i64 %65, %66
  %68 = load ptr, ptr %7, align 8, !tbaa !9
  store i64 %67, ptr %68, align 8, !tbaa !5
  br label %69

69:                                               ; preds = %64, %61
  store i16 0, ptr %4, align 2
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %8) #2
  br label %70

70:                                               ; preds = %69, %16
  %71 = load i16, ptr %4, align 2
  ret i16 %71
}

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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"flash_chip", !6, i64 0, !13, i64 8, !7, i64 16}
!13 = !{!"short", !7, i64 0}
!14 = !{!12, !13, i64 8}
!15 = !{!13, !13, i64 0}
!16 = !{!17, !6, i64 0}
!17 = !{!"flash_region", !6, i64 0, !13, i64 8, !13, i64 10}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}
!20 = !{!17, !13, i64 10}


; 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.flash_chip = type { i64, i16, [4 x %struct.flash_region] }
%struct.flash_region = type { i64, i16, i16 }

@flash = external hidden local_unnamed_addr global %struct.flash_chip, align 8

; Function Attrs: nofree norecurse nosync nounwind memory(read, argmem: write, inaccessiblemem: none) uwtable
define dso_local signext range(i16 -1, 1) i16 @flash_sector_limits(i64 noundef %0, ptr noundef writeonly %1, ptr noundef writeonly %2) local_unnamed_addr #0 {
  %4 = load i64, ptr @flash, align 8, !tbaa !5
  %.not = icmp ugt i64 %4, %0
  br i1 %.not, label %5, label %25

5:                                                ; preds = %3
  %6 = load i16, ptr getelementptr inbounds (i8, ptr @flash, i64 8), align 8, !tbaa !11
  br label %7

7:                                                ; preds = %7, %5
  %.018.in = phi i16 [ %6, %5 ], [ %.018, %7 ]
  %.018 = add i16 %.018.in, -1
  %8 = zext i16 %.018 to i64
  %9 = getelementptr inbounds [4 x %struct.flash_region], ptr getelementptr inbounds (i8, ptr @flash, i64 16), i64 0, i64 %8
  %10 = load i64, ptr %9, align 8, !tbaa !12
  %11 = icmp ugt i64 %10, %0
  br i1 %11, label %7, label %12, !llvm.loop !14

12:                                               ; preds = %7
  %13 = getelementptr inbounds i8, ptr %9, i64 10
  %14 = load i16, ptr %13, align 2, !tbaa !16
  %15 = zext i16 %14 to i64
  %16 = shl nuw nsw i64 %15, 8
  %17 = sub i64 %0, %10
  %18 = sub nsw i64 0, %16
  %19 = and i64 %17, %18
  %20 = add i64 %19, %10
  %.not23 = icmp eq ptr %1, null
  br i1 %.not23, label %22, label %21

21:                                               ; preds = %12
  store i64 %20, ptr %1, align 8, !tbaa !17
  br label %22

22:                                               ; preds = %21, %12
  %.not24 = icmp eq ptr %2, null
  br i1 %.not24, label %25, label %23

23:                                               ; preds = %22
  %24 = add i64 %20, %16
  store i64 %24, ptr %2, align 8, !tbaa !17
  br label %25

25:                                               ; preds = %22, %23, %3
  %.0 = phi i16 [ -1, %3 ], [ 0, %23 ], [ 0, %22 ]
  ret i16 %.0
}

attributes #0 = { nofree norecurse nosync nounwind memory(read, argmem: write, inaccessiblemem: none) 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, !7, i64 0}
!6 = !{!"flash_chip", !7, i64 0, !10, i64 8, !8, i64 16}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"short", !8, i64 0}
!11 = !{!6, !10, i64 8}
!12 = !{!13, !7, i64 0}
!13 = !{!"flash_region", !7, i64 0, !10, i64 8, !10, i64 10}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!13, !10, i64 10}
!17 = !{!7, !7, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass