; LLVM IR from likle/cwalk :: src (cwalk)

; 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.cwk_segment = type { ptr, ptr, ptr, ptr, i64 }

; 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
declare dso_local void @cwk_path_get_root(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare hidden void @cwk_path_terminate_output(ptr noundef, i64 noundef, i64 noundef) #1

; Function Attrs: nounwind uwtable
declare hidden i64 @cwk_path_output_sized(ptr noundef, i64 noundef, i64 noundef, ptr noundef, i64 noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @cwk_path_get_last_segment(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i64 @cwk_path_change_basename(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3) #1 {
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca %struct.cwk_segment, align 8
  %11 = alloca i64, align 8
  %12 = alloca i64, align 8
  %13 = alloca i64, align 8
  %14 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store ptr %2, ptr %8, align 8, !tbaa !5
  store i64 %3, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 40, ptr %10) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #2
  %15 = load ptr, ptr %6, align 8, !tbaa !5
  %16 = call zeroext i1 @cwk_path_get_last_segment(ptr noundef %15, ptr noundef %10)
  br i1 %16, label %68, label %17

17:                                               ; preds = %4
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  call void @cwk_path_get_root(ptr noundef %18, ptr noundef %12)
  %19 = load ptr, ptr %8, align 8, !tbaa !5
  %20 = load i64, ptr %9, align 8, !tbaa !9
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  %22 = load i64, ptr %12, align 8, !tbaa !9
  %23 = call i64 @cwk_path_output_sized(ptr noundef %19, i64 noundef %20, i64 noundef 0, ptr noundef %21, i64 noundef %22)
  store i64 %23, ptr %11, align 8, !tbaa !9
  br label %24

24:                                               ; preds = %27, %17
  %25 = load ptr, ptr %7, align 8, !tbaa !5
  %26 = call zeroext i1 @cwk_path_is_separator(ptr noundef %25)
  br i1 %26, label %27, label %30

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

30:                                               ; preds = %24
  store i64 0, ptr %13, align 8, !tbaa !9
  br label %31

31:                                               ; preds = %37, %30
  %32 = load ptr, ptr %7, align 8, !tbaa !5
  %33 = load i64, ptr %13, align 8, !tbaa !9
  %34 = getelementptr inbounds i8, ptr %32, i64 %33
  %35 = load i8, ptr %34, align 1, !tbaa !13
  %36 = icmp ne i8 %35, 0
  br i1 %36, label %37, label %40

37:                                               ; preds = %31
  %38 = load i64, ptr %13, align 8, !tbaa !9
  %39 = add i64 %38, 1
  store i64 %39, ptr %13, align 8, !tbaa !9
  br label %31, !llvm.loop !14

40:                                               ; preds = %31
  br label %41

41:                                               ; preds = %52, %40
  %42 = load i64, ptr %13, align 8, !tbaa !9
  %43 = icmp ugt i64 %42, 0
  br i1 %43, label %44, label %50

44:                                               ; preds = %41
  %45 = load ptr, ptr %7, align 8, !tbaa !5
  %46 = load i64, ptr %13, align 8, !tbaa !9
  %47 = sub i64 %46, 1
  %48 = getelementptr inbounds i8, ptr %45, i64 %47
  %49 = call zeroext i1 @cwk_path_is_separator(ptr noundef %48)
  br label %50

50:                                               ; preds = %44, %41
  %51 = phi i1 [ false, %41 ], [ %49, %44 ]
  br i1 %51, label %52, label %55

52:                                               ; preds = %50
  %53 = load i64, ptr %13, align 8, !tbaa !9
  %54 = add i64 %53, -1
  store i64 %54, ptr %13, align 8, !tbaa !9
  br label %41, !llvm.loop !15

55:                                               ; preds = %50
  %56 = load ptr, ptr %8, align 8, !tbaa !5
  %57 = load i64, ptr %9, align 8, !tbaa !9
  %58 = load i64, ptr %11, align 8, !tbaa !9
  %59 = load ptr, ptr %7, align 8, !tbaa !5
  %60 = load i64, ptr %13, align 8, !tbaa !9
  %61 = call i64 @cwk_path_output_sized(ptr noundef %56, i64 noundef %57, i64 noundef %58, ptr noundef %59, i64 noundef %60)
  %62 = load i64, ptr %11, align 8, !tbaa !9
  %63 = add i64 %62, %61
  store i64 %63, ptr %11, align 8, !tbaa !9
  %64 = load ptr, ptr %8, align 8, !tbaa !5
  %65 = load i64, ptr %9, align 8, !tbaa !9
  %66 = load i64, ptr %11, align 8, !tbaa !9
  call void @cwk_path_terminate_output(ptr noundef %64, i64 noundef %65, i64 noundef %66)
  %67 = load i64, ptr %11, align 8, !tbaa !9
  store i64 %67, ptr %5, align 8
  store i32 1, ptr %14, align 4
  br label %73

68:                                               ; preds = %4
  %69 = load ptr, ptr %7, align 8, !tbaa !5
  %70 = load ptr, ptr %8, align 8, !tbaa !5
  %71 = load i64, ptr %9, align 8, !tbaa !9
  %72 = call i64 @cwk_path_change_segment(ptr noundef %10, ptr noundef %69, ptr noundef %70, i64 noundef %71)
  store i64 %72, ptr %5, align 8
  store i32 1, ptr %14, align 4
  br label %73

73:                                               ; preds = %68, %55
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 40, ptr %10) #2
  %74 = load i64, ptr %5, align 8
  ret i64 %74
}

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @cwk_path_is_separator(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local i64 @cwk_path_change_segment(ptr noundef, ptr noundef, ptr noundef, i64 noundef) #1

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


; 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.cwk_segment = type { ptr, ptr, ptr, ptr, i64 }

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @cwk_path_get_root(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden void @cwk_path_terminate_output(ptr noundef, i64 noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden i64 @cwk_path_output_sized(ptr noundef, i64 noundef, i64 noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @cwk_path_get_last_segment(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i64 @cwk_path_change_basename(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3) local_unnamed_addr #1 {
  %5 = alloca %struct.cwk_segment, align 8
  %6 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 40, ptr nonnull %5) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %6) #2
  %7 = call zeroext i1 @cwk_path_get_last_segment(ptr noundef %0, ptr noundef nonnull %5)
  br i1 %7, label %23, label %8

8:                                                ; preds = %4
  call void @cwk_path_get_root(ptr noundef %0, ptr noundef nonnull %6)
  %9 = load i64, ptr %6, align 8, !tbaa !5
  %10 = call i64 @cwk_path_output_sized(ptr noundef %2, i64 noundef %3, i64 noundef 0, ptr noundef %0, i64 noundef %9)
  br label %11

11:                                               ; preds = %11, %8
  %.028 = phi ptr [ %1, %8 ], [ %13, %11 ]
  %12 = call zeroext i1 @cwk_path_is_separator(ptr noundef %.028)
  %13 = getelementptr inbounds i8, ptr %.028, i64 1
  br i1 %12, label %11, label %.preheader30, !llvm.loop !9

.preheader30:                                     ; preds = %11, %.preheader30
  %.0 = phi i64 [ %16, %.preheader30 ], [ 0, %11 ]
  %14 = getelementptr inbounds i8, ptr %.028, i64 %.0
  %15 = load i8, ptr %14, align 1, !tbaa !11
  %.not = icmp eq i8 %15, 0
  %16 = add i64 %.0, 1
  br i1 %.not, label %.preheader, label %.preheader30, !llvm.loop !12

.preheader:                                       ; preds = %.preheader30, %17
  %.1 = phi i64 [ %18, %17 ], [ %.0, %.preheader30 ]
  %.not29 = icmp eq i64 %.1, 0
  br i1 %.not29, label %.critedge, label %17

17:                                               ; preds = %.preheader
  %18 = add i64 %.1, -1
  %19 = getelementptr inbounds i8, ptr %.028, i64 %18
  %20 = call zeroext i1 @cwk_path_is_separator(ptr noundef %19)
  br i1 %20, label %.preheader, label %.critedge, !llvm.loop !13

.critedge:                                        ; preds = %.preheader, %17
  %21 = call i64 @cwk_path_output_sized(ptr noundef %2, i64 noundef %3, i64 noundef %10, ptr noundef %.028, i64 noundef %.1)
  %22 = add i64 %21, %10
  call void @cwk_path_terminate_output(ptr noundef %2, i64 noundef %3, i64 noundef %22)
  br label %25

23:                                               ; preds = %4
  %24 = call i64 @cwk_path_change_segment(ptr noundef nonnull %5, ptr noundef %1, ptr noundef %2, i64 noundef %3)
  br label %25

25:                                               ; preds = %23, %.critedge
  %.027 = phi i64 [ %24, %23 ], [ %22, %.critedge ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %6) #2
  call void @llvm.lifetime.end.p0(i64 40, ptr nonnull %5) #2
  ret i64 %.027
}

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @cwk_path_is_separator(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local i64 @cwk_path_change_segment(ptr noundef, ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #1

attributes #0 = { mustprogress 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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !10}
!13 = distinct !{!13, !10}


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