; LLVM IR from ritikbanger/Hacktoberfest2022-DSA :: 01. Mathematics/chinese_remainder.ll (ZN17chinese_remainder14relative_primeEv)

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

%class.chinese_remainder = type { i32, i32, [100 x i32], [100 x i32], [100 x i32], [100 x 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: mustprogress uwtable
declare dso_local noundef i32 @_ZN17chinese_remainder3gcdEii(i32 noundef, i32 noundef) #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_ZN17chinese_remainder14relative_primeEv(ptr noundef nonnull align 4 dereferenceable(1608) %0) #1 align 2 {
  %2 = alloca i1, align 1
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  %7 = load ptr, ptr %3, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %4, align 4, !tbaa !9
  br label %8

8:                                                ; preds = %45, %1
  %9 = load i32, ptr %4, align 4, !tbaa !9
  %10 = getelementptr inbounds %class.chinese_remainder, ptr %7, i32 0, i32 1
  %11 = load i32, ptr %10, align 4, !tbaa !11
  %12 = sub nsw i32 %11, 1
  %13 = icmp slt i32 %9, %12
  br i1 %13, label %15, label %14

14:                                               ; preds = %8
  store i32 2, ptr %5, align 4
  br label %48

15:                                               ; preds = %8
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %16 = load i32, ptr %4, align 4, !tbaa !9
  %17 = add nsw i32 %16, 1
  store i32 %17, ptr %6, align 4, !tbaa !9
  br label %18

18:                                               ; preds = %39, %15
  %19 = load i32, ptr %6, align 4, !tbaa !9
  %20 = getelementptr inbounds %class.chinese_remainder, ptr %7, i32 0, i32 1
  %21 = load i32, ptr %20, align 4, !tbaa !11
  %22 = icmp slt i32 %19, %21
  br i1 %22, label %24, label %23

23:                                               ; preds = %18
  store i32 5, ptr %5, align 4
  br label %42

24:                                               ; preds = %18
  %25 = getelementptr inbounds %class.chinese_remainder, ptr %7, i32 0, i32 4
  %26 = load i32, ptr %4, align 4, !tbaa !9
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [100 x i32], ptr %25, i64 0, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !9
  %30 = getelementptr inbounds %class.chinese_remainder, ptr %7, i32 0, i32 4
  %31 = load i32, ptr %6, align 4, !tbaa !9
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [100 x i32], ptr %30, i64 0, i64 %32
  %34 = load i32, ptr %33, align 4, !tbaa !9
  %35 = call noundef i32 @_ZN17chinese_remainder3gcdEii(i32 noundef %29, i32 noundef %34)
  %36 = icmp ne i32 %35, 1
  br i1 %36, label %37, label %38

37:                                               ; preds = %24
  store i1 false, ptr %2, align 1
  store i32 1, ptr %5, align 4
  br label %42

38:                                               ; preds = %24
  br label %39

39:                                               ; preds = %38
  %40 = load i32, ptr %6, align 4, !tbaa !9
  %41 = add nsw i32 %40, 1
  store i32 %41, ptr %6, align 4, !tbaa !9
  br label %18, !llvm.loop !13

42:                                               ; preds = %37, %23
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  %43 = load i32, ptr %5, align 4
  switch i32 %43, label %48 [
    i32 5, label %44
  ]

44:                                               ; preds = %42
  br label %45

45:                                               ; preds = %44
  %46 = load i32, ptr %4, align 4, !tbaa !9
  %47 = add nsw i32 %46, 1
  store i32 %47, ptr %4, align 4, !tbaa !9
  br label %8, !llvm.loop !15

48:                                               ; preds = %42, %14
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %49 = load i32, ptr %5, align 4
  switch i32 %49, label %53 [
    i32 2, label %50
    i32 1, label %51
  ]

50:                                               ; preds = %48
  store i1 true, ptr %2, align 1
  br label %51

51:                                               ; preds = %50, %48
  %52 = load i1, ptr %2, align 1
  ret i1 %52

53:                                               ; preds = %48
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 4}
!12 = !{!"_ZTS17chinese_remainder", !10, i64 0, !10, i64 4, !7, i64 8, !7, i64 408, !7, i64 808, !7, i64 1208}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


; 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: mustprogress uwtable
declare dso_local noundef i32 @_ZN17chinese_remainder3gcdEii(i32 noundef, i32 noundef) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_ZN17chinese_remainder14relative_primeEv(ptr nocapture noundef nonnull readonly align 4 dereferenceable(1608) %0) local_unnamed_addr #0 align 2 {
  %2 = getelementptr inbounds i8, ptr %0, i64 4
  %3 = load i32, ptr %2, align 4, !tbaa !5
  %4 = icmp slt i32 %3, 2
  br i1 %4, label %.thread, label %.lr.ph21

.lr.ph21:                                         ; preds = %1
  %5 = getelementptr inbounds i8, ptr %0, i64 808
  br label %9

.loopexit:                                        ; preds = %13, %9
  %6 = phi i32 [ %10, %9 ], [ %14, %13 ]
  %7 = add nsw i32 %6, -1
  %8 = sext i32 %7 to i64
  %.not30 = icmp slt i64 %indvars.iv.next29, %8
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  br i1 %.not30, label %9, label %.thread

9:                                                ; preds = %.lr.ph21, %.loopexit
  %10 = phi i32 [ %3, %.lr.ph21 ], [ %6, %.loopexit ]
  %indvars.iv28 = phi i64 [ 0, %.lr.ph21 ], [ %indvars.iv.next29, %.loopexit ]
  %indvars.iv = phi i64 [ 1, %.lr.ph21 ], [ %indvars.iv.next, %.loopexit ]
  %indvars.iv.next29 = add nuw nsw i64 %indvars.iv28, 1
  %11 = sext i32 %10 to i64
  %.not1418 = icmp slt i64 %indvars.iv.next29, %11
  br i1 %.not1418, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %9
  %12 = getelementptr inbounds [100 x i32], ptr %5, i64 0, i64 %indvars.iv28
  br label %16

13:                                               ; preds = %16
  %indvars.iv.next26 = add nuw nsw i64 %indvars.iv25, 1
  %14 = load i32, ptr %2, align 4, !tbaa !5
  %15 = trunc nuw i64 %indvars.iv.next26 to i32
  %.not14 = icmp sgt i32 %14, %15
  br i1 %.not14, label %16, label %.loopexit, !llvm.loop !10

16:                                               ; preds = %.lr.ph, %13
  %indvars.iv25 = phi i64 [ %indvars.iv, %.lr.ph ], [ %indvars.iv.next26, %13 ]
  %17 = load i32, ptr %12, align 4, !tbaa !12
  %18 = getelementptr inbounds [100 x i32], ptr %5, i64 0, i64 %indvars.iv25
  %19 = load i32, ptr %18, align 4, !tbaa !12
  %20 = tail call noundef i32 @_ZN17chinese_remainder3gcdEii(i32 noundef %17, i32 noundef %19)
  %.not = icmp eq i32 %20, 1
  br i1 %.not, label %13, label %.thread

.thread:                                          ; preds = %.loopexit, %16, %1
  %21 = phi i1 [ true, %1 ], [ false, %16 ], [ true, %.loopexit ]
  ret i1 %21
}

attributes #0 = { mustprogress 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, !7, i64 4}
!6 = !{!"_ZTS17chinese_remainder", !7, i64 0, !7, i64 4, !8, i64 8, !8, i64 408, !8, i64 808, !8, i64 1208}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = !{!7, !7, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass