; 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.HAL_SD_CardStatusTypedef = type { i8, i8, i16, i32, i8, i8, i8, i16, i8, i8, i8, i8, i8 }
%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: nounwind uwtable
define dso_local i32 @HAL_SD_Init(ptr noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca %struct.HAL_SD_CardStatusTypedef, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 20, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = icmp eq ptr %9, null
  br i1 %10, label %11, label %12

11:                                               ; preds = %1
  store i32 1, ptr %2, align 4
  store i32 1, ptr %8, align 4
  br label %103

12:                                               ; preds = %1
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.SD_HandleTypeDef, ptr %13, i32 0, i32 8
  %15 = load volatile i32, ptr %14, align 8, !tbaa !9
  %16 = icmp eq i32 %15, 0
  br i1 %16, label %17, label %21

17:                                               ; preds = %12
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.SD_HandleTypeDef, ptr %18, i32 0, i32 2
  store i32 0, ptr %19, align 4, !tbaa !14
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  call void @HAL_SD_MspInit(ptr noundef %20)
  br label %21

21:                                               ; preds = %17, %12
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.SD_HandleTypeDef, ptr %22, i32 0, i32 8
  store volatile i32 3, ptr %23, align 8, !tbaa !9
  %24 = load ptr, ptr %3, align 8, !tbaa !5
  %25 = call i32 @HAL_SD_InitCard(ptr noundef %24)
  %26 = icmp ne i32 %25, 0
  br i1 %26, label %27, label %28

27:                                               ; preds = %21
  store i32 1, ptr %2, align 4
  store i32 1, ptr %8, align 4
  br label %103

28:                                               ; preds = %21
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = call i32 @HAL_SD_GetCardStatus(ptr noundef %29, ptr noundef %4)
  %31 = icmp ne i32 %30, 0
  br i1 %31, label %32, label %33

32:                                               ; preds = %28
  store i32 1, ptr %2, align 4
  store i32 1, ptr %8, align 4
  br label %103

33:                                               ; preds = %28
  %34 = getelementptr inbounds %struct.HAL_SD_CardStatusTypedef, ptr %4, i32 0, i32 10
  %35 = load volatile i8, ptr %34, align 4, !tbaa !15
  %36 = zext i8 %35 to i32
  store i32 %36, ptr %5, align 4, !tbaa !18
  %37 = getelementptr inbounds %struct.HAL_SD_CardStatusTypedef, ptr %4, i32 0, i32 11
  %38 = load volatile i8, ptr %37, align 1, !tbaa !19
  %39 = zext i8 %38 to i32
  store i32 %39, ptr %6, align 4, !tbaa !18
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.SD_HandleTypeDef, ptr %40, i32 0, i32 10
  %42 = getelementptr inbounds %struct.HAL_SD_CardInfoTypeDef, ptr %41, i32 0, i32 0
  %43 = load i32, ptr %42, align 8, !tbaa !20
  %44 = icmp eq i32 %43, 1
  br i1 %44, label %45, label %55

45:                                               ; preds = %33
  %46 = load i32, ptr %5, align 4, !tbaa !18
  %47 = icmp ne i32 %46, 0
  br i1 %47, label %51, label %48

48:                                               ; preds = %45
  %49 = load i32, ptr %6, align 4, !tbaa !18
  %50 = icmp ne i32 %49, 0
  br i1 %50, label %51, label %55

51:                                               ; preds = %48, %45
  %52 = load ptr, ptr %3, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.SD_HandleTypeDef, ptr %52, i32 0, i32 10
  %54 = getelementptr inbounds %struct.HAL_SD_CardInfoTypeDef, ptr %53, i32 0, i32 8
  store i32 512, ptr %54, align 8, !tbaa !21
  br label %70

55:                                               ; preds = %48, %33
  %56 = load ptr, ptr %3, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.SD_HandleTypeDef, ptr %56, i32 0, i32 10
  %58 = getelementptr inbounds %struct.HAL_SD_CardInfoTypeDef, ptr %57, i32 0, i32 0
  %59 = load i32, ptr %58, align 8, !tbaa !20
  %60 = icmp eq i32 %59, 1
  br i1 %60, label %61, label %65

61:                                               ; preds = %55
  %62 = load ptr, ptr %3, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.SD_HandleTypeDef, ptr %62, i32 0, i32 10
  %64 = getelementptr inbounds %struct.HAL_SD_CardInfoTypeDef, ptr %63, i32 0, i32 8
  store i32 256, ptr %64, align 8, !tbaa !21
  br label %69

65:                                               ; preds = %55
  %66 = load ptr, ptr %3, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.SD_HandleTypeDef, ptr %66, i32 0, i32 10
  %68 = getelementptr inbounds %struct.HAL_SD_CardInfoTypeDef, ptr %67, i32 0, i32 8
  store i32 0, ptr %68, align 8, !tbaa !21
  br label %69

69:                                               ; preds = %65, %61
  br label %70

70:                                               ; preds = %69, %51
  %71 = load ptr, ptr %3, align 8, !tbaa !5
  %72 = load ptr, ptr %3, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.SD_HandleTypeDef, ptr %72, i32 0, i32 1
  %74 = getelementptr inbounds %struct.SDMMC_InitTypeDef, ptr %73, i32 0, i32 2
  %75 = load i32, ptr %74, align 8, !tbaa !22
  %76 = call i32 @HAL_SD_ConfigWideBusOperation(ptr noundef %71, i32 noundef %75)
  %77 = icmp ne i32 %76, 0
  br i1 %77, label %78, label %79

78:                                               ; preds = %70
  store i32 1, ptr %2, align 4
  store i32 1, ptr %8, align 4
  br label %103

79:                                               ; preds = %70
  %80 = call i32 @HAL_GetTick()
  store i32 %80, ptr %7, align 4, !tbaa !18
  br label %81

81:                                               ; preds = %95, %79
  %82 = load ptr, ptr %3, align 8, !tbaa !5
  %83 = call i32 @HAL_SD_GetCardState(ptr noundef %82)
  %84 = icmp ne i32 %83, 4
  br i1 %84, label %85, label %96

85:                                               ; preds = %81
  %86 = call i32 @HAL_GetTick()
  %87 = load i32, ptr %7, align 4, !tbaa !18
  %88 = sub i32 %86, %87
  %89 = icmp uge i32 %88, -1
  br i1 %89, label %90, label %95

90:                                               ; preds = %85
  %91 = load ptr, ptr %3, align 8, !tbaa !5
  %92 = getelementptr inbounds %struct.SD_HandleTypeDef, ptr %91, i32 0, i32 9
  store volatile i32 -2147483648, ptr %92, align 4, !tbaa !23
  %93 = load ptr, ptr %3, align 8, !tbaa !5
  %94 = getelementptr inbounds %struct.SD_HandleTypeDef, ptr %93, i32 0, i32 8
  store volatile i32 1, ptr %94, align 8, !tbaa !9
  store i32 3, ptr %2, align 4
  store i32 1, ptr %8, align 4
  br label %103

95:                                               ; preds = %85
  br label %81, !llvm.loop !24

96:                                               ; preds = %81
  %97 = load ptr, ptr %3, align 8, !tbaa !5
  %98 = getelementptr inbounds %struct.SD_HandleTypeDef, ptr %97, i32 0, i32 9
  store volatile i32 0, ptr %98, align 4, !tbaa !23
  %99 = load ptr, ptr %3, align 8, !tbaa !5
  %100 = getelementptr inbounds %struct.SD_HandleTypeDef, ptr %99, i32 0, i32 7
  store volatile i32 0, ptr %100, align 4, !tbaa !26
  %101 = load ptr, ptr %3, align 8, !tbaa !5
  %102 = getelementptr inbounds %struct.SD_HandleTypeDef, ptr %101, i32 0, i32 8
  store volatile i32 1, ptr %102, align 8, !tbaa !9
  store i32 0, ptr %2, align 4
  store i32 1, ptr %8, align 4
  br label %103

103:                                              ; preds = %96, %90, %78, %32, %27, %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 20, ptr %4) #3
  %104 = load i32, ptr %2, align 4
  ret i32 %104
}

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

; Function Attrs: nounwind uwtable
declare dso_local void @HAL_SD_MspInit(ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @HAL_SD_InitCard(ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @HAL_SD_GetCardStatus(ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @HAL_SD_ConfigWideBusOperation(ptr noundef, i32 noundef) #0

declare i32 @HAL_GetTick() #2

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

; 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 = { "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, !12, i64 64}
!10 = !{!"", !6, i64 0, !11, i64 8, !12, i64 28, !6, i64 32, !12, i64 40, !6, i64 48, !12, i64 56, !12, i64 60, !12, i64 64, !12, i64 68, !13, i64 72, !7, i64 108, !7, i64 124}
!11 = !{!"", !12, i64 0, !12, i64 4, !12, i64 8, !12, i64 12, !12, i64 16}
!12 = !{!"int", !7, i64 0}
!13 = !{!"", !12, i64 0, !12, i64 4, !12, i64 8, !12, i64 12, !12, i64 16, !12, i64 20, !12, i64 24, !12, i64 28, !12, i64 32}
!14 = !{!10, !12, i64 28}
!15 = !{!16, !7, i64 16}
!16 = !{!"", !7, i64 0, !7, i64 1, !17, i64 2, !12, i64 4, !7, i64 8, !7, i64 9, !7, i64 10, !17, i64 12, !7, i64 14, !7, i64 15, !7, i64 16, !7, i64 17, !7, i64 18}
!17 = !{!"short", !7, i64 0}
!18 = !{!12, !12, i64 0}
!19 = !{!16, !7, i64 17}
!20 = !{!10, !12, i64 72}
!21 = !{!10, !12, i64 104}
!22 = !{!10, !12, i64 16}
!23 = !{!10, !12, i64 68}
!24 = distinct !{!24, !25}
!25 = !{!"llvm.loop.mustprogress"}
!26 = !{!10, !12, i64 60}


; 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.HAL_SD_CardStatusTypedef = type { i8, i8, i16, i32, i8, i8, i8, i16, i8, i8, i8, i8, i8 }

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 4) i32 @HAL_SD_Init(ptr noundef %0) local_unnamed_addr #0 {
  %2 = alloca %struct.HAL_SD_CardStatusTypedef, align 4
  call void @llvm.lifetime.start.p0(i64 20, ptr nonnull %2) #3
  %3 = icmp eq ptr %0, null
  br i1 %3, label %45, label %4

4:                                                ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 64
  %6 = load volatile i32, ptr %5, align 8, !tbaa !5
  %7 = icmp eq i32 %6, 0
  br i1 %7, label %8, label %10

8:                                                ; preds = %4
  %9 = getelementptr inbounds i8, ptr %0, i64 28
  store i32 0, ptr %9, align 4, !tbaa !13
  tail call void @HAL_SD_MspInit(ptr noundef nonnull %0)
  br label %10

10:                                               ; preds = %8, %4
  store volatile i32 3, ptr %5, align 8, !tbaa !5
  %11 = tail call i32 @HAL_SD_InitCard(ptr noundef nonnull %0)
  %.not = icmp eq i32 %11, 0
  br i1 %.not, label %12, label %45

12:                                               ; preds = %10
  %13 = call i32 @HAL_SD_GetCardStatus(ptr noundef nonnull %0, ptr noundef nonnull %2)
  %.not25 = icmp eq i32 %13, 0
  br i1 %.not25, label %14, label %45

14:                                               ; preds = %12
  %15 = getelementptr inbounds i8, ptr %2, i64 16
  %16 = load volatile i8, ptr %15, align 4, !tbaa !14
  %17 = getelementptr inbounds i8, ptr %2, i64 17
  %18 = load volatile i8, ptr %17, align 1, !tbaa !17
  %19 = getelementptr inbounds i8, ptr %0, i64 72
  %20 = load i32, ptr %19, align 8, !tbaa !18
  %21 = icmp eq i32 %20, 1
  br i1 %21, label %22, label %28

22:                                               ; preds = %14
  %23 = icmp ne i8 %16, 0
  %24 = icmp ne i8 %18, 0
  %or.cond = select i1 %23, i1 true, i1 %24
  %25 = getelementptr inbounds i8, ptr %0, i64 104
  br i1 %or.cond, label %26, label %27

26:                                               ; preds = %22
  store i32 512, ptr %25, align 8, !tbaa !19
  br label %30

27:                                               ; preds = %22
  store i32 256, ptr %25, align 8, !tbaa !19
  br label %30

28:                                               ; preds = %14
  %29 = getelementptr inbounds i8, ptr %0, i64 104
  store i32 0, ptr %29, align 8, !tbaa !19
  br label %30

30:                                               ; preds = %27, %28, %26
  %31 = getelementptr inbounds i8, ptr %0, i64 16
  %32 = load i32, ptr %31, align 8, !tbaa !20
  %33 = call i32 @HAL_SD_ConfigWideBusOperation(ptr noundef nonnull %0, i32 noundef %32)
  %.not26 = icmp eq i32 %33, 0
  br i1 %.not26, label %34, label %45

34:                                               ; preds = %30
  %35 = call i32 @HAL_GetTick() #3
  br label %36

36:                                               ; preds = %38, %34
  %37 = call i32 @HAL_SD_GetCardState(ptr noundef nonnull %0)
  %.not27 = icmp eq i32 %37, 4
  br i1 %.not27, label %42, label %38

38:                                               ; preds = %36
  %39 = call i32 @HAL_GetTick() #3
  %40 = sub i32 %39, %35
  %41 = icmp eq i32 %40, -1
  br i1 %41, label %.sink.split, label %36, !llvm.loop !21

42:                                               ; preds = %36
  %43 = getelementptr inbounds i8, ptr %0, i64 68
  store volatile i32 0, ptr %43, align 4, !tbaa !23
  br label %.sink.split

.sink.split:                                      ; preds = %38, %42
  %.sink29 = phi i64 [ 60, %42 ], [ 68, %38 ]
  %.sink = phi i32 [ 0, %42 ], [ -2147483648, %38 ]
  %.0.ph = phi i32 [ 0, %42 ], [ 3, %38 ]
  %44 = getelementptr inbounds i8, ptr %0, i64 %.sink29
  store volatile i32 %.sink, ptr %44, align 4, !tbaa !24
  store volatile i32 1, ptr %5, align 8, !tbaa !5
  br label %45

45:                                               ; preds = %.sink.split, %30, %12, %10, %1
  %.0 = phi i32 [ 1, %1 ], [ 1, %10 ], [ 1, %12 ], [ 1, %30 ], [ %.0.ph, %.sink.split ]
  call void @llvm.lifetime.end.p0(i64 20, ptr nonnull %2) #3
  ret i32 %.0
}

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

; Function Attrs: nounwind uwtable
declare dso_local void @HAL_SD_MspInit(ptr noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @HAL_SD_GetCardStatus(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @HAL_SD_ConfigWideBusOperation(ptr noundef, i32 noundef) local_unnamed_addr #0

declare i32 @HAL_GetTick() local_unnamed_addr #2

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

; Function Attrs: mustprogress 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !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 28}
!14 = !{!15, !8, i64 16}
!15 = !{!"", !8, i64 0, !8, i64 1, !16, i64 2, !11, i64 4, !8, i64 8, !8, i64 9, !8, i64 10, !16, i64 12, !8, i64 14, !8, i64 15, !8, i64 16, !8, i64 17, !8, i64 18}
!16 = !{!"short", !8, i64 0}
!17 = !{!15, !8, i64 17}
!18 = !{!6, !11, i64 72}
!19 = !{!6, !11, i64 104}
!20 = !{!6, !11, i64 16}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}
!23 = !{!6, !11, i64 68}
!24 = !{!11, !11, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass