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

; 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 i32, align 4
@b = external dso_local global i32, align 4
@n = external dso_local global i32, align 4
@.str = external hidden unnamed_addr constant [7 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [22 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 i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i8, align 1
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  store i32 1, ptr %2, align 4, !tbaa !5
  br label %12

12:                                               ; preds = %100, %0
  %13 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @a, ptr noundef @b, ptr noundef @n)
  %14 = icmp eq i32 %13, 3
  br i1 %14, label %15, label %22

15:                                               ; preds = %12
  %16 = load i32, ptr @a, align 4, !tbaa !5
  %17 = load i32, ptr @b, align 4, !tbaa !5
  %18 = add nsw i32 %16, %17
  %19 = load i32, ptr @n, align 4, !tbaa !5
  %20 = add nsw i32 %18, %19
  %21 = icmp ne i32 %20, 0
  br label %22

22:                                               ; preds = %15, %12
  %23 = phi i1 [ false, %12 ], [ %21, %15 ]
  br i1 %23, label %25, label %24

24:                                               ; preds = %22
  store i32 2, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #4
  br label %103

25:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  %26 = load i32, ptr @a, align 4, !tbaa !5
  %27 = load i32, ptr @b, align 4, !tbaa !5
  %28 = call noundef i32 @_ZSt5__gcdIiET_S0_S0_(i32 noundef %26, i32 noundef %27)
  store i32 %28, ptr %4, align 4, !tbaa !5
  %29 = load i32, ptr %4, align 4, !tbaa !5
  %30 = load i32, ptr @a, align 4, !tbaa !5
  %31 = sdiv i32 %30, %29
  store i32 %31, ptr @a, align 4, !tbaa !5
  %32 = load i32, ptr %4, align 4, !tbaa !5
  %33 = load i32, ptr @b, align 4, !tbaa !5
  %34 = sdiv i32 %33, %32
  store i32 %34, ptr @b, align 4, !tbaa !5
  %35 = load i32, ptr @b, align 4, !tbaa !5
  %36 = load i32, ptr @n, align 4, !tbaa !5
  %37 = icmp sle i32 %35, %36
  br i1 %37, label %38, label %45

38:                                               ; preds = %25
  %39 = load i32, ptr %2, align 4, !tbaa !5
  %40 = load i32, ptr @a, align 4, !tbaa !5
  %41 = load i32, ptr @b, align 4, !tbaa !5
  %42 = load i32, ptr @a, align 4, !tbaa !5
  %43 = load i32, ptr @b, align 4, !tbaa !5
  %44 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %39, i32 noundef %40, i32 noundef %41, i32 noundef %42, i32 noundef %43)
  store i32 4, ptr %3, align 4
  br label %97

45:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 0, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  store i32 1, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  store i32 1, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  store i32 1, ptr %10, align 4, !tbaa !5
  %46 = load i32, ptr %5, align 4, !tbaa !5
  %47 = load i32, ptr %9, align 4, !tbaa !5
  %48 = add nsw i32 %46, %47
  store i32 %48, ptr %7, align 4, !tbaa !5
  %49 = load i32, ptr %6, align 4, !tbaa !5
  %50 = load i32, ptr %10, align 4, !tbaa !5
  %51 = add nsw i32 %49, %50
  store i32 %51, ptr %8, align 4, !tbaa !5
  br label %52

52:                                               ; preds = %83, %45
  %53 = load i32, ptr %8, align 4, !tbaa !5
  %54 = load i32, ptr @n, align 4, !tbaa !5
  %55 = icmp sle i32 %53, %54
  br i1 %55, label %56, label %90

56:                                               ; preds = %52
  call void @llvm.lifetime.start.p0(i64 1, ptr %11) #4
  %57 = load i32, ptr @a, align 4, !tbaa !5
  %58 = sext i32 %57 to i64
  %59 = load i32, ptr %8, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = mul nsw i64 %58, %60
  %62 = load i32, ptr @b, align 4, !tbaa !5
  %63 = sext i32 %62 to i64
  %64 = load i32, ptr %7, align 4, !tbaa !5
  %65 = sext i32 %64 to i64
  %66 = mul nsw i64 %63, %65
  %67 = icmp sgt i64 %61, %66
  %68 = zext i1 %67 to i8
  store i8 %68, ptr %11, align 1, !tbaa !9
  %69 = load i32, ptr %7, align 4, !tbaa !5
  %70 = load i8, ptr %11, align 1, !tbaa !9, !range !11, !noundef !12
  %71 = trunc i8 %70 to i1
  br i1 %71, label %72, label %73

72:                                               ; preds = %56
  br label %74

73:                                               ; preds = %56
  br label %74

74:                                               ; preds = %73, %72
  %75 = phi ptr [ %5, %72 ], [ %9, %73 ]
  store i32 %69, ptr %75, align 4, !tbaa !5
  %76 = load i32, ptr %8, align 4, !tbaa !5
  %77 = load i8, ptr %11, align 1, !tbaa !9, !range !11, !noundef !12
  %78 = trunc i8 %77 to i1
  br i1 %78, label %79, label %80

79:                                               ; preds = %74
  br label %81

80:                                               ; preds = %74
  br label %81

81:                                               ; preds = %80, %79
  %82 = phi ptr [ %6, %79 ], [ %10, %80 ]
  store i32 %76, ptr %82, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 1, ptr %11) #4
  br label %83

83:                                               ; preds = %81
  %84 = load i32, ptr %5, align 4, !tbaa !5
  %85 = load i32, ptr %9, align 4, !tbaa !5
  %86 = add nsw i32 %84, %85
  store i32 %86, ptr %7, align 4, !tbaa !5
  %87 = load i32, ptr %6, align 4, !tbaa !5
  %88 = load i32, ptr %10, align 4, !tbaa !5
  %89 = add nsw i32 %87, %88
  store i32 %89, ptr %8, align 4, !tbaa !5
  br label %52, !llvm.loop !13

90:                                               ; preds = %52
  %91 = load i32, ptr %2, align 4, !tbaa !5
  %92 = load i32, ptr %5, align 4, !tbaa !5
  %93 = load i32, ptr %6, align 4, !tbaa !5
  %94 = load i32, ptr %9, align 4, !tbaa !5
  %95 = load i32, ptr %10, align 4, !tbaa !5
  %96 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %91, i32 noundef %92, i32 noundef %93, i32 noundef %94, i32 noundef %95)
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  store i32 0, ptr %3, align 4
  br label %97

97:                                               ; preds = %90, %38
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  %98 = load i32, ptr %3, align 4
  switch i32 %98, label %104 [
    i32 0, label %99
    i32 4, label %100
  ]

99:                                               ; preds = %97
  br label %100

100:                                              ; preds = %99, %97
  %101 = load i32, ptr %2, align 4, !tbaa !5
  %102 = add nsw i32 %101, 1
  store i32 %102, ptr %2, align 4, !tbaa !5
  br label %12, !llvm.loop !15

103:                                              ; preds = %24
  ret i32 0

104:                                              ; preds = %97
  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: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZSt5__gcdIiET_S0_S0_(i32 noundef, i32 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 = { 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 = !{!"bool", !7, i64 0}
!11 = !{i8 0, i8 2}
!12 = !{}
!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"

@a = external dso_local global i32, align 4
@b = external dso_local global i32, align 4
@n = external dso_local global i32, align 4
@.str = external hidden unnamed_addr constant [7 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [22 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)
  %2 = icmp eq i32 %1, 3
  br i1 %2, label %.lr.ph, label %.critedge

.lr.ph:                                           ; preds = %0, %27
  %.053 = phi i32 [ %28, %27 ], [ 1, %0 ]
  %3 = load i32, ptr @a, align 4, !tbaa !5
  %4 = load i32, ptr @b, align 4, !tbaa !5
  %5 = add nsw i32 %4, %3
  %6 = load i32, ptr @n, align 4, !tbaa !5
  %7 = sub i32 0, %6
  %.not = icmp eq i32 %5, %7
  br i1 %.not, label %.critedge, label %8

.critedge:                                        ; preds = %.lr.ph, %27, %0
  ret i32 0

8:                                                ; preds = %.lr.ph
  %9 = tail call noundef i32 @_ZSt5__gcdIiET_S0_S0_(i32 noundef %3, i32 noundef %4)
  %10 = load i32, ptr @a, align 4, !tbaa !5
  %11 = sdiv i32 %10, %9
  store i32 %11, ptr @a, align 4, !tbaa !5
  %12 = load i32, ptr @b, align 4, !tbaa !5
  %13 = sdiv i32 %12, %9
  store i32 %13, ptr @b, align 4, !tbaa !5
  %14 = load i32, ptr @n, align 4, !tbaa !5
  %.not41 = icmp sgt i32 %13, %14
  br i1 %.not41, label %.preheader, label %17

.preheader:                                       ; preds = %8
  %.not4243 = icmp slt i32 %14, 2
  br i1 %.not4243, label %._crit_edge, label %.cont9.lr.ph

.cont9.lr.ph:                                     ; preds = %.preheader
  %15 = sext i32 %11 to i64
  %16 = sext i32 %13 to i64
  br label %.cont9

17:                                               ; preds = %8
  %18 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %.053, i32 noundef %11, i32 noundef %13, i32 noundef %11, i32 noundef %13)
  br label %27

.cont9:                                           ; preds = %.cont9.lr.ph, %.cont9
  %.03149 = phi i32 [ 1, %.cont9.lr.ph ], [ %.1, %.cont9 ]
  %.03248 = phi i32 [ 1, %.cont9.lr.ph ], [ %.032..035, %.cont9 ]
  %.03447 = phi i32 [ 2, %.cont9.lr.ph ], [ %25, %.cont9 ]
  %.03546 = phi i32 [ 1, %.cont9.lr.ph ], [ %24, %.cont9 ]
  %.03645 = phi i32 [ 1, %.cont9.lr.ph ], [ %.137, %.cont9 ]
  %.03844 = phi i32 [ 0, %.cont9.lr.ph ], [ %.035..038, %.cont9 ]
  %19 = sext i32 %.03447 to i64
  %20 = mul nsw i64 %19, %15
  %21 = sext i32 %.03546 to i64
  %22 = mul nsw i64 %21, %16
  %23 = icmp sgt i64 %20, %22
  %.035..038 = select i1 %23, i32 %.03546, i32 %.03844
  %.032..035 = select i1 %23, i32 %.03248, i32 %.03546
  %.137 = select i1 %23, i32 %.03447, i32 %.03645
  %.1 = select i1 %23, i32 %.03149, i32 %.03447
  %24 = add nsw i32 %.035..038, %.032..035
  %25 = add nsw i32 %.137, %.1
  %.not42 = icmp sgt i32 %25, %14
  br i1 %.not42, label %._crit_edge, label %.cont9, !llvm.loop !9

._crit_edge:                                      ; preds = %.cont9, %.preheader
  %.038.lcssa = phi i32 [ 0, %.preheader ], [ %.035..038, %.cont9 ]
  %.036.lcssa = phi i32 [ 1, %.preheader ], [ %.137, %.cont9 ]
  %.032.lcssa = phi i32 [ 1, %.preheader ], [ %.032..035, %.cont9 ]
  %.031.lcssa = phi i32 [ 1, %.preheader ], [ %.1, %.cont9 ]
  %26 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i32 noundef %.053, i32 noundef %.038.lcssa, i32 noundef %.036.lcssa, i32 noundef %.032.lcssa, i32 noundef %.031.lcssa)
  br label %27

27:                                               ; preds = %._crit_edge, %17
  %28 = add nuw nsw i32 %.053, 1
  %29 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @a, ptr noundef nonnull @b, ptr noundef nonnull @n)
  %30 = icmp eq i32 %29, 3
  br i1 %30, label %.lr.ph, label %.critedge, !llvm.loop !11
}

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZSt5__gcdIiET_S0_S0_(i32 noundef, i32 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 = { 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 = !{!"int", !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