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

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

@.str.54 = external hidden unnamed_addr constant [7 x i8], align 1

; 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

declare i32 @printf(ptr noundef, ...) #1

; 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

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

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

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

21:                                               ; preds = %20, %19
  %22 = load i32, ptr %8, align 4, !tbaa !9
  %23 = load i32, ptr %9, 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 %12, align 8
  %27 = alloca i8, i64 %25, align 16
  store i64 %25, ptr %13, align 8
  %28 = load i32, ptr %8, align 4, !tbaa !9
  %29 = load i32, ptr %9, align 4, !tbaa !9
  %30 = add i32 %28, %29
  %31 = zext i32 %30 to i64
  %32 = alloca i8, i64 %31, align 16
  store i64 %31, ptr %14, align 8
  %33 = load i8, ptr %10, align 1, !tbaa !11
  %34 = zext i8 %33 to i32
  %35 = icmp eq i32 %34, 19
  br i1 %35, label %36, label %45

36:                                               ; preds = %21
  %37 = load i32, ptr %6, align 4, !tbaa !9
  %38 = ashr i32 %37, 24
  %39 = and i32 255, %38
  %40 = trunc i32 %39 to i8
  %41 = load i32, ptr %11, align 4, !tbaa !9
  %42 = add i32 %41, 1
  store i32 %42, ptr %11, align 4, !tbaa !9
  %43 = zext i32 %41 to i64
  %44 = getelementptr inbounds i8, ptr %27, i64 %43
  store i8 %40, ptr %44, align 1, !tbaa !11
  br label %45

45:                                               ; preds = %36, %21
  %46 = load i32, ptr %6, align 4, !tbaa !9
  %47 = ashr i32 %46, 16
  %48 = and i32 255, %47
  %49 = trunc i32 %48 to i8
  %50 = load i32, ptr %11, align 4, !tbaa !9
  %51 = add i32 %50, 1
  store i32 %51, ptr %11, align 4, !tbaa !9
  %52 = zext i32 %50 to i64
  %53 = getelementptr inbounds i8, ptr %27, i64 %52
  store i8 %49, ptr %53, align 1, !tbaa !11
  %54 = load i32, ptr %6, align 4, !tbaa !9
  %55 = ashr i32 %54, 8
  %56 = and i32 255, %55
  %57 = trunc i32 %56 to i8
  %58 = load i32, ptr %11, align 4, !tbaa !9
  %59 = add i32 %58, 1
  store i32 %59, ptr %11, align 4, !tbaa !9
  %60 = zext i32 %58 to i64
  %61 = getelementptr inbounds i8, ptr %27, i64 %60
  store i8 %57, ptr %61, align 1, !tbaa !11
  %62 = load i32, ptr %6, align 4, !tbaa !9
  %63 = and i32 255, %62
  %64 = trunc i32 %63 to i8
  %65 = load i32, ptr %11, align 4, !tbaa !9
  %66 = add i32 %65, 1
  store i32 %66, ptr %11, align 4, !tbaa !9
  %67 = zext i32 %65 to i64
  %68 = getelementptr inbounds i8, ptr %27, i64 %67
  store i8 %64, ptr %68, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #5
  %69 = getelementptr inbounds %class.SPIFlash, ptr %16, i32 0, i32 1
  %70 = load ptr, ptr %69, align 8, !tbaa !12
  %71 = load i8, ptr %10, align 1, !tbaa !11
  %72 = load i32, ptr %8, align 4, !tbaa !9
  %73 = load i32, ptr %9, align 4, !tbaa !9
  %74 = add i32 %72, %73
  %75 = load ptr, ptr %70, align 8, !tbaa !15
  %76 = getelementptr inbounds ptr, ptr %75, i64 2
  %77 = load ptr, ptr %76, align 8
  %78 = call noundef i32 %77(ptr noundef nonnull align 8 dereferenceable(56) %70, i8 noundef zeroext %71, ptr noundef %27, ptr noundef %32, i32 noundef %74)
  store i32 %78, ptr %15, align 4, !tbaa !9
  %79 = load i32, ptr %15, align 4, !tbaa !9
  %80 = icmp eq i32 %79, 0
  br i1 %80, label %81, label %88

81:                                               ; preds = %45
  %82 = load ptr, ptr %7, align 8, !tbaa !5
  %83 = load i32, ptr %9, align 4, !tbaa !9
  %84 = zext i32 %83 to i64
  %85 = getelementptr inbounds i8, ptr %32, i64 %84
  %86 = load i32, ptr %8, align 4, !tbaa !9
  %87 = sext i32 %86 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %82, ptr align 1 %85, i64 %87, i1 false)
  br label %90

88:                                               ; preds = %45
  %89 = call i32 (ptr, ...) @printf(ptr noundef @.str.54)
  br label %90

90:                                               ; preds = %88, %81
  %91 = load i32, ptr %15, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #5
  %92 = load ptr, ptr %12, align 8
  call void @llvm.stackrestore.p0(ptr %92)
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #5
  call void @llvm.lifetime.end.p0(i64 1, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  ret i32 %91
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 = { 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 #5 = { 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"

@.str.54 = external hidden unnamed_addr constant [7 x i8], align 1

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #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

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

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

13:                                               ; preds = %10, %4
  %.024 = phi i32 [ 1, %10 ], [ 0, %4 ]
  %14 = lshr i32 %1, 16
  %15 = trunc i32 %14 to i8
  %16 = zext nneg i32 %.024 to i64
  %17 = getelementptr inbounds i8, ptr %8, i64 %16
  store i8 %15, ptr %17, align 1, !tbaa !5
  %18 = lshr i32 %1, 8
  %19 = trunc i32 %18 to i8
  %20 = or disjoint i32 %.024, 2
  %21 = getelementptr inbounds i8, ptr %17, i64 1
  store i8 %19, ptr %21, align 1, !tbaa !5
  %22 = trunc i32 %1 to i8
  %23 = zext nneg i32 %20 to i64
  %24 = getelementptr inbounds i8, ptr %8, i64 %23
  store i8 %22, ptr %24, align 1, !tbaa !5
  %25 = getelementptr inbounds i8, ptr %0, i64 8
  %26 = load ptr, ptr %25, align 8, !tbaa !8
  %27 = load ptr, ptr %26, align 8, !tbaa !13
  %28 = getelementptr inbounds i8, ptr %27, i64 16
  %29 = load ptr, ptr %28, align 8
  %30 = call noundef i32 %29(ptr noundef nonnull align 8 dereferenceable(56) %26, i8 noundef zeroext %., ptr noundef nonnull %8, ptr noundef nonnull %9, i32 noundef %6)
  %31 = icmp eq i32 %30, 0
  br i1 %31, label %32, label %36

32:                                               ; preds = %13
  %33 = zext nneg i32 %.25 to i64
  %34 = getelementptr inbounds i8, ptr %9, i64 %33
  %35 = sext i32 %3 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %2, ptr nonnull align 1 %34, i64 %35, i1 false)
  br label %38

36:                                               ; preds = %13
  %37 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.54)
  br label %38

38:                                               ; preds = %36, %32
  ret i32 %30
}

attributes #0 = { nofree nounwind "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) }
attributes #2 = { 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" }

!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