; LLVM IR from yettabaa/cub3D :: Common/Libft (ft_substr)

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

@.str = external hidden unnamed_addr constant [1 x i8], align 1

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

13:                                               ; preds = %3
  store ptr null, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %81

14:                                               ; preds = %3
  %15 = load i32, ptr %6, align 4, !tbaa !9
  %16 = zext i32 %15 to i64
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = call i64 @ft_strlen(ptr noundef %17)
  %19 = icmp uge i64 %16, %18
  br i1 %19, label %20, label %22

20:                                               ; preds = %14
  %21 = call ptr @ft_strdup(ptr noundef @.str)
  store ptr %21, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %81

22:                                               ; preds = %14
  %23 = load i64, ptr %7, align 8, !tbaa !11
  %24 = load i32, ptr %6, align 4, !tbaa !9
  %25 = zext i32 %24 to i64
  %26 = add i64 %23, %25
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = call i64 @ft_strlen(ptr noundef %27)
  %29 = icmp ugt i64 %26, %28
  br i1 %29, label %30, label %39

30:                                               ; preds = %22
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = call i64 @ft_strlen(ptr noundef %31)
  %33 = mul i64 1, %32
  %34 = load i32, ptr %6, align 4, !tbaa !9
  %35 = zext i32 %34 to i64
  %36 = sub i64 %33, %35
  %37 = add i64 %36, 1
  %38 = call noalias ptr @malloc(i64 noundef %37) #5
  store ptr %38, ptr %8, align 8, !tbaa !5
  br label %44

39:                                               ; preds = %22
  %40 = load i64, ptr %7, align 8, !tbaa !11
  %41 = add i64 %40, 1
  %42 = mul i64 1, %41
  %43 = call noalias ptr @malloc(i64 noundef %42) #5
  store ptr %43, ptr %8, align 8, !tbaa !5
  br label %44

44:                                               ; preds = %39, %30
  %45 = load ptr, ptr %8, align 8, !tbaa !5
  %46 = icmp ne ptr %45, null
  br i1 %46, label %48, label %47

47:                                               ; preds = %44
  store ptr null, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %81

48:                                               ; preds = %44
  br label %49

49:                                               ; preds = %63, %48
  %50 = load i64, ptr %9, align 8, !tbaa !11
  %51 = load i64, ptr %7, align 8, !tbaa !11
  %52 = icmp ult i64 %50, %51
  br i1 %52, label %53, label %61

53:                                               ; preds = %49
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  %55 = load i32, ptr %6, align 4, !tbaa !9
  %56 = zext i32 %55 to i64
  %57 = getelementptr inbounds i8, ptr %54, i64 %56
  %58 = load i8, ptr %57, align 1, !tbaa !13
  %59 = sext i8 %58 to i32
  %60 = icmp ne i32 %59, 0
  br label %61

61:                                               ; preds = %53, %49
  %62 = phi i1 [ false, %49 ], [ %60, %53 ]
  br i1 %62, label %63, label %76

63:                                               ; preds = %61
  %64 = load ptr, ptr %5, align 8, !tbaa !5
  %65 = load i32, ptr %6, align 4, !tbaa !9
  %66 = zext i32 %65 to i64
  %67 = getelementptr inbounds i8, ptr %64, i64 %66
  %68 = load i8, ptr %67, align 1, !tbaa !13
  %69 = load ptr, ptr %8, align 8, !tbaa !5
  %70 = load i64, ptr %9, align 8, !tbaa !11
  %71 = getelementptr inbounds i8, ptr %69, i64 %70
  store i8 %68, ptr %71, align 1, !tbaa !13
  %72 = load i32, ptr %6, align 4, !tbaa !9
  %73 = add i32 %72, 1
  store i32 %73, ptr %6, align 4, !tbaa !9
  %74 = load i64, ptr %9, align 8, !tbaa !11
  %75 = add i64 %74, 1
  store i64 %75, ptr %9, align 8, !tbaa !11
  br label %49, !llvm.loop !14

76:                                               ; preds = %61
  %77 = load ptr, ptr %8, align 8, !tbaa !5
  %78 = load i64, ptr %9, align 8, !tbaa !11
  %79 = getelementptr inbounds i8, ptr %77, i64 %78
  store i8 0, ptr %79, align 1, !tbaa !13
  %80 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %80, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %81

81:                                               ; preds = %76, %47, %20, %13
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  %82 = load ptr, ptr %4, align 8
  ret ptr %82
}

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

declare i64 @ft_strlen(ptr noundef) #2

declare ptr @ft_strdup(ptr noundef) #2

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #3

; 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 = { "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 allocsize(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 #4 = { nounwind }
attributes #5 = { nounwind allocsize(0) }

!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 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !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"

@.str = external hidden unnamed_addr constant [1 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local ptr @ft_substr(ptr noundef %0, i32 noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %26, label %4

4:                                                ; preds = %3
  %5 = zext i32 %1 to i64
  %6 = tail call i64 @ft_strlen(ptr noundef nonnull %0) #3
  %.not28 = icmp ugt i64 %6, %5
  br i1 %.not28, label %9, label %7

7:                                                ; preds = %4
  %8 = tail call ptr @ft_strdup(ptr noundef nonnull @.str) #3
  br label %26

9:                                                ; preds = %4
  %10 = add i64 %5, %2
  %11 = tail call i64 @ft_strlen(ptr noundef nonnull %0) #3
  %12 = icmp ugt i64 %10, %11
  br i1 %12, label %13, label %15

13:                                               ; preds = %9
  %14 = tail call i64 @ft_strlen(ptr noundef nonnull %0) #3
  %reass.sub = sub i64 %14, %5
  br label %15

15:                                               ; preds = %9, %13
  %.sink36 = phi i64 [ %reass.sub, %13 ], [ %2, %9 ]
  %16 = add i64 %.sink36, 1
  %17 = tail call noalias ptr @malloc(i64 noundef %16) #4
  %.not29 = icmp eq ptr %17, null
  br i1 %.not29, label %26, label %.preheader

.preheader:                                       ; preds = %15
  %.not35 = icmp eq i64 %2, 0
  br i1 %.not35, label %.critedge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %21
  %.032 = phi i64 [ %24, %21 ], [ 0, %.preheader ]
  %.02531 = phi i32 [ %23, %21 ], [ %1, %.preheader ]
  %18 = zext i32 %.02531 to i64
  %19 = getelementptr inbounds i8, ptr %0, i64 %18
  %20 = load i8, ptr %19, align 1, !tbaa !5
  %.not30 = icmp eq i8 %20, 0
  br i1 %.not30, label %.critedge, label %21

21:                                               ; preds = %.lr.ph
  %22 = getelementptr inbounds i8, ptr %17, i64 %.032
  store i8 %20, ptr %22, align 1, !tbaa !5
  %23 = add i32 %.02531, 1
  %24 = add nuw i64 %.032, 1
  %exitcond.not = icmp eq i64 %24, %2
  br i1 %exitcond.not, label %.critedge, label %.lr.ph, !llvm.loop !8

.critedge:                                        ; preds = %.lr.ph, %21, %.preheader
  %.0.lcssa = phi i64 [ 0, %.preheader ], [ %2, %21 ], [ %.032, %.lr.ph ]
  %25 = getelementptr inbounds i8, ptr %17, i64 %.0.lcssa
  store i8 0, ptr %25, align 1, !tbaa !5
  br label %26

26:                                               ; preds = %15, %3, %.critedge, %7
  %.024 = phi ptr [ %8, %7 ], [ %17, %.critedge ], [ null, %3 ], [ null, %15 ]
  ret ptr %.024
}

declare i64 @ft_strlen(ptr noundef) local_unnamed_addr #1

declare ptr @ft_strdup(ptr noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #2

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 = { "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 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 allocsize(0) }

!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: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass