; LLVM IR from daanx/mimalloc-bench :: bench/cfrac (pimod)

; 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.precisionType = type { i16, i16, i16, i8, [1 x i16] }

@.str = external hidden unnamed_addr constant [6 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [33 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [15 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @pimod(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i16, align 2
  %8 = alloca i16, align 2
  %9 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.start.p0(i64 2, ptr %7) #4
  store i16 0, ptr %7, align 2, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 2, ptr %8) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  store i32 0, ptr %9, align 4, !tbaa !9
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = call ptr @pparmq(ptr noundef %10)
  %12 = load i32, ptr %4, align 4, !tbaa !9
  %13 = icmp slt i32 %12, 0
  br i1 %13, label %14, label %18

14:                                               ; preds = %2
  %15 = load i32, ptr %4, align 4, !tbaa !9
  %16 = sub nsw i32 0, %15
  %17 = trunc i32 %16 to i16
  store i16 %17, ptr %8, align 2, !tbaa !11
  br label %21

18:                                               ; preds = %2
  %19 = load i32, ptr %4, align 4, !tbaa !9
  %20 = trunc i32 %19 to i16
  store i16 %20, ptr %8, align 2, !tbaa !11
  br label %21

21:                                               ; preds = %18, %14
  %22 = load i16, ptr %8, align 2, !tbaa !11
  %23 = zext i16 %22 to i32
  %24 = icmp sge i32 %23, 65536
  br i1 %24, label %25, label %27

25:                                               ; preds = %21
  %26 = call ptr @errorp(i32 noundef 4, ptr noundef @.str, ptr noundef @.str.1)
  br label %75

27:                                               ; preds = %21
  %28 = load i16, ptr %8, align 2, !tbaa !11
  %29 = zext i16 %28 to i32
  %30 = icmp eq i32 %29, 0
  br i1 %30, label %31, label %33

31:                                               ; preds = %27
  %32 = call ptr @errorp(i32 noundef 4, ptr noundef @.str, ptr noundef @.str.2)
  br label %75

33:                                               ; preds = %27
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.precisionType, ptr %34, i32 0, i32 4
  %36 = getelementptr inbounds [1 x i16], ptr %35, i64 0, i64 0
  %37 = load ptr, ptr %3, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.precisionType, ptr %37, i32 0, i32 2
  %39 = load i16, ptr %38, align 2, !tbaa !13
  %40 = zext i16 %39 to i32
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds i16, ptr %36, i64 %41
  store ptr %42, ptr %5, align 8, !tbaa !5
  store i16 0, ptr %7, align 2, !tbaa !11
  br label %43

43:                                               ; preds = %58, %33
  %44 = load i16, ptr %7, align 2, !tbaa !11
  %45 = zext i16 %44 to i32
  %46 = shl i32 %45, 16
  store i32 %46, ptr %6, align 4, !tbaa !9
  %47 = load ptr, ptr %5, align 8, !tbaa !5
  %48 = getelementptr inbounds i16, ptr %47, i32 -1
  store ptr %48, ptr %5, align 8, !tbaa !5
  %49 = load i16, ptr %48, align 2, !tbaa !11
  %50 = zext i16 %49 to i32
  %51 = load i32, ptr %6, align 4, !tbaa !9
  %52 = add i32 %51, %50
  store i32 %52, ptr %6, align 4, !tbaa !9
  %53 = load i32, ptr %6, align 4, !tbaa !9
  %54 = load i16, ptr %8, align 2, !tbaa !11
  %55 = zext i16 %54 to i32
  %56 = urem i32 %53, %55
  %57 = trunc i32 %56 to i16
  store i16 %57, ptr %7, align 2, !tbaa !11
  br label %58

58:                                               ; preds = %43
  %59 = load ptr, ptr %5, align 8, !tbaa !5
  %60 = load ptr, ptr %3, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.precisionType, ptr %60, i32 0, i32 4
  %62 = getelementptr inbounds [1 x i16], ptr %61, i64 0, i64 0
  %63 = icmp ugt ptr %59, %62
  br i1 %63, label %43, label %64, !llvm.loop !15

64:                                               ; preds = %58
  %65 = load i16, ptr %7, align 2, !tbaa !11
  %66 = zext i16 %65 to i32
  store i32 %66, ptr %9, align 4, !tbaa !9
  %67 = load ptr, ptr %3, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.precisionType, ptr %67, i32 0, i32 3
  %69 = load i8, ptr %68, align 2, !tbaa !17
  %70 = icmp ne i8 %69, 0
  br i1 %70, label %71, label %74

71:                                               ; preds = %64
  %72 = load i32, ptr %9, align 4, !tbaa !9
  %73 = sub nsw i32 0, %72
  store i32 %73, ptr %9, align 4, !tbaa !9
  br label %74

74:                                               ; preds = %71, %64
  br label %75

75:                                               ; preds = %74, %31, %25
  %76 = load ptr, ptr %3, align 8, !tbaa !5
  call void @pdestroy(ptr noundef %76)
  %77 = load i32, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 2, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 2, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  ret i32 %77
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @pparmq(ptr noundef) #2

declare ptr @errorp(i32 noundef, ptr noundef, ptr noundef) #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @pdestroy(ptr noundef) #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 = { inlinehint 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 #3 = { "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 #4 = { 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, !12, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!14, !12, i64 4}
!14 = !{!"", !12, i64 0, !12, i64 2, !12, i64 4, !7, i64 6, !7, i64 8}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!14, !7, i64 6}


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

@.str = external hidden unnamed_addr constant [6 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [15 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local range(i32 -65534, 65535) i32 @pimod(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = tail call ptr @pparmq(ptr noundef %0)
  %.019 = tail call i32 @llvm.abs.i32(i32 %1, i1 false)
  %4 = and i32 %.019, 65535
  %5 = icmp eq i32 %4, 0
  br i1 %5, label %6, label %8

6:                                                ; preds = %2
  %7 = tail call ptr @errorp(i32 noundef 4, ptr noundef nonnull @.str, ptr noundef nonnull @.str.2) #4
  br label %23

8:                                                ; preds = %2
  %9 = getelementptr inbounds i8, ptr %0, i64 4
  %10 = load i16, ptr %9, align 2, !tbaa !5
  %11 = zext i16 %10 to i64
  %.idx = shl nuw nsw i64 %11, 1
  %.add = add nuw nsw i64 %.idx, 8
  br label %12

12:                                               ; preds = %12, %8
  %.021.idx = phi i64 [ %.add, %8 ], [ %.021.add, %12 ]
  %.020 = phi i32 [ 0, %8 ], [ %17, %12 ]
  %13 = shl nuw i32 %.020, 16
  %.021.add = add nsw i64 %.021.idx, -2
  %.ptr = getelementptr inbounds i8, ptr %0, i64 %.021.add
  %14 = load i16, ptr %.ptr, align 2, !tbaa !10
  %15 = zext i16 %14 to i32
  %16 = or disjoint i32 %13, %15
  %17 = urem i32 %16, %4
  %18 = icmp ugt i64 %.021.idx, 10
  br i1 %18, label %12, label %19, !llvm.loop !11

19:                                               ; preds = %12
  %20 = getelementptr inbounds i8, ptr %0, i64 6
  %21 = load i8, ptr %20, align 2, !tbaa !13
  %.not = icmp eq i8 %21, 0
  %22 = sub nsw i32 0, %17
  %spec.select = select i1 %.not, i32 %17, i32 %22
  br label %23

23:                                               ; preds = %19, %6
  %.0 = phi i32 [ 0, %6 ], [ %spec.select, %19 ]
  tail call void @pdestroy(ptr noundef %0)
  ret i32 %.0
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @pparmq(ptr noundef) local_unnamed_addr #1

declare ptr @errorp(i32 noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @pdestroy(ptr noundef) local_unnamed_addr #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.abs.i32(i32, i1 immarg) #3

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 = { inlinehint 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { 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 4}
!6 = !{!"", !7, i64 0, !7, i64 2, !7, i64 4, !8, i64 6, !8, i64 8}
!7 = !{!"short", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!7, !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!6, !8, i64 6}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopUnrollPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass