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

@ticks = external hidden global i32, align 4

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

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

; Function Attrs: nounwind uwtable
define dso_local void @osTickAcknowledgeIRQ() #0 {
  %1 = alloca i64, align 8
  %2 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #3
  %3 = call i64 @GetLoadValue()
  store i64 %3, ptr %1, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  %4 = load i32, ptr @ticks, align 4, !tbaa !9
  %5 = zext i32 %4 to i64
  %6 = load i64, ptr %1, align 8, !tbaa !5
  %7 = add i64 %5, %6
  store i64 %7, ptr %2, align 8, !tbaa !5
  %8 = load i64, ptr %2, align 8, !tbaa !5
  %9 = load i64, ptr %1, align 8, !tbaa !5
  %10 = icmp ugt i64 %8, %9
  %11 = zext i1 %10 to i32
  %12 = sext i32 %11 to i64
  %13 = call i64 @llvm.expect.i64(i64 %12, i64 1)
  %14 = icmp ne i64 %13, 0
  br i1 %14, label %15, label %17

15:                                               ; preds = %0
  %16 = load i64, ptr %2, align 8, !tbaa !5
  call void @SetCompareValue(i64 noundef %16)
  br label %20

17:                                               ; preds = %0
  call void @SetLoadValue(i64 noundef 0)
  %18 = load i32, ptr @ticks, align 4, !tbaa !9
  %19 = zext i32 %18 to i64
  call void @SetCompareValue(i64 noundef %19)
  br label %20

20:                                               ; preds = %17, %15
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #3
  ret void
}

; 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 hidden i64 @GetLoadValue() #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(none)
declare i64 @llvm.expect.i64(i64, i64) #2

; 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 = { nocallback nofree nosync nounwind willreturn memory(none) }
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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !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"

@ticks = external hidden local_unnamed_addr global i32, align 4

; 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

; Function Attrs: nounwind uwtable
define dso_local void @osTickAcknowledgeIRQ() local_unnamed_addr #0 {
  %1 = tail call i64 @GetLoadValue()
  %2 = load i32, ptr @ticks, align 4, !tbaa !5
  %3 = zext i32 %2 to i64
  %4 = add i64 %1, %3
  %5 = icmp ugt i64 %4, %1
  br i1 %5, label %9, label %6, !prof !9

6:                                                ; preds = %0
  tail call void @SetLoadValue(i64 noundef 0)
  %7 = load i32, ptr @ticks, align 4, !tbaa !5
  %8 = zext i32 %7 to i64
  br label %9

9:                                                ; preds = %0, %6
  %.sink = phi i64 [ %8, %6 ], [ %4, %0 ]
  tail call void @SetCompareValue(i64 noundef %.sink)
  ret void
}

; Function Attrs: nounwind uwtable
declare hidden i64 @GetLoadValue() local_unnamed_addr #0

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 = !{!"branch_weights", !"expected", i32 2000, i32 1}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,LowerExpectIntrinsicPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass