; LLVM IR from tangjz/acm-icpc :: TDL_training/2016-03-20 (G - Reversed Continued Fraction - lzy)

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

@p = external dso_local global i32, align 4
@q = external dso_local global i32, align 4
@n = external dso_local global i32, align 4
@a = external dso_local global [233 x i32], align 16
@.str = external hidden unnamed_addr constant [21 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1
@stdin = external global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [22 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [2 x i8], align 1
@stdout = external global ptr, align 8
@.str.4 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [7 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
  store i32 0, ptr %1, align 4
  %5 = load ptr, ptr @stdin, align 8, !tbaa !5
  %6 = call ptr @freopen(ptr noundef @.str, ptr noundef @.str.1, ptr noundef %5)
  %7 = load ptr, ptr @stdout, align 8, !tbaa !5
  %8 = call ptr @freopen(ptr noundef @.str.2, ptr noundef @.str.3, ptr noundef %7)
  %9 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.4, ptr noundef @p, ptr noundef @q)
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  br label %10

10:                                               ; preds = %22, %0
  %11 = load i32, ptr @p, align 4, !tbaa !9
  %12 = icmp ne i32 %11, 0
  br i1 %12, label %14, label %13

13:                                               ; preds = %10
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  br label %28

14:                                               ; preds = %10
  %15 = load i32, ptr @q, align 4, !tbaa !9
  %16 = load i32, ptr @p, align 4, !tbaa !9
  %17 = sdiv i32 %15, %16
  %18 = load i32, ptr @n, align 4, !tbaa !9
  %19 = add nsw i32 %18, 1
  store i32 %19, ptr @n, align 4, !tbaa !9
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [233 x i32], ptr @a, i64 0, i64 %20
  store i32 %17, ptr %21, align 4, !tbaa !9
  br label %22

22:                                               ; preds = %14
  %23 = load i32, ptr @q, align 4, !tbaa !9
  %24 = load i32, ptr @p, align 4, !tbaa !9
  %25 = srem i32 %23, %24
  store i32 %25, ptr %2, align 4, !tbaa !9
  %26 = load i32, ptr @p, align 4, !tbaa !9
  store i32 %26, ptr @q, align 4, !tbaa !9
  %27 = load i32, ptr %2, align 4, !tbaa !9
  store i32 %27, ptr @p, align 4, !tbaa !9
  br label %10, !llvm.loop !11

28:                                               ; preds = %13
  store i32 0, ptr @p, align 4, !tbaa !9
  store i32 1, ptr @q, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 1, ptr %3, align 4, !tbaa !9
  br label %29

29:                                               ; preds = %45, %28
  %30 = load i32, ptr %3, align 4, !tbaa !9
  %31 = load i32, ptr @n, align 4, !tbaa !9
  %32 = icmp sle i32 %30, %31
  br i1 %32, label %34, label %33

33:                                               ; preds = %29
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %48

34:                                               ; preds = %29
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  %35 = load i32, ptr %3, align 4, !tbaa !9
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [233 x i32], ptr @a, i64 0, i64 %36
  %38 = load i32, ptr %37, align 4, !tbaa !9
  %39 = load i32, ptr @q, align 4, !tbaa !9
  %40 = mul nsw i32 %38, %39
  %41 = load i32, ptr @p, align 4, !tbaa !9
  %42 = add nsw i32 %40, %41
  store i32 %42, ptr %4, align 4, !tbaa !9
  %43 = load i32, ptr @q, align 4, !tbaa !9
  store i32 %43, ptr @p, align 4, !tbaa !9
  %44 = load i32, ptr %4, align 4, !tbaa !9
  store i32 %44, ptr @q, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %45

45:                                               ; preds = %34
  %46 = load i32, ptr %3, align 4, !tbaa !9
  %47 = add nsw i32 %46, 1
  store i32 %47, ptr %3, align 4, !tbaa !9
  br label %29, !llvm.loop !13

48:                                               ; preds = %33
  %49 = load i32, ptr @p, align 4, !tbaa !9
  %50 = load i32, ptr @q, align 4, !tbaa !9
  %51 = call i32 (ptr, ...) @printf(ptr noundef @.str.5, i32 noundef %49, i32 noundef %50)
  ret i32 0
}

declare ptr @freopen(ptr noundef, ptr noundef, ptr noundef) #1

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

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

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

declare i32 @printf(ptr noundef, ...) #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 = { "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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 = 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"

@p = external dso_local global i32, align 4
@q = external dso_local global i32, align 4
@n = external dso_local local_unnamed_addr global i32, align 4
@a = external dso_local local_unnamed_addr global [233 x i32], align 16
@.str = external hidden unnamed_addr constant [21 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1
@stdin = external local_unnamed_addr global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [22 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [2 x i8], align 1
@stdout = external local_unnamed_addr global ptr, align 8
@.str.4 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [7 x i8], align 1

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = load ptr, ptr @stdin, align 8, !tbaa !5
  %2 = tail call ptr @freopen(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, ptr noundef %1)
  %3 = load ptr, ptr @stdout, align 8, !tbaa !5
  %4 = tail call ptr @freopen(ptr noundef nonnull @.str.2, ptr noundef nonnull @.str.3, ptr noundef %3)
  %5 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.4, ptr noundef nonnull @p, ptr noundef nonnull @q)
  %.pr = load i32, ptr @p, align 4, !tbaa !9
  %n.promoted = load i32, ptr @n, align 4, !tbaa !9
  %.not7 = icmp eq i32 %.pr, 0
  br i1 %.not7, label %8, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %0
  %q.promoted = load i32, ptr @q, align 4, !tbaa !9
  %6 = sext i32 %n.promoted to i64
  br label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph
  %7 = trunc nsw i64 %indvars.iv.next to i32
  store i32 %7, ptr @n, align 4, !tbaa !9
  br label %8

8:                                                ; preds = %._crit_edge, %0
  %9 = phi i32 [ %7, %._crit_edge ], [ %n.promoted, %0 ]
  store i32 0, ptr @p, align 4, !tbaa !9
  store i32 1, ptr @q, align 4, !tbaa !9
  %.not611 = icmp slt i32 %9, 1
  br i1 %.not611, label %23, label %.lr.ph14.preheader

.lr.ph14.preheader:                               ; preds = %8
  %10 = zext nneg i32 %9 to i64
  %xtraiter = and i64 %10, 3
  %11 = icmp ult i32 %9, 4
  br i1 %11, label %._crit_edge15.unr-lcssa, label %.lr.ph14.preheader.new

.lr.ph14.preheader.new:                           ; preds = %.lr.ph14.preheader
  %unroll_iter = and i64 %10, 2147483644
  br label %.lr.ph14

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv = phi i64 [ %6, %.lr.ph.preheader ], [ %indvars.iv.next, %.lr.ph ]
  %12 = phi i32 [ %.pr, %.lr.ph.preheader ], [ %16, %.lr.ph ]
  %13 = phi i32 [ %q.promoted, %.lr.ph.preheader ], [ %12, %.lr.ph ]
  %14 = sdiv i32 %13, %12
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %15 = getelementptr inbounds [233 x i32], ptr @a, i64 0, i64 %indvars.iv.next
  store i32 %14, ptr %15, align 4, !tbaa !9
  %16 = srem i32 %13, %12
  %.not = icmp eq i32 %16, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !11

._crit_edge15.unr-lcssa:                          ; preds = %.lr.ph14, %.lr.ph14.preheader
  %.lcssa28.ph = phi i32 [ poison, %.lr.ph14.preheader ], [ %40, %.lr.ph14 ]
  %.lcssa.ph = phi i32 [ poison, %.lr.ph14.preheader ], [ %44, %.lr.ph14 ]
  %indvars.iv23.unr = phi i64 [ 1, %.lr.ph14.preheader ], [ %indvars.iv.next24.3, %.lr.ph14 ]
  %.unr = phi i32 [ 1, %.lr.ph14.preheader ], [ %44, %.lr.ph14 ]
  %.unr29 = phi i32 [ 0, %.lr.ph14.preheader ], [ %40, %.lr.ph14 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge15, label %.lr.ph14.epil

.lr.ph14.epil:                                    ; preds = %._crit_edge15.unr-lcssa, %.lr.ph14.epil
  %indvars.iv23.epil = phi i64 [ %indvars.iv.next24.epil, %.lr.ph14.epil ], [ %indvars.iv23.unr, %._crit_edge15.unr-lcssa ]
  %17 = phi i32 [ %22, %.lr.ph14.epil ], [ %.unr, %._crit_edge15.unr-lcssa ]
  %18 = phi i32 [ %17, %.lr.ph14.epil ], [ %.unr29, %._crit_edge15.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph14.epil ], [ 0, %._crit_edge15.unr-lcssa ]
  %19 = getelementptr inbounds [233 x i32], ptr @a, i64 0, i64 %indvars.iv23.epil
  %20 = load i32, ptr %19, align 4, !tbaa !9
  %21 = mul nsw i32 %17, %20
  %22 = add nsw i32 %21, %18
  %indvars.iv.next24.epil = add nuw nsw i64 %indvars.iv23.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge15, label %.lr.ph14.epil, !llvm.loop !13

._crit_edge15:                                    ; preds = %.lr.ph14.epil, %._crit_edge15.unr-lcssa
  %.lcssa28 = phi i32 [ %.lcssa28.ph, %._crit_edge15.unr-lcssa ], [ %17, %.lr.ph14.epil ]
  %.lcssa = phi i32 [ %.lcssa.ph, %._crit_edge15.unr-lcssa ], [ %22, %.lr.ph14.epil ]
  store i32 %.lcssa28, ptr @p, align 4, !tbaa !9
  store i32 %.lcssa, ptr @q, align 4, !tbaa !9
  br label %23

23:                                               ; preds = %._crit_edge15, %8
  %24 = phi i32 [ %.lcssa, %._crit_edge15 ], [ 1, %8 ]
  %25 = phi i32 [ %.lcssa28, %._crit_edge15 ], [ 0, %8 ]
  %26 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.5, i32 noundef %25, i32 noundef %24)
  ret i32 0

.lr.ph14:                                         ; preds = %.lr.ph14, %.lr.ph14.preheader.new
  %indvars.iv23 = phi i64 [ 1, %.lr.ph14.preheader.new ], [ %indvars.iv.next24.3, %.lr.ph14 ]
  %27 = phi i32 [ 1, %.lr.ph14.preheader.new ], [ %44, %.lr.ph14 ]
  %28 = phi i32 [ 0, %.lr.ph14.preheader.new ], [ %40, %.lr.ph14 ]
  %niter = phi i64 [ 0, %.lr.ph14.preheader.new ], [ %niter.next.3, %.lr.ph14 ]
  %29 = getelementptr inbounds [233 x i32], ptr @a, i64 0, i64 %indvars.iv23
  %30 = load i32, ptr %29, align 4, !tbaa !9
  %31 = mul nsw i32 %27, %30
  %32 = add nsw i32 %31, %28
  %indvars.iv.next24 = add nuw nsw i64 %indvars.iv23, 1
  %33 = getelementptr inbounds [233 x i32], ptr @a, i64 0, i64 %indvars.iv.next24
  %34 = load i32, ptr %33, align 4, !tbaa !9
  %35 = mul nsw i32 %32, %34
  %36 = add nsw i32 %35, %27
  %indvars.iv.next24.1 = add nuw nsw i64 %indvars.iv23, 2
  %37 = getelementptr inbounds [233 x i32], ptr @a, i64 0, i64 %indvars.iv.next24.1
  %38 = load i32, ptr %37, align 4, !tbaa !9
  %39 = mul nsw i32 %36, %38
  %40 = add nsw i32 %39, %32
  %indvars.iv.next24.2 = add nuw nsw i64 %indvars.iv23, 3
  %41 = getelementptr inbounds [233 x i32], ptr @a, i64 0, i64 %indvars.iv.next24.2
  %42 = load i32, ptr %41, align 4, !tbaa !9
  %43 = mul nsw i32 %40, %42
  %44 = add nsw i32 %43, %36
  %indvars.iv.next24.3 = add nuw nsw i64 %indvars.iv23, 4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %._crit_edge15.unr-lcssa, label %.lr.ph14, !llvm.loop !15
}

declare ptr @freopen(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

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

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

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 = { "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 = { 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" }

!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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.unroll.disable"}
!15 = distinct !{!15, !12}


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