; LLVM IR from devicehive/esp8266-firmware :: esp-utils/common/serialport_posix.ll (ZN10SerialPort12findNextPortEb)

; 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] }

@_ZZN10SerialPort12findNextPortEbE3buf = external hidden global [512 x i8], align 16
@_ZZN10SerialPort12findNextPortEbE2dp = external hidden global ptr, align 8
@.str.1 = external hidden unnamed_addr constant [5 x i8], align 1
@_ZL14TTYUSB_PATTERN = external hidden constant [7 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [6 x i8], align 1

; 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: mustprogress uwtable
define dso_local noundef ptr @_ZN10SerialPort12findNextPortEb(i1 noundef zeroext %0) #1 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca i8, align 1
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = zext i1 %0 to i8
  store i8 %6, ptr %3, align 1, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #5
  store i8 0, ptr @_ZZN10SerialPort12findNextPortEbE3buf, align 16, !tbaa !9
  %7 = load ptr, ptr @_ZZN10SerialPort12findNextPortEbE2dp, align 8, !tbaa !10
  %8 = icmp ne ptr %7, null
  br i1 %8, label %15, label %9

9:                                                ; preds = %1
  %10 = call ptr @opendir(ptr noundef @.str.1)
  store ptr %10, ptr @_ZZN10SerialPort12findNextPortEbE2dp, align 8, !tbaa !10
  %11 = load ptr, ptr @_ZZN10SerialPort12findNextPortEbE2dp, align 8, !tbaa !10
  %12 = icmp ne ptr %11, null
  br i1 %12, label %14, label %13

13:                                               ; preds = %9
  store ptr @_ZZN10SerialPort12findNextPortEbE3buf, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %47

14:                                               ; preds = %9
  br label %15

15:                                               ; preds = %14, %1
  %16 = load ptr, ptr @_ZZN10SerialPort12findNextPortEbE2dp, align 8, !tbaa !10
  %17 = icmp ne ptr %16, null
  br i1 %17, label %18, label %36

18:                                               ; preds = %15
  br label %19

19:                                               ; preds = %34, %18
  %20 = load ptr, ptr @_ZZN10SerialPort12findNextPortEbE2dp, align 8, !tbaa !10
  %21 = call ptr @readdir(ptr noundef %20)
  store ptr %21, ptr %4, align 8, !tbaa !10
  %22 = icmp ne ptr %21, null
  br i1 %22, label %23, label %35

23:                                               ; preds = %19
  %24 = load ptr, ptr %4, align 8, !tbaa !10
  %25 = getelementptr inbounds %struct.dirent, ptr %24, i32 0, i32 4
  %26 = getelementptr inbounds [256 x i8], ptr %25, i64 0, i64 0
  %27 = call i32 @strncmp(ptr noundef %26, ptr noundef @_ZL14TTYUSB_PATTERN, i64 noundef 6) #6
  %28 = icmp eq i32 %27, 0
  br i1 %28, label %29, label %34

29:                                               ; preds = %23
  %30 = load ptr, ptr %4, align 8, !tbaa !10
  %31 = getelementptr inbounds %struct.dirent, ptr %30, i32 0, i32 4
  %32 = getelementptr inbounds [256 x i8], ptr %31, i64 0, i64 0
  %33 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef @_ZZN10SerialPort12findNextPortEbE3buf, i64 noundef 512, ptr noundef @.str.2, ptr noundef @.str.1, ptr noundef %32) #5
  br label %35

34:                                               ; preds = %23
  br label %19, !llvm.loop !12

35:                                               ; preds = %29, %19
  br label %36

36:                                               ; preds = %35, %15
  %37 = load i8, ptr %3, align 1, !tbaa !5, !range !14, !noundef !15
  %38 = trunc i8 %37 to i1
  br i1 %38, label %39, label %46

39:                                               ; preds = %36
  %40 = load ptr, ptr @_ZZN10SerialPort12findNextPortEbE2dp, align 8, !tbaa !10
  %41 = icmp ne ptr %40, null
  br i1 %41, label %42, label %45

42:                                               ; preds = %39
  %43 = load ptr, ptr @_ZZN10SerialPort12findNextPortEbE2dp, align 8, !tbaa !10
  %44 = call i32 @closedir(ptr noundef %43)
  store ptr null, ptr @_ZZN10SerialPort12findNextPortEbE2dp, align 8, !tbaa !10
  br label %45

45:                                               ; preds = %42, %39
  br label %46

46:                                               ; preds = %45, %36
  store ptr @_ZZN10SerialPort12findNextPortEbE3buf, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %47

47:                                               ; preds = %46, %13
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #5
  %48 = load ptr, ptr %2, align 8
  ret ptr %48
}

declare ptr @opendir(ptr noundef) #2

declare ptr @readdir(ptr noundef) #2

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

; Function Attrs: nounwind
declare i32 @snprintf(ptr noundef, i64 noundef, ptr noundef, ...) #4

declare i32 @closedir(ptr noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 = { "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 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 = !{!"bool", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"any pointer", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{i8 0, i8 2}
!15 = !{}


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

@_ZZN10SerialPort12findNextPortEbE3buf = external hidden global [512 x i8], align 16
@_ZZN10SerialPort12findNextPortEbE2dp = external hidden local_unnamed_addr global ptr, align 8
@.str.1 = external hidden unnamed_addr constant [5 x i8], align 1
@_ZL14TTYUSB_PATTERN = external hidden constant [7 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [6 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local noundef nonnull ptr @_ZN10SerialPort12findNextPortEb(i1 noundef zeroext %0) local_unnamed_addr #0 align 2 {
  store i8 0, ptr @_ZZN10SerialPort12findNextPortEbE3buf, align 16, !tbaa !5
  %2 = load ptr, ptr @_ZZN10SerialPort12findNextPortEbE2dp, align 8, !tbaa !8
  %.not = icmp eq ptr %2, null
  br i1 %.not, label %3, label %.preheader

3:                                                ; preds = %1
  %4 = tail call ptr @opendir(ptr noundef nonnull @.str.1)
  store ptr %4, ptr @_ZZN10SerialPort12findNextPortEbE2dp, align 8, !tbaa !8
  %.not5 = icmp eq ptr %4, null
  br i1 %.not5, label %18, label %.preheader

.preheader:                                       ; preds = %3, %1
  br label %5

5:                                                ; preds = %.preheader, %8
  %6 = load ptr, ptr @_ZZN10SerialPort12findNextPortEbE2dp, align 8, !tbaa !8
  %7 = tail call ptr @readdir(ptr noundef %6)
  %.not7 = icmp eq ptr %7, null
  br i1 %.not7, label %.loopexit, label %8

8:                                                ; preds = %5
  %9 = getelementptr inbounds i8, ptr %7, i64 19
  %10 = tail call i32 @strncmp(ptr noundef nonnull dereferenceable(1) %9, ptr noundef nonnull dereferenceable(1) @_ZL14TTYUSB_PATTERN, i64 noundef 6) #4
  %11 = icmp eq i32 %10, 0
  br i1 %11, label %12, label %5, !llvm.loop !10

12:                                               ; preds = %8
  %13 = tail call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef nonnull dereferenceable(1) @_ZZN10SerialPort12findNextPortEbE3buf, i64 noundef 512, ptr noundef nonnull @.str.2, ptr noundef nonnull @.str.1, ptr noundef nonnull %9) #5
  br label %.loopexit

.loopexit:                                        ; preds = %5, %12
  %14 = load ptr, ptr @_ZZN10SerialPort12findNextPortEbE2dp, align 8
  %15 = icmp ne ptr %14, null
  %or.cond = select i1 %0, i1 %15, i1 false
  br i1 %or.cond, label %16, label %18

16:                                               ; preds = %.loopexit
  %17 = tail call i32 @closedir(ptr noundef nonnull %14)
  store ptr null, ptr @_ZZN10SerialPort12findNextPortEbE2dp, align 8, !tbaa !8
  br label %18

18:                                               ; preds = %.loopexit, %16, %3
  ret ptr @_ZZN10SerialPort12findNextPortEbE3buf
}

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

declare ptr @readdir(ptr noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i32 @strncmp(ptr nocapture noundef, ptr nocapture noundef, i64 noundef) local_unnamed_addr #3

; Function Attrs: nofree nounwind
declare noundef i32 @snprintf(ptr noalias nocapture noundef writeonly, i64 noundef, 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 = { mustprogress 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 = { 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 #4 = { nounwind willreturn memory(read) }
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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"any pointer", !6, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


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