; LLVM IR from smv1999/CompetitiveProgrammingQuestionBank :: CodeVita Problems (single_lane_highway_question)

; 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 [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [6 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca double, align 8
  %8 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  %9 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %2)
  %10 = load i32, ptr %2, align 4, !tbaa !5
  %11 = zext i32 %10 to i64
  %12 = call ptr @llvm.stacksave.p0()
  store ptr %12, ptr %3, align 8
  %13 = alloca i32, i64 %11, align 16
  store i64 %11, ptr %4, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %14

14:                                               ; preds = %24, %0
  %15 = load i32, ptr %5, align 4, !tbaa !5
  %16 = load i32, ptr %2, align 4, !tbaa !5
  %17 = icmp slt i32 %15, %16
  br i1 %17, label %19, label %18

18:                                               ; preds = %14
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br label %27

19:                                               ; preds = %14
  %20 = load i32, ptr %5, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds i32, ptr %13, i64 %21
  %23 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %22)
  br label %24

24:                                               ; preds = %19
  %25 = load i32, ptr %5, align 4, !tbaa !5
  %26 = add nsw i32 %25, 1
  store i32 %26, ptr %5, align 4, !tbaa !5
  br label %14, !llvm.loop !9

27:                                               ; preds = %18
  %28 = load i32, ptr %2, align 4, !tbaa !5
  %29 = icmp eq i32 %28, 0
  br i1 %29, label %33, label %30

30:                                               ; preds = %27
  %31 = load i32, ptr %2, align 4, !tbaa !5
  %32 = icmp eq i32 %31, 1
  br i1 %32, label %33, label %38

33:                                               ; preds = %30, %27
  %34 = load i32, ptr %2, align 4, !tbaa !5
  %35 = sitofp i32 %34 to float
  %36 = fpext float %35 to double
  %37 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, double noundef %36)
  store i32 0, ptr %1, align 4
  store i32 1, ptr %6, align 4
  br label %56

38:                                               ; preds = %30
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  store double 0.000000e+00, ptr %7, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  store i32 1, ptr %8, align 4, !tbaa !5
  br label %39

39:                                               ; preds = %50, %38
  %40 = load i32, ptr %8, align 4, !tbaa !5
  %41 = load i32, ptr %2, align 4, !tbaa !5
  %42 = icmp sle i32 %40, %41
  br i1 %42, label %44, label %43

43:                                               ; preds = %39
  store i32 5, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  br label %53

44:                                               ; preds = %39
  %45 = load i32, ptr %8, align 4, !tbaa !5
  %46 = sitofp i32 %45 to double
  %47 = fdiv double 1.000000e+00, %46
  %48 = load double, ptr %7, align 8, !tbaa !11
  %49 = fadd double %48, %47
  store double %49, ptr %7, align 8, !tbaa !11
  br label %50

50:                                               ; preds = %44
  %51 = load i32, ptr %8, align 4, !tbaa !5
  %52 = add nsw i32 %51, 1
  store i32 %52, ptr %8, align 4, !tbaa !5
  br label %39, !llvm.loop !13

53:                                               ; preds = %43
  %54 = load double, ptr %7, align 8, !tbaa !11
  %55 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, double noundef %54)
  store i32 0, ptr %1, align 4
  store i32 1, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  br label %56

56:                                               ; preds = %53, %33
  %57 = load ptr, ptr %3, align 8
  call void @llvm.stackrestore.p0(ptr %57)
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #4
  %58 = load i32, ptr %1, align 4
  ret i32 %58
}

; 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
declare ptr @llvm.stacksave.p0() #3

; 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

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #3

attributes #0 = { 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 = { 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 = { nocallback nofree nosync nounwind willreturn }
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/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !12, i64 0}
!12 = !{!"double", !7, i64 0}
!13 = distinct !{!13, !10}


; 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 [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [6 x i8], align 1

; Function Attrs: nofree nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #4
  %2 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %1)
  %3 = load i32, ptr %1, align 4, !tbaa !5
  %4 = zext i32 %3 to i64
  %5 = call ptr @llvm.stacksave.p0()
  %6 = alloca i32, i64 %4, align 16
  %7 = load i32, ptr %1, align 4
  %8 = icmp sgt i32 %7, 0
  br i1 %8, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %0
  %.lcssa = phi i32 [ %7, %0 ], [ %12, %.lr.ph ]
  %or.cond = icmp ult i32 %.lcssa, 2
  br i1 %or.cond, label %15, label %.preheader

.preheader:                                       ; preds = %._crit_edge
  %.not15 = icmp slt i32 %.lcssa, 1
  br i1 %.not15, label %._crit_edge19, label %.lr.ph18.preheader

.lr.ph18.preheader:                               ; preds = %.preheader
  %xtraiter = and i32 %.lcssa, 3
  %9 = icmp ult i32 %.lcssa, 4
  br i1 %9, label %._crit_edge19.loopexit.unr-lcssa, label %.lr.ph18.preheader.new

.lr.ph18.preheader.new:                           ; preds = %.lr.ph18.preheader
  %unroll_iter = and i32 %.lcssa, 2147483644
  br label %.lr.ph18

.lr.ph:                                           ; preds = %0, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %0 ]
  %10 = getelementptr inbounds i32, ptr %6, i64 %indvars.iv
  %11 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %10)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %12 = load i32, ptr %1, align 4
  %13 = sext i32 %12 to i64
  %14 = icmp slt i64 %indvars.iv.next, %13
  br i1 %14, label %.lr.ph, label %._crit_edge, !llvm.loop !9

15:                                               ; preds = %._crit_edge
  %16 = uitofp nneg i32 %.lcssa to double
  br label %._crit_edge19

.lr.ph18:                                         ; preds = %.lr.ph18, %.lr.ph18.preheader.new
  %.017 = phi i32 [ 1, %.lr.ph18.preheader.new ], [ %32, %.lr.ph18 ]
  %.01016 = phi double [ 0.000000e+00, %.lr.ph18.preheader.new ], [ %31, %.lr.ph18 ]
  %niter = phi i32 [ 0, %.lr.ph18.preheader.new ], [ %niter.next.3, %.lr.ph18 ]
  %17 = uitofp nneg i32 %.017 to double
  %18 = fdiv double 1.000000e+00, %17
  %19 = fadd double %.01016, %18
  %20 = add nuw nsw i32 %.017, 1
  %21 = uitofp nneg i32 %20 to double
  %22 = fdiv double 1.000000e+00, %21
  %23 = fadd double %19, %22
  %24 = add nuw nsw i32 %.017, 2
  %25 = uitofp nneg i32 %24 to double
  %26 = fdiv double 1.000000e+00, %25
  %27 = fadd double %23, %26
  %28 = add nuw i32 %.017, 3
  %29 = uitofp nneg i32 %28 to double
  %30 = fdiv double 1.000000e+00, %29
  %31 = fadd double %27, %30
  %32 = add nuw i32 %.017, 4
  %niter.next.3 = add i32 %niter, 4
  %niter.ncmp.3 = icmp eq i32 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %._crit_edge19.loopexit.unr-lcssa, label %.lr.ph18, !llvm.loop !11

._crit_edge19.loopexit.unr-lcssa:                 ; preds = %.lr.ph18, %.lr.ph18.preheader
  %.lcssa28.ph = phi double [ poison, %.lr.ph18.preheader ], [ %31, %.lr.ph18 ]
  %.017.unr = phi i32 [ 1, %.lr.ph18.preheader ], [ %32, %.lr.ph18 ]
  %.01016.unr = phi double [ 0.000000e+00, %.lr.ph18.preheader ], [ %31, %.lr.ph18 ]
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge19, label %.lr.ph18.epil

.lr.ph18.epil:                                    ; preds = %._crit_edge19.loopexit.unr-lcssa, %.lr.ph18.epil
  %.017.epil = phi i32 [ %36, %.lr.ph18.epil ], [ %.017.unr, %._crit_edge19.loopexit.unr-lcssa ]
  %.01016.epil = phi double [ %35, %.lr.ph18.epil ], [ %.01016.unr, %._crit_edge19.loopexit.unr-lcssa ]
  %epil.iter = phi i32 [ %epil.iter.next, %.lr.ph18.epil ], [ 0, %._crit_edge19.loopexit.unr-lcssa ]
  %33 = uitofp nneg i32 %.017.epil to double
  %34 = fdiv double 1.000000e+00, %33
  %35 = fadd double %.01016.epil, %34
  %36 = add nuw i32 %.017.epil, 1
  %epil.iter.next = add i32 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i32 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge19, label %.lr.ph18.epil, !llvm.loop !12

._crit_edge19:                                    ; preds = %._crit_edge19.loopexit.unr-lcssa, %.lr.ph18.epil, %.preheader, %15
  %.010.lcssa.sink = phi double [ %16, %15 ], [ 0.000000e+00, %.preheader ], [ %.lcssa28.ph, %._crit_edge19.loopexit.unr-lcssa ], [ %35, %.lr.ph18.epil ]
  %37 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, double noundef %.010.lcssa.sink)
  call void @llvm.stackrestore.p0(ptr %5)
  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
declare ptr @llvm.stacksave.p0() #3

; 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: mustprogress nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #3

attributes #0 = { nofree 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 = { mustprogress nocallback nofree nosync nounwind willreturn }
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/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.unroll.disable"}


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