; LLVM IR from DanEngelbrecht/longtail :: lib/ratelimitedprogress (longtail_ratelimitedprogress)

; 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.RateLimitedProgress = type { %struct.Longtail_ProgressAPI, ptr, i32, i32, i32, i32 }
%struct.Longtail_ProgressAPI = type { %struct.Longtail_API, ptr }
%struct.Longtail_API = type { ptr }

; 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 void @RateLimitedProgress_OnProgress(ptr noundef %0, i32 noundef %1, i32 noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store i32 %2, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %11, ptr %7, align 8, !tbaa !5
  %12 = load i32, ptr %6, align 4, !tbaa !9
  %13 = load i32, ptr %5, align 4, !tbaa !9
  %14 = icmp ult i32 %12, %13
  br i1 %14, label %15, label %57

15:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %16 = load i32, ptr %6, align 4, !tbaa !9
  %17 = mul i32 100, %16
  %18 = load i32, ptr %5, align 4, !tbaa !9
  %19 = udiv i32 %17, %18
  store i32 %19, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %20 = load i32, ptr %8, align 4, !tbaa !9
  %21 = load ptr, ptr %7, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.RateLimitedProgress, ptr %21, i32 0, i32 3
  %23 = load i32, ptr %22, align 4, !tbaa !11
  %24 = sub i32 %20, %23
  store i32 %24, ptr %9, align 4, !tbaa !9
  %25 = load ptr, ptr %7, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.RateLimitedProgress, ptr %25, i32 0, i32 2
  %27 = load i32, ptr %26, align 8, !tbaa !15
  %28 = icmp ne i32 %27, 0
  br i1 %28, label %29, label %35

29:                                               ; preds = %15
  %30 = load i32, ptr %9, align 4, !tbaa !9
  %31 = load ptr, ptr %7, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.RateLimitedProgress, ptr %31, i32 0, i32 5
  %33 = load i32, ptr %32, align 4, !tbaa !16
  %34 = icmp uge i32 %30, %33
  br i1 %34, label %35, label %49

35:                                               ; preds = %29, %15
  %36 = load ptr, ptr %7, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.RateLimitedProgress, ptr %36, i32 0, i32 1
  %38 = load ptr, ptr %37, align 8, !tbaa !17
  %39 = getelementptr inbounds %struct.Longtail_ProgressAPI, ptr %38, i32 0, i32 1
  %40 = load ptr, ptr %39, align 8, !tbaa !18
  %41 = load ptr, ptr %7, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.RateLimitedProgress, ptr %41, i32 0, i32 1
  %43 = load ptr, ptr %42, align 8, !tbaa !17
  %44 = load i32, ptr %5, align 4, !tbaa !9
  %45 = load i32, ptr %6, align 4, !tbaa !9
  call void %40(ptr noundef %43, i32 noundef %44, i32 noundef %45)
  %46 = load i32, ptr %8, align 4, !tbaa !9
  %47 = load ptr, ptr %7, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.RateLimitedProgress, ptr %47, i32 0, i32 3
  store i32 %46, ptr %48, align 4, !tbaa !11
  br label %49

49:                                               ; preds = %35, %29
  %50 = load ptr, ptr %7, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.RateLimitedProgress, ptr %50, i32 0, i32 2
  %52 = load i32, ptr %51, align 8, !tbaa !15
  %53 = add i32 %52, 1
  store i32 %53, ptr %51, align 8, !tbaa !15
  %54 = load i32, ptr %6, align 4, !tbaa !9
  %55 = load ptr, ptr %7, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.RateLimitedProgress, ptr %55, i32 0, i32 4
  store i32 %54, ptr %56, align 8, !tbaa !19
  store i32 1, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %71

57:                                               ; preds = %3
  %58 = load ptr, ptr %7, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.RateLimitedProgress, ptr %58, i32 0, i32 1
  %60 = load ptr, ptr %59, align 8, !tbaa !17
  %61 = getelementptr inbounds %struct.Longtail_ProgressAPI, ptr %60, i32 0, i32 1
  %62 = load ptr, ptr %61, align 8, !tbaa !18
  %63 = load ptr, ptr %7, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.RateLimitedProgress, ptr %63, i32 0, i32 1
  %65 = load ptr, ptr %64, align 8, !tbaa !17
  %66 = load i32, ptr %5, align 4, !tbaa !9
  %67 = load i32, ptr %6, align 4, !tbaa !9
  call void %62(ptr noundef %65, i32 noundef %66, i32 noundef %67)
  %68 = load i32, ptr %6, align 4, !tbaa !9
  %69 = load ptr, ptr %7, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.RateLimitedProgress, ptr %69, i32 0, i32 4
  store i32 %68, ptr %70, align 8, !tbaa !19
  store i32 0, ptr %10, align 4
  br label %71

71:                                               ; preds = %57, %49
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  %72 = load i32, ptr %10, align 4
  switch i32 %72, label %74 [
    i32 0, label %73
    i32 1, label %73
  ]

73:                                               ; preds = %71, %71
  ret void

74:                                               ; preds = %71
  unreachable
}

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 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 28}
!12 = !{!"RateLimitedProgress", !13, i64 0, !6, i64 16, !10, i64 24, !10, i64 28, !10, i64 32, !10, i64 36}
!13 = !{!"Longtail_ProgressAPI", !14, i64 0, !6, i64 8}
!14 = !{!"Longtail_API", !6, i64 0}
!15 = !{!12, !10, i64 24}
!16 = !{!12, !10, i64 36}
!17 = !{!12, !6, i64 16}
!18 = !{!13, !6, i64 8}
!19 = !{!12, !10, i64 32}


; 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 void @RateLimitedProgress_OnProgress(ptr nocapture noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = icmp ult i32 %2, %1
  br i1 %4, label %5, label %24

5:                                                ; preds = %3
  %6 = mul i32 %2, 100
  %7 = udiv i32 %6, %1
  %8 = getelementptr inbounds i8, ptr %0, i64 28
  %9 = getelementptr inbounds i8, ptr %0, i64 24
  %10 = load i32, ptr %9, align 8, !tbaa !5
  %.not = icmp eq i32 %10, 0
  br i1 %.not, label %16, label %11

11:                                               ; preds = %5
  %12 = load i32, ptr %8, align 4, !tbaa !13
  %13 = sub i32 %7, %12
  %14 = getelementptr inbounds i8, ptr %0, i64 36
  %15 = load i32, ptr %14, align 4, !tbaa !14
  %.not26 = icmp ult i32 %13, %15
  br i1 %.not26, label %21, label %16

16:                                               ; preds = %11, %5
  %17 = getelementptr inbounds i8, ptr %0, i64 16
  %18 = load ptr, ptr %17, align 8, !tbaa !15
  %19 = getelementptr inbounds i8, ptr %18, i64 8
  %20 = load ptr, ptr %19, align 8, !tbaa !16
  tail call void %20(ptr noundef %18, i32 noundef %1, i32 noundef %2) #1
  store i32 %7, ptr %8, align 4, !tbaa !13
  %.pre = load i32, ptr %9, align 8, !tbaa !5
  br label %21

21:                                               ; preds = %16, %11
  %22 = phi i32 [ %.pre, %16 ], [ %10, %11 ]
  %23 = add i32 %22, 1
  store i32 %23, ptr %9, align 8, !tbaa !5
  br label %29

24:                                               ; preds = %3
  %25 = getelementptr inbounds i8, ptr %0, i64 16
  %26 = load ptr, ptr %25, align 8, !tbaa !15
  %27 = getelementptr inbounds i8, ptr %26, i64 8
  %28 = load ptr, ptr %27, align 8, !tbaa !16
  tail call void %28(ptr noundef %26, i32 noundef %1, i32 noundef %2) #1
  br label %29

29:                                               ; preds = %24, %21
  %30 = getelementptr inbounds i8, ptr %0, i64 32
  store i32 %2, ptr %30, align 8, !tbaa !17
  ret void
}

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 = { 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, !12, i64 24}
!6 = !{!"RateLimitedProgress", !7, i64 0, !9, i64 16, !12, i64 24, !12, i64 28, !12, i64 32, !12, i64 36}
!7 = !{!"Longtail_ProgressAPI", !8, i64 0, !9, i64 8}
!8 = !{!"Longtail_API", !9, i64 0}
!9 = !{!"any pointer", !10, i64 0}
!10 = !{!"omnipotent char", !11, i64 0}
!11 = !{!"Simple C/C++ TBAA"}
!12 = !{!"int", !10, i64 0}
!13 = !{!6, !12, i64 28}
!14 = !{!6, !12, i64 36}
!15 = !{!6, !9, i64 16}
!16 = !{!7, !9, i64 8}
!17 = !{!6, !12, i64 32}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass