; LLVM IR from tangjz/acm-icpc :: TDL_training/2015-10-05/L - Language Recognition - tjz.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"

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

@tot = external dso_local global i32, align 4
@str = external dso_local global [31 x i8], align 16
@e = external dso_local global [155031 x %struct.Trie], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1

; Function Attrs: mustprogress 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) #4
  store i32 0, ptr %1, align 4, !tbaa !5
  %3 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef @str)
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %4

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

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

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

31:                                               ; preds = %11
  %32 = load i32, ptr @tot, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [155031 x %struct.Trie], ptr @e, i64 0, i64 %33
  call void @llvm.memset.p0.i64(ptr align 4 %34, i8 0, i64 108, i1 false)
  %35 = load i32, ptr @tot, align 4, !tbaa !5
  %36 = add nsw i32 %35, 1
  store i32 %36, ptr @tot, align 4, !tbaa !5
  %37 = load i32, ptr %1, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [155031 x %struct.Trie], ptr @e, i64 0, i64 %38
  %40 = getelementptr inbounds %struct.Trie, ptr %39, i32 0, i32 0
  %41 = load i32, ptr %2, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [31 x i8], ptr @str, i64 0, i64 %42
  %44 = load i8, ptr %43, align 1, !tbaa !9
  %45 = sext i8 %44 to i64
  %46 = getelementptr inbounds [26 x i32], ptr %40, i64 0, i64 %45
  store i32 %35, ptr %46, align 4, !tbaa !5
  br label %47

47:                                               ; preds = %31, %11
  %48 = load i32, ptr %1, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [155031 x %struct.Trie], ptr @e, i64 0, i64 %49
  %51 = getelementptr inbounds %struct.Trie, ptr %50, i32 0, i32 0
  %52 = load i32, ptr %2, align 4, !tbaa !5
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds [31 x i8], ptr @str, i64 0, i64 %53
  %55 = load i8, ptr %54, align 1, !tbaa !9
  %56 = sext i8 %55 to i64
  %57 = getelementptr inbounds [26 x i32], ptr %51, i64 0, i64 %56
  %58 = load i32, ptr %57, align 4, !tbaa !5
  store i32 %58, ptr %1, align 4, !tbaa !5
  br label %59

59:                                               ; preds = %47
  %60 = load i32, ptr %2, align 4, !tbaa !5
  %61 = add nsw i32 %60, 1
  store i32 %61, ptr %2, align 4, !tbaa !5
  br label %4, !llvm.loop !10

62:                                               ; preds = %10
  %63 = load i32, ptr %1, align 4, !tbaa !5
  %64 = sext i32 %63 to i64
  %65 = getelementptr inbounds [155031 x %struct.Trie], ptr @e, i64 0, i64 %64
  %66 = getelementptr inbounds %struct.Trie, ptr %65, i32 0, i32 1
  store i8 1, ptr %66, align 4, !tbaa !12
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #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 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = !{!13, !14, i64 104}
!13 = !{!"_ZTS4Trie", !7, i64 0, !14, i64 104}
!14 = !{!"bool", !7, i64 0}


; 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 { [26 x i32], i8 }

@tot = external dso_local local_unnamed_addr global i32, align 4
@str = external dso_local global [31 x i8], align 16
@e = external dso_local local_unnamed_addr global [155031 x %struct.Trie], align 16
@.str = external hidden unnamed_addr constant [3 x i8], align 1

; Function Attrs: mustprogress nofree nounwind uwtable
define dso_local void @_Z6insertv() local_unnamed_addr #0 {
  %1 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull @str)
  %2 = load i8, ptr @str, align 16, !tbaa !5
  %.not13 = icmp eq i8 %2, 0
  br i1 %.not13, 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.loopexit:                             ; preds = %18
  %3 = sext i32 %19 to i64
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %0
  %.010.lcssa = phi i64 [ 0, %0 ], [ %3, %._crit_edge.loopexit ]
  %4 = getelementptr inbounds [155031 x %struct.Trie], ptr @e, i64 0, i64 %.010.lcssa, i32 1
  store i8 1, ptr %4, align 4, !tbaa !10
  ret void

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

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

18:                                               ; preds = %14, %.lr.ph
  %19 = phi i32 [ %7, %14 ], [ %13, %.lr.ph ]
  %20 = phi i32 [ %17, %14 ], [ %7, %.lr.ph ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %21 = getelementptr inbounds [31 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.loopexit, label %.lr.ph, !llvm.loop !13
}

; 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, !12, i64 104}
!11 = !{!"_ZTS4Trie", !6, i64 0, !12, i64 104}
!12 = !{!"bool", !6, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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