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

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

%class.SerialPort = type <{ ptr, i32, i8, i8, [2 x i8], ptr, i32, i8, [3 x i8], i32, [256 x i8], [4 x i8] }>

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

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_ZN10SerialPort12thread_startEPv(ptr noundef %0) #0 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca [1024 x i8], align 16
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i8, align 1
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  %10 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %10, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  %11 = load ptr, ptr %3, align 8, !tbaa !5
  %12 = call noundef i32 @_ZN10SerialPort7get_comEv(ptr noundef nonnull align 8 dereferenceable(292) %11)
  store i32 %12, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 1024, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1024, ptr %6) #4
  br label %13

13:                                               ; preds = %74, %1
  br label %14

14:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  %15 = load i32, ptr %4, align 4, !tbaa !9
  %16 = getelementptr inbounds [1024 x i8], ptr %6, i64 0, i64 0
  %17 = call i64 @read(i32 noundef %15, ptr noundef %16, i64 noundef 1024)
  %18 = trunc i64 %17 to i32
  store i32 %18, ptr %7, align 4, !tbaa !9
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  %20 = getelementptr inbounds %class.SerialPort, ptr %19, i32 0, i32 3
  store i8 1, ptr %20, align 1, !tbaa !11
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = getelementptr inbounds %class.SerialPort, ptr %21, i32 0, i32 2
  %23 = load i8, ptr %22, align 4, !tbaa !14, !range !15, !noundef !16
  %24 = trunc i8 %23 to i1
  br i1 %24, label %26, label %25

25:                                               ; preds = %14
  store i32 3, ptr %8, align 4
  br label %72

26:                                               ; preds = %14
  %27 = load i32, ptr %7, align 4, !tbaa !9
  %28 = icmp sgt i32 %27, 0
  br i1 %28, label %29, label %47

29:                                               ; preds = %26
  %30 = load ptr, ptr %3, align 8, !tbaa !5
  %31 = call noundef i32 @_ZN10SerialPort7getTickEv()
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = getelementptr inbounds %class.SerialPort, ptr %32, i32 0, i32 9
  store i32 %31, ptr %33, align 8, !tbaa !17
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = getelementptr inbounds %class.SerialPort, ptr %34, i32 0, i32 7
  store i8 0, ptr %35, align 4, !tbaa !18
  %36 = load i32, ptr %7, align 4, !tbaa !9
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [1024 x i8], ptr %6, i64 0, i64 %37
  store i8 0, ptr %38, align 1, !tbaa !19
  %39 = load ptr, ptr %3, align 8, !tbaa !5
  %40 = getelementptr inbounds [1024 x i8], ptr %6, i64 0, i64 0
  %41 = load i32, ptr %7, align 4, !tbaa !9
  call void @_Z18SerialPortRecievedP10SerialPortPKcj(ptr noundef %39, ptr noundef %40, i32 noundef %41)
  %42 = load i32, ptr %7, align 4, !tbaa !9
  %43 = load ptr, ptr %3, align 8, !tbaa !5
  %44 = getelementptr inbounds %class.SerialPort, ptr %43, i32 0, i32 6
  %45 = load i32, ptr %44, align 8, !tbaa !20
  %46 = add i32 %45, %42
  store i32 %46, ptr %44, align 8, !tbaa !20
  br label %71

47:                                               ; preds = %26
  %48 = load i32, ptr %7, align 4, !tbaa !9
  %49 = icmp eq i32 %48, 0
  br i1 %49, label %50, label %66

50:                                               ; preds = %47
  call void @llvm.lifetime.start.p0(i64 1, ptr %9) #4
  %51 = load ptr, ptr %3, align 8, !tbaa !5
  %52 = getelementptr inbounds %class.SerialPort, ptr %51, i32 0, i32 7
  %53 = load i8, ptr %52, align 4, !tbaa !18, !range !15, !noundef !16
  %54 = trunc i8 %53 to i1
  %55 = zext i1 %54 to i8
  store i8 %55, ptr %9, align 1, !tbaa !21
  %56 = load ptr, ptr %3, align 8, !tbaa !5
  %57 = getelementptr inbounds %class.SerialPort, ptr %56, i32 0, i32 7
  store i8 1, ptr %57, align 4, !tbaa !18
  %58 = load i8, ptr %9, align 1, !tbaa !21, !range !15, !noundef !16
  %59 = trunc i8 %58 to i1
  %60 = zext i1 %59 to i32
  %61 = icmp eq i32 %60, 0
  br i1 %61, label %62, label %64

62:                                               ; preds = %50
  %63 = load ptr, ptr %3, align 8, !tbaa !5
  call void @_Z15SerialPortErrorP10SerialPortPKc(ptr noundef %63, ptr noundef @.str)
  br label %64

64:                                               ; preds = %62, %50
  %65 = load ptr, ptr %3, align 8, !tbaa !5
  call void @_ZN10SerialPort5sleepEj(ptr noundef nonnull align 8 dereferenceable(292) %65, i32 noundef 10)
  call void @llvm.lifetime.end.p0(i64 1, ptr %9) #4
  br label %70

66:                                               ; preds = %47
  %67 = load ptr, ptr %3, align 8, !tbaa !5
  %68 = getelementptr inbounds %class.SerialPort, ptr %67, i32 0, i32 7
  store i8 0, ptr %68, align 4, !tbaa !18
  %69 = load ptr, ptr %3, align 8, !tbaa !5
  call void @_ZN10SerialPort5sleepEj(ptr noundef nonnull align 8 dereferenceable(292) %69, i32 noundef 10)
  br label %70

70:                                               ; preds = %66, %64
  br label %71

71:                                               ; preds = %70, %29
  store i32 0, ptr %8, align 4
  br label %72

72:                                               ; preds = %71, %25
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  %73 = load i32, ptr %8, align 4
  switch i32 %73, label %76 [
    i32 0, label %74
    i32 3, label %75
  ]

74:                                               ; preds = %72
  br label %13, !llvm.loop !22

75:                                               ; preds = %72
  store i32 1, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 1024, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  ret ptr null

76:                                               ; preds = %72
  unreachable
}

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

declare noundef i32 @_ZN10SerialPort7get_comEv(ptr noundef nonnull align 8 dereferenceable(292)) #2

declare i64 @read(i32 noundef, ptr noundef, i64 noundef) #2

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZN10SerialPort7getTickEv() #3 align 2

declare void @_Z18SerialPortRecievedP10SerialPortPKcj(ptr noundef, ptr noundef, i32 noundef) #2

declare void @_Z15SerialPortErrorP10SerialPortPKc(ptr noundef, ptr noundef) #2

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN10SerialPort5sleepEj(ptr noundef nonnull align 8 dereferenceable(292), i32 noundef) #0 align 2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { 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 = { mustprogress 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 #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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !13, i64 13}
!12 = !{!"_ZTS10SerialPort", !10, i64 8, !13, i64 12, !13, i64 13, !6, i64 16, !10, i64 24, !13, i64 28, !10, i64 32, !7, i64 36}
!13 = !{!"bool", !7, i64 0}
!14 = !{!12, !13, i64 12}
!15 = !{i8 0, i8 2}
!16 = !{}
!17 = !{!12, !10, i64 32}
!18 = !{!12, !13, i64 28}
!19 = !{!7, !7, i64 0}
!20 = !{!12, !10, i64 24}
!21 = !{!13, !13, i64 0}
!22 = distinct !{!22, !23}
!23 = !{!"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 [33 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local noalias noundef ptr @_ZN10SerialPort12thread_startEPv(ptr noundef nonnull %0) local_unnamed_addr #0 align 2 {
  %2 = alloca [1024 x i8], align 16
  %3 = tail call noundef i32 @_ZN10SerialPort7get_comEv(ptr noundef nonnull align 8 dereferenceable(292) %0)
  call void @llvm.lifetime.start.p0(i64 1024, ptr nonnull %2) #5
  %4 = call i64 @read(i32 noundef %3, ptr noundef nonnull %2, i64 noundef 1024)
  %5 = getelementptr inbounds i8, ptr %0, i64 13
  store i8 1, ptr %5, align 1, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %0, i64 12
  %7 = load i8, ptr %6, align 4, !tbaa !12, !range !13, !noundef !14
  %8 = trunc nuw i8 %7 to i1
  br i1 %8, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %1
  %9 = getelementptr inbounds i8, ptr %0, i64 28
  %10 = getelementptr inbounds i8, ptr %0, i64 32
  %11 = getelementptr inbounds i8, ptr %0, i64 24
  br label %12

12:                                               ; preds = %.lr.ph, %30
  %13 = phi i64 [ %4, %.lr.ph ], [ %31, %30 ]
  %14 = trunc i64 %13 to i32
  %15 = icmp sgt i32 %14, 0
  br i1 %15, label %16, label %22

16:                                               ; preds = %12
  %17 = call noundef i32 @_ZN10SerialPort7getTickEv()
  store i32 %17, ptr %10, align 8, !tbaa !15
  store i8 0, ptr %9, align 4, !tbaa !16
  %18 = and i64 %13, 2147483647
  %19 = getelementptr inbounds [1024 x i8], ptr %2, i64 0, i64 %18
  store i8 0, ptr %19, align 1, !tbaa !17
  call void @_Z18SerialPortRecievedP10SerialPortPKcj(ptr noundef nonnull %0, ptr noundef nonnull %2, i32 noundef %14)
  %20 = load i32, ptr %11, align 8, !tbaa !18
  %21 = add i32 %20, %14
  store i32 %21, ptr %11, align 8, !tbaa !18
  br label %30

22:                                               ; preds = %12
  %23 = icmp eq i32 %14, 0
  br i1 %23, label %24, label %29

24:                                               ; preds = %22
  %25 = load i8, ptr %9, align 4, !tbaa !16, !range !13, !noundef !14
  store i8 1, ptr %9, align 4, !tbaa !16
  %26 = icmp eq i8 %25, 0
  br i1 %26, label %27, label %28

27:                                               ; preds = %24
  call void @_Z15SerialPortErrorP10SerialPortPKc(ptr noundef nonnull %0, ptr noundef nonnull @.str)
  br label %28

28:                                               ; preds = %27, %24
  call void @_ZN10SerialPort5sleepEj(ptr noundef nonnull align 8 dereferenceable(292) %0, i32 noundef 10)
  br label %30

29:                                               ; preds = %22
  store i8 0, ptr %9, align 4, !tbaa !16
  call void @_ZN10SerialPort5sleepEj(ptr noundef nonnull align 8 dereferenceable(292) %0, i32 noundef 10)
  br label %30

30:                                               ; preds = %16, %29, %28
  %31 = call i64 @read(i32 noundef %3, ptr noundef nonnull %2, i64 noundef 1024)
  store i8 1, ptr %5, align 1, !tbaa !5
  %32 = load i8, ptr %6, align 4, !tbaa !12, !range !13, !noundef !14
  %33 = trunc nuw i8 %32 to i1
  br i1 %33, label %12, label %._crit_edge

._crit_edge:                                      ; preds = %30, %1
  call void @llvm.lifetime.end.p0(i64 1024, ptr nonnull %2) #5
  ret ptr null
}

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

declare noundef i32 @_ZN10SerialPort7get_comEv(ptr noundef nonnull align 8 dereferenceable(292)) local_unnamed_addr #2

; Function Attrs: nofree
declare noundef i64 @read(i32 noundef, ptr nocapture noundef, i64 noundef) local_unnamed_addr #3

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZN10SerialPort7getTickEv() local_unnamed_addr #4 align 2

declare void @_Z18SerialPortRecievedP10SerialPortPKcj(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

declare void @_Z15SerialPortErrorP10SerialPortPKc(ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN10SerialPort5sleepEj(ptr noundef nonnull align 8 dereferenceable(292), i32 noundef) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { mustprogress 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 = { nofree "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 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 #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, !10, i64 13}
!6 = !{!"_ZTS10SerialPort", !7, i64 8, !10, i64 12, !10, i64 13, !11, i64 16, !7, i64 24, !10, i64 28, !7, i64 32, !8, i64 36}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"bool", !8, i64 0}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!6, !10, i64 12}
!13 = !{i8 0, i8 2}
!14 = !{}
!15 = !{!6, !7, i64 32}
!16 = !{!6, !10, i64 28}
!17 = !{!8, !8, i64 0}
!18 = !{!6, !7, i64 24}


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