; LLVM IR from reazon-research/OpenArm :: control/canfd_control/Drivers/STM32H7xx_HAL_Driver/Src (stm32h7xx_hal_tim_ex)

; 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.TIM_HandleTypeDef = type { ptr, %struct.TIM_Base_InitTypeDef, i32, [7 x ptr], i32, i32, [6 x i32], [4 x i32], i32 }
%struct.TIM_Base_InitTypeDef = type { i32, i32, i32, i32, i32, i32 }
%struct.TIM_TypeDef = type { i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, 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

; 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_TIMEx_ReArmBreakInput(ptr noundef %0, i32 noundef %1) #1 {
  %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
  store i32 0, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %9 = load i32, ptr %5, align 4, !tbaa !9
  switch i32 %9, label %90 [
    i32 1, label %10
    i32 2, label %50
  ]

10:                                               ; preds = %2
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.TIM_HandleTypeDef, ptr %11, i32 0, i32 0
  %13 = load ptr, ptr %12, align 8, !tbaa !11
  %14 = getelementptr inbounds %struct.TIM_TypeDef, ptr %13, i32 0, i32 17
  %15 = load volatile i32, ptr %14, align 4, !tbaa !14
  %16 = zext i32 %15 to i64
  %17 = and i64 %16, 268435456
  %18 = icmp eq i64 %17, 268435456
  br i1 %18, label %19, label %49

19:                                               ; preds = %10
  %20 = call i32 @HAL_GetTick()
  store i32 %20, ptr %7, align 4, !tbaa !9
  br label %21

21:                                               ; preds = %47, %19
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.TIM_HandleTypeDef, ptr %22, i32 0, i32 0
  %24 = load ptr, ptr %23, align 8, !tbaa !11
  %25 = getelementptr inbounds %struct.TIM_TypeDef, ptr %24, i32 0, i32 17
  %26 = load volatile i32, ptr %25, align 4, !tbaa !14
  %27 = zext i32 %26 to i64
  %28 = and i64 %27, 67108864
  %29 = icmp ne i64 %28, 0
  br i1 %29, label %30, label %48

30:                                               ; preds = %21
  %31 = call i32 @HAL_GetTick()
  %32 = load i32, ptr %7, align 4, !tbaa !9
  %33 = sub i32 %31, %32
  %34 = zext i32 %33 to i64
  %35 = icmp ugt i64 %34, 5
  br i1 %35, label %36, label %47

36:                                               ; preds = %30
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.TIM_HandleTypeDef, ptr %37, i32 0, i32 0
  %39 = load ptr, ptr %38, align 8, !tbaa !11
  %40 = getelementptr inbounds %struct.TIM_TypeDef, ptr %39, i32 0, i32 17
  %41 = load volatile i32, ptr %40, align 4, !tbaa !14
  %42 = zext i32 %41 to i64
  %43 = and i64 %42, 67108864
  %44 = icmp ne i64 %43, 0
  br i1 %44, label %45, label %46

45:                                               ; preds = %36
  store i32 3, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %93

46:                                               ; preds = %36
  br label %47

47:                                               ; preds = %46, %30
  br label %21, !llvm.loop !16

48:                                               ; preds = %21
  br label %49

49:                                               ; preds = %48, %10
  br label %91

50:                                               ; preds = %2
  %51 = load ptr, ptr %4, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.TIM_HandleTypeDef, ptr %51, i32 0, i32 0
  %53 = load ptr, ptr %52, align 8, !tbaa !11
  %54 = getelementptr inbounds %struct.TIM_TypeDef, ptr %53, i32 0, i32 17
  %55 = load volatile i32, ptr %54, align 4, !tbaa !14
  %56 = zext i32 %55 to i64
  %57 = and i64 %56, 536870912
  %58 = icmp eq i64 %57, 536870912
  br i1 %58, label %59, label %89

59:                                               ; preds = %50
  %60 = call i32 @HAL_GetTick()
  store i32 %60, ptr %7, align 4, !tbaa !9
  br label %61

61:                                               ; preds = %87, %59
  %62 = load ptr, ptr %4, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.TIM_HandleTypeDef, ptr %62, i32 0, i32 0
  %64 = load ptr, ptr %63, align 8, !tbaa !11
  %65 = getelementptr inbounds %struct.TIM_TypeDef, ptr %64, i32 0, i32 17
  %66 = load volatile i32, ptr %65, align 4, !tbaa !14
  %67 = zext i32 %66 to i64
  %68 = and i64 %67, 134217728
  %69 = icmp ne i64 %68, 0
  br i1 %69, label %70, label %88

70:                                               ; preds = %61
  %71 = call i32 @HAL_GetTick()
  %72 = load i32, ptr %7, align 4, !tbaa !9
  %73 = sub i32 %71, %72
  %74 = zext i32 %73 to i64
  %75 = icmp ugt i64 %74, 5
  br i1 %75, label %76, label %87

76:                                               ; preds = %70
  %77 = load ptr, ptr %4, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.TIM_HandleTypeDef, ptr %77, i32 0, i32 0
  %79 = load ptr, ptr %78, align 8, !tbaa !11
  %80 = getelementptr inbounds %struct.TIM_TypeDef, ptr %79, i32 0, i32 17
  %81 = load volatile i32, ptr %80, align 4, !tbaa !14
  %82 = zext i32 %81 to i64
  %83 = and i64 %82, 134217728
  %84 = icmp ne i64 %83, 0
  br i1 %84, label %85, label %86

85:                                               ; preds = %76
  store i32 3, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %93

86:                                               ; preds = %76
  br label %87

87:                                               ; preds = %86, %70
  br label %61, !llvm.loop !18

88:                                               ; preds = %61
  br label %89

89:                                               ; preds = %88, %50
  br label %91

90:                                               ; preds = %2
  store i32 1, ptr %6, align 4, !tbaa !9
  br label %91

91:                                               ; preds = %90, %89, %49
  %92 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %92, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %93

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

declare i32 @HAL_GetTick() #2

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 = { "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, !6, i64 0}
!12 = !{!"", !6, i64 0, !13, i64 8, !10, i64 32, !7, i64 40, !10, i64 96, !10, i64 100, !7, i64 104, !7, i64 128, !10, i64 144}
!13 = !{!"", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20}
!14 = !{!15, !10, i64 68}
!15 = !{!"", !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, !10, i64 36, !10, i64 40, !10, i64 44, !10, i64 48, !10, i64 52, !10, i64 56, !10, i64 60, !10, i64 64, !10, i64 68, !10, i64 72, !10, i64 76, !10, i64 80, !10, i64 84, !10, i64 88, !10, i64 92, !10, i64 96, !10, i64 100, !10, i64 104}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = distinct !{!18, !17}


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

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 4) i32 @HAL_TIMEx_ReArmBreakInput(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  switch i32 %1, label %.loopexit [
    i32 1, label %3
    i32 2, label %25
  ]

3:                                                ; preds = %2
  %4 = load ptr, ptr %0, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %4, i64 68
  %6 = load volatile i32, ptr %5, align 4, !tbaa !12
  %7 = and i32 %6, 268435456
  %.not13 = icmp eq i32 %7, 0
  br i1 %.not13, label %.loopexit, label %8

8:                                                ; preds = %3
  %9 = tail call i32 @HAL_GetTick() #2
  %10 = load ptr, ptr %0, align 8, !tbaa !5
  %11 = getelementptr inbounds i8, ptr %10, i64 68
  %12 = load volatile i32, ptr %11, align 4, !tbaa !12
  %13 = and i32 %12, 67108864
  %.not1421 = icmp eq i32 %13, 0
  br i1 %.not1421, label %.loopexit, label %.lr.ph22

.lr.ph22:                                         ; preds = %8, %21
  %14 = tail call i32 @HAL_GetTick() #2
  %15 = sub i32 %14, %9
  %16 = icmp ugt i32 %15, 5
  %.pre28 = load ptr, ptr %0, align 8, !tbaa !5
  br i1 %16, label %17, label %21

17:                                               ; preds = %.lr.ph22
  %18 = getelementptr inbounds i8, ptr %.pre28, i64 68
  %19 = load volatile i32, ptr %18, align 4, !tbaa !12
  %20 = and i32 %19, 67108864
  %.not15 = icmp eq i32 %20, 0
  br i1 %.not15, label %21, label %.loopexit

21:                                               ; preds = %17, %.lr.ph22
  %22 = getelementptr inbounds i8, ptr %.pre28, i64 68
  %23 = load volatile i32, ptr %22, align 4, !tbaa !12
  %24 = and i32 %23, 67108864
  %.not14 = icmp eq i32 %24, 0
  br i1 %.not14, label %.loopexit, label %.lr.ph22, !llvm.loop !14

25:                                               ; preds = %2
  %26 = load ptr, ptr %0, align 8, !tbaa !5
  %27 = getelementptr inbounds i8, ptr %26, i64 68
  %28 = load volatile i32, ptr %27, align 4, !tbaa !12
  %29 = and i32 %28, 536870912
  %.not = icmp eq i32 %29, 0
  br i1 %.not, label %.loopexit, label %30

30:                                               ; preds = %25
  %31 = tail call i32 @HAL_GetTick() #2
  %32 = load ptr, ptr %0, align 8, !tbaa !5
  %33 = getelementptr inbounds i8, ptr %32, i64 68
  %34 = load volatile i32, ptr %33, align 4, !tbaa !12
  %35 = and i32 %34, 134217728
  %.not1118 = icmp eq i32 %35, 0
  br i1 %.not1118, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %30, %43
  %36 = tail call i32 @HAL_GetTick() #2
  %37 = sub i32 %36, %31
  %38 = icmp ugt i32 %37, 5
  %.pre = load ptr, ptr %0, align 8, !tbaa !5
  br i1 %38, label %39, label %43

39:                                               ; preds = %.lr.ph
  %40 = getelementptr inbounds i8, ptr %.pre, i64 68
  %41 = load volatile i32, ptr %40, align 4, !tbaa !12
  %42 = and i32 %41, 134217728
  %.not12 = icmp eq i32 %42, 0
  br i1 %.not12, label %43, label %.loopexit

43:                                               ; preds = %39, %.lr.ph
  %44 = getelementptr inbounds i8, ptr %.pre, i64 68
  %45 = load volatile i32, ptr %44, align 4, !tbaa !12
  %46 = and i32 %45, 134217728
  %.not11 = icmp eq i32 %46, 0
  br i1 %.not11, label %.loopexit, label %.lr.ph, !llvm.loop !16

.loopexit:                                        ; preds = %39, %43, %17, %21, %30, %8, %3, %25, %2
  %.010 = phi i32 [ 0, %25 ], [ 0, %3 ], [ 1, %2 ], [ 0, %8 ], [ 0, %30 ], [ 3, %17 ], [ 0, %21 ], [ 3, %39 ], [ 0, %43 ]
  ret i32 %.010
}

declare i32 @HAL_GetTick() 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 = { "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, !7, i64 0}
!6 = !{!"", !7, i64 0, !10, i64 8, !11, i64 32, !8, i64 40, !11, i64 96, !11, i64 100, !8, i64 104, !8, i64 128, !11, i64 144}
!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, i64 20}
!11 = !{!"int", !8, i64 0}
!12 = !{!13, !11, i64 68}
!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, !11, i64 68, !11, i64 72, !11, i64 76, !11, i64 80, !11, i64 84, !11, i64 88, !11, i64 92, !11, i64 96, !11, i64 100, !11, i64 104}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


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