; LLVM IR from tangjz/acm-icpc :: self_training/2015-04-25/D.ll (Z4findi)

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

@fa = external dso_local global [2000010 x i32], align 16
@top = external dso_local global i32, align 4
@stack = external dso_local global [2000010 x i32], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z4findi(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  %6 = load i32, ptr %3, align 4, !tbaa !5
  %7 = load i32, ptr %3, align 4, !tbaa !5
  %8 = sext i32 %7 to i64
  %9 = getelementptr inbounds [2000010 x i32], ptr @fa, i64 0, i64 %8
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %11 = icmp eq i32 %6, %10
  br i1 %11, label %12, label %14

12:                                               ; preds = %1
  %13 = load i32, ptr %3, align 4, !tbaa !5
  store i32 %13, ptr %2, align 4
  br label %60

14:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  %15 = load i32, ptr %3, align 4, !tbaa !5
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds [2000010 x i32], ptr @fa, i64 0, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !5
  store i32 %18, ptr %4, align 4, !tbaa !5
  store i32 0, ptr @top, align 4, !tbaa !5
  %19 = load i32, ptr %3, align 4, !tbaa !5
  %20 = load i32, ptr @top, align 4, !tbaa !5
  %21 = add nsw i32 %20, 1
  store i32 %21, ptr @top, align 4, !tbaa !5
  %22 = sext i32 %20 to i64
  %23 = getelementptr inbounds [2000010 x i32], ptr @stack, i64 0, i64 %22
  store i32 %19, ptr %23, align 4, !tbaa !5
  br label %24

24:                                               ; preds = %31, %14
  %25 = load i32, ptr %4, align 4, !tbaa !5
  %26 = load i32, ptr %4, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [2000010 x i32], ptr @fa, i64 0, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = icmp ne i32 %25, %29
  br i1 %30, label %31, label %41

31:                                               ; preds = %24
  %32 = load i32, ptr %4, align 4, !tbaa !5
  %33 = load i32, ptr @top, align 4, !tbaa !5
  %34 = add nsw i32 %33, 1
  store i32 %34, ptr @top, align 4, !tbaa !5
  %35 = sext i32 %33 to i64
  %36 = getelementptr inbounds [2000010 x i32], ptr @stack, i64 0, i64 %35
  store i32 %32, ptr %36, align 4, !tbaa !5
  %37 = load i32, ptr %4, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [2000010 x i32], ptr @fa, i64 0, i64 %38
  %40 = load i32, ptr %39, align 4, !tbaa !5
  store i32 %40, ptr %4, align 4, !tbaa !5
  br label %24, !llvm.loop !9

41:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %42

42:                                               ; preds = %55, %41
  %43 = load i32, ptr %5, align 4, !tbaa !5
  %44 = load i32, ptr @top, align 4, !tbaa !5
  %45 = icmp slt i32 %43, %44
  br i1 %45, label %47, label %46

46:                                               ; preds = %42
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %58

47:                                               ; preds = %42
  %48 = load i32, ptr %4, align 4, !tbaa !5
  %49 = load i32, ptr %5, align 4, !tbaa !5
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds [2000010 x i32], ptr @stack, i64 0, i64 %50
  %52 = load i32, ptr %51, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds [2000010 x i32], ptr @fa, i64 0, i64 %53
  store i32 %48, ptr %54, align 4, !tbaa !5
  br label %55

55:                                               ; preds = %47
  %56 = load i32, ptr %5, align 4, !tbaa !5
  %57 = add nsw i32 %56, 1
  store i32 %57, ptr %5, align 4, !tbaa !5
  br label %42, !llvm.loop !11

58:                                               ; preds = %46
  %59 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %59, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %60

60:                                               ; preds = %58, %12
  %61 = load i32, ptr %2, align 4
  ret i32 %61
}

; 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !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"

@fa = external dso_local local_unnamed_addr global [2000010 x i32], align 16
@top = external dso_local local_unnamed_addr global i32, align 4
@stack = external dso_local local_unnamed_addr global [2000010 x i32], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z4findi(i32 noundef %0) local_unnamed_addr #0 {
  %2 = sext i32 %0 to i64
  %3 = getelementptr inbounds [2000010 x i32], ptr @fa, i64 0, i64 %2
  %4 = load i32, ptr %3, align 4, !tbaa !5
  %5 = icmp eq i32 %4, %0
  br i1 %5, label %.loopexit, label %6

6:                                                ; preds = %1
  store i32 1, ptr @top, align 4, !tbaa !5
  store i32 %0, ptr @stack, align 16, !tbaa !5
  %7 = sext i32 %4 to i64
  %8 = getelementptr inbounds [2000010 x i32], ptr @fa, i64 0, i64 %7
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %.not19 = icmp eq i32 %4, %9
  br i1 %.not19, label %.lr.ph22.preheader, label %.lr.ph

.preheader:                                       ; preds = %.lr.ph
  %10 = trunc nuw i64 %indvars.iv.next to i32
  store i32 %10, ptr @top, align 4, !tbaa !5
  %11 = icmp sgt i32 %10, 0
  br i1 %11, label %.lr.ph22.preheader, label %.loopexit

.lr.ph22.preheader:                               ; preds = %6, %.preheader
  %.015.lcssa29 = phi i32 [ %15, %.preheader ], [ %4, %6 ]
  %12 = phi i64 [ %indvars.iv.next, %.preheader ], [ 1, %6 ]
  %wide.trip.count = and i64 %12, 4294967295
  %13 = add nsw i64 %wide.trip.count, -1
  %xtraiter = and i64 %12, 3
  %14 = icmp ult i64 %13, 3
  br i1 %14, label %.loopexit.loopexit.unr-lcssa, label %.lr.ph22.preheader.new

.lr.ph22.preheader.new:                           ; preds = %.lr.ph22.preheader
  %unroll_iter = sub nsw i64 %wide.trip.count, %xtraiter
  br label %.lr.ph22

.lr.ph:                                           ; preds = %6, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 1, %6 ]
  %15 = phi i32 [ %19, %.lr.ph ], [ %9, %6 ]
  %.01520 = phi i32 [ %15, %.lr.ph ], [ %4, %6 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %16 = getelementptr inbounds [2000010 x i32], ptr @stack, i64 0, i64 %indvars.iv
  store i32 %.01520, ptr %16, align 4, !tbaa !5
  %17 = sext i32 %15 to i64
  %18 = getelementptr inbounds [2000010 x i32], ptr @fa, i64 0, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %.not = icmp eq i32 %15, %19
  br i1 %.not, label %.preheader, label %.lr.ph, !llvm.loop !9

.lr.ph22:                                         ; preds = %.lr.ph22, %.lr.ph22.preheader.new
  %indvars.iv25 = phi i64 [ 0, %.lr.ph22.preheader.new ], [ %indvars.iv.next26.3, %.lr.ph22 ]
  %niter = phi i64 [ 0, %.lr.ph22.preheader.new ], [ %niter.next.3, %.lr.ph22 ]
  %20 = getelementptr inbounds [2000010 x i32], ptr @stack, i64 0, i64 %indvars.iv25
  %21 = load i32, ptr %20, align 16, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [2000010 x i32], ptr @fa, i64 0, i64 %22
  store i32 %.015.lcssa29, ptr %23, align 4, !tbaa !5
  %indvars.iv.next26 = or disjoint i64 %indvars.iv25, 1
  %24 = getelementptr inbounds [2000010 x i32], ptr @stack, i64 0, i64 %indvars.iv.next26
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [2000010 x i32], ptr @fa, i64 0, i64 %26
  store i32 %.015.lcssa29, ptr %27, align 4, !tbaa !5
  %indvars.iv.next26.1 = or disjoint i64 %indvars.iv25, 2
  %28 = getelementptr inbounds [2000010 x i32], ptr @stack, i64 0, i64 %indvars.iv.next26.1
  %29 = load i32, ptr %28, align 8, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [2000010 x i32], ptr @fa, i64 0, i64 %30
  store i32 %.015.lcssa29, ptr %31, align 4, !tbaa !5
  %indvars.iv.next26.2 = or disjoint i64 %indvars.iv25, 3
  %32 = getelementptr inbounds [2000010 x i32], ptr @stack, i64 0, i64 %indvars.iv.next26.2
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [2000010 x i32], ptr @fa, i64 0, i64 %34
  store i32 %.015.lcssa29, ptr %35, align 4, !tbaa !5
  %indvars.iv.next26.3 = add nuw nsw i64 %indvars.iv25, 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 %.loopexit.loopexit.unr-lcssa, label %.lr.ph22, !llvm.loop !11

.loopexit.loopexit.unr-lcssa:                     ; preds = %.lr.ph22, %.lr.ph22.preheader
  %indvars.iv25.unr = phi i64 [ 0, %.lr.ph22.preheader ], [ %indvars.iv.next26.3, %.lr.ph22 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.loopexit, label %.lr.ph22.epil

.lr.ph22.epil:                                    ; preds = %.loopexit.loopexit.unr-lcssa, %.lr.ph22.epil
  %indvars.iv25.epil = phi i64 [ %indvars.iv.next26.epil, %.lr.ph22.epil ], [ %indvars.iv25.unr, %.loopexit.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph22.epil ], [ 0, %.loopexit.loopexit.unr-lcssa ]
  %36 = getelementptr inbounds [2000010 x i32], ptr @stack, i64 0, i64 %indvars.iv25.epil
  %37 = load i32, ptr %36, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [2000010 x i32], ptr @fa, i64 0, i64 %38
  store i32 %.015.lcssa29, ptr %39, align 4, !tbaa !5
  %indvars.iv.next26.epil = add nuw nsw i64 %indvars.iv25.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 %.loopexit, label %.lr.ph22.epil, !llvm.loop !12

.loopexit:                                        ; preds = %.loopexit.loopexit.unr-lcssa, %.lr.ph22.epil, %.preheader, %1
  %.016 = phi i32 [ %0, %1 ], [ %15, %.preheader ], [ %.015.lcssa29, %.lr.ph22.epil ], [ %.015.lcssa29, %.loopexit.loopexit.unr-lcssa ]
  ret i32 %.016
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.unroll.disable"}


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