; LLVM IR from tangjz/acm-icpc :: lydsy/2905.ll (Z6inserti)

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

%struct.Trie = type { i32, [26 x i32], i32 }

@pos = external dso_local global [300001 x i32], align 16
@w = external dso_local global [300001 x i32], align 16
@tot = external dso_local global i32, align 4
@e = external dso_local global [300001 x %struct.Trie], align 16
@str = external dso_local global [300001 x i8], align 16
@.str = external hidden unnamed_addr constant [5 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local void @_Z6inserti(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  store i32 0, ptr %3, align 4, !tbaa !5
  %5 = load i32, ptr %2, align 4, !tbaa !5
  %6 = sext i32 %5 to i64
  %7 = getelementptr inbounds i32, ptr @w, i64 %6
  %8 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @str, ptr noundef %7)
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %9

9:                                                ; preds = %69, %1
  %10 = load i32, ptr %4, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [300001 x i8], ptr @str, i64 0, i64 %11
  %13 = load i8, ptr %12, align 1, !tbaa !9
  %14 = icmp ne i8 %13, 0
  br i1 %14, label %16, label %15

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

16:                                               ; preds = %9
  %17 = load i32, ptr %4, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [300001 x i8], ptr @str, i64 0, i64 %18
  %20 = load i8, ptr %19, align 1, !tbaa !9
  %21 = sext i8 %20 to i32
  %22 = sub nsw i32 %21, 97
  %23 = trunc i32 %22 to i8
  store i8 %23, ptr %19, align 1, !tbaa !9
  %24 = load i32, ptr %3, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [300001 x %struct.Trie], ptr @e, i64 0, i64 %25
  %27 = getelementptr inbounds %struct.Trie, ptr %26, i32 0, i32 1
  %28 = load i32, ptr %4, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [300001 x i8], ptr @str, i64 0, i64 %29
  %31 = load i8, ptr %30, align 1, !tbaa !9
  %32 = sext i8 %31 to i64
  %33 = getelementptr inbounds [26 x i32], ptr %27, i64 0, i64 %32
  %34 = load i32, ptr %33, align 4, !tbaa !5
  %35 = icmp ne i32 %34, 0
  br i1 %35, label %57, label %36

36:                                               ; preds = %16
  %37 = load i32, ptr @tot, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds %struct.Trie, ptr @e, i64 %38
  call void @llvm.memset.p0.i64(ptr align 4 %39, i8 0, i64 112, i1 false)
  %40 = load i32, ptr @tot, align 4, !tbaa !5
  %41 = load i32, ptr %3, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [300001 x %struct.Trie], ptr @e, i64 0, i64 %42
  %44 = getelementptr inbounds %struct.Trie, ptr %43, i32 0, i32 1
  %45 = load i32, ptr %4, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [300001 x i8], ptr @str, i64 0, i64 %46
  %48 = load i8, ptr %47, align 1, !tbaa !9
  %49 = sext i8 %48 to i64
  %50 = getelementptr inbounds [26 x i32], ptr %44, i64 0, i64 %49
  store i32 %40, ptr %50, align 4, !tbaa !5
  %51 = load i32, ptr %3, align 4, !tbaa !5
  %52 = load i32, ptr @tot, align 4, !tbaa !5
  %53 = add nsw i32 %52, 1
  store i32 %53, ptr @tot, align 4, !tbaa !5
  %54 = sext i32 %52 to i64
  %55 = getelementptr inbounds [300001 x %struct.Trie], ptr @e, i64 0, i64 %54
  %56 = getelementptr inbounds %struct.Trie, ptr %55, i32 0, i32 0
  store i32 %51, ptr %56, align 16, !tbaa !10
  br label %57

57:                                               ; preds = %36, %16
  %58 = load i32, ptr %3, align 4, !tbaa !5
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [300001 x %struct.Trie], ptr @e, i64 0, i64 %59
  %61 = getelementptr inbounds %struct.Trie, ptr %60, i32 0, i32 1
  %62 = load i32, ptr %4, align 4, !tbaa !5
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [300001 x i8], ptr @str, i64 0, i64 %63
  %65 = load i8, ptr %64, align 1, !tbaa !9
  %66 = sext i8 %65 to i64
  %67 = getelementptr inbounds [26 x i32], ptr %61, i64 0, i64 %66
  %68 = load i32, ptr %67, align 4, !tbaa !5
  store i32 %68, ptr %3, align 4, !tbaa !5
  br label %69

69:                                               ; preds = %57
  %70 = load i32, ptr %4, align 4, !tbaa !5
  %71 = add nsw i32 %70, 1
  store i32 %71, ptr %4, align 4, !tbaa !5
  br label %9, !llvm.loop !12

72:                                               ; preds = %15
  %73 = load i32, ptr %3, align 4, !tbaa !5
  %74 = load i32, ptr %2, align 4, !tbaa !5
  %75 = sext i32 %74 to i64
  %76 = getelementptr inbounds [300001 x i32], ptr @pos, i64 0, i64 %75
  store i32 %73, ptr %76, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  ret void
}

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

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

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 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 nounwind willreturn memory(argmem: write) }
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 = !{!7, !7, i64 0}
!10 = !{!11, !6, i64 0}
!11 = !{!"_ZTS4Trie", !6, i64 0, !7, i64 4, !6, i64 108}
!12 = distinct !{!12, !13}
!13 = !{!"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"

%struct.Trie = type { i32, [26 x i32], i32 }

@pos = external dso_local local_unnamed_addr global [300001 x i32], align 16
@w = external dso_local global [300001 x i32], align 16
@tot = external dso_local local_unnamed_addr global i32, align 4
@e = external dso_local local_unnamed_addr global [300001 x %struct.Trie], align 16
@str = external dso_local global [300001 x i8], align 16
@.str = external hidden unnamed_addr constant [5 x i8], align 1

; Function Attrs: mustprogress nofree nounwind uwtable
define dso_local void @_Z6inserti(i32 noundef %0) local_unnamed_addr #0 {
  %2 = sext i32 %0 to i64
  %3 = getelementptr inbounds i32, ptr @w, i64 %2
  %4 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @str, ptr noundef nonnull %3)
  %5 = load i8, ptr @str, align 16, !tbaa !5
  %.not16 = icmp eq i8 %5, 0
  br i1 %.not16, label %._crit_edge, label %.lr.ph.preheader

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

._crit_edge:                                      ; preds = %19, %1
  %.013.lcssa = phi i32 [ 0, %1 ], [ %20, %19 ]
  %6 = getelementptr inbounds [300001 x i32], ptr @pos, i64 0, i64 %2
  store i32 %.013.lcssa, ptr %6, align 4, !tbaa !8
  ret void

.lr.ph:                                           ; preds = %.lr.ph.preheader, %19
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %19 ]
  %7 = phi i8 [ %5, %.lr.ph.preheader ], [ %23, %19 ]
  %8 = phi ptr [ @str, %.lr.ph.preheader ], [ %22, %19 ]
  %.01317 = phi i32 [ 0, %.lr.ph.preheader ], [ %20, %19 ]
  %9 = phi i32 [ %tot.promoted, %.lr.ph.preheader ], [ %21, %19 ]
  %10 = add i8 %7, -97
  store i8 %10, ptr %8, align 1, !tbaa !5
  %11 = sext i32 %.01317 to i64
  %12 = sext i8 %10 to i64
  %13 = getelementptr inbounds [300001 x %struct.Trie], ptr @e, i64 0, i64 %11, i32 1, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !8
  %.not15 = icmp eq i32 %14, 0
  br i1 %.not15, label %15, label %19

15:                                               ; preds = %.lr.ph
  %16 = sext i32 %9 to i64
  %17 = getelementptr inbounds %struct.Trie, ptr @e, i64 %16
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(112) %17, i8 0, i64 112, i1 false)
  store i32 %9, ptr %13, align 4, !tbaa !8
  %18 = add nsw i32 %9, 1
  store i32 %18, ptr @tot, align 4, !tbaa !8
  store i32 %.01317, ptr %17, align 16, !tbaa !10
  %.pre = load i32, ptr %13, align 4, !tbaa !8
  br label %19

19:                                               ; preds = %15, %.lr.ph
  %20 = phi i32 [ %.pre, %15 ], [ %14, %.lr.ph ]
  %21 = phi i32 [ %18, %15 ], [ %9, %.lr.ph ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %22 = getelementptr inbounds [300001 x i8], ptr @str, i64 0, i64 %indvars.iv.next
  %23 = load i8, ptr %22, align 1, !tbaa !5
  %.not = icmp eq i8 %23, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !12
}

; Function Attrs: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

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

attributes #0 = { mustprogress 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 = { 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 #2 = { 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 = !{!11, !9, i64 0}
!11 = !{!"_ZTS4Trie", !9, i64 0, !6, i64 4, !9, i64 108}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


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