; LLVM IR from koshkin-sergey/mbOS :: Device/GigaDevice/GD32VF1xx/Driver (OS_TIMER_GD32VF1xx)

; 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.SysTimer_s = type { i32, i32, i32, i32, [1016 x i32], i32, i32, i32, i32 }

@SystemCoreClock = external global i32, align 4
@ticks = external hidden global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local i32 @osTickSetup(i32 noundef %0, ptr noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  store i32 %0, ptr %4, align 4, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !9
  %6 = load ptr, ptr %5, align 8, !tbaa !9
  %7 = load i32, ptr %4, align 4, !tbaa !5
  %8 = icmp eq i32 %7, 0
  br i1 %8, label %9, label %10

9:                                                ; preds = %2
  store i32 -1, ptr %3, align 4
  br label %25

10:                                               ; preds = %2
  %11 = load i32, ptr @SystemCoreClock, align 4, !tbaa !5
  %12 = load i32, ptr %4, align 4, !tbaa !5
  %13 = mul i32 %12, 4
  %14 = udiv i32 %11, %13
  store i32 %14, ptr @ticks, align 4, !tbaa !5
  %15 = load volatile i32, ptr getelementptr inbounds (%struct.SysTimer_s, ptr inttoptr (i64 3506438144 to ptr), i32 0, i32 7), align 4, !tbaa !11
  %16 = zext i32 %15 to i64
  %17 = or i64 %16, 1
  %18 = trunc i64 %17 to i32
  store volatile i32 %18, ptr getelementptr inbounds (%struct.SysTimer_s, ptr inttoptr (i64 3506438144 to ptr), i32 0, i32 7), align 4, !tbaa !11
  call void @SetLoadValue(i64 noundef 0)
  %19 = load i32, ptr @ticks, align 4, !tbaa !5
  %20 = zext i32 %19 to i64
  call void @SetCompareValue(i64 noundef %20)
  %21 = call i32 @IRQ_Disable(i32 noundef 7)
  %22 = call i32 @IRQ_ClearPending(i32 noundef 7)
  %23 = call i32 @IRQ_SetPriority(i32 noundef 7, i32 noundef 0)
  %24 = call i32 @IRQ_Enable(i32 noundef 7)
  store i32 0, ptr %3, align 4
  br label %25

25:                                               ; preds = %10, %9
  %26 = load i32, ptr %3, align 4
  ret i32 %26
}

; Function Attrs: nounwind uwtable
declare hidden void @SetLoadValue(i64 noundef) #0

; Function Attrs: nounwind uwtable
declare hidden void @SetCompareValue(i64 noundef) #0

declare i32 @IRQ_Disable(i32 noundef) #1

declare i32 @IRQ_ClearPending(i32 noundef) #1

declare i32 @IRQ_SetPriority(i32 noundef, i32 noundef) #1

declare i32 @IRQ_Enable(i32 noundef) #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" }

!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, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !6, i64 4088}
!12 = !{!"SysTimer_s", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12, !7, i64 16, !6, i64 4080, !6, i64 4084, !6, i64 4088, !6, i64 4092}


; 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
@ticks = external hidden local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @osTickSetup(i32 noundef %0, ptr nocapture noundef readnone %1) local_unnamed_addr #0 {
  %3 = icmp eq i32 %0, 0
  br i1 %3, label %16, label %4

4:                                                ; preds = %2
  %5 = load i32, ptr @SystemCoreClock, align 4, !tbaa !5
  %6 = shl i32 %0, 2
  %7 = udiv i32 %5, %6
  store i32 %7, ptr @ticks, align 4, !tbaa !5
  %8 = load volatile i32, ptr inttoptr (i64 3506442232 to ptr), align 8, !tbaa !9
  %9 = or i32 %8, 1
  store volatile i32 %9, ptr inttoptr (i64 3506442232 to ptr), align 8, !tbaa !9
  tail call void @SetLoadValue(i64 noundef 0)
  %10 = load i32, ptr @ticks, align 4, !tbaa !5
  %11 = zext i32 %10 to i64
  tail call void @SetCompareValue(i64 noundef %11)
  %12 = tail call i32 @IRQ_Disable(i32 noundef 7) #2
  %13 = tail call i32 @IRQ_ClearPending(i32 noundef 7) #2
  %14 = tail call i32 @IRQ_SetPriority(i32 noundef 7, i32 noundef 0) #2
  %15 = tail call i32 @IRQ_Enable(i32 noundef 7) #2
  br label %16

16:                                               ; preds = %2, %4
  %.0 = phi i32 [ 0, %4 ], [ -1, %2 ]
  ret i32 %.0
}

; Function Attrs: nounwind uwtable
declare hidden void @SetLoadValue(i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden void @SetCompareValue(i64 noundef) local_unnamed_addr #0

declare i32 @IRQ_Disable(i32 noundef) local_unnamed_addr #1

declare i32 @IRQ_ClearPending(i32 noundef) local_unnamed_addr #1

declare i32 @IRQ_SetPriority(i32 noundef, i32 noundef) local_unnamed_addr #1

declare i32 @IRQ_Enable(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 = { "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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 4088}
!10 = !{!"SysTimer_s", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12, !7, i64 16, !6, i64 4080, !6, i64 4084, !6, i64 4088, !6, i64 4092}


; Effective O3 passes: GlobalOptPass,IPSCCPPass,InferAlignmentPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass