; LLVM IR from wkoszek/kmnsim :: src/subr (strlcat)

; 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: nounwind uwtable
define dso_local i64 @strlcat(ptr noalias noundef %0, ptr noalias noundef %1, i64 noundef %2) #0 {
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  %12 = 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 8, ptr %8) #3
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %13, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %14 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %14, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %15 = load i64, ptr %7, align 8, !tbaa !9
  store i64 %15, ptr %10, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  br label %16

16:                                               ; preds = %27, %3
  %17 = load i64, ptr %10, align 8, !tbaa !9
  %18 = add i64 %17, -1
  store i64 %18, ptr %10, align 8, !tbaa !9
  %19 = icmp ne i64 %17, 0
  br i1 %19, label %20, label %25

20:                                               ; preds = %16
  %21 = load ptr, ptr %8, align 8, !tbaa !5
  %22 = load i8, ptr %21, align 1, !tbaa !11
  %23 = sext i8 %22 to i32
  %24 = icmp ne i32 %23, 0
  br label %25

25:                                               ; preds = %20, %16
  %26 = phi i1 [ false, %16 ], [ %24, %20 ]
  br i1 %26, label %27, label %30

27:                                               ; preds = %25
  %28 = load ptr, ptr %8, align 8, !tbaa !5
  %29 = getelementptr inbounds i8, ptr %28, i32 1
  store ptr %29, ptr %8, align 8, !tbaa !5
  br label %16, !llvm.loop !12

30:                                               ; preds = %25
  %31 = load ptr, ptr %8, align 8, !tbaa !5
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = ptrtoint ptr %31 to i64
  %34 = ptrtoint ptr %32 to i64
  %35 = sub i64 %33, %34
  store i64 %35, ptr %11, align 8, !tbaa !9
  %36 = load i64, ptr %7, align 8, !tbaa !9
  %37 = load i64, ptr %11, align 8, !tbaa !9
  %38 = sub i64 %36, %37
  store i64 %38, ptr %10, align 8, !tbaa !9
  %39 = load i64, ptr %10, align 8, !tbaa !9
  %40 = icmp eq i64 %39, 0
  br i1 %40, label %41, label %46

41:                                               ; preds = %30
  %42 = load i64, ptr %11, align 8, !tbaa !9
  %43 = load ptr, ptr %9, align 8, !tbaa !5
  %44 = call i64 @strlen(ptr noundef %43) #4
  %45 = add i64 %42, %44
  store i64 %45, ptr %4, align 8
  store i32 1, ptr %12, align 4
  br label %74

46:                                               ; preds = %30
  br label %47

47:                                               ; preds = %62, %46
  %48 = load ptr, ptr %9, align 8, !tbaa !5
  %49 = load i8, ptr %48, align 1, !tbaa !11
  %50 = sext i8 %49 to i32
  %51 = icmp ne i32 %50, 0
  br i1 %51, label %52, label %65

52:                                               ; preds = %47
  %53 = load i64, ptr %10, align 8, !tbaa !9
  %54 = icmp ne i64 %53, 1
  br i1 %54, label %55, label %62

55:                                               ; preds = %52
  %56 = load ptr, ptr %9, align 8, !tbaa !5
  %57 = load i8, ptr %56, align 1, !tbaa !11
  %58 = load ptr, ptr %8, align 8, !tbaa !5
  %59 = getelementptr inbounds i8, ptr %58, i32 1
  store ptr %59, ptr %8, align 8, !tbaa !5
  store i8 %57, ptr %58, align 1, !tbaa !11
  %60 = load i64, ptr %10, align 8, !tbaa !9
  %61 = add i64 %60, -1
  store i64 %61, ptr %10, align 8, !tbaa !9
  br label %62

62:                                               ; preds = %55, %52
  %63 = load ptr, ptr %9, align 8, !tbaa !5
  %64 = getelementptr inbounds i8, ptr %63, i32 1
  store ptr %64, ptr %9, align 8, !tbaa !5
  br label %47, !llvm.loop !14

65:                                               ; preds = %47
  %66 = load ptr, ptr %8, align 8, !tbaa !5
  store i8 0, ptr %66, align 1, !tbaa !11
  %67 = load i64, ptr %11, align 8, !tbaa !9
  %68 = load ptr, ptr %9, align 8, !tbaa !5
  %69 = load ptr, ptr %6, align 8, !tbaa !5
  %70 = ptrtoint ptr %68 to i64
  %71 = ptrtoint ptr %69 to i64
  %72 = sub i64 %70, %71
  %73 = add i64 %67, %72
  store i64 %73, ptr %4, align 8
  store i32 1, ptr %12, align 4
  br label %74

74:                                               ; preds = %65, %41
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %75 = load i64, ptr %4, align 8
  ret i64 %75
}

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


; 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 nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local i64 @strlcat(ptr noalias noundef %0, ptr noalias noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %.not32 = icmp eq i64 %2, 0
  br i1 %.not32, label %.critedge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %scevgep = getelementptr i8, ptr %0, i64 %2
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %5
  %.in = phi i64 [ %6, %5 ], [ %2, %.lr.ph.preheader ]
  %.02533 = phi ptr [ %7, %5 ], [ %0, %.lr.ph.preheader ]
  %4 = load i8, ptr %.02533, align 1, !tbaa !5
  %.not29 = icmp eq i8 %4, 0
  br i1 %.not29, label %.critedge, label %5

5:                                                ; preds = %.lr.ph
  %6 = add i64 %.in, -1
  %7 = getelementptr inbounds i8, ptr %.02533, i64 1
  %.not = icmp eq i64 %6, 0
  br i1 %.not, label %.critedge, label %.lr.ph, !llvm.loop !8

.critedge:                                        ; preds = %.lr.ph, %5, %3
  %.025.lcssa = phi ptr [ %0, %3 ], [ %scevgep, %5 ], [ %.02533, %.lr.ph ]
  %8 = ptrtoint ptr %.025.lcssa to i64
  %9 = ptrtoint ptr %0 to i64
  %10 = sub i64 %8, %9
  %11 = sub i64 %2, %10
  %12 = icmp eq i64 %11, 0
  br i1 %12, label %14, label %.preheader

.preheader:                                       ; preds = %.critedge
  %13 = load i8, ptr %1, align 1, !tbaa !5
  %.not3036 = icmp eq i8 %13, 0
  br i1 %.not3036, label %._crit_edge, label %.lr.ph40

14:                                               ; preds = %.critedge
  %15 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %1) #2
  br label %26

.lr.ph40:                                         ; preds = %.preheader, %20
  %16 = phi i8 [ %22, %20 ], [ %13, %.preheader ]
  %.139 = phi i64 [ %.2, %20 ], [ %11, %.preheader ]
  %.02438 = phi ptr [ %21, %20 ], [ %1, %.preheader ]
  %.12637 = phi ptr [ %.227, %20 ], [ %.025.lcssa, %.preheader ]
  %.not31 = icmp eq i64 %.139, 1
  br i1 %.not31, label %20, label %17

17:                                               ; preds = %.lr.ph40
  %18 = getelementptr inbounds i8, ptr %.12637, i64 1
  store i8 %16, ptr %.12637, align 1, !tbaa !5
  %19 = add i64 %.139, -1
  br label %20

20:                                               ; preds = %17, %.lr.ph40
  %.227 = phi ptr [ %18, %17 ], [ %.12637, %.lr.ph40 ]
  %.2 = phi i64 [ %19, %17 ], [ 1, %.lr.ph40 ]
  %21 = getelementptr inbounds i8, ptr %.02438, i64 1
  %22 = load i8, ptr %21, align 1, !tbaa !5
  %.not30 = icmp eq i8 %22, 0
  br i1 %.not30, label %._crit_edge, label %.lr.ph40, !llvm.loop !10

._crit_edge:                                      ; preds = %20, %.preheader
  %.126.lcssa = phi ptr [ %.025.lcssa, %.preheader ], [ %.227, %20 ]
  %.024.lcssa = phi ptr [ %1, %.preheader ], [ %21, %20 ]
  store i8 0, ptr %.126.lcssa, align 1, !tbaa !5
  %23 = ptrtoint ptr %.024.lcssa to i64
  %24 = ptrtoint ptr %1 to i64
  %25 = sub i64 %23, %24
  br label %26

26:                                               ; preds = %._crit_edge, %14
  %.pn = phi i64 [ %15, %14 ], [ %25, %._crit_edge ]
  %.0 = add i64 %.pn, %10
  ret i64 %.0
}

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

attributes #0 = { nofree nounwind memory(readwrite, 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}
!10 = distinct !{!10, !9}


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