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

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

@n = external dso_local global i32, align 4
@seq = external dso_local global [100001 x i32], align 16

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z8dsu_findi(i32 noundef) #0

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5buildiPi(i32 noundef %0, ptr noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !9
  %11 = load ptr, ptr %4, align 8, !tbaa !9
  call void @llvm.memset.p0.i64(ptr align 4 %11, i8 63, i64 400004, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 0, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  store i32 -1, ptr %6, align 4, !tbaa !5
  br label %12

12:                                               ; preds = %41, %2
  %13 = load i32, ptr %5, align 4, !tbaa !5
  %14 = load i32, ptr @n, align 4, !tbaa !5
  %15 = icmp slt i32 %13, %14
  br i1 %15, label %17, label %16

16:                                               ; preds = %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br label %44

17:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  %18 = load i32, ptr %5, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [100001 x i32], ptr @seq, i64 0, i64 %19
  %21 = load i32, ptr %20, align 4, !tbaa !5
  %22 = call noundef i32 @_Z8dsu_findi(i32 noundef %21)
  store i32 %22, ptr %7, align 4, !tbaa !5
  %23 = load i32, ptr %3, align 4, !tbaa !5
  %24 = load i32, ptr %7, align 4, !tbaa !5
  %25 = icmp eq i32 %23, %24
  br i1 %25, label %26, label %28

26:                                               ; preds = %17
  %27 = load i32, ptr %5, align 4, !tbaa !5
  store i32 %27, ptr %6, align 4, !tbaa !5
  br label %40

28:                                               ; preds = %17
  %29 = load i32, ptr %6, align 4, !tbaa !5
  %30 = icmp ne i32 %29, -1
  br i1 %30, label %31, label %39

31:                                               ; preds = %28
  %32 = load ptr, ptr %4, align 8, !tbaa !9
  %33 = load i32, ptr %7, align 4, !tbaa !5
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds i32, ptr %32, i64 %34
  %36 = load i32, ptr %5, align 4, !tbaa !5
  %37 = load i32, ptr %6, align 4, !tbaa !5
  %38 = sub nsw i32 %36, %37
  call void @_Z3updRii(ptr noundef nonnull align 4 dereferenceable(4) %35, i32 noundef %38)
  br label %39

39:                                               ; preds = %31, %28
  br label %40

40:                                               ; preds = %39, %26
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  br label %41

41:                                               ; preds = %40
  %42 = load i32, ptr %5, align 4, !tbaa !5
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %5, align 4, !tbaa !5
  br label %12, !llvm.loop !11

44:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  %45 = load i32, ptr @n, align 4, !tbaa !5
  %46 = sub nsw i32 %45, 1
  store i32 %46, ptr %8, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  store i32 -1, ptr %9, align 4, !tbaa !5
  br label %47

47:                                               ; preds = %75, %44
  %48 = load i32, ptr %8, align 4, !tbaa !5
  %49 = icmp sge i32 %48, 0
  br i1 %49, label %51, label %50

50:                                               ; preds = %47
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  br label %78

51:                                               ; preds = %47
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  %52 = load i32, ptr %8, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds [100001 x i32], ptr @seq, i64 0, i64 %53
  %55 = load i32, ptr %54, align 4, !tbaa !5
  %56 = call noundef i32 @_Z8dsu_findi(i32 noundef %55)
  store i32 %56, ptr %10, align 4, !tbaa !5
  %57 = load i32, ptr %3, align 4, !tbaa !5
  %58 = load i32, ptr %10, align 4, !tbaa !5
  %59 = icmp eq i32 %57, %58
  br i1 %59, label %60, label %62

60:                                               ; preds = %51
  %61 = load i32, ptr %8, align 4, !tbaa !5
  store i32 %61, ptr %9, align 4, !tbaa !5
  br label %74

62:                                               ; preds = %51
  %63 = load i32, ptr %9, align 4, !tbaa !5
  %64 = icmp ne i32 %63, -1
  br i1 %64, label %65, label %73

65:                                               ; preds = %62
  %66 = load ptr, ptr %4, align 8, !tbaa !9
  %67 = load i32, ptr %10, align 4, !tbaa !5
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds i32, ptr %66, i64 %68
  %70 = load i32, ptr %9, align 4, !tbaa !5
  %71 = load i32, ptr %8, align 4, !tbaa !5
  %72 = sub nsw i32 %70, %71
  call void @_Z3updRii(ptr noundef nonnull align 4 dereferenceable(4) %69, i32 noundef %72)
  br label %73

73:                                               ; preds = %65, %62
  br label %74

74:                                               ; preds = %73, %60
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  br label %75

75:                                               ; preds = %74
  %76 = load i32, ptr %8, align 4, !tbaa !5
  %77 = add nsw i32 %76, -1
  store i32 %77, ptr %8, align 4, !tbaa !5
  br label %47, !llvm.loop !13

78:                                               ; preds = %50
  ret void
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local void @_Z3updRii(ptr noundef nonnull align 4 dereferenceable(4), i32 noundef) #3

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

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


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

@n = external dso_local local_unnamed_addr global i32, align 4
@seq = external dso_local local_unnamed_addr global [100001 x i32], align 16

; Function Attrs: mustprogress uwtable
declare dso_local noundef i32 @_Z8dsu_findi(i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5buildiPi(i32 noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 dereferenceable(400004) %1, i8 63, i64 400004, i1 false)
  %3 = load i32, ptr @n, align 4, !tbaa !5
  %4 = icmp sgt i32 %3, 0
  br i1 %4, label %.lr.ph, label %._crit_edge

.preheader:                                       ; preds = %17
  %5 = icmp sgt i32 %18, 0
  br i1 %5, label %.lr.ph35.preheader, label %._crit_edge

.lr.ph35.preheader:                               ; preds = %.preheader
  %6 = zext nneg i32 %18 to i64
  br label %.lr.ph35

.lr.ph:                                           ; preds = %2, %17
  %indvars.iv = phi i64 [ %indvars.iv.next, %17 ], [ 0, %2 ]
  %.02430 = phi i32 [ %.125, %17 ], [ -1, %2 ]
  %7 = getelementptr inbounds [100001 x i32], ptr @seq, i64 0, i64 %indvars.iv
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %9 = tail call noundef i32 @_Z8dsu_findi(i32 noundef %8)
  %10 = icmp eq i32 %9, %0
  %11 = trunc nuw nsw i64 %indvars.iv to i32
  br i1 %10, label %17, label %12

12:                                               ; preds = %.lr.ph
  %.not29 = icmp eq i32 %.02430, -1
  br i1 %.not29, label %17, label %13

13:                                               ; preds = %12
  %14 = sext i32 %9 to i64
  %15 = getelementptr inbounds i32, ptr %1, i64 %14
  %16 = sub nsw i32 %11, %.02430
  tail call void @_Z3updRii(ptr noundef nonnull align 4 dereferenceable(4) %15, i32 noundef %16)
  br label %17

17:                                               ; preds = %.lr.ph, %12, %13
  %.125 = phi i32 [ %.02430, %13 ], [ -1, %12 ], [ %11, %.lr.ph ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %18 = load i32, ptr @n, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = icmp slt i64 %indvars.iv.next, %19
  br i1 %20, label %.lr.ph, label %.preheader, !llvm.loop !9

._crit_edge:                                      ; preds = %31, %2, %.preheader
  ret void

.lr.ph35:                                         ; preds = %.lr.ph35.preheader, %31
  %indvars.iv38 = phi i64 [ %6, %.lr.ph35.preheader ], [ %indvars.iv.next39, %31 ]
  %.02233 = phi i32 [ -1, %.lr.ph35.preheader ], [ %.1, %31 ]
  %indvars.iv.next39 = add nsw i64 %indvars.iv38, -1
  %21 = getelementptr inbounds [100001 x i32], ptr @seq, i64 0, i64 %indvars.iv.next39
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %23 = tail call noundef i32 @_Z8dsu_findi(i32 noundef %22)
  %24 = icmp eq i32 %23, %0
  %25 = trunc nuw nsw i64 %indvars.iv.next39 to i32
  br i1 %24, label %31, label %26

26:                                               ; preds = %.lr.ph35
  %.not = icmp eq i32 %.02233, -1
  br i1 %.not, label %31, label %27

27:                                               ; preds = %26
  %28 = sext i32 %23 to i64
  %29 = getelementptr inbounds i32, ptr %1, i64 %28
  %30 = sub nsw i32 %.02233, %25
  tail call void @_Z3updRii(ptr noundef nonnull align 4 dereferenceable(4) %29, i32 noundef %30)
  br label %31

31:                                               ; preds = %.lr.ph35, %26, %27
  %.1 = phi i32 [ %.02233, %27 ], [ -1, %26 ], [ %25, %.lr.ph35 ]
  %32 = icmp ugt i64 %indvars.iv38, 1
  br i1 %32, label %.lr.ph35, label %._crit_edge, !llvm.loop !11
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local void @_Z3updRii(ptr noundef nonnull align 4 dereferenceable(4), i32 noundef) local_unnamed_addr #2

attributes #0 = { mustprogress 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 nounwind willreturn memory(argmem: write) }
attributes #2 = { inlinehint 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" }

!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}


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