; LLVM IR from ctlst-tech/catpilot :: bsp/mcu/core/stm32/h753/hal/src (stm32h7xx_ll_sdmmc)

; 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.SDMMC_TypeDef = type { i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, [3 x i32], i32, i32, i32, i32, [8 x i32], i32, [222 x i32], i32 }

@SystemCoreClock = external global i32, align 4

; 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: nounwind uwtable
declare dso_local zeroext i8 @SDMMC_GetCommandResponse(ptr noundef) #1

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

; Function Attrs: nounwind uwtable
define dso_local i32 @SDMMC_GetCmdResp6(ptr noundef %0, i8 noundef zeroext %1, ptr noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i8, align 1
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i8 %1, ptr %6, align 1, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %12 = load i32, ptr @SystemCoreClock, align 4, !tbaa !10
  %13 = udiv i32 %12, 8
  %14 = udiv i32 %13, 1000
  %15 = mul i32 5000, %14
  store i32 %15, ptr %10, align 4, !tbaa !10
  br label %16

16:                                               ; preds = %35, %3
  %17 = load i32, ptr %10, align 4, !tbaa !10
  %18 = add i32 %17, -1
  store i32 %18, ptr %10, align 4, !tbaa !10
  %19 = icmp eq i32 %17, 0
  br i1 %19, label %20, label %21

20:                                               ; preds = %16
  store i32 -2147483648, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %91

21:                                               ; preds = %16
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.SDMMC_TypeDef, ptr %22, i32 0, i32 13
  %24 = load volatile i32, ptr %23, align 4, !tbaa !12
  store i32 %24, ptr %9, align 4, !tbaa !10
  br label %25

25:                                               ; preds = %21
  %26 = load i32, ptr %9, align 4, !tbaa !10
  %27 = zext i32 %26 to i64
  %28 = and i64 %27, 69
  %29 = icmp eq i64 %28, 0
  br i1 %29, label %35, label %30

30:                                               ; preds = %25
  %31 = load i32, ptr %9, align 4, !tbaa !10
  %32 = zext i32 %31 to i64
  %33 = and i64 %32, 8192
  %34 = icmp ne i64 %33, 0
  br label %35

35:                                               ; preds = %30, %25
  %36 = phi i1 [ true, %25 ], [ %34, %30 ]
  br i1 %36, label %16, label %37, !llvm.loop !14

37:                                               ; preds = %35
  %38 = load ptr, ptr %5, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.SDMMC_TypeDef, ptr %38, i32 0, i32 13
  %40 = load volatile i32, ptr %39, align 4, !tbaa !12
  %41 = zext i32 %40 to i64
  %42 = and i64 %41, 4
  %43 = icmp ne i64 %42, 0
  br i1 %43, label %44, label %47

44:                                               ; preds = %37
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.SDMMC_TypeDef, ptr %45, i32 0, i32 14
  store volatile i32 4, ptr %46, align 4, !tbaa !16
  store i32 4, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %91

47:                                               ; preds = %37
  %48 = load ptr, ptr %5, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.SDMMC_TypeDef, ptr %48, i32 0, i32 13
  %50 = load volatile i32, ptr %49, align 4, !tbaa !12
  %51 = zext i32 %50 to i64
  %52 = and i64 %51, 1
  %53 = icmp ne i64 %52, 0
  br i1 %53, label %54, label %57

54:                                               ; preds = %47
  %55 = load ptr, ptr %5, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.SDMMC_TypeDef, ptr %55, i32 0, i32 14
  store volatile i32 1, ptr %56, align 4, !tbaa !16
  store i32 1, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %91

57:                                               ; preds = %47
  br label %58

58:                                               ; preds = %57
  br label %59

59:                                               ; preds = %58
  %60 = load ptr, ptr %5, align 8, !tbaa !5
  %61 = call zeroext i8 @SDMMC_GetCommandResponse(ptr noundef %60)
  %62 = zext i8 %61 to i32
  %63 = load i8, ptr %6, align 1, !tbaa !9
  %64 = zext i8 %63 to i32
  %65 = icmp ne i32 %62, %64
  br i1 %65, label %66, label %67

66:                                               ; preds = %59
  store i32 1, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %91

67:                                               ; preds = %59
  %68 = load ptr, ptr %5, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.SDMMC_TypeDef, ptr %68, i32 0, i32 14
  store volatile i32 2097349, ptr %69, align 4, !tbaa !16
  %70 = load ptr, ptr %5, align 8, !tbaa !5
  %71 = call i32 @SDMMC_GetResponse(ptr noundef %70, i32 noundef 0)
  store i32 %71, ptr %8, align 4, !tbaa !10
  %72 = load i32, ptr %8, align 4, !tbaa !10
  %73 = and i32 %72, 57344
  %74 = icmp eq i32 %73, 0
  br i1 %74, label %75, label %80

75:                                               ; preds = %67
  %76 = load i32, ptr %8, align 4, !tbaa !10
  %77 = lshr i32 %76, 16
  %78 = trunc i32 %77 to i16
  %79 = load ptr, ptr %7, align 8, !tbaa !5
  store i16 %78, ptr %79, align 2, !tbaa !17
  store i32 0, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %91

80:                                               ; preds = %67
  %81 = load i32, ptr %8, align 4, !tbaa !10
  %82 = and i32 %81, 16384
  %83 = icmp eq i32 %82, 16384
  br i1 %83, label %84, label %85

84:                                               ; preds = %80
  store i32 8192, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %91

85:                                               ; preds = %80
  %86 = load i32, ptr %8, align 4, !tbaa !10
  %87 = and i32 %86, 32768
  %88 = icmp eq i32 %87, 32768
  br i1 %88, label %89, label %90

89:                                               ; preds = %85
  store i32 4096, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %91

90:                                               ; preds = %85
  store i32 65536, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %91

91:                                               ; preds = %90, %89, %84, %75, %66, %54, %44, %20
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  %92 = load i32, ptr %4, align 4
  ret i32 %92
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !7, i64 0}
!12 = !{!13, !11, i64 52}
!13 = !{!"", !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, !11, i64 36, !11, i64 40, !11, i64 44, !11, i64 48, !11, i64 52, !11, i64 56, !11, i64 60, !11, i64 64, !7, i64 68, !11, i64 80, !11, i64 84, !11, i64 88, !11, i64 92, !7, i64 96, !11, i64 128, !7, i64 132, !11, i64 1020}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!13, !11, i64 56}
!17 = !{!18, !18, i64 0}
!18 = !{!"short", !7, 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"

@SystemCoreClock = external local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
declare dso_local zeroext i8 @SDMMC_GetCommandResponse(ptr noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, -2147483647) i32 @SDMMC_GetCmdResp6(ptr noundef %0, i8 noundef zeroext %1, ptr nocapture noundef writeonly %2) local_unnamed_addr #0 {
  %4 = load i32, ptr @SystemCoreClock, align 4, !tbaa !5
  %5 = udiv i32 %4, 8000
  %6 = mul nuw i32 %5, 5000
  %7 = getelementptr inbounds i8, ptr %0, i64 52
  br label %8

8:                                                ; preds = %10, %3
  %.0 = phi i32 [ %6, %3 ], [ %11, %10 ]
  %9 = icmp eq i32 %.0, 0
  br i1 %9, label %.loopexit, label %10

10:                                               ; preds = %8
  %11 = add i32 %.0, -1
  %12 = load volatile i32, ptr %7, align 4, !tbaa !9
  %13 = zext i32 %12 to i64
  %14 = and i64 %13, 69
  %15 = icmp eq i64 %14, 0
  %16 = and i64 %13, 8192
  %17 = icmp ne i64 %16, 0
  %18 = or i1 %15, %17
  br i1 %18, label %8, label %19, !llvm.loop !11

19:                                               ; preds = %10
  %20 = load volatile i32, ptr %7, align 4, !tbaa !9
  %21 = and i32 %20, 4
  %.not = icmp eq i32 %21, 0
  br i1 %.not, label %24, label %22

22:                                               ; preds = %19
  %23 = getelementptr inbounds i8, ptr %0, i64 56
  store volatile i32 4, ptr %23, align 4, !tbaa !13
  br label %.loopexit

24:                                               ; preds = %19
  %25 = load volatile i32, ptr %7, align 4, !tbaa !9
  %26 = and i32 %25, 1
  %.not18 = icmp eq i32 %26, 0
  br i1 %.not18, label %29, label %27

27:                                               ; preds = %24
  %28 = getelementptr inbounds i8, ptr %0, i64 56
  store volatile i32 1, ptr %28, align 4, !tbaa !13
  br label %.loopexit

29:                                               ; preds = %24
  %30 = tail call zeroext i8 @SDMMC_GetCommandResponse(ptr noundef nonnull %0)
  %.not19 = icmp eq i8 %30, %1
  br i1 %.not19, label %31, label %.loopexit

31:                                               ; preds = %29
  %32 = getelementptr inbounds i8, ptr %0, i64 56
  store volatile i32 2097349, ptr %32, align 4, !tbaa !13
  %33 = tail call i32 @SDMMC_GetResponse(ptr noundef nonnull %0, i32 noundef 0)
  %34 = and i32 %33, 57344
  %35 = icmp eq i32 %34, 0
  br i1 %35, label %36, label %39

36:                                               ; preds = %31
  %37 = lshr i32 %33, 16
  %38 = trunc nuw i32 %37 to i16
  store i16 %38, ptr %2, align 2, !tbaa !14
  br label %.loopexit

39:                                               ; preds = %31
  %40 = and i32 %33, 16384
  %.not20 = icmp eq i32 %40, 0
  br i1 %.not20, label %41, label %.loopexit

41:                                               ; preds = %39
  %42 = and i32 %33, 32768
  %.not21 = icmp eq i32 %42, 0
  %. = select i1 %.not21, i32 65536, i32 4096
  br label %.loopexit

.loopexit:                                        ; preds = %8, %41, %39, %29, %36, %27, %22
  %.017 = phi i32 [ 4, %22 ], [ 1, %27 ], [ 0, %36 ], [ 1, %29 ], [ 8192, %39 ], [ %., %41 ], [ -2147483648, %8 ]
  ret i32 %.017
}

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

!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 = !{!10, !6, i64 52}
!10 = !{!"", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12, !6, i64 16, !6, i64 20, !6, i64 24, !6, i64 28, !6, i64 32, !6, i64 36, !6, i64 40, !6, i64 44, !6, i64 48, !6, i64 52, !6, i64 56, !6, i64 60, !6, i64 64, !7, i64 68, !6, i64 80, !6, i64 84, !6, i64 88, !6, i64 92, !7, i64 96, !6, i64 128, !7, i64 132, !6, i64 1020}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!10, !6, i64 56}
!14 = !{!15, !15, i64 0}
!15 = !{!"short", !7, i64 0}


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