; LLVM IR from trabucayre/openFPGALoader :: src/spiFlash.ll (ZN8SPIFlash10write_pageEiPKhi)

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

%class.SPIFlash = type <{ ptr, ptr, i8, [3 x i8], i32, ptr, i8, [7 x i8] }>

; 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: mustprogress uwtable
declare dso_local noundef i32 @_ZN8SPIFlash12write_enableEv(ptr noundef nonnull align 8 dereferenceable(33)) #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN8SPIFlash10write_pageEiPKhi(ptr noundef nonnull align 8 dereferenceable(33) %0, i32 noundef %1, ptr noundef %2, i32 noundef %3) #1 align 2 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i8, align 1
  %12 = alloca i32, align 4
  %13 = alloca ptr, align 8
  %14 = alloca i64, align 8
  %15 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !9
  store ptr %2, ptr %8, align 8, !tbaa !5
  store i32 %3, ptr %9, align 4, !tbaa !9
  %16 = load ptr, ptr %6, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.start.p0(i64 1, ptr %11) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  store i32 0, ptr %12, align 4, !tbaa !9
  %17 = load i32, ptr %7, align 4, !tbaa !9
  %18 = icmp sle i32 %17, 16777215
  br i1 %18, label %19, label %20

19:                                               ; preds = %4
  store i32 3, ptr %10, align 4, !tbaa !9
  store i8 2, ptr %11, align 1, !tbaa !11
  br label %21

20:                                               ; preds = %4
  store i32 4, ptr %10, align 4, !tbaa !9
  store i8 18, ptr %11, align 1, !tbaa !11
  br label %21

21:                                               ; preds = %20, %19
  %22 = load i32, ptr %9, align 4, !tbaa !9
  %23 = load i32, ptr %10, align 4, !tbaa !9
  %24 = add i32 %22, %23
  %25 = zext i32 %24 to i64
  %26 = call ptr @llvm.stacksave.p0()
  store ptr %26, ptr %13, align 8
  %27 = alloca i8, i64 %25, align 16
  store i64 %25, ptr %14, align 8
  %28 = load i8, ptr %11, align 1, !tbaa !11
  %29 = zext i8 %28 to i32
  %30 = icmp eq i32 %29, 18
  br i1 %30, label %31, label %40

31:                                               ; preds = %21
  %32 = load i32, ptr %7, align 4, !tbaa !9
  %33 = ashr i32 %32, 24
  %34 = and i32 255, %33
  %35 = trunc i32 %34 to i8
  %36 = load i32, ptr %12, align 4, !tbaa !9
  %37 = add i32 %36, 1
  store i32 %37, ptr %12, align 4, !tbaa !9
  %38 = zext i32 %36 to i64
  %39 = getelementptr inbounds i8, ptr %27, i64 %38
  store i8 %35, ptr %39, align 1, !tbaa !11
  br label %40

40:                                               ; preds = %31, %21
  %41 = load i32, ptr %7, align 4, !tbaa !9
  %42 = ashr i32 %41, 16
  %43 = and i32 255, %42
  %44 = trunc i32 %43 to i8
  %45 = load i32, ptr %12, align 4, !tbaa !9
  %46 = add i32 %45, 1
  store i32 %46, ptr %12, align 4, !tbaa !9
  %47 = zext i32 %45 to i64
  %48 = getelementptr inbounds i8, ptr %27, i64 %47
  store i8 %44, ptr %48, align 1, !tbaa !11
  %49 = load i32, ptr %7, align 4, !tbaa !9
  %50 = ashr i32 %49, 8
  %51 = and i32 255, %50
  %52 = trunc i32 %51 to i8
  %53 = load i32, ptr %12, align 4, !tbaa !9
  %54 = add i32 %53, 1
  store i32 %54, ptr %12, align 4, !tbaa !9
  %55 = zext i32 %53 to i64
  %56 = getelementptr inbounds i8, ptr %27, i64 %55
  store i8 %52, ptr %56, align 1, !tbaa !11
  %57 = load i32, ptr %7, align 4, !tbaa !9
  %58 = and i32 255, %57
  %59 = trunc i32 %58 to i8
  %60 = load i32, ptr %12, align 4, !tbaa !9
  %61 = add i32 %60, 1
  store i32 %61, ptr %12, align 4, !tbaa !9
  %62 = zext i32 %60 to i64
  %63 = getelementptr inbounds i8, ptr %27, i64 %62
  store i8 %59, ptr %63, align 1, !tbaa !11
  %64 = load i32, ptr %10, align 4, !tbaa !9
  %65 = zext i32 %64 to i64
  %66 = getelementptr inbounds i8, ptr %27, i64 %65
  %67 = load ptr, ptr %8, align 8, !tbaa !5
  %68 = load i32, ptr %9, align 4, !tbaa !9
  %69 = sext i32 %68 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %66, ptr align 1 %67, i64 %69, i1 false)
  %70 = call noundef i32 @_ZN8SPIFlash12write_enableEv(ptr noundef nonnull align 8 dereferenceable(33) %16)
  %71 = icmp eq i32 %70, -1
  br i1 %71, label %72, label %73

72:                                               ; preds = %40
  store i32 -1, ptr %5, align 4
  store i32 1, ptr %15, align 4
  br label %90

73:                                               ; preds = %40
  %74 = getelementptr inbounds %class.SPIFlash, ptr %16, i32 0, i32 1
  %75 = load ptr, ptr %74, align 8, !tbaa !12
  %76 = load i8, ptr %11, align 1, !tbaa !11
  %77 = load i32, ptr %9, align 4, !tbaa !9
  %78 = load i32, ptr %10, align 4, !tbaa !9
  %79 = add i32 %77, %78
  %80 = load ptr, ptr %75, align 8, !tbaa !15
  %81 = getelementptr inbounds ptr, ptr %80, i64 2
  %82 = load ptr, ptr %81, align 8
  %83 = call noundef i32 %82(ptr noundef nonnull align 8 dereferenceable(56) %75, i8 noundef zeroext %76, ptr noundef %27, ptr noundef null, i32 noundef %79)
  %84 = getelementptr inbounds %class.SPIFlash, ptr %16, i32 0, i32 1
  %85 = load ptr, ptr %84, align 8, !tbaa !12
  %86 = load ptr, ptr %85, align 8, !tbaa !15
  %87 = getelementptr inbounds ptr, ptr %86, i64 4
  %88 = load ptr, ptr %87, align 8
  %89 = call noundef i32 %88(ptr noundef nonnull align 8 dereferenceable(56) %85, i8 noundef zeroext 5, i8 noundef zeroext 1, i8 noundef zeroext 0, i32 noundef 1000, i1 noundef zeroext false)
  store i32 %89, ptr %5, align 4
  store i32 1, ptr %15, align 4
  br label %90

90:                                               ; preds = %73, %72
  %91 = load ptr, ptr %13, align 8
  call void @llvm.stackrestore.p0(ptr %91)
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  %92 = load i32, ptr %5, align 4
  ret i32 %92
}

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #2

; 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) #3

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 nosync nounwind willreturn }
attributes #3 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !6, i64 8}
!13 = !{!"_ZTS8SPIFlash", !6, i64 8, !7, i64 16, !10, i64 20, !6, i64 24, !14, i64 32}
!14 = !{!"bool", !7, i64 0}
!15 = !{!16, !16, i64 0}
!16 = !{!"vtable pointer", !8, 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"

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_ZN8SPIFlash12write_enableEv(ptr noundef nonnull align 8 dereferenceable(33)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN8SPIFlash10write_pageEiPKhi(ptr noundef nonnull align 8 dereferenceable(33) %0, i32 noundef %1, ptr nocapture noundef readonly %2, i32 noundef %3) local_unnamed_addr #0 align 2 {
  %5 = icmp sgt i32 %1, 16777215
  %. = select i1 %5, i8 18, i8 2
  %.23 = select i1 %5, i32 4, i32 3
  %6 = add i32 %.23, %3
  %7 = zext i32 %6 to i64
  %8 = alloca i8, i64 %7, align 16
  br i1 %5, label %9, label %12

9:                                                ; preds = %4
  %10 = lshr i32 %1, 24
  %11 = trunc nuw i32 %10 to i8
  store i8 %11, ptr %8, align 16, !tbaa !5
  br label %12

12:                                               ; preds = %9, %4
  %.021 = phi i32 [ 1, %9 ], [ 0, %4 ]
  %13 = lshr i32 %1, 16
  %14 = trunc i32 %13 to i8
  %15 = zext nneg i32 %.021 to i64
  %16 = getelementptr inbounds i8, ptr %8, i64 %15
  store i8 %14, ptr %16, align 1, !tbaa !5
  %17 = lshr i32 %1, 8
  %18 = trunc i32 %17 to i8
  %19 = or disjoint i32 %.021, 2
  %20 = getelementptr inbounds i8, ptr %16, i64 1
  store i8 %18, ptr %20, align 1, !tbaa !5
  %21 = trunc i32 %1 to i8
  %22 = zext nneg i32 %19 to i64
  %23 = getelementptr inbounds i8, ptr %8, i64 %22
  store i8 %21, ptr %23, align 1, !tbaa !5
  %24 = zext nneg i32 %.23 to i64
  %25 = getelementptr inbounds i8, ptr %8, i64 %24
  %26 = sext i32 %3 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %25, ptr align 1 %2, i64 %26, i1 false)
  %27 = tail call noundef i32 @_ZN8SPIFlash12write_enableEv(ptr noundef nonnull align 8 dereferenceable(33) %0)
  %28 = icmp eq i32 %27, -1
  br i1 %28, label %41, label %29

29:                                               ; preds = %12
  %30 = getelementptr inbounds i8, ptr %0, i64 8
  %31 = load ptr, ptr %30, align 8, !tbaa !8
  %32 = load ptr, ptr %31, align 8, !tbaa !13
  %33 = getelementptr inbounds i8, ptr %32, i64 16
  %34 = load ptr, ptr %33, align 8
  %35 = call noundef i32 %34(ptr noundef nonnull align 8 dereferenceable(56) %31, i8 noundef zeroext %., ptr noundef nonnull %8, ptr noundef null, i32 noundef %6)
  %36 = load ptr, ptr %30, align 8, !tbaa !8
  %37 = load ptr, ptr %36, align 8, !tbaa !13
  %38 = getelementptr inbounds i8, ptr %37, i64 32
  %39 = load ptr, ptr %38, align 8
  %40 = call noundef i32 %39(ptr noundef nonnull align 8 dereferenceable(56) %36, i8 noundef zeroext 5, i8 noundef zeroext 1, i8 noundef zeroext 0, i32 noundef 1000, i1 noundef zeroext false)
  br label %41

41:                                               ; preds = %12, %29
  %.0 = phi i32 [ %40, %29 ], [ -1, %12 ]
  ret i32 %.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) #1

attributes #0 = { mustprogress 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 nounwind willreturn memory(argmem: readwrite) }

!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++ TBAA"}
!8 = !{!9, !10, i64 8}
!9 = !{!"_ZTS8SPIFlash", !10, i64 8, !6, i64 16, !11, i64 20, !10, i64 24, !12, i64 32}
!10 = !{!"any pointer", !6, i64 0}
!11 = !{!"int", !6, i64 0}
!12 = !{!"bool", !6, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"vtable pointer", !7, i64 0}


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