; LLVM IR from lichen-community-systems/signaletic :: hosts/daisy/vendor/libDaisy/Drivers/STM32H7xx_HAL_Driver/Src (sd)

; 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.SD_HandleTypeDef = type { ptr, %struct.SDMMC_InitTypeDef, i32, ptr, i32, ptr, i32, i32, i32, i32, %struct.HAL_SD_CardInfoTypeDef, [4 x i32], [4 x i32] }
%struct.SDMMC_InitTypeDef = type { i32, i32, i32, i32, i32 }
%struct.HAL_SD_CardInfoTypeDef = type { i32, i32, i32, i32, i32, i32, i32, i32, i32 }

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

declare i32 @HAL_GetTick() #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @HAL_SD_GetCardState(ptr noundef) #2

; 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 i32 @HAL_SD_ConfigSpeedBusOperation(ptr noundef %0, i32 noundef %1) #2 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !9
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.SD_HandleTypeDef, ptr %9, i32 0, i32 8
  store volatile i32 3, ptr %10, align 8, !tbaa !11
  %11 = load i32, ptr %5, align 4, !tbaa !9
  switch i32 %11, label %77 [
    i32 0, label %12
    i32 2, label %42
    i32 1, label %82
    i32 3, label %76
  ]

12:                                               ; preds = %2
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.SD_HandleTypeDef, ptr %13, i32 0, i32 10
  %15 = getelementptr inbounds %struct.HAL_SD_CardInfoTypeDef, ptr %14, i32 0, i32 8
  %16 = load i32, ptr %15, align 8, !tbaa !15
  %17 = icmp eq i32 %16, 512
  br i1 %17, label %30, label %18

18:                                               ; preds = %12
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.SD_HandleTypeDef, ptr %19, i32 0, i32 10
  %21 = getelementptr inbounds %struct.HAL_SD_CardInfoTypeDef, ptr %20, i32 0, i32 8
  %22 = load i32, ptr %21, align 8, !tbaa !15
  %23 = icmp eq i32 %22, 256
  br i1 %23, label %30, label %24

24:                                               ; preds = %18
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.SD_HandleTypeDef, ptr %25, i32 0, i32 10
  %27 = getelementptr inbounds %struct.HAL_SD_CardInfoTypeDef, ptr %26, i32 0, i32 0
  %28 = load i32, ptr %27, align 8, !tbaa !16
  %29 = icmp eq i32 %28, 1
  br i1 %29, label %30, label %40

30:                                               ; preds = %24, %18, %12
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = call i32 @SD_HighSpeed(ptr noundef %31)
  %33 = icmp ne i32 %32, 0
  br i1 %33, label %34, label %39

34:                                               ; preds = %30
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.SD_HandleTypeDef, ptr %35, i32 0, i32 9
  %37 = load volatile i32, ptr %36, align 4, !tbaa !17
  %38 = or i32 %37, 268435456
  store volatile i32 %38, ptr %36, align 4, !tbaa !17
  store i32 1, ptr %7, align 4, !tbaa !9
  br label %39

39:                                               ; preds = %34, %30
  br label %41

40:                                               ; preds = %24
  br label %41

41:                                               ; preds = %40, %39
  br label %82

42:                                               ; preds = %2
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.SD_HandleTypeDef, ptr %43, i32 0, i32 10
  %45 = getelementptr inbounds %struct.HAL_SD_CardInfoTypeDef, ptr %44, i32 0, i32 8
  %46 = load i32, ptr %45, align 8, !tbaa !15
  %47 = icmp eq i32 %46, 512
  br i1 %47, label %60, label %48

48:                                               ; preds = %42
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.SD_HandleTypeDef, ptr %49, i32 0, i32 10
  %51 = getelementptr inbounds %struct.HAL_SD_CardInfoTypeDef, ptr %50, i32 0, i32 8
  %52 = load i32, ptr %51, align 8, !tbaa !15
  %53 = icmp eq i32 %52, 256
  br i1 %53, label %60, label %54

54:                                               ; preds = %48
  %55 = load ptr, ptr %4, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.SD_HandleTypeDef, ptr %55, i32 0, i32 10
  %57 = getelementptr inbounds %struct.HAL_SD_CardInfoTypeDef, ptr %56, i32 0, i32 0
  %58 = load i32, ptr %57, align 8, !tbaa !16
  %59 = icmp eq i32 %58, 1
  br i1 %59, label %60, label %70

60:                                               ; preds = %54, %48, %42
  %61 = load ptr, ptr %4, align 8, !tbaa !5
  %62 = call i32 @SD_HighSpeed(ptr noundef %61)
  %63 = icmp ne i32 %62, 0
  br i1 %63, label %64, label %69

64:                                               ; preds = %60
  %65 = load ptr, ptr %4, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.SD_HandleTypeDef, ptr %65, i32 0, i32 9
  %67 = load volatile i32, ptr %66, align 4, !tbaa !17
  %68 = or i32 %67, 268435456
  store volatile i32 %68, ptr %66, align 4, !tbaa !17
  store i32 1, ptr %7, align 4, !tbaa !9
  br label %69

69:                                               ; preds = %64, %60
  br label %75

70:                                               ; preds = %54
  %71 = load ptr, ptr %4, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.SD_HandleTypeDef, ptr %71, i32 0, i32 9
  %73 = load volatile i32, ptr %72, align 4, !tbaa !17
  %74 = or i32 %73, 268435456
  store volatile i32 %74, ptr %72, align 4, !tbaa !17
  store i32 1, ptr %7, align 4, !tbaa !9
  br label %75

75:                                               ; preds = %70, %69
  br label %82

76:                                               ; preds = %2
  br label %77

77:                                               ; preds = %76, %2
  %78 = load ptr, ptr %4, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.SD_HandleTypeDef, ptr %78, i32 0, i32 9
  %80 = load volatile i32, ptr %79, align 4, !tbaa !17
  %81 = or i32 %80, 134217728
  store volatile i32 %81, ptr %79, align 4, !tbaa !17
  store i32 1, ptr %7, align 4, !tbaa !9
  br label %82

82:                                               ; preds = %77, %75, %41, %2
  %83 = call i32 @HAL_GetTick()
  store i32 %83, ptr %6, align 4, !tbaa !9
  br label %84

84:                                               ; preds = %98, %82
  %85 = load ptr, ptr %4, align 8, !tbaa !5
  %86 = call i32 @HAL_SD_GetCardState(ptr noundef %85)
  %87 = icmp ne i32 %86, 4
  br i1 %87, label %88, label %99

88:                                               ; preds = %84
  %89 = call i32 @HAL_GetTick()
  %90 = load i32, ptr %6, align 4, !tbaa !9
  %91 = sub i32 %89, %90
  %92 = icmp uge i32 %91, -1
  br i1 %92, label %93, label %98

93:                                               ; preds = %88
  %94 = load ptr, ptr %4, align 8, !tbaa !5
  %95 = getelementptr inbounds %struct.SD_HandleTypeDef, ptr %94, i32 0, i32 9
  store volatile i32 -2147483648, ptr %95, align 4, !tbaa !17
  %96 = load ptr, ptr %4, align 8, !tbaa !5
  %97 = getelementptr inbounds %struct.SD_HandleTypeDef, ptr %96, i32 0, i32 8
  store volatile i32 1, ptr %97, align 8, !tbaa !11
  store i32 3, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %103

98:                                               ; preds = %88
  br label %84, !llvm.loop !18

99:                                               ; preds = %84
  %100 = load ptr, ptr %4, align 8, !tbaa !5
  %101 = getelementptr inbounds %struct.SD_HandleTypeDef, ptr %100, i32 0, i32 8
  store volatile i32 1, ptr %101, align 8, !tbaa !11
  %102 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %102, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %103

103:                                              ; preds = %99, %93
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  %104 = load i32, ptr %3, align 4
  ret i32 %104
}

; Function Attrs: nounwind uwtable
declare hidden i32 @SD_HighSpeed(ptr noundef) #2

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 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 64}
!12 = !{!"", !6, i64 0, !13, i64 8, !10, i64 28, !6, i64 32, !10, i64 40, !6, i64 48, !10, i64 56, !10, i64 60, !10, i64 64, !10, i64 68, !14, i64 72, !7, i64 108, !7, i64 124}
!13 = !{!"", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !10, i64 16}
!14 = !{!"", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20, !10, i64 24, !10, i64 28, !10, i64 32}
!15 = !{!12, !10, i64 104}
!16 = !{!12, !10, i64 72}
!17 = !{!12, !10, i64 68}
!18 = distinct !{!18, !19}
!19 = !{!"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"

declare i32 @HAL_GetTick() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @HAL_SD_GetCardState(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @HAL_SD_ConfigSpeedBusOperation(ptr noundef %0, i32 noundef %1) local_unnamed_addr #1 {
  %3 = getelementptr inbounds i8, ptr %0, i64 64
  store volatile i32 3, ptr %3, align 8, !tbaa !5
  switch i32 %1, label %.sink.split [
    i32 0, label %4
    i32 2, label %13
    i32 1, label %25
  ]

4:                                                ; preds = %2
  %5 = getelementptr inbounds i8, ptr %0, i64 104
  %6 = load i32, ptr %5, align 8, !tbaa !13
  switch i32 %6, label %7 [
    i32 512, label %11
    i32 256, label %11
  ]

7:                                                ; preds = %4
  %8 = getelementptr inbounds i8, ptr %0, i64 72
  %9 = load i32, ptr %8, align 8, !tbaa !14
  %10 = icmp eq i32 %9, 1
  br i1 %10, label %11, label %25

11:                                               ; preds = %4, %4, %7
  %12 = tail call i32 @SD_HighSpeed(ptr noundef nonnull %0)
  %.not23 = icmp eq i32 %12, 0
  br i1 %.not23, label %25, label %.sink.split

13:                                               ; preds = %2
  %14 = getelementptr inbounds i8, ptr %0, i64 104
  %15 = load i32, ptr %14, align 8, !tbaa !13
  switch i32 %15, label %16 [
    i32 512, label %20
    i32 256, label %20
  ]

16:                                               ; preds = %13
  %17 = getelementptr inbounds i8, ptr %0, i64 72
  %18 = load i32, ptr %17, align 8, !tbaa !14
  %19 = icmp eq i32 %18, 1
  br i1 %19, label %20, label %.sink.split

20:                                               ; preds = %13, %13, %16
  %21 = tail call i32 @SD_HighSpeed(ptr noundef nonnull %0)
  %.not = icmp eq i32 %21, 0
  br i1 %.not, label %25, label %.sink.split

.sink.split:                                      ; preds = %2, %16, %20, %11
  %.sink27 = phi i32 [ 268435456, %11 ], [ 268435456, %20 ], [ 268435456, %16 ], [ 134217728, %2 ]
  %22 = getelementptr inbounds i8, ptr %0, i64 68
  %23 = load volatile i32, ptr %22, align 4, !tbaa !15
  %24 = or i32 %23, %.sink27
  store volatile i32 %24, ptr %22, align 4, !tbaa !15
  br label %25

25:                                               ; preds = %.sink.split, %20, %11, %7, %2
  %.0 = phi i32 [ 0, %2 ], [ 0, %20 ], [ 0, %11 ], [ 0, %7 ], [ 1, %.sink.split ]
  %26 = tail call i32 @HAL_GetTick() #2
  br label %27

27:                                               ; preds = %29, %25
  %28 = tail call i32 @HAL_SD_GetCardState(ptr noundef %0)
  %.not24 = icmp eq i32 %28, 4
  br i1 %.not24, label %.loopexit, label %29

29:                                               ; preds = %27
  %30 = tail call i32 @HAL_GetTick() #2
  %31 = sub i32 %30, %26
  %32 = icmp eq i32 %31, -1
  br i1 %32, label %33, label %27, !llvm.loop !16

33:                                               ; preds = %29
  %34 = getelementptr inbounds i8, ptr %0, i64 68
  store volatile i32 -2147483648, ptr %34, align 4, !tbaa !15
  br label %.loopexit

.loopexit:                                        ; preds = %27, %33
  %.020 = phi i32 [ 3, %33 ], [ %.0, %27 ]
  store volatile i32 1, ptr %3, align 8, !tbaa !5
  ret i32 %.020
}

; Function Attrs: nounwind uwtable
declare hidden i32 @SD_HighSpeed(ptr noundef) local_unnamed_addr #1

attributes #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 = { 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, !11, i64 64}
!6 = !{!"", !7, i64 0, !10, i64 8, !11, i64 28, !7, i64 32, !11, i64 40, !7, i64 48, !11, i64 56, !11, i64 60, !11, i64 64, !11, i64 68, !12, i64 72, !8, i64 108, !8, i64 124}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12, !11, i64 16}
!11 = !{!"int", !8, i64 0}
!12 = !{!"", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12, !11, i64 16, !11, i64 20, !11, i64 24, !11, i64 28, !11, i64 32}
!13 = !{!6, !11, i64 104}
!14 = !{!6, !11, i64 72}
!15 = !{!6, !11, i64 68}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass