; LLVM IR from tangjz/acm-icpc :: 51nod/1146.ll (Z4calci)

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

@fib = external dso_local global [30 x i32], align 16
@tlen = external dso_local global i32, align 4
@f = external dso_local global [100010 x i32], align 16
@str = external dso_local global [30 x [514239 x i8]], align 16
@tmp = external dso_local global [100010 x i8], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z4calci(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds [30 x i32], ptr @fib, i64 0, i64 %9
  store ptr %10, ptr %3, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %11 = load i32, ptr %2, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [30 x [514239 x i8]], ptr @str, i64 0, i64 %12
  %14 = getelementptr inbounds [514239 x i8], ptr %13, i64 0, i64 0
  store ptr %14, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 0, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 0, ptr %7, align 4, !tbaa !5
  br label %15

15:                                               ; preds = %73, %1
  %16 = load i32, ptr %6, align 4, !tbaa !5
  %17 = load ptr, ptr %3, align 8, !tbaa !9
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = icmp slt i32 %16, %18
  br i1 %19, label %21, label %20

20:                                               ; preds = %15
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %76

21:                                               ; preds = %15
  br label %22

22:                                               ; preds = %40, %21
  %23 = load i32, ptr %7, align 4, !tbaa !5
  %24 = icmp ne i32 %23, 0
  br i1 %24, label %25, label %38

25:                                               ; preds = %22
  %26 = load ptr, ptr %5, align 8, !tbaa !9
  %27 = load i32, ptr %6, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds i8, ptr %26, i64 %28
  %30 = load i8, ptr %29, align 1, !tbaa !11
  %31 = sext i8 %30 to i32
  %32 = load i32, ptr %7, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [100010 x i8], ptr @tmp, i64 0, i64 %33
  %35 = load i8, ptr %34, align 1, !tbaa !11
  %36 = sext i8 %35 to i32
  %37 = icmp ne i32 %31, %36
  br label %38

38:                                               ; preds = %25, %22
  %39 = phi i1 [ false, %22 ], [ %37, %25 ]
  br i1 %39, label %40, label %45

40:                                               ; preds = %38
  %41 = load i32, ptr %7, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [100010 x i32], ptr @f, i64 0, i64 %42
  %44 = load i32, ptr %43, align 4, !tbaa !5
  store i32 %44, ptr %7, align 4, !tbaa !5
  br label %22, !llvm.loop !12

45:                                               ; preds = %38
  %46 = load ptr, ptr %5, align 8, !tbaa !9
  %47 = load i32, ptr %6, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds i8, ptr %46, i64 %48
  %50 = load i8, ptr %49, align 1, !tbaa !11
  %51 = sext i8 %50 to i32
  %52 = load i32, ptr %7, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds [100010 x i8], ptr @tmp, i64 0, i64 %53
  %55 = load i8, ptr %54, align 1, !tbaa !11
  %56 = sext i8 %55 to i32
  %57 = icmp eq i32 %51, %56
  br i1 %57, label %58, label %61

58:                                               ; preds = %45
  %59 = load i32, ptr %7, align 4, !tbaa !5
  %60 = add nsw i32 %59, 1
  store i32 %60, ptr %7, align 4, !tbaa !5
  br label %61

61:                                               ; preds = %58, %45
  %62 = load i32, ptr %7, align 4, !tbaa !5
  %63 = load i32, ptr @tlen, align 4, !tbaa !5
  %64 = icmp eq i32 %62, %63
  br i1 %64, label %65, label %72

65:                                               ; preds = %61
  %66 = load i32, ptr %4, align 4, !tbaa !5
  %67 = add nsw i32 %66, 1
  store i32 %67, ptr %4, align 4, !tbaa !5
  %68 = load i32, ptr %7, align 4, !tbaa !5
  %69 = sext i32 %68 to i64
  %70 = getelementptr inbounds [100010 x i32], ptr @f, i64 0, i64 %69
  %71 = load i32, ptr %70, align 4, !tbaa !5
  store i32 %71, ptr %7, align 4, !tbaa !5
  br label %72

72:                                               ; preds = %65, %61
  br label %73

73:                                               ; preds = %72
  %74 = load i32, ptr %6, align 4, !tbaa !5
  %75 = add nsw i32 %74, 1
  store i32 %75, ptr %6, align 4, !tbaa !5
  br label %15, !llvm.loop !14

76:                                               ; preds = %20
  %77 = load i32, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  ret i32 %77
}

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

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

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


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

@fib = external dso_local local_unnamed_addr global [30 x i32], align 16
@tlen = external dso_local local_unnamed_addr global i32, align 4
@f = external dso_local local_unnamed_addr global [100010 x i32], align 16
@str = external dso_local local_unnamed_addr global [30 x [514239 x i8]], align 16
@tmp = external dso_local local_unnamed_addr global [100010 x i8], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z4calci(i32 noundef %0) local_unnamed_addr #0 {
  %2 = sext i32 %0 to i64
  %3 = getelementptr inbounds [30 x i32], ptr @fib, i64 0, i64 %2
  %4 = getelementptr inbounds [30 x [514239 x i8]], ptr @str, i64 0, i64 %2
  %5 = load i32, ptr %3, align 4, !tbaa !5
  %6 = icmp sgt i32 %5, 0
  br i1 %6, label %.preheader.lr.ph, label %._crit_edge

.preheader.lr.ph:                                 ; preds = %1
  %7 = load i32, ptr @tlen, align 4, !tbaa !5
  %wide.trip.count = zext nneg i32 %5 to i64
  %8 = sext i32 %7 to i64
  %9 = getelementptr inbounds [100010 x i32], ptr @f, i64 0, i64 %8
  br label %.preheader

.preheader:                                       ; preds = %.preheader.lr.ph, %25
  %indvars.iv = phi i64 [ 0, %.preheader.lr.ph ], [ %indvars.iv.next, %25 ]
  %.027 = phi i32 [ 0, %.preheader.lr.ph ], [ %.3, %25 ]
  %.01825 = phi i32 [ 0, %.preheader.lr.ph ], [ %.119, %25 ]
  %.not21 = icmp eq i32 %.027, 0
  %.phi.trans.insert = getelementptr inbounds i8, ptr %4, i64 %indvars.iv
  %.pre = load i8, ptr %.phi.trans.insert, align 1, !tbaa !9
  br i1 %.not21, label %.critedge, label %.lr.ph

._crit_edge:                                      ; preds = %25, %1
  %.018.lcssa = phi i32 [ 0, %1 ], [ %.119, %25 ]
  ret i32 %.018.lcssa

.lr.ph:                                           ; preds = %.preheader, %13
  %.122 = phi i32 [ %15, %13 ], [ %.027, %.preheader ]
  %10 = sext i32 %.122 to i64
  %11 = getelementptr inbounds [100010 x i8], ptr @tmp, i64 0, i64 %10
  %12 = load i8, ptr %11, align 1, !tbaa !9
  %.not20 = icmp eq i8 %.pre, %12
  br i1 %.not20, label %.critedge, label %13

13:                                               ; preds = %.lr.ph
  %14 = getelementptr inbounds [100010 x i32], ptr @f, i64 0, i64 %10
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %.not = icmp eq i32 %15, 0
  br i1 %.not, label %.critedge, label %.lr.ph, !llvm.loop !10

.critedge:                                        ; preds = %.lr.ph, %13, %.preheader
  %.1.lcssa = phi i32 [ 0, %.preheader ], [ 0, %13 ], [ %.122, %.lr.ph ]
  %16 = sext i32 %.1.lcssa to i64
  %17 = getelementptr inbounds [100010 x i8], ptr @tmp, i64 0, i64 %16
  %18 = load i8, ptr %17, align 1, !tbaa !9
  %19 = icmp eq i8 %.pre, %18
  %20 = zext i1 %19 to i32
  %spec.select = add nsw i32 %.1.lcssa, %20
  %21 = icmp eq i32 %spec.select, %7
  br i1 %21, label %22, label %25

22:                                               ; preds = %.critedge
  %23 = add nsw i32 %.01825, 1
  %24 = load i32, ptr %9, align 4, !tbaa !5
  br label %25

25:                                               ; preds = %.critedge, %22
  %.119 = phi i32 [ %23, %22 ], [ %.01825, %.critedge ]
  %.3 = phi i32 [ %24, %22 ], [ %spec.select, %.critedge ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.preheader, !llvm.loop !12
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) 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 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !11}


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