; LLVM IR from VastoLorde95/Competitive-Programming :: SPOJ/Week 1/ciphers (factorial)

; 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 [5 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 i64, align 8
  %3 = alloca ptr, 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
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #4
  %9 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %2)
  %10 = load i64, ptr %2, align 8, !tbaa !5
  %11 = call ptr @llvm.stacksave.p0()
  store ptr %11, ptr %3, align 8
  %12 = alloca i64, i64 %10, align 16
  store i64 %10, ptr %4, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  store i64 0, ptr %5, align 8, !tbaa !5
  br label %13

13:                                               ; preds = %17, %0
  %14 = load i64, ptr %5, align 8, !tbaa !5
  %15 = load i64, ptr %2, align 8, !tbaa !5
  %16 = icmp ult i64 %14, %15
  br i1 %16, label %17, label %23

17:                                               ; preds = %13
  %18 = load i64, ptr %5, align 8, !tbaa !5
  %19 = getelementptr inbounds i64, ptr %12, i64 %18
  %20 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %19)
  %21 = load i64, ptr %5, align 8, !tbaa !5
  %22 = add i64 %21, 1
  store i64 %22, ptr %5, align 8, !tbaa !5
  br label %13, !llvm.loop !9

23:                                               ; preds = %13
  store i64 0, ptr %5, align 8, !tbaa !5
  br label %24

24:                                               ; preds = %44, %23
  %25 = load i64, ptr %5, align 8, !tbaa !5
  %26 = load i64, ptr %2, align 8, !tbaa !5
  %27 = icmp ult i64 %25, %26
  br i1 %27, label %28, label %49

28:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %29 = load i64, ptr %5, align 8, !tbaa !5
  %30 = getelementptr inbounds i64, ptr %12, i64 %29
  %31 = load i64, ptr %30, align 8, !tbaa !5
  store i64 %31, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  store i64 5, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  store i64 0, ptr %8, align 8, !tbaa !5
  br label %32

32:                                               ; preds = %36, %28
  %33 = load i64, ptr %7, align 8, !tbaa !5
  %34 = load i64, ptr %6, align 8, !tbaa !5
  %35 = icmp ult i64 %33, %34
  br i1 %35, label %36, label %44

36:                                               ; preds = %32
  %37 = load i64, ptr %6, align 8, !tbaa !5
  %38 = load i64, ptr %7, align 8, !tbaa !5
  %39 = udiv i64 %37, %38
  %40 = load i64, ptr %8, align 8, !tbaa !5
  %41 = add i64 %40, %39
  store i64 %41, ptr %8, align 8, !tbaa !5
  %42 = load i64, ptr %7, align 8, !tbaa !5
  %43 = mul i64 %42, 5
  store i64 %43, ptr %7, align 8, !tbaa !5
  br label %32, !llvm.loop !11

44:                                               ; preds = %32
  %45 = load i64, ptr %8, align 8, !tbaa !5
  %46 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i64 noundef %45)
  %47 = load i64, ptr %5, align 8, !tbaa !5
  %48 = add i64 %47, 1
  store i64 %48, ptr %5, align 8, !tbaa !5
  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
  br label %24, !llvm.loop !12

49:                                               ; preds = %24
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  %50 = load ptr, ptr %3, align 8
  call void @llvm.stackrestore.p0(ptr %50)
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #4
  %51 = load i32, ptr %1, align 4
  ret i32 %51
}

; 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

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

; 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 = !{!"long long", !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, !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 [5 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 i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %1) #4
  %2 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %1)
  %3 = load i64, ptr %1, align 8, !tbaa !5
  %4 = call ptr @llvm.stacksave.p0()
  %5 = alloca i64, i64 %3, align 16
  %6 = load i64, ptr %1, align 8, !tbaa !5
  %.not = icmp eq i64 %6, 0
  br i1 %.not, label %._crit_edge23, label %.lr.ph

.preheader:                                       ; preds = %.lr.ph
  %7 = icmp eq i64 %11, 0
  br i1 %7, label %._crit_edge23, label %.lr.ph22

.lr.ph:                                           ; preds = %0, %.lr.ph
  %.01517 = phi i64 [ %10, %.lr.ph ], [ 0, %0 ]
  %8 = getelementptr inbounds i64, ptr %5, i64 %.01517
  %9 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %8)
  %10 = add nuw i64 %.01517, 1
  %11 = load i64, ptr %1, align 8, !tbaa !5
  %12 = icmp ult i64 %10, %11
  br i1 %12, label %.lr.ph, label %.preheader, !llvm.loop !9

.lr.ph22:                                         ; preds = %.preheader, %._crit_edge
  %.121 = phi i64 [ %21, %._crit_edge ], [ 0, %.preheader ]
  %13 = getelementptr inbounds i64, ptr %5, i64 %.121
  %14 = load i64, ptr %13, align 8, !tbaa !5
  %15 = icmp ugt i64 %14, 5
  br i1 %15, label %.lr.ph20, label %._crit_edge

.lr.ph20:                                         ; preds = %.lr.ph22, %.lr.ph20
  %.019 = phi i64 [ %17, %.lr.ph20 ], [ 0, %.lr.ph22 ]
  %.01418 = phi i64 [ %18, %.lr.ph20 ], [ 5, %.lr.ph22 ]
  %16 = udiv i64 %14, %.01418
  %17 = add i64 %16, %.019
  %18 = mul i64 %.01418, 5
  %19 = icmp ult i64 %18, %14
  br i1 %19, label %.lr.ph20, label %._crit_edge, !llvm.loop !11

._crit_edge:                                      ; preds = %.lr.ph20, %.lr.ph22
  %.0.lcssa = phi i64 [ 0, %.lr.ph22 ], [ %17, %.lr.ph20 ]
  %20 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i64 noundef %.0.lcssa)
  %21 = add nuw i64 %.121, 1
  %22 = load i64, ptr %1, align 8, !tbaa !5
  %23 = icmp ult i64 %21, %22
  br i1 %23, label %.lr.ph22, label %._crit_edge23, !llvm.loop !12

._crit_edge23:                                    ; preds = %._crit_edge, %0, %.preheader
  call void @llvm.stackrestore.p0(ptr %4)
  call void @llvm.lifetime.end.p0(i64 8, 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: nofree nounwind
declare noundef i32 @printf(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: 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 = !{!"long long", !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, !10}


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