; LLVM IR from VastoLorde95/Competitive-Programming :: CodeChef/WF/Medium Hard/LYRC/brute.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.Node = type { [100 x i32], i32, i32, i8, i32, i32 }

@node = external dso_local global [2000000 x %struct.Node], align 16
@sz = external dso_local global i32, align 4
@pattern = external dso_local global [5003 x i8], align 16

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z6insertv() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i8, align 1
  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
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  %5 = call i64 @strlen(ptr noundef @pattern) #4
  %6 = trunc i64 %5 to i32
  store i32 %6, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %4) #3
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %7

7:                                                ; preds = %55, %0
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = load i32, ptr %3, align 4, !tbaa !5
  %10 = icmp slt i32 %8, %9
  br i1 %10, label %11, label %58

11:                                               ; preds = %7
  %12 = load i32, ptr %2, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [5003 x i8], ptr @pattern, i64 0, i64 %13
  %15 = load i8, ptr %14, align 1, !tbaa !9
  store i8 %15, ptr %4, align 1, !tbaa !9
  %16 = load i32, ptr %1, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [2000000 x %struct.Node], ptr @node, i64 0, i64 %17
  %19 = getelementptr inbounds %struct.Node, ptr %18, i32 0, i32 0
  %20 = load i8, ptr %4, align 1, !tbaa !9
  %21 = sext i8 %20 to i64
  %22 = getelementptr inbounds [100 x i32], ptr %19, i64 0, i64 %21
  %23 = load i32, ptr %22, align 4, !tbaa !5
  %24 = icmp eq i32 %23, 0
  br i1 %24, label %25, label %46

25:                                               ; preds = %11
  %26 = load i32, ptr @sz, align 4, !tbaa !5
  %27 = add nsw i32 %26, 1
  store i32 %27, ptr @sz, align 4, !tbaa !5
  %28 = load i32, ptr @sz, align 4, !tbaa !5
  %29 = load i32, ptr %1, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [2000000 x %struct.Node], ptr @node, i64 0, i64 %30
  %32 = getelementptr inbounds %struct.Node, ptr %31, i32 0, i32 0
  %33 = load i8, ptr %4, align 1, !tbaa !9
  %34 = sext i8 %33 to i64
  %35 = getelementptr inbounds [100 x i32], ptr %32, i64 0, i64 %34
  store i32 %28, ptr %35, align 4, !tbaa !5
  %36 = load i32, ptr %1, align 4, !tbaa !5
  %37 = load i32, ptr @sz, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [2000000 x %struct.Node], ptr @node, i64 0, i64 %38
  %40 = getelementptr inbounds %struct.Node, ptr %39, i32 0, i32 1
  store i32 %36, ptr %40, align 4, !tbaa !10
  %41 = load i8, ptr %4, align 1, !tbaa !9
  %42 = load i32, ptr @sz, align 4, !tbaa !5
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [2000000 x %struct.Node], ptr @node, i64 0, i64 %43
  %45 = getelementptr inbounds %struct.Node, ptr %44, i32 0, i32 3
  store i8 %41, ptr %45, align 4, !tbaa !12
  br label %46

46:                                               ; preds = %25, %11
  %47 = load i32, ptr %1, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [2000000 x %struct.Node], ptr @node, i64 0, i64 %48
  %50 = getelementptr inbounds %struct.Node, ptr %49, i32 0, i32 0
  %51 = load i8, ptr %4, align 1, !tbaa !9
  %52 = sext i8 %51 to i64
  %53 = getelementptr inbounds [100 x i32], ptr %50, 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 = %46
  %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 %7, !llvm.loop !13

58:                                               ; preds = %7
  %59 = load i32, ptr %1, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 1, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  ret i32 %59
}

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

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #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 = { nounwind willreturn memory(read) "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 = { nounwind }
attributes #4 = { nounwind willreturn memory(read) }

!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 400}
!11 = !{!"_ZTS4Node", !7, i64 0, !6, i64 400, !6, i64 404, !7, i64 408, !6, i64 412, !6, i64 416}
!12 = !{!11, !7, i64 408}
!13 = distinct !{!13, !14}
!14 = !{!"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.Node = type { [100 x i32], i32, i32, i8, i32, i32 }

@node = external dso_local local_unnamed_addr global [2000000 x %struct.Node], align 16
@sz = external dso_local local_unnamed_addr global i32, align 4
@pattern = external dso_local global [5003 x i8], align 16

; Function Attrs: mustprogress nofree nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i32 @_Z6insertv() local_unnamed_addr #0 {
  %1 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) @pattern) #2
  %2 = trunc i64 %1 to i32
  %3 = icmp sgt i32 %2, 0
  br i1 %3, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %0
  %sz.promoted = load i32, ptr @sz, align 4, !tbaa !5
  %wide.trip.count = and i64 %1, 2147483647
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %18
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %18 ]
  %.015 = phi i32 [ 0, %.lr.ph.preheader ], [ %19, %18 ]
  %4 = phi i32 [ %sz.promoted, %.lr.ph.preheader ], [ %20, %18 ]
  %5 = getelementptr inbounds [5003 x i8], ptr @pattern, i64 0, i64 %indvars.iv
  %6 = load i8, ptr %5, align 1, !tbaa !9
  %7 = sext i32 %.015 to i64
  %8 = getelementptr inbounds [2000000 x %struct.Node], ptr @node, i64 0, i64 %7
  %9 = sext i8 %6 to i64
  %10 = getelementptr inbounds [100 x i32], ptr %8, i64 0, i64 %9
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %12 = icmp eq i32 %11, 0
  br i1 %12, label %13, label %18

13:                                               ; preds = %.lr.ph
  %14 = add nsw i32 %4, 1
  store i32 %14, ptr @sz, align 4, !tbaa !5
  store i32 %14, ptr %10, align 4, !tbaa !5
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [2000000 x %struct.Node], ptr @node, i64 0, i64 %15, i32 1
  store i32 %.015, ptr %16, align 4, !tbaa !10
  %17 = getelementptr inbounds [2000000 x %struct.Node], ptr @node, i64 0, i64 %15, i32 3
  store i8 %6, ptr %17, align 4, !tbaa !12
  %.pre = load i32, ptr %10, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %13, %.lr.ph
  %19 = phi i32 [ %.pre, %13 ], [ %11, %.lr.ph ]
  %20 = phi i32 [ %14, %13 ], [ %4, %.lr.ph ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !13

._crit_edge:                                      ; preds = %18, %0
  %.0.lcssa = phi i32 [ 0, %0 ], [ %19, %18 ]
  ret i32 %.0.lcssa
}

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #1

attributes #0 = { mustprogress nofree nounwind memory(readwrite, argmem: none, 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 nofree nounwind willreturn memory(argmem: read) "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 = { nounwind willreturn memory(read) }

!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 400}
!11 = !{!"_ZTS4Node", !7, i64 0, !6, i64 400, !6, i64 404, !7, i64 408, !6, i64 412, !6, i64 416}
!12 = !{!11, !7, i64 408}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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