; LLVM IR from tangjz/acm-icpc :: self_training/2015-04-25/J.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
@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 [3 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 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @str)
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  store i32 0, ptr %4, align 4, !tbaa !5
  br label %6

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

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

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

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

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

66:                                               ; preds = %54
  %67 = load i32, ptr %4, align 4, !tbaa !5
  %68 = add nsw i32 %67, 1
  store i32 %68, ptr %4, align 4, !tbaa !5
  br label %6, !llvm.loop !12

69:                                               ; preds = %12
  %70 = load i32, ptr %3, align 4, !tbaa !5
  %71 = load i32, ptr %2, align 4, !tbaa !5
  %72 = sext i32 %71 to i64
  %73 = getelementptr inbounds [300001 x i32], ptr @pos, i64 0, i64 %72
  store i32 %70, ptr %73, 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
@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 [3 x i8], align 1

; Function Attrs: mustprogress nofree nounwind uwtable
define dso_local void @_Z6inserti(i32 noundef %0) local_unnamed_addr #0 {
  %2 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @str)
  %3 = load i8, ptr @str, align 16, !tbaa !5
  %.not15 = icmp eq i8 %3, 0
  br i1 %.not15, 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 = %18, %1
  %.012.lcssa = phi i32 [ 0, %1 ], [ %19, %18 ]
  %4 = sext i32 %0 to i64
  %5 = getelementptr inbounds [300001 x i32], ptr @pos, i64 0, i64 %4
  store i32 %.012.lcssa, ptr %5, align 4, !tbaa !8
  ret void

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

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

18:                                               ; preds = %14, %.lr.ph
  %19 = phi i32 [ %.pre, %14 ], [ %13, %.lr.ph ]
  %20 = phi i32 [ %17, %14 ], [ %8, %.lr.ph ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %21 = getelementptr inbounds [300001 x i8], ptr @str, i64 0, i64 %indvars.iv.next
  %22 = load i8, ptr %21, align 1, !tbaa !5
  %.not = icmp eq i8 %22, 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