; LLVM IR from tangjz/acm-icpc :: TDL_training/2015-11-02/upsolved (G - Rational Number Approximation - dby)

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

@a = external dso_local global i64, align 8
@b = external dso_local global i64, align 8
@n = external dso_local global i64, align 8
@.str = external hidden unnamed_addr constant [13 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [10 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [21 x i8], align 1

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

11:                                               ; preds = %89, %87, %0
  %12 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @a, ptr noundef @b, ptr noundef @n)
  %13 = icmp ne i32 %12, -1
  br i1 %13, label %14, label %25

14:                                               ; preds = %11
  %15 = load i64, ptr @a, align 8, !tbaa !9
  %16 = icmp ne i64 %15, 0
  br i1 %16, label %23, label %17

17:                                               ; preds = %14
  %18 = load i64, ptr @b, align 8, !tbaa !9
  %19 = icmp ne i64 %18, 0
  br i1 %19, label %23, label %20

20:                                               ; preds = %17
  %21 = load i64, ptr @n, align 8, !tbaa !9
  %22 = icmp ne i64 %21, 0
  br label %23

23:                                               ; preds = %20, %17, %14
  %24 = phi i1 [ true, %17 ], [ true, %14 ], [ %22, %20 ]
  br label %25

25:                                               ; preds = %23, %11
  %26 = phi i1 [ false, %11 ], [ %24, %23 ]
  br i1 %26, label %27, label %90

27:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %28 = load i64, ptr @a, align 8, !tbaa !9
  %29 = load i64, ptr @b, align 8, !tbaa !9
  %30 = call noundef i64 @_Z3gcdxx(i64 noundef %28, i64 noundef %29)
  store i64 %30, ptr %9, align 8, !tbaa !9
  %31 = load i64, ptr %9, align 8, !tbaa !9
  %32 = load i64, ptr @a, align 8, !tbaa !9
  %33 = sdiv i64 %32, %31
  store i64 %33, ptr @a, align 8, !tbaa !9
  %34 = load i64, ptr %9, align 8, !tbaa !9
  %35 = load i64, ptr @b, align 8, !tbaa !9
  %36 = sdiv i64 %35, %34
  store i64 %36, ptr @b, align 8, !tbaa !9
  %37 = load i32, ptr %2, align 4, !tbaa !5
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr %2, align 4, !tbaa !5
  %39 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %38)
  %40 = load i64, ptr @n, align 8, !tbaa !9
  %41 = load i64, ptr @b, align 8, !tbaa !9
  %42 = icmp sge i64 %40, %41
  br i1 %42, label %43, label %49

43:                                               ; preds = %27
  %44 = load i64, ptr @a, align 8, !tbaa !9
  %45 = load i64, ptr @b, align 8, !tbaa !9
  %46 = load i64, ptr @a, align 8, !tbaa !9
  %47 = load i64, ptr @b, align 8, !tbaa !9
  %48 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i64 noundef %44, i64 noundef %45, i64 noundef %46, i64 noundef %47)
  store i32 2, ptr %10, align 4
  br label %87, !llvm.loop !11

49:                                               ; preds = %27
  store i64 0, ptr %3, align 8, !tbaa !9
  store i64 1, ptr %5, align 8, !tbaa !9
  store i64 1, ptr %4, align 8, !tbaa !9
  store i64 1, ptr %6, align 8, !tbaa !9
  %50 = load i64, ptr %3, align 8, !tbaa !9
  %51 = load i64, ptr %4, align 8, !tbaa !9
  %52 = add nsw i64 %50, %51
  store i64 %52, ptr %7, align 8, !tbaa !9
  %53 = load i64, ptr %5, align 8, !tbaa !9
  %54 = load i64, ptr %6, align 8, !tbaa !9
  %55 = add nsw i64 %53, %54
  store i64 %55, ptr %8, align 8, !tbaa !9
  br label %56

56:                                               ; preds = %74, %49
  %57 = load i64, ptr %8, align 8, !tbaa !9
  %58 = load i64, ptr @n, align 8, !tbaa !9
  %59 = icmp sle i64 %57, %58
  br i1 %59, label %60, label %81

60:                                               ; preds = %56
  %61 = load i64, ptr @a, align 8, !tbaa !9
  %62 = load i64, ptr %8, align 8, !tbaa !9
  %63 = mul nsw i64 %61, %62
  %64 = load i64, ptr @b, align 8, !tbaa !9
  %65 = load i64, ptr %7, align 8, !tbaa !9
  %66 = mul nsw i64 %64, %65
  %67 = icmp slt i64 %63, %66
  br i1 %67, label %68, label %71

68:                                               ; preds = %60
  %69 = load i64, ptr %7, align 8, !tbaa !9
  store i64 %69, ptr %4, align 8, !tbaa !9
  %70 = load i64, ptr %8, align 8, !tbaa !9
  store i64 %70, ptr %6, align 8, !tbaa !9
  br label %74

71:                                               ; preds = %60
  %72 = load i64, ptr %7, align 8, !tbaa !9
  store i64 %72, ptr %3, align 8, !tbaa !9
  %73 = load i64, ptr %8, align 8, !tbaa !9
  store i64 %73, ptr %5, align 8, !tbaa !9
  br label %74

74:                                               ; preds = %71, %68
  %75 = load i64, ptr %3, align 8, !tbaa !9
  %76 = load i64, ptr %4, align 8, !tbaa !9
  %77 = add nsw i64 %75, %76
  store i64 %77, ptr %7, align 8, !tbaa !9
  %78 = load i64, ptr %5, align 8, !tbaa !9
  %79 = load i64, ptr %6, align 8, !tbaa !9
  %80 = add nsw i64 %78, %79
  store i64 %80, ptr %8, align 8, !tbaa !9
  br label %56, !llvm.loop !13

81:                                               ; preds = %56
  %82 = load i64, ptr %3, align 8, !tbaa !9
  %83 = load i64, ptr %5, align 8, !tbaa !9
  %84 = load i64, ptr %4, align 8, !tbaa !9
  %85 = load i64, ptr %6, align 8, !tbaa !9
  %86 = call i32 (ptr, ...) @printf(ptr noundef @.str.2, i64 noundef %82, i64 noundef %83, i64 noundef %84, i64 noundef %85)
  store i32 0, ptr %10, align 4
  br label %87

87:                                               ; preds = %81, %43
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  %88 = load i32, ptr %10, align 4
  switch i32 %88, label %92 [
    i32 0, label %89
    i32 2, label %11
  ]

89:                                               ; preds = %87
  br label %11, !llvm.loop !11

90:                                               ; preds = %25
  store i32 0, ptr %1, align 4
  store i32 1, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #4
  %91 = load i32, ptr %1, align 4
  ret i32 %91

92:                                               ; preds = %87
  unreachable
}

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

declare i32 @__isoc99_scanf(ptr noundef, ...) #2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i64 @_Z3gcdxx(i64 noundef, i64 noundef) #3

declare i32 @printf(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 = { mustprogress norecurse 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 = { "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 = { inlinehint mustprogress 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 #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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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

@a = external dso_local global i64, align 8
@b = external dso_local global i64, align 8
@n = external dso_local global i64, align 8
@.str = external hidden unnamed_addr constant [13 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [10 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [21 x i8], align 1

; Function Attrs: mustprogress norecurse nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @a, ptr noundef nonnull @b, ptr noundef nonnull @n)
  %.not50 = icmp eq i32 %1, -1
  br i1 %.not50, label %.critedge, label %.lr.ph53

.lr.ph53:                                         ; preds = %0, %28
  %.03651 = phi i32 [ %13, %28 ], [ 0, %0 ]
  %2 = load i64, ptr @a, align 8, !tbaa !5
  %3 = icmp ne i64 %2, 0
  %4 = load i64, ptr @b, align 8
  %5 = icmp ne i64 %4, 0
  %or.cond = select i1 %3, i1 true, i1 %5
  %6 = load i64, ptr @n, align 8
  %7 = icmp ne i64 %6, 0
  %or.cond5 = select i1 %or.cond, i1 true, i1 %7
  br i1 %or.cond5, label %.critedge3, label %.critedge

.critedge3:                                       ; preds = %.lr.ph53
  %8 = tail call noundef i64 @_Z3gcdxx(i64 noundef %2, i64 noundef %4)
  %9 = load i64, ptr @a, align 8, !tbaa !5
  %10 = sdiv i64 %9, %8
  store i64 %10, ptr @a, align 8, !tbaa !5
  %11 = load i64, ptr @b, align 8, !tbaa !5
  %12 = sdiv i64 %11, %8
  store i64 %12, ptr @b, align 8, !tbaa !5
  %13 = add nuw nsw i32 %.03651, 1
  %14 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %13)
  %15 = load i64, ptr @n, align 8, !tbaa !5
  %16 = load i64, ptr @b, align 8, !tbaa !5
  %.not38 = icmp slt i64 %15, %16
  br i1 %.not38, label %.preheader, label %18

.preheader:                                       ; preds = %.critedge3
  %.not3940 = icmp slt i64 %15, 2
  br i1 %.not3940, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader
  %17 = load i64, ptr @a, align 8, !tbaa !5
  br label %21

18:                                               ; preds = %.critedge3
  %19 = load i64, ptr @a, align 8, !tbaa !5
  %20 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i64 noundef %19, i64 noundef %16, i64 noundef %19, i64 noundef %16)
  br label %28, !llvm.loop !9

21:                                               ; preds = %.lr.ph, %21
  %.046 = phi i64 [ 2, %.lr.ph ], [ %26, %21 ]
  %.02845 = phi i64 [ 1, %.lr.ph ], [ %25, %21 ]
  %.02944 = phi i64 [ 1, %.lr.ph ], [ %.0..029, %21 ]
  %.03043 = phi i64 [ 1, %.lr.ph ], [ %.030..0, %21 ]
  %.03242 = phi i64 [ 1, %.lr.ph ], [ %.028..032, %21 ]
  %.03441 = phi i64 [ 0, %.lr.ph ], [ %.034..028, %21 ]
  %22 = mul nsw i64 %17, %.046
  %23 = mul nsw i64 %.02845, %16
  %24 = icmp slt i64 %22, %23
  %.034..028 = select i1 %24, i64 %.03441, i64 %.02845
  %.028..032 = select i1 %24, i64 %.02845, i64 %.03242
  %.030..0 = select i1 %24, i64 %.03043, i64 %.046
  %.0..029 = select i1 %24, i64 %.046, i64 %.02944
  %25 = add nsw i64 %.034..028, %.028..032
  %26 = add nsw i64 %.030..0, %.0..029
  %.not39 = icmp sgt i64 %26, %15
  br i1 %.not39, label %._crit_edge, label %21, !llvm.loop !11

._crit_edge:                                      ; preds = %21, %.preheader
  %.034.lcssa = phi i64 [ 0, %.preheader ], [ %.034..028, %21 ]
  %.032.lcssa = phi i64 [ 1, %.preheader ], [ %.028..032, %21 ]
  %.030.lcssa = phi i64 [ 1, %.preheader ], [ %.030..0, %21 ]
  %.029.lcssa = phi i64 [ 1, %.preheader ], [ %.0..029, %21 ]
  %27 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i64 noundef %.034.lcssa, i64 noundef %.030.lcssa, i64 noundef %.032.lcssa, i64 noundef %.029.lcssa)
  br label %28

28:                                               ; preds = %._crit_edge, %18
  %29 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @a, ptr noundef nonnull @b, ptr noundef nonnull @n)
  %.not = icmp eq i32 %29, -1
  br i1 %.not, label %.critedge, label %.lr.ph53

.critedge:                                        ; preds = %28, %.lr.ph53, %0
  ret i32 0
}

; Function Attrs: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i64 @_Z3gcdxx(i64 noundef, i64 noundef) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

attributes #0 = { mustprogress norecurse 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 = { nofree nounwind "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 = { inlinehint mustprogress 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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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