; 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 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 willreturn memory(read)
declare i64 @strlen(ptr noundef) #2

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

; Function Attrs: nounwind uwtable
define dso_local i64 @cwk_path_change_segment(ptr noundef %0, ptr noundef %1, ptr noundef %2, i64 noundef %3) #1 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  store i64 %3, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %12 = load ptr, ptr %7, align 8, !tbaa !5
  %13 = load i64, ptr %8, align 8, !tbaa !9
  %14 = load ptr, ptr %5, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.cwk_segment, ptr %14, i32 0, i32 0
  %16 = load ptr, ptr %15, align 8, !tbaa !11
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.cwk_segment, ptr %17, i32 0, i32 2
  %19 = load ptr, ptr %18, align 8, !tbaa !13
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.cwk_segment, ptr %20, i32 0, i32 0
  %22 = load ptr, ptr %21, align 8, !tbaa !11
  %23 = ptrtoint ptr %19 to i64
  %24 = ptrtoint ptr %22 to i64
  %25 = sub i64 %23, %24
  %26 = call i64 @cwk_path_output_sized(ptr noundef %12, i64 noundef %13, i64 noundef 0, ptr noundef %16, i64 noundef %25)
  store i64 %26, ptr %9, align 8, !tbaa !9
  br label %27

27:                                               ; preds = %30, %4
  %28 = load ptr, ptr %6, align 8, !tbaa !5
  %29 = call zeroext i1 @cwk_path_is_separator(ptr noundef %28)
  br i1 %29, label %30, label %33

30:                                               ; preds = %27
  %31 = load ptr, ptr %6, align 8, !tbaa !5
  %32 = getelementptr inbounds i8, ptr %31, i32 1
  store ptr %32, ptr %6, align 8, !tbaa !5
  br label %27, !llvm.loop !14

33:                                               ; preds = %27
  store i64 0, ptr %10, align 8, !tbaa !9
  br label %34

34:                                               ; preds = %40, %33
  %35 = load ptr, ptr %6, align 8, !tbaa !5
  %36 = load i64, ptr %10, align 8, !tbaa !9
  %37 = getelementptr inbounds i8, ptr %35, i64 %36
  %38 = load i8, ptr %37, align 1, !tbaa !16
  %39 = icmp ne i8 %38, 0
  br i1 %39, label %40, label %43

40:                                               ; preds = %34
  %41 = load i64, ptr %10, align 8, !tbaa !9
  %42 = add i64 %41, 1
  store i64 %42, ptr %10, align 8, !tbaa !9
  br label %34, !llvm.loop !17

43:                                               ; preds = %34
  br label %44

44:                                               ; preds = %55, %43
  %45 = load i64, ptr %10, align 8, !tbaa !9
  %46 = icmp ugt i64 %45, 0
  br i1 %46, label %47, label %53

47:                                               ; preds = %44
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = load i64, ptr %10, align 8, !tbaa !9
  %50 = sub i64 %49, 1
  %51 = getelementptr inbounds i8, ptr %48, i64 %50
  %52 = call zeroext i1 @cwk_path_is_separator(ptr noundef %51)
  br label %53

53:                                               ; preds = %47, %44
  %54 = phi i1 [ false, %44 ], [ %52, %47 ]
  br i1 %54, label %55, label %58

55:                                               ; preds = %53
  %56 = load i64, ptr %10, align 8, !tbaa !9
  %57 = add i64 %56, -1
  store i64 %57, ptr %10, align 8, !tbaa !9
  br label %44, !llvm.loop !18

58:                                               ; preds = %53
  %59 = load ptr, ptr %5, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.cwk_segment, ptr %59, i32 0, i32 3
  %61 = load ptr, ptr %60, align 8, !tbaa !19
  %62 = call i64 @strlen(ptr noundef %61) #4
  store i64 %62, ptr %11, align 8, !tbaa !9
  %63 = load ptr, ptr %7, align 8, !tbaa !5
  %64 = load i64, ptr %8, align 8, !tbaa !9
  %65 = load i64, ptr %9, align 8, !tbaa !9
  %66 = load i64, ptr %10, align 8, !tbaa !9
  %67 = add i64 %65, %66
  %68 = load ptr, ptr %5, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.cwk_segment, ptr %68, i32 0, i32 3
  %70 = load ptr, ptr %69, align 8, !tbaa !19
  %71 = load i64, ptr %11, align 8, !tbaa !9
  %72 = call i64 @cwk_path_output_sized(ptr noundef %63, i64 noundef %64, i64 noundef %67, ptr noundef %70, i64 noundef %71)
  %73 = load ptr, ptr %7, align 8, !tbaa !5
  %74 = load i64, ptr %8, align 8, !tbaa !9
  %75 = load i64, ptr %9, align 8, !tbaa !9
  %76 = load ptr, ptr %6, align 8, !tbaa !5
  %77 = load i64, ptr %10, align 8, !tbaa !9
  %78 = call i64 @cwk_path_output_sized(ptr noundef %73, i64 noundef %74, i64 noundef %75, ptr noundef %76, i64 noundef %77)
  %79 = load i64, ptr %9, align 8, !tbaa !9
  %80 = add i64 %79, %78
  store i64 %80, ptr %9, align 8, !tbaa !9
  %81 = load i64, ptr %11, align 8, !tbaa !9
  %82 = load i64, ptr %9, align 8, !tbaa !9
  %83 = add i64 %82, %81
  store i64 %83, ptr %9, align 8, !tbaa !9
  %84 = load ptr, ptr %7, align 8, !tbaa !5
  %85 = load i64, ptr %8, align 8, !tbaa !9
  %86 = load i64, ptr %9, align 8, !tbaa !9
  call void @cwk_path_terminate_output(ptr noundef %84, i64 noundef %85, i64 noundef %86)
  %87 = load i64, ptr %9, align 8, !tbaa !9
  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
  ret i64 %87
}

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 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 = !{!12, !6, i64 0}
!12 = !{!"cwk_segment", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !10, i64 32}
!13 = !{!12, !6, i64 16}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!7, !7, i64 0}
!17 = distinct !{!17, !15}
!18 = distinct !{!18, !15}
!19 = !{!12, !6, i64 24}


; 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: nounwind uwtable
declare hidden void @cwk_path_terminate_output(ptr noundef, i64 noundef, i64 noundef) local_unnamed_addr #0

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

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

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

; Function Attrs: nounwind uwtable
define dso_local noundef i64 @cwk_path_change_segment(ptr nocapture noundef readonly %0, ptr noundef %1, ptr noundef %2, i64 noundef %3) local_unnamed_addr #0 {
  %5 = load ptr, ptr %0, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %0, i64 16
  %7 = load ptr, ptr %6, align 8, !tbaa !11
  %8 = ptrtoint ptr %7 to i64
  %9 = ptrtoint ptr %5 to i64
  %10 = sub i64 %8, %9
  %11 = tail call i64 @cwk_path_output_sized(ptr noundef %2, i64 noundef %3, i64 noundef 0, ptr noundef %5, i64 noundef %10)
  br label %12

12:                                               ; preds = %12, %4
  %.0 = phi ptr [ %1, %4 ], [ %14, %12 ]
  %13 = tail call zeroext i1 @cwk_path_is_separator(ptr noundef %.0)
  %14 = getelementptr inbounds i8, ptr %.0, i64 1
  br i1 %13, label %12, label %.preheader34, !llvm.loop !12

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

.preheader:                                       ; preds = %.preheader34, %18
  %.1 = phi i64 [ %19, %18 ], [ %.032, %.preheader34 ]
  %.not33 = icmp eq i64 %.1, 0
  br i1 %.not33, label %.critedge, label %18

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

.critedge:                                        ; preds = %.preheader, %18
  %22 = getelementptr inbounds i8, ptr %0, i64 24
  %23 = load ptr, ptr %22, align 8, !tbaa !17
  %24 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %23) #2
  %25 = add i64 %.1, %11
  %26 = tail call i64 @cwk_path_output_sized(ptr noundef %2, i64 noundef %3, i64 noundef %25, ptr noundef %23, i64 noundef %24)
  %27 = tail call i64 @cwk_path_output_sized(ptr noundef %2, i64 noundef %3, i64 noundef %11, ptr noundef %.0, i64 noundef %.1)
  %28 = add i64 %24, %11
  %29 = add i64 %28, %27
  tail call void @cwk_path_terminate_output(ptr noundef %2, i64 noundef %3, i64 noundef %29)
  ret i64 %29
}

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 = { 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, !7, i64 0}
!6 = !{!"cwk_segment", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !10, i64 32}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!6, !7, i64 16}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!8, !8, i64 0}
!15 = distinct !{!15, !13}
!16 = distinct !{!16, !13}
!17 = !{!6, !7, i64 24}


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