; LLVM IR from ihucos/plash :: clean (remove_broken_links_here)

; 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.dirent = type { i64, i64, i16, i8, [256 x i8] }

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

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

7:                                                ; preds = %0
  %8 = call i32 (ptr, ...) @pl_fatal(ptr noundef @.str.1)
  br label %9

9:                                                ; preds = %7, %0
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  br label %10

10:                                               ; preds = %37, %9
  %11 = load ptr, ptr %2, align 8, !tbaa !9
  %12 = call ptr @readdir(ptr noundef %11)
  store ptr %12, ptr %3, align 8, !tbaa !9
  %13 = icmp ne ptr %12, null
  br i1 %13, label %14, label %38

14:                                               ; preds = %10
  %15 = load ptr, ptr %3, align 8, !tbaa !9
  %16 = getelementptr inbounds %struct.dirent, ptr %15, i32 0, i32 3
  %17 = load i8, ptr %16, align 2, !tbaa !11
  %18 = zext i8 %17 to i32
  %19 = icmp eq i32 %18, 10
  br i1 %19, label %20, label %37

20:                                               ; preds = %14
  %21 = load ptr, ptr %3, align 8, !tbaa !9
  %22 = getelementptr inbounds %struct.dirent, ptr %21, i32 0, i32 4
  %23 = getelementptr inbounds [256 x i8], ptr %22, i64 0, i64 0
  %24 = call i32 @access(ptr noundef %23, i32 noundef 0) #4
  %25 = icmp eq i32 %24, -1
  br i1 %25, label %26, label %36

26:                                               ; preds = %20
  %27 = load ptr, ptr %3, align 8, !tbaa !9
  %28 = getelementptr inbounds %struct.dirent, ptr %27, i32 0, i32 4
  %29 = getelementptr inbounds [256 x i8], ptr %28, i64 0, i64 0
  %30 = call i32 @unlink(ptr noundef %29) #4
  %31 = icmp ne i32 %30, -1
  br i1 %31, label %32, label %35

32:                                               ; preds = %26
  %33 = load i64, ptr %1, align 8, !tbaa !5
  %34 = add i64 %33, 1
  store i64 %34, ptr %1, align 8, !tbaa !5
  br label %35

35:                                               ; preds = %32, %26
  br label %36

36:                                               ; preds = %35, %20
  br label %37

37:                                               ; preds = %36, %14
  br label %10, !llvm.loop !14

38:                                               ; preds = %10
  %39 = load ptr, ptr %2, align 8, !tbaa !9
  %40 = call i32 @closedir(ptr noundef %39)
  %41 = load i64, ptr %1, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #4
  ret i64 %41
}

; 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 @pl_fatal(ptr noundef, ...) #2

declare ptr @readdir(ptr noundef) #2

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

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

declare i32 @closedir(ptr noundef) #2

; 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 "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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !7, i64 18}
!12 = !{!"dirent", !6, i64 0, !6, i64 8, !13, i64 16, !7, i64 18, !7, i64 19}
!13 = !{!"short", !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 [2 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [8 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i64 @remove_broken_links_here() local_unnamed_addr #0 {
  %1 = tail call ptr @opendir(ptr noundef nonnull @.str)
  %2 = icmp eq ptr %1, null
  br i1 %2, label %3, label %5

3:                                                ; preds = %0
  %4 = tail call i32 (ptr, ...) @pl_fatal(ptr noundef nonnull @.str.1) #3
  br label %5

5:                                                ; preds = %3, %0
  %6 = tail call ptr @readdir(ptr noundef %1) #3
  %.not9 = icmp eq ptr %6, null
  br i1 %.not9, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %5, %18
  %7 = phi ptr [ %19, %18 ], [ %6, %5 ]
  %.010 = phi i64 [ %.1, %18 ], [ 0, %5 ]
  %8 = getelementptr inbounds i8, ptr %7, i64 18
  %9 = load i8, ptr %8, align 2, !tbaa !5
  %10 = icmp eq i8 %9, 10
  br i1 %10, label %11, label %18

11:                                               ; preds = %.lr.ph
  %12 = getelementptr inbounds i8, ptr %7, i64 19
  %13 = tail call i32 @access(ptr noundef nonnull %12, i32 noundef 0) #3
  %14 = icmp eq i32 %13, -1
  br i1 %14, label %15, label %18

15:                                               ; preds = %11
  %16 = tail call i32 @unlink(ptr noundef nonnull %12) #3
  %.not8 = icmp ne i32 %16, -1
  %17 = zext i1 %.not8 to i64
  %spec.select = add i64 %.010, %17
  br label %18

18:                                               ; preds = %15, %11, %.lr.ph
  %.1 = phi i64 [ %.010, %11 ], [ %.010, %.lr.ph ], [ %spec.select, %15 ]
  %19 = tail call ptr @readdir(ptr noundef %1) #3
  %.not = icmp eq ptr %19, null
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !11

._crit_edge:                                      ; preds = %18, %5
  %.0.lcssa = phi i64 [ 0, %5 ], [ %.1, %18 ]
  %20 = tail call i32 @closedir(ptr noundef %1)
  ret i64 %.0.lcssa
}

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

declare i32 @pl_fatal(ptr noundef, ...) local_unnamed_addr #2

declare ptr @readdir(ptr noundef) local_unnamed_addr #2

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

; Function Attrs: nofree nounwind
declare noundef i32 @unlink(ptr nocapture noundef readonly) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i32 @closedir(ptr nocapture noundef) local_unnamed_addr #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 = { 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 #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 }

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


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