; LLVM IR from tangjz/acm-icpc :: codeforces/gym/100540D.ll (Z6insertv)

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

@tot = external dso_local global i32, align 4
@nxt = external dso_local global [100001 x [26 x i32]], align 16
@str = external dso_local global [1001 x i8], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z6insertv() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  store i32 0, ptr %1, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %3

3:                                                ; preds = %55, %0
  %4 = load i32, ptr %2, align 4, !tbaa !5
  %5 = sext i32 %4 to i64
  %6 = getelementptr inbounds [1001 x i8], ptr @str, i64 0, i64 %5
  %7 = load i8, ptr %6, align 1, !tbaa !9
  %8 = icmp ne i8 %7, 0
  br i1 %8, label %10, label %9

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

10:                                               ; preds = %3
  %11 = load i32, ptr %2, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [1001 x i8], ptr @str, i64 0, i64 %12
  %14 = load i8, ptr %13, align 1, !tbaa !9
  %15 = sext i8 %14 to i32
  %16 = sub nsw i32 %15, 97
  %17 = trunc i32 %16 to i8
  store i8 %17, ptr %13, align 1, !tbaa !9
  %18 = load i32, ptr %1, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = getelementptr inbounds [100001 x [26 x i32]], ptr @nxt, i64 0, i64 %19
  %21 = load i32, ptr %2, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [1001 x i8], ptr @str, i64 0, i64 %22
  %24 = load i8, ptr %23, align 1, !tbaa !9
  %25 = sext i8 %24 to i64
  %26 = getelementptr inbounds [26 x i32], ptr %20, i64 0, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = icmp ne i32 %27, 0
  br i1 %28, label %44, label %29

29:                                               ; preds = %10
  %30 = load i32, ptr @tot, align 4, !tbaa !5
  %31 = load i32, ptr %1, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [100001 x [26 x i32]], ptr @nxt, i64 0, i64 %32
  %34 = load i32, ptr %2, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [1001 x i8], ptr @str, i64 0, i64 %35
  %37 = load i8, ptr %36, align 1, !tbaa !9
  %38 = sext i8 %37 to i64
  %39 = getelementptr inbounds [26 x i32], ptr %33, i64 0, i64 %38
  store i32 %30, ptr %39, align 4, !tbaa !5
  %40 = load i32, ptr @tot, align 4, !tbaa !5
  %41 = add nsw i32 %40, 1
  store i32 %41, ptr @tot, align 4, !tbaa !5
  %42 = sext i32 %40 to i64
  %43 = getelementptr inbounds [26 x i32], ptr @nxt, i64 %42
  call void @llvm.memset.p0.i64(ptr align 4 %43, i8 0, i64 104, i1 false)
  br label %44

44:                                               ; preds = %29, %10
  %45 = load i32, ptr %1, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [100001 x [26 x i32]], ptr @nxt, i64 0, i64 %46
  %48 = load i32, ptr %2, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [1001 x i8], ptr @str, i64 0, i64 %49
  %51 = load i8, ptr %50, align 1, !tbaa !9
  %52 = sext i8 %51 to i64
  %53 = getelementptr inbounds [26 x i32], ptr %47, i64 0, i64 %52
  %54 = load i32, ptr %53, align 4, !tbaa !5
  store i32 %54, ptr %1, align 4, !tbaa !5
  br label %55

55:                                               ; preds = %44
  %56 = load i32, ptr %2, align 4, !tbaa !5
  %57 = add nsw i32 %56, 1
  store i32 %57, ptr %2, align 4, !tbaa !5
  br label %3, !llvm.loop !10

58:                                               ; preds = %9
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  ret void
}

; 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 nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

; 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { 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 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


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

@tot = external dso_local local_unnamed_addr global i32, align 4
@nxt = external dso_local local_unnamed_addr global [100001 x [26 x i32]], align 16
@str = external dso_local local_unnamed_addr global [1001 x i8], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: write, inaccessiblemem: none) uwtable
define dso_local void @_Z6insertv() local_unnamed_addr #0 {
  %1 = load i8, ptr @str, align 16, !tbaa !5
  %.not12 = icmp eq i8 %1, 0
  br i1 %.not12, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %0
  %tot.promoted = load i32, ptr @tot, align 4, !tbaa !8
  br label %.lr.ph

._crit_edge:                                      ; preds = %14, %0
  ret void

.lr.ph:                                           ; preds = %.lr.ph.preheader, %14
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %14 ]
  %2 = phi i8 [ %1, %.lr.ph.preheader ], [ %18, %14 ]
  %3 = phi ptr [ @str, %.lr.ph.preheader ], [ %17, %14 ]
  %.0913 = phi i32 [ 0, %.lr.ph.preheader ], [ %15, %14 ]
  %4 = phi i32 [ %tot.promoted, %.lr.ph.preheader ], [ %16, %14 ]
  %5 = add i8 %2, -97
  store i8 %5, ptr %3, align 1, !tbaa !5
  %6 = sext i32 %.0913 to i64
  %7 = sext i8 %5 to i64
  %8 = getelementptr inbounds [100001 x [26 x i32]], ptr @nxt, i64 0, i64 %6, i64 %7
  %9 = load i32, ptr %8, align 4, !tbaa !8
  %.not11 = icmp eq i32 %9, 0
  br i1 %.not11, label %10, label %14

10:                                               ; preds = %.lr.ph
  store i32 %4, ptr %8, align 4, !tbaa !8
  %11 = add nsw i32 %4, 1
  store i32 %11, ptr @tot, align 4, !tbaa !8
  %12 = sext i32 %4 to i64
  %13 = getelementptr inbounds [26 x i32], ptr @nxt, i64 %12
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(104) %13, i8 0, i64 104, i1 false)
  %.pre = load i32, ptr %8, align 4, !tbaa !8
  br label %14

14:                                               ; preds = %10, %.lr.ph
  %15 = phi i32 [ %.pre, %10 ], [ %9, %.lr.ph ]
  %16 = phi i32 [ %11, %10 ], [ %4, %.lr.ph ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %17 = getelementptr inbounds [1001 x i8], ptr @str, i64 0, i64 %indvars.iv.next
  %18 = load i8, ptr %17, align 1, !tbaa !5
  %.not = icmp eq i8 %18, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !10
}

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

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: write, 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" }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"int", !6, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


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