; LLVM IR from ravireddy07/cpp-book :: src/Code-Chef (LFEB14B)

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

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

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i64, align 8
  %3 = alloca i32, align 4
  %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
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #3
  store i64 1000000007, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  %10 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %3)
  br label %11

11:                                               ; preds = %51, %0
  %12 = load i32, ptr %3, align 4, !tbaa !9
  %13 = add nsw i32 %12, -1
  store i32 %13, ptr %3, align 4, !tbaa !9
  %14 = icmp ne i32 %12, 0
  br i1 %14, label %15, label %58

15:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %16 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.1, ptr noundef %4)
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  store i64 0, ptr %5, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  store i64 0, ptr %6, align 8, !tbaa !11
  br label %17

17:                                               ; preds = %36, %15
  %18 = load i64, ptr %4, align 8, !tbaa !11
  %19 = add nsw i64 %18, -1
  store i64 %19, ptr %4, align 8, !tbaa !11
  %20 = icmp ne i64 %18, 0
  br i1 %20, label %21, label %37

21:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %22 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.2, ptr noundef %7)
  %23 = load i64, ptr %7, align 8, !tbaa !11
  %24 = load i64, ptr %5, align 8, !tbaa !11
  %25 = icmp sgt i64 %23, %24
  br i1 %25, label %26, label %28

26:                                               ; preds = %21
  %27 = load i64, ptr %7, align 8, !tbaa !11
  store i64 %27, ptr %5, align 8, !tbaa !11
  store i64 1, ptr %6, align 8, !tbaa !11
  br label %36

28:                                               ; preds = %21
  %29 = load i64, ptr %7, align 8, !tbaa !11
  %30 = load i64, ptr %5, align 8, !tbaa !11
  %31 = icmp eq i64 %29, %30
  br i1 %31, label %32, label %35

32:                                               ; preds = %28
  %33 = load i64, ptr %6, align 8, !tbaa !11
  %34 = add nsw i64 %33, 1
  store i64 %34, ptr %6, align 8, !tbaa !11
  br label %35

35:                                               ; preds = %32, %28
  br label %36

36:                                               ; preds = %35, %26
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  br label %17, !llvm.loop !13

37:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  store i64 1, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  store i64 0, ptr %9, align 8, !tbaa !11
  br label %38

38:                                               ; preds = %48, %37
  %39 = load i64, ptr %9, align 8, !tbaa !11
  %40 = load i64, ptr %6, align 8, !tbaa !11
  %41 = icmp slt i64 %39, %40
  br i1 %41, label %43, label %42

42:                                               ; preds = %38
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  br label %51

43:                                               ; preds = %38
  %44 = load i64, ptr %8, align 8, !tbaa !5
  %45 = mul nsw i64 %44, 2
  store i64 %45, ptr %8, align 8, !tbaa !5
  %46 = load i64, ptr %8, align 8, !tbaa !5
  %47 = srem i64 %46, 1000000007
  store i64 %47, ptr %8, align 8, !tbaa !5
  br label %48

48:                                               ; preds = %43
  %49 = load i64, ptr %9, align 8, !tbaa !11
  %50 = add nsw i64 %49, 1
  store i64 %50, ptr %9, align 8, !tbaa !11
  br label %38, !llvm.loop !15

51:                                               ; preds = %42
  %52 = load i64, ptr %8, align 8, !tbaa !5
  %53 = add nsw i64 %52, 1000000007
  %54 = sub nsw i64 %53, 1
  %55 = srem i64 %54, 1000000007
  store i64 %55, ptr %8, align 8, !tbaa !5
  %56 = load i64, ptr %8, align 8, !tbaa !5
  %57 = call i32 (ptr, ...) @printf(ptr noundef @.str.3, i64 noundef %56)
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  br label %11, !llvm.loop !16

58:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #3
  ret i32 0
}

; 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

declare i32 @printf(ptr noundef, ...) #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 = { 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 = { 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 = !{!"long long", !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, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}
!16 = distinct !{!16, !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"

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

; Function Attrs: mustprogress nofree norecurse nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca i32, align 4
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #4
  %4 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %1)
  %5 = load i32, ptr %1, align 4, !tbaa !5
  %6 = add nsw i32 %5, -1
  store i32 %6, ptr %1, align 4, !tbaa !5
  %.not24 = icmp eq i32 %5, 0
  br i1 %.not24, label %._crit_edge27, label %.lr.ph26

.lr.ph26:                                         ; preds = %0, %._crit_edge
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #4
  %7 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %2)
  %8 = load i64, ptr %2, align 8, !tbaa !9
  %9 = add nsw i64 %8, -1
  store i64 %9, ptr %2, align 8, !tbaa !9
  %.not1617 = icmp eq i64 %8, 0
  br i1 %.not1617, label %._crit_edge, label %.lr.ph

.preheader:                                       ; preds = %.lr.ph
  %10 = icmp sgt i64 %.1, 0
  br i1 %10, label %.lr.ph22.preheader, label %._crit_edge

.lr.ph22.preheader:                               ; preds = %.preheader
  %xtraiter = and i64 %.1, 3
  %11 = icmp ult i64 %.1, 4
  br i1 %11, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph22.preheader.new

.lr.ph22.preheader.new:                           ; preds = %.lr.ph22.preheader
  %unroll_iter = and i64 %.1, 9223372036854775804
  br label %.lr.ph22

.lr.ph:                                           ; preds = %.lr.ph26, %.lr.ph
  %.01119 = phi i64 [ %.1, %.lr.ph ], [ 0, %.lr.ph26 ]
  %.01218 = phi i64 [ %.113, %.lr.ph ], [ 0, %.lr.ph26 ]
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #4
  %12 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.2, ptr noundef nonnull %3)
  %13 = load i64, ptr %3, align 8, !tbaa !9
  %14 = icmp sgt i64 %13, %.01218
  %15 = icmp eq i64 %13, %.01218
  %16 = zext i1 %15 to i64
  %spec.select = add nsw i64 %.01119, %16
  %.113 = call i64 @llvm.smax.i64(i64 %13, i64 %.01218)
  %.1 = select i1 %14, i64 1, i64 %spec.select
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #4
  %17 = load i64, ptr %2, align 8, !tbaa !9
  %18 = add nsw i64 %17, -1
  store i64 %18, ptr %2, align 8, !tbaa !9
  %.not16 = icmp eq i64 %17, 0
  br i1 %.not16, label %.preheader, label %.lr.ph, !llvm.loop !11

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph22, %.lr.ph22.preheader
  %.lcssa.ph = phi i64 [ poison, %.lr.ph22.preheader ], [ %33, %.lr.ph22 ]
  %.01020.unr = phi i64 [ 1, %.lr.ph22.preheader ], [ %33, %.lr.ph22 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph22.epil

.lr.ph22.epil:                                    ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph22.epil
  %.01020.epil = phi i64 [ %20, %.lr.ph22.epil ], [ %.01020.unr, %._crit_edge.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph22.epil ], [ 0, %._crit_edge.loopexit.unr-lcssa ]
  %19 = shl nsw i64 %.01020.epil, 1
  %20 = srem i64 %19, 1000000007
  %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_edge, label %.lr.ph22.epil, !llvm.loop !13

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph22.epil, %.lr.ph26, %.preheader
  %.010.lcssa = phi i64 [ 1, %.preheader ], [ 1, %.lr.ph26 ], [ %.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %20, %.lr.ph22.epil ]
  %.010.lcssa.fr = freeze i64 %.010.lcssa
  %21 = add i64 %.010.lcssa.fr, 1000000006
  %.urem = add i64 %.010.lcssa.fr, -1
  %.cmp = icmp ult i64 %21, 1000000007
  %22 = select i1 %.cmp, i64 %21, i64 %.urem
  %23 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3, i64 noundef %22)
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #4
  %24 = load i32, ptr %1, align 4, !tbaa !5
  %25 = add nsw i32 %24, -1
  store i32 %25, ptr %1, align 4, !tbaa !5
  %.not = icmp eq i32 %24, 0
  br i1 %.not, label %._crit_edge27, label %.lr.ph26, !llvm.loop !15

.lr.ph22:                                         ; preds = %.lr.ph22, %.lr.ph22.preheader.new
  %.01020 = phi i64 [ 1, %.lr.ph22.preheader.new ], [ %33, %.lr.ph22 ]
  %niter = phi i64 [ 0, %.lr.ph22.preheader.new ], [ %niter.next.3, %.lr.ph22 ]
  %26 = shl nsw i64 %.01020, 1
  %27 = srem i64 %26, 1000000007
  %28 = shl nsw i64 %27, 1
  %29 = srem i64 %28, 1000000007
  %30 = shl nsw i64 %29, 1
  %31 = srem i64 %30, 1000000007
  %32 = shl nsw i64 %31, 1
  %33 = srem i64 %32, 1000000007
  %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_edge.loopexit.unr-lcssa, label %.lr.ph22, !llvm.loop !16

._crit_edge27:                                    ; preds = %._crit_edge, %0
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #4
  ret i32 0
}

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

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

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

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.smax.i64(i64, i64) #3

attributes #0 = { mustprogress nofree 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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" }
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, !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", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.unroll.disable"}
!15 = distinct !{!15, !12}
!16 = distinct !{!16, !12}


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