; LLVM IR from ctlst-tech/catpilot :: os/freertos/services/ls (ls)

; 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.stat = type { i64, i64, i32 }
%struct.dirent = type { [32 x i8], i32 }

@.str = external hidden unnamed_addr constant [4 x i8], align 1
@FreeRTOS_errno = external global i32, align 4
@.str.1 = external hidden unnamed_addr constant [10 x i8], align 1

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

12:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 24, ptr %4) #4
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  %14 = call i32 @stat(ptr noundef %13, ptr noundef %4)
  %15 = icmp slt i32 %14, 0
  br i1 %15, label %16, label %20

16:                                               ; preds = %12
  %17 = load i32, ptr @FreeRTOS_errno, align 4, !tbaa !9
  %18 = call ptr @strerror(i32 noundef %17) #4
  %19 = call i32 (ptr, ...) @printf_(ptr noundef @.str, ptr noundef %18)
  store i32 1, ptr %5, align 4
  br label %25

20:                                               ; preds = %12
  %21 = getelementptr inbounds %struct.stat, ptr %4, i32 0, i32 0
  %22 = load i64, ptr %21, align 8, !tbaa !11
  %23 = load ptr, ptr %2, align 8, !tbaa !5
  %24 = call i32 (ptr, ...) @printf_(ptr noundef @.str.1, i64 noundef %22, ptr noundef %23)
  store i32 0, ptr %5, align 4
  br label %25

25:                                               ; preds = %20, %16
  call void @llvm.lifetime.end.p0(i64 24, ptr %4) #4
  %26 = load i32, ptr %5, align 4
  switch i32 %26, label %51 [
    i32 0, label %27
  ]

27:                                               ; preds = %25
  br label %50

28:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  store i32 -1, ptr %6, align 4, !tbaa !9
  br label %29

29:                                               ; preds = %46, %28
  %30 = load i32, ptr %6, align 4, !tbaa !9
  %31 = icmp ne i32 %30, 0
  br i1 %31, label %32, label %47

32:                                               ; preds = %29
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %33 = load ptr, ptr %3, align 8, !tbaa !5
  %34 = call ptr @readdir(ptr noundef %33)
  store ptr %34, ptr %7, align 8, !tbaa !5
  %35 = load ptr, ptr %7, align 8, !tbaa !5
  %36 = icmp ne ptr %35, null
  br i1 %36, label %37, label %45

37:                                               ; preds = %32
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.dirent, ptr %38, i32 0, i32 1
  %40 = load i32, ptr %39, align 4, !tbaa !14
  %41 = load ptr, ptr %7, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.dirent, ptr %41, i32 0, i32 0
  %43 = getelementptr inbounds [32 x i8], ptr %42, i64 0, i64 0
  %44 = call i32 (ptr, ...) @printf_(ptr noundef @.str.1, i32 noundef %40, ptr noundef %43)
  br label %46

45:                                               ; preds = %32
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %46

46:                                               ; preds = %45, %37
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  br label %29, !llvm.loop !16

47:                                               ; preds = %29
  %48 = load ptr, ptr %3, align 8, !tbaa !5
  %49 = call i32 @closedir(ptr noundef %48)
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  br label %50

50:                                               ; preds = %47, %27
  store i32 0, ptr %5, align 4
  br label %51

51:                                               ; preds = %50, %25
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  %52 = load i32, ptr %5, align 4
  switch i32 %52, label %54 [
    i32 0, label %53
    i32 1, label %53
  ]

53:                                               ; preds = %51, %51
  ret void

54:                                               ; preds = %51
  unreachable
}

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

declare ptr @opendir(ptr noundef) #2

declare i32 @stat(ptr noundef, ptr noundef) #2

declare i32 @printf_(ptr noundef, ...) #2

; Function Attrs: nounwind
declare ptr @strerror(i32 noundef) #3

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

declare ptr @readdir(ptr noundef) #2

declare i32 @closedir(ptr noundef) #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 = { 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 "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 }

!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, !13, i64 0}
!12 = !{!"stat", !13, i64 0, !13, i64 8, !10, i64 16}
!13 = !{!"long", !7, i64 0}
!14 = !{!15, !10, i64 32}
!15 = !{!"dirent", !7, i64 0, !10, i64 32}
!16 = distinct !{!16, !17}
!17 = !{!"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"

%struct.stat = type { i64, i64, i32 }

@.str = external hidden unnamed_addr constant [4 x i8], align 1
@FreeRTOS_errno = external local_unnamed_addr global i32, align 4
@.str.1 = external hidden unnamed_addr constant [10 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @ls_print_dir_content(ptr noundef %0) local_unnamed_addr #0 {
  %2 = alloca %struct.stat, align 8
  %3 = tail call ptr @opendir(ptr noundef %0)
  %4 = icmp eq ptr %3, null
  br i1 %4, label %6, label %.preheader.preheader

.preheader.preheader:                             ; preds = %1
  %5 = tail call ptr @readdir(ptr noundef nonnull %3) #5
  %.not1314 = icmp eq ptr %5, null
  br i1 %.not1314, label %.preheader._crit_edge, label %.preheader

6:                                                ; preds = %1
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %2) #5
  %7 = call i32 @stat(ptr noundef %0, ptr noundef nonnull %2)
  %8 = icmp sgt i32 %7, -1
  br i1 %8, label %13, label %9

9:                                                ; preds = %6
  %10 = load i32, ptr @FreeRTOS_errno, align 4, !tbaa !5
  %11 = tail call ptr @strerror(i32 noundef %10) #5
  %12 = tail call i32 (ptr, ...) @printf_(ptr noundef nonnull @.str, ptr noundef %11) #5
  br label %16

13:                                               ; preds = %6
  %14 = load i64, ptr %2, align 8, !tbaa !9
  %15 = tail call i32 (ptr, ...) @printf_(ptr noundef nonnull @.str.1, i64 noundef %14, ptr noundef %0) #5
  br label %16

16:                                               ; preds = %13, %9
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %2) #5
  br label %23

.preheader:                                       ; preds = %.preheader.preheader, %.preheader
  %17 = phi ptr [ %21, %.preheader ], [ %5, %.preheader.preheader ]
  %18 = getelementptr inbounds i8, ptr %17, i64 32
  %19 = load i32, ptr %18, align 4, !tbaa !12
  %20 = tail call i32 (ptr, ...) @printf_(ptr noundef nonnull @.str.1, i32 noundef %19, ptr noundef nonnull %17) #5
  %21 = tail call ptr @readdir(ptr noundef nonnull %3) #5
  %.not13 = icmp eq ptr %21, null
  br i1 %.not13, label %.preheader._crit_edge, label %.preheader, !llvm.loop !14

.preheader._crit_edge:                            ; preds = %.preheader, %.preheader.preheader
  %22 = tail call i32 @closedir(ptr noundef nonnull %3)
  br label %23

23:                                               ; preds = %16, %.preheader._crit_edge
  ret void
}

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

; Function Attrs: nofree nounwind
declare noalias noundef ptr @opendir(ptr nocapture noundef readonly) local_unnamed_addr #2

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

declare i32 @printf_(ptr noundef, ...) local_unnamed_addr #3

; Function Attrs: nounwind
declare ptr @strerror(i32 noundef) local_unnamed_addr #4

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

declare ptr @readdir(ptr noundef) local_unnamed_addr #3

; Function Attrs: nofree nounwind
declare noundef i32 @closedir(ptr nocapture 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { "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 }

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


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass