; LLVM IR from narke/Aragveli :: src/kernel/lib/c (string)

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

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @strncmp(ptr noundef %0, ptr noundef %1, i64 noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i16, align 2
  %9 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %8) #2
  store i16 0, ptr %8, align 2, !tbaa !11
  store i16 0, ptr %8, align 2, !tbaa !11
  br label %10

10:                                               ; preds = %46, %3
  %11 = load i16, ptr %8, align 2, !tbaa !11
  %12 = zext i16 %11 to i64
  %13 = load i64, ptr %7, align 8, !tbaa !9
  %14 = icmp ult i64 %12, %13
  br i1 %14, label %15, label %27

15:                                               ; preds = %10
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %16, i32 1
  store ptr %17, ptr %5, align 8, !tbaa !5
  %18 = load i8, ptr %16, align 1, !tbaa !13
  %19 = sext i8 %18 to i32
  %20 = icmp ne i32 %19, 0
  br i1 %20, label %21, label %27

21:                                               ; preds = %15
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = getelementptr inbounds i8, ptr %22, i32 1
  store ptr %23, ptr %6, align 8, !tbaa !5
  %24 = load i8, ptr %22, align 1, !tbaa !13
  %25 = sext i8 %24 to i32
  %26 = icmp ne i32 %25, 0
  br label %27

27:                                               ; preds = %21, %15, %10
  %28 = phi i1 [ false, %15 ], [ false, %10 ], [ %26, %21 ]
  br i1 %28, label %29, label %49

29:                                               ; preds = %27
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = load i8, ptr %30, align 1, !tbaa !13
  %32 = sext i8 %31 to i32
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = load i8, ptr %33, align 1, !tbaa !13
  %35 = sext i8 %34 to i32
  %36 = icmp ne i32 %32, %35
  br i1 %36, label %37, label %45

37:                                               ; preds = %29
  %38 = load ptr, ptr %5, align 8, !tbaa !5
  %39 = load i8, ptr %38, align 1, !tbaa !13
  %40 = sext i8 %39 to i32
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = load i8, ptr %41, align 1, !tbaa !13
  %43 = sext i8 %42 to i32
  %44 = sub nsw i32 %40, %43
  store i32 %44, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %50

45:                                               ; preds = %29
  br label %46

46:                                               ; preds = %45
  %47 = load i16, ptr %8, align 2, !tbaa !11
  %48 = add i16 %47, 1
  store i16 %48, ptr %8, align 2, !tbaa !11
  br label %10, !llvm.loop !14

49:                                               ; preds = %27
  store i32 0, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %50

50:                                               ; preds = %49, %37
  call void @llvm.lifetime.end.p0(i64 2, ptr %8) #2
  %51 = load i32, ptr %4, align 4
  ret i32 %51
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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"

; Function Attrs: nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) uwtable
define dso_local range(i32 -255, 256) i32 @strncmp(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1, i64 noundef %2) local_unnamed_addr #0 {
  %.not22 = icmp eq i64 %2, 0
  br i1 %.not22, label %.critedge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %.pre = load i8, ptr %0, align 1, !tbaa !5
  br label %.lr.ph

4:                                                ; preds = %12
  %5 = add i16 %.019, 1
  %6 = zext i16 %5 to i64
  %7 = icmp ult i64 %6, %2
  br i1 %7, label %.lr.ph, label %.critedge, !llvm.loop !8

.lr.ph:                                           ; preds = %.lr.ph.preheader, %4
  %8 = phi i8 [ %14, %4 ], [ %.pre, %.lr.ph.preheader ]
  %.019 = phi i16 [ %5, %4 ], [ 0, %.lr.ph.preheader ]
  %.01018 = phi ptr [ %13, %4 ], [ %1, %.lr.ph.preheader ]
  %.01117 = phi ptr [ %9, %4 ], [ %0, %.lr.ph.preheader ]
  %9 = getelementptr inbounds i8, ptr %.01117, i64 1
  %.not = icmp eq i8 %8, 0
  br i1 %.not, label %.critedge, label %10

10:                                               ; preds = %.lr.ph
  %11 = load i8, ptr %.01018, align 1, !tbaa !5
  %.not14 = icmp eq i8 %11, 0
  br i1 %.not14, label %.critedge, label %12

12:                                               ; preds = %10
  %13 = getelementptr inbounds i8, ptr %.01018, i64 1
  %14 = load i8, ptr %9, align 1, !tbaa !5
  %15 = load i8, ptr %13, align 1, !tbaa !5
  %.not15 = icmp eq i8 %14, %15
  br i1 %.not15, label %4, label %16

16:                                               ; preds = %12
  %17 = sext i8 %15 to i32
  %18 = sext i8 %14 to i32
  %19 = sub nsw i32 %18, %17
  br label %.critedge

.critedge:                                        ; preds = %.lr.ph, %4, %10, %3, %16
  %.09 = phi i32 [ %19, %16 ], [ 0, %3 ], [ 0, %10 ], [ 0, %4 ], [ 0, %.lr.ph ]
  ret i32 %.09
}

attributes #0 = { nofree norecurse nosync nounwind memory(read, 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" }

!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/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


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