; LLVM IR from freakout42/macrocalc :: arx/src (lib_path)

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

@lib_path.pathname = external hidden global [1024 x i8], align 16
@__const.lib_path.sep = external hidden unnamed_addr constant [2 x i8], align 1
@.str = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local ptr @lib_path(ptr noundef %0, ptr noundef %1, i32 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca [2 x i8], align 1
  %10 = alloca [1025 x i8], align 16
  %11 = alloca ptr, 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 i32 %2, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  call void @llvm.lifetime.start.p0(i64 2, ptr %9) #5
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %9, ptr align 1 @__const.lib_path.sep, i64 2, i1 false)
  call void @llvm.lifetime.start.p0(i64 1025, ptr %10) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #5
  %13 = getelementptr inbounds [1025 x i8], ptr %10, i64 0, i64 0
  store ptr %13, ptr %11, align 8, !tbaa !5
  %14 = load ptr, ptr %5, align 8, !tbaa !5
  %15 = icmp eq ptr %14, null
  br i1 %15, label %20, label %16

16:                                               ; preds = %3
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = call i64 @strlen(ptr noundef %17) #6
  %19 = icmp uge i64 %18, 1024
  br i1 %19, label %20, label %21

20:                                               ; preds = %16, %3
  store ptr null, ptr %4, align 8
  store i32 1, ptr %12, align 4
  br label %42

21:                                               ; preds = %16
  %22 = getelementptr inbounds [1025 x i8], ptr %10, i64 0, i64 0
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = call ptr @strcpy(ptr noundef %22, ptr noundef %23) #5
  br label %25

25:                                               ; preds = %40, %21
  %26 = load ptr, ptr %11, align 8, !tbaa !5
  %27 = getelementptr inbounds [2 x i8], ptr %9, i64 0, i64 0
  %28 = call ptr @strtok(ptr noundef %26, ptr noundef %27) #5
  store ptr %28, ptr %8, align 8, !tbaa !5
  %29 = icmp ne ptr %28, null
  br i1 %29, label %30, label %41

30:                                               ; preds = %25
  %31 = load ptr, ptr %8, align 8, !tbaa !5
  %32 = call ptr @strcpy(ptr noundef @lib_path.pathname, ptr noundef %31) #5
  %33 = call ptr @strcat(ptr noundef @lib_path.pathname, ptr noundef @.str) #5
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = call ptr @strcat(ptr noundef @lib_path.pathname, ptr noundef %34) #5
  %36 = load i32, ptr %7, align 4, !tbaa !9
  %37 = call i32 @access(ptr noundef @lib_path.pathname, i32 noundef %36) #5
  %38 = icmp ne i32 %37, 0
  br i1 %38, label %40, label %39

39:                                               ; preds = %30
  store ptr @lib_path.pathname, ptr %4, align 8
  store i32 1, ptr %12, align 4
  br label %42

40:                                               ; preds = %30
  store ptr null, ptr %11, align 8, !tbaa !5
  br label %25, !llvm.loop !11

41:                                               ; preds = %25
  store ptr null, ptr %4, align 8
  store i32 1, ptr %12, align 4
  br label %42

42:                                               ; preds = %41, %39, %20
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #5
  call void @llvm.lifetime.end.p0(i64 1025, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 2, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  %43 = load ptr, ptr %4, align 8
  ret ptr %43
}

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #3

; Function Attrs: nounwind
declare ptr @strcpy(ptr noundef, ptr noundef) #4

; Function Attrs: nounwind
declare ptr @strtok(ptr noundef, ptr noundef) #4

; Function Attrs: nounwind
declare ptr @strcat(ptr noundef, ptr noundef) #4

; Function Attrs: nounwind
declare i32 @access(ptr noundef, i32 noundef) #4

; 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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 #4 = { nounwind "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 #5 = { nounwind }
attributes #6 = { 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 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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"

@lib_path.pathname = external hidden global [1024 x i8], align 16
@__const.lib_path.sep = external hidden unnamed_addr constant [2 x i8], align 1
@.str = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: nofree nounwind uwtable
define dso_local noundef ptr @lib_path(ptr noundef readonly %0, ptr nocapture noundef readonly %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = alloca [1025 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 1025, ptr nonnull %4) #6
  %5 = icmp eq ptr %0, null
  br i1 %5, label %.loopexit, label %6

6:                                                ; preds = %3
  %7 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %0) #7
  %8 = icmp ugt i64 %7, 1023
  br i1 %8, label %.loopexit, label %.peel.begin

.peel.begin:                                      ; preds = %6
  %9 = call ptr @strcpy(ptr noundef nonnull dereferenceable(1) %4, ptr noundef nonnull dereferenceable(1) %0) #6
  %10 = call ptr @strtok(ptr noundef nonnull %4, ptr noundef nonnull @__const.lib_path.sep) #6
  %.not.peel = icmp eq ptr %10, null
  br i1 %.not.peel, label %.loopexit, label %11

11:                                               ; preds = %.peel.begin
  %12 = call ptr @strcpy(ptr noundef nonnull dereferenceable(1) @lib_path.pathname, ptr noundef nonnull dereferenceable(1) %10) #6
  %13 = call ptr @strcat(ptr noundef nonnull dereferenceable(1) @lib_path.pathname, ptr noundef nonnull dereferenceable(1) @.str) #6
  %14 = call ptr @strcat(ptr noundef nonnull dereferenceable(1) @lib_path.pathname, ptr noundef nonnull dereferenceable(1) %1) #6
  %15 = call i32 @access(ptr noundef nonnull @lib_path.pathname, i32 noundef %2) #6
  %.not10.peel = icmp eq i32 %15, 0
  br i1 %.not10.peel, label %.loopexit, label %.peel.next

.peel.next:                                       ; preds = %11, %17
  %16 = call ptr @strtok(ptr noundef null, ptr noundef nonnull @__const.lib_path.sep) #6
  %.not = icmp eq ptr %16, null
  br i1 %.not, label %.loopexit, label %17

17:                                               ; preds = %.peel.next
  %18 = call ptr @strcpy(ptr noundef nonnull dereferenceable(1) @lib_path.pathname, ptr noundef nonnull dereferenceable(1) %16) #6
  %19 = call ptr @strcat(ptr noundef nonnull dereferenceable(1) @lib_path.pathname, ptr noundef nonnull dereferenceable(1) @.str) #6
  %20 = call ptr @strcat(ptr noundef nonnull dereferenceable(1) @lib_path.pathname, ptr noundef nonnull dereferenceable(1) %1) #6
  %21 = call i32 @access(ptr noundef nonnull @lib_path.pathname, i32 noundef %2) #6
  %.not10 = icmp eq i32 %21, 0
  br i1 %.not10, label %.loopexit, label %.peel.next, !llvm.loop !5

.loopexit:                                        ; preds = %.peel.next, %17, %.peel.begin, %11, %3, %6
  %.07 = phi ptr [ null, %6 ], [ null, %3 ], [ null, %.peel.begin ], [ @lib_path.pathname, %11 ], [ @lib_path.pathname, %17 ], [ null, %.peel.next ]
  call void @llvm.lifetime.end.p0(i64 1025, ptr nonnull %4) #6
  ret ptr %.07
}

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

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

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: readwrite)
declare ptr @strcpy(ptr noalias noundef returned writeonly, ptr noalias nocapture noundef readonly) local_unnamed_addr #3

; Function Attrs: mustprogress nofree nounwind willreturn
declare ptr @strtok(ptr noundef, ptr nocapture noundef readonly) local_unnamed_addr #4

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: readwrite)
declare ptr @strcat(ptr noalias noundef returned, ptr noalias nocapture noundef readonly) local_unnamed_addr #3

; Function Attrs: nofree nounwind
declare noundef i32 @access(ptr nocapture noundef readonly, i32 noundef) local_unnamed_addr #5

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

attributes #0 = { nofree 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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { mustprogress nofree nounwind willreturn memory(argmem: readwrite) "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 = { mustprogress nofree nounwind willreturn "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 #5 = { nofree nounwind "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 #6 = { nounwind }
attributes #7 = { 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 = distinct !{!5, !6, !7}
!6 = !{!"llvm.loop.mustprogress"}
!7 = !{!"llvm.loop.peeled.count", i32 1}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopFullUnrollPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass