; LLVM IR from lichen-community-systems/signaletic :: hosts/daisy/vendor/libDaisy/src/util (sd_diskio)

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

@ReadStatus = external hidden global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local i32 @SD_read(i8 noundef zeroext %0, ptr noundef %1, i64 noundef %2, i32 noundef %3) #0 {
  %5 = alloca i8, align 1
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store i8 %0, ptr %5, align 1, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !8
  store i64 %2, ptr %7, align 8, !tbaa !10
  store i32 %3, ptr %8, align 4, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  store i32 1, ptr %9, align 4, !tbaa !12
  store i32 0, ptr @ReadStatus, align 4, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  %12 = load ptr, ptr %6, align 8, !tbaa !8
  %13 = ptrtoint ptr %12 to i32
  %14 = and i32 %13, -32
  store i32 %14, ptr %11, align 4, !tbaa !12
  %15 = load i32, ptr %11, align 4, !tbaa !12
  %16 = zext i32 %15 to i64
  %17 = inttoptr i64 %16 to ptr
  %18 = load i32, ptr %8, align 4, !tbaa !12
  %19 = mul i32 %18, 512
  %20 = load ptr, ptr %6, align 8, !tbaa !8
  %21 = ptrtoint ptr %20 to i32
  %22 = load i32, ptr %11, align 4, !tbaa !12
  %23 = sub i32 %21, %22
  %24 = add i32 %19, %23
  call void @SCB_CleanDCache_by_Addr(ptr noundef %17, i32 noundef %24)
  %25 = load ptr, ptr %6, align 8, !tbaa !8
  %26 = load i64, ptr %7, align 8, !tbaa !10
  %27 = trunc i64 %26 to i32
  %28 = load i32, ptr %8, align 4, !tbaa !12
  %29 = call zeroext i8 @BSP_SD_ReadBlocks_DMA(ptr noundef %25, i32 noundef %27, i32 noundef %28)
  %30 = zext i8 %29 to i32
  %31 = icmp eq i32 %30, 0
  br i1 %31, label %32, label %74

32:                                               ; preds = %4
  %33 = call i32 @HAL_GetTick()
  store i32 %33, ptr %10, align 4, !tbaa !12
  br label %34

34:                                               ; preds = %44, %32
  %35 = load i32, ptr @ReadStatus, align 4, !tbaa !12
  %36 = icmp eq i32 %35, 0
  br i1 %36, label %37, label %42

37:                                               ; preds = %34
  %38 = call i32 @HAL_GetTick()
  %39 = load i32, ptr %10, align 4, !tbaa !12
  %40 = sub i32 %38, %39
  %41 = icmp ult i32 %40, 30000
  br label %42

42:                                               ; preds = %37, %34
  %43 = phi i1 [ false, %34 ], [ %41, %37 ]
  br i1 %43, label %44, label %45

44:                                               ; preds = %42
  br label %34, !llvm.loop !14

45:                                               ; preds = %42
  %46 = load i32, ptr @ReadStatus, align 4, !tbaa !12
  %47 = icmp eq i32 %46, 0
  br i1 %47, label %48, label %49

48:                                               ; preds = %45
  store i32 1, ptr %9, align 4, !tbaa !12
  br label %73

49:                                               ; preds = %45
  store i32 0, ptr @ReadStatus, align 4, !tbaa !12
  %50 = call i32 @HAL_GetTick()
  store i32 %50, ptr %10, align 4, !tbaa !12
  br label %51

51:                                               ; preds = %71, %49
  %52 = call i32 @HAL_GetTick()
  %53 = load i32, ptr %10, align 4, !tbaa !12
  %54 = sub i32 %52, %53
  %55 = icmp ult i32 %54, 30000
  br i1 %55, label %56, label %72

56:                                               ; preds = %51
  %57 = call zeroext i8 @BSP_SD_GetCardState()
  %58 = zext i8 %57 to i32
  %59 = icmp eq i32 %58, 0
  br i1 %59, label %60, label %71

60:                                               ; preds = %56
  store i32 0, ptr %9, align 4, !tbaa !12
  %61 = load i32, ptr %11, align 4, !tbaa !12
  %62 = zext i32 %61 to i64
  %63 = inttoptr i64 %62 to ptr
  %64 = load i32, ptr %8, align 4, !tbaa !12
  %65 = mul i32 %64, 512
  %66 = load ptr, ptr %6, align 8, !tbaa !8
  %67 = ptrtoint ptr %66 to i32
  %68 = load i32, ptr %11, align 4, !tbaa !12
  %69 = sub i32 %67, %68
  %70 = add i32 %65, %69
  call void @SCB_InvalidateDCache_by_Addr(ptr noundef %63, i32 noundef %70)
  br label %72

71:                                               ; preds = %56
  br label %51, !llvm.loop !16

72:                                               ; preds = %60, %51
  br label %73

73:                                               ; preds = %72, %48
  br label %74

74:                                               ; preds = %73, %4
  %75 = load i32, ptr %9, align 4, !tbaa !12
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  ret i32 %75
}

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @SCB_CleanDCache_by_Addr(ptr noundef, i32 noundef) #2

declare zeroext i8 @BSP_SD_ReadBlocks_DMA(ptr noundef, i32 noundef, i32 noundef) #3

declare i32 @HAL_GetTick() #3

declare zeroext i8 @BSP_SD_GetCardState() #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @SCB_InvalidateDCache_by_Addr(ptr noundef, i32 noundef) #2

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

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 = { inlinehint 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 = { "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, !11, i64 0}
!11 = !{!"long", !6, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !6, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


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

@ReadStatus = external hidden local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @SD_read(i8 noundef zeroext %0, ptr noundef %1, i64 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  store i32 0, ptr @ReadStatus, align 4, !tbaa !5
  %5 = ptrtoint ptr %1 to i64
  %6 = trunc i64 %5 to i32
  %7 = and i64 %5, 4294967264
  %8 = inttoptr i64 %7 to ptr
  %9 = shl i32 %3, 9
  %10 = and i32 %6, 31
  %11 = or disjoint i32 %9, %10
  tail call void @SCB_CleanDCache_by_Addr(ptr noundef %8, i32 noundef %11)
  %12 = trunc i64 %2 to i32
  %13 = tail call zeroext i8 @BSP_SD_ReadBlocks_DMA(ptr noundef %1, i32 noundef %12, i32 noundef %3) #3
  %14 = icmp eq i8 %13, 0
  br i1 %14, label %15, label %.loopexit

15:                                               ; preds = %4
  %16 = tail call i32 @HAL_GetTick() #3
  %.old = load i32, ptr @ReadStatus, align 4, !tbaa !5
  %.old1 = icmp eq i32 %.old, 0
  br i1 %.old1, label %.preheader, label %.critedge.thread

.preheader:                                       ; preds = %15, %.preheader
  %17 = tail call i32 @HAL_GetTick() #3
  %18 = sub i32 %17, %16
  %19 = icmp ult i32 %18, 30000
  %20 = load i32, ptr @ReadStatus, align 4
  %21 = icmp eq i32 %20, 0
  %or.cond = select i1 %19, i1 %21, i1 false
  br i1 %or.cond, label %.preheader, label %.critedge, !llvm.loop !9

.critedge:                                        ; preds = %.preheader
  br i1 %21, label %.loopexit, label %.critedge.thread

.critedge.thread:                                 ; preds = %15, %.critedge
  store i32 0, ptr @ReadStatus, align 4, !tbaa !5
  %22 = tail call i32 @HAL_GetTick() #3
  br label %23

23:                                               ; preds = %27, %.critedge.thread
  %24 = tail call i32 @HAL_GetTick() #3
  %25 = sub i32 %24, %22
  %26 = icmp ult i32 %25, 30000
  br i1 %26, label %27, label %.loopexit

27:                                               ; preds = %23
  %28 = tail call zeroext i8 @BSP_SD_GetCardState() #3
  %29 = icmp eq i8 %28, 0
  br i1 %29, label %30, label %23, !llvm.loop !11

30:                                               ; preds = %27
  tail call void @SCB_InvalidateDCache_by_Addr(ptr noundef %8, i32 noundef %11)
  br label %.loopexit

.loopexit:                                        ; preds = %23, %.critedge, %30, %4
  %.0 = phi i32 [ 0, %30 ], [ 1, %4 ], [ 1, %.critedge ], [ 1, %23 ]
  ret i32 %.0
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @SCB_CleanDCache_by_Addr(ptr noundef, i32 noundef) local_unnamed_addr #1

declare zeroext i8 @BSP_SD_ReadBlocks_DMA(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #2

declare i32 @HAL_GetTick() local_unnamed_addr #2

declare zeroext i8 @BSP_SD_GetCardState() local_unnamed_addr #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @SCB_InvalidateDCache_by_Addr(ptr noundef, i32 noundef) local_unnamed_addr #1

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 = { inlinehint 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 = { "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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass