; LLVM IR from devicehive/esp8266-firmware :: esp-utils (esp-terminal)

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

@term = external dso_local global ptr, align 8
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@isWorking = external dso_local global i8, align 1
@.str.2 = external hidden unnamed_addr constant [2 x i8], align 1
@stdout = external global ptr, align 8
@.str.6 = external hidden unnamed_addr constant [68 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [46 x i8], align 1
@.str.8 = external hidden unnamed_addr constant [158 x i8], align 1
@.str.9 = external hidden unnamed_addr constant [36 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
declare dso_local noundef ptr @_Z10detectPortv() #1

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

declare noundef ptr @_ZN10SerialPort4openEPKc(ptr noundef) #2

declare void @_ZN10SerialPort4sendEPKc(ptr noundef nonnull align 8 dereferenceable(292), ptr noundef) #2

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main(i32 noundef %0, ptr noundef %1) #3 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca [5 x i8], align 1
  store i32 0, ptr %3, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !9
  %9 = load ptr, ptr @stdout, align 8, !tbaa !9
  call void @setbuf(ptr noundef %9, ptr noundef null) #6
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #6
  %10 = load i32, ptr %4, align 4, !tbaa !5
  %11 = icmp sgt i32 %10, 1
  br i1 %11, label %12, label %32

12:                                               ; preds = %2
  %13 = load i32, ptr %4, align 4, !tbaa !5
  %14 = icmp sgt i32 %13, 2
  br i1 %14, label %15, label %18

15:                                               ; preds = %12
  %16 = call i32 (ptr, ...) @printf(ptr noundef @.str.6)
  %17 = call i32 @getchar()
  store i32 1, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %79

18:                                               ; preds = %12
  %19 = load ptr, ptr %5, align 8, !tbaa !9
  %20 = getelementptr inbounds ptr, ptr %19, i64 1
  %21 = load ptr, ptr %20, align 8, !tbaa !9
  %22 = call noundef ptr @_ZN10SerialPort4openEPKc(ptr noundef %21)
  store ptr %22, ptr %6, align 8, !tbaa !9
  %23 = load ptr, ptr %6, align 8, !tbaa !9
  %24 = icmp ne ptr %23, null
  br i1 %24, label %31, label %25

25:                                               ; preds = %18
  %26 = load ptr, ptr %5, align 8, !tbaa !9
  %27 = getelementptr inbounds ptr, ptr %26, i64 1
  %28 = load ptr, ptr %27, align 8, !tbaa !9
  %29 = call i32 (ptr, ...) @printf(ptr noundef @.str.7, ptr noundef %28)
  %30 = call i32 @getchar()
  store i32 1, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %79

31:                                               ; preds = %18
  br label %40

32:                                               ; preds = %2
  %33 = call noundef ptr @_Z10detectPortv()
  store ptr %33, ptr %6, align 8, !tbaa !9
  %34 = load ptr, ptr %6, align 8, !tbaa !9
  %35 = icmp ne ptr %34, null
  br i1 %35, label %39, label %36

36:                                               ; preds = %32
  %37 = call i32 (ptr, ...) @printf(ptr noundef @.str.8, i32 noundef 20)
  %38 = call i32 @getchar()
  store i32 1, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %79

39:                                               ; preds = %32
  br label %40

40:                                               ; preds = %39, %31
  %41 = call i32 (ptr, ...) @printf(ptr noundef @.str.9)
  %42 = call noundef ptr @_ZN8Terminal4openEv()
  store ptr %42, ptr @term, align 8, !tbaa !9
  store i8 1, ptr @isWorking, align 1, !tbaa !11
  %43 = load ptr, ptr %6, align 8, !tbaa !9
  call void @_ZN10SerialPort4sendEPKc(ptr noundef nonnull align 8 dereferenceable(292) %43, ptr noundef @.str.2)
  br label %44

44:                                               ; preds = %63, %40
  br label %45

45:                                               ; preds = %44
  %46 = load ptr, ptr %6, align 8, !tbaa !9
  %47 = call noundef zeroext i1 @_ZN10SerialPort11isReadErrorEv(ptr noundef nonnull align 8 dereferenceable(292) %46)
  br i1 %47, label %48, label %49

48:                                               ; preds = %45
  br label %64

49:                                               ; preds = %45
  call void @llvm.lifetime.start.p0(i64 5, ptr %8) #6
  %50 = load ptr, ptr @term, align 8, !tbaa !9
  %51 = getelementptr inbounds [5 x i8], ptr %8, i64 0, i64 0
  call void @_ZN8Terminal3getEPc(ptr noundef nonnull align 8 dereferenceable(8) %50, ptr noundef %51)
  %52 = getelementptr inbounds [5 x i8], ptr %8, i64 0, i64 0
  %53 = load i8, ptr %52, align 1, !tbaa !13
  %54 = sext i8 %53 to i32
  %55 = icmp eq i32 %54, 17
  br i1 %55, label %56, label %58

56:                                               ; preds = %49
  %57 = call i32 (ptr, ...) @printf(ptr noundef @.str)
  store i32 3, ptr %7, align 4
  br label %61

58:                                               ; preds = %49
  %59 = load ptr, ptr %6, align 8, !tbaa !9
  %60 = getelementptr inbounds [5 x i8], ptr %8, i64 0, i64 0
  call void @_ZN10SerialPort4sendEPKc(ptr noundef nonnull align 8 dereferenceable(292) %59, ptr noundef %60)
  store i32 0, ptr %7, align 4
  br label %61

61:                                               ; preds = %58, %56
  call void @llvm.lifetime.end.p0(i64 5, ptr %8) #6
  %62 = load i32, ptr %7, align 4
  switch i32 %62, label %81 [
    i32 0, label %63
    i32 3, label %64
  ]

63:                                               ; preds = %61
  br label %44, !llvm.loop !14

64:                                               ; preds = %61, %48
  %65 = load ptr, ptr %6, align 8, !tbaa !9
  %66 = icmp eq ptr %65, null
  br i1 %66, label %71, label %67

67:                                               ; preds = %64
  %68 = load ptr, ptr %65, align 8, !tbaa !16
  %69 = getelementptr inbounds ptr, ptr %68, i64 1
  %70 = load ptr, ptr %69, align 8
  call void %70(ptr noundef nonnull align 8 dereferenceable(292) %65) #6
  br label %71

71:                                               ; preds = %67, %64
  %72 = load ptr, ptr @term, align 8, !tbaa !9
  %73 = icmp eq ptr %72, null
  br i1 %73, label %78, label %74

74:                                               ; preds = %71
  %75 = load ptr, ptr %72, align 8, !tbaa !16
  %76 = getelementptr inbounds ptr, ptr %75, i64 1
  %77 = load ptr, ptr %76, align 8
  call void %77(ptr noundef nonnull align 8 dereferenceable(8) %72) #6
  br label %78

78:                                               ; preds = %74, %71
  store i32 0, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %79

79:                                               ; preds = %78, %36, %25, %15
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #6
  %80 = load i32, ptr %3, align 4
  ret i32 %80

81:                                               ; preds = %61
  unreachable
}

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

; Function Attrs: inlinehint mustprogress uwtable
declare i32 @getchar() #5

declare noundef ptr @_ZN8Terminal4openEv() #2

declare noundef zeroext i1 @_ZN10SerialPort11isReadErrorEv(ptr noundef nonnull align 8 dereferenceable(292)) #2

declare void @_ZN8Terminal3getEPc(ptr noundef nonnull align 8 dereferenceable(8), 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 = { mustprogress norecurse 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 "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 = { inlinehint 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 #6 = { 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"bool", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!17, !17, i64 0}
!17 = !{!"vtable pointer", !8, i64 0}


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

@term = external dso_local local_unnamed_addr global ptr, align 8
@.str = external hidden unnamed_addr constant [3 x i8], align 1
@isWorking = external dso_local local_unnamed_addr global i8, align 1
@.str.2 = external hidden unnamed_addr constant [2 x i8], align 1
@stdout = external local_unnamed_addr global ptr, align 8
@.str.6 = external hidden unnamed_addr constant [68 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [46 x i8], align 1
@.str.8 = external hidden unnamed_addr constant [158 x i8], align 1
@.str.9 = external hidden unnamed_addr constant [36 x i8], align 1

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

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

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_Z10detectPortv() local_unnamed_addr #1

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

declare noundef ptr @_ZN10SerialPort4openEPKc(ptr noundef) local_unnamed_addr #3

declare void @_ZN10SerialPort4sendEPKc(ptr noundef nonnull align 8 dereferenceable(292), ptr noundef) local_unnamed_addr #3

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef range(i32 0, 2) i32 @main(i32 noundef %0, ptr nocapture noundef readonly %1) local_unnamed_addr #4 {
  %3 = alloca [5 x i8], align 1
  %4 = load ptr, ptr @stdout, align 8, !tbaa !5
  tail call void @setbuf(ptr noundef %4, ptr noundef null) #6
  %5 = icmp sgt i32 %0, 1
  br i1 %5, label %6, label %18

6:                                                ; preds = %2
  %.not17 = icmp eq i32 %0, 2
  br i1 %.not17, label %10, label %7

7:                                                ; preds = %6
  %8 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.6)
  %9 = tail call i32 @getchar()
  br label %42

10:                                               ; preds = %6
  %11 = getelementptr inbounds i8, ptr %1, i64 8
  %12 = load ptr, ptr %11, align 8, !tbaa !5
  %13 = tail call noundef ptr @_ZN10SerialPort4openEPKc(ptr noundef %12)
  %.not18 = icmp eq ptr %13, null
  br i1 %.not18, label %14, label %23

14:                                               ; preds = %10
  %15 = load ptr, ptr %11, align 8, !tbaa !5
  %16 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.7, ptr noundef %15)
  %17 = tail call i32 @getchar()
  br label %42

18:                                               ; preds = %2
  %19 = tail call noundef ptr @_Z10detectPortv()
  %.not = icmp eq ptr %19, null
  br i1 %.not, label %20, label %23

20:                                               ; preds = %18
  %21 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.8, i32 noundef 20)
  %22 = tail call i32 @getchar()
  br label %42

23:                                               ; preds = %18, %10
  %.011 = phi ptr [ %13, %10 ], [ %19, %18 ]
  %24 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.9)
  %25 = tail call noundef ptr @_ZN8Terminal4openEv()
  store ptr %25, ptr @term, align 8, !tbaa !5
  store i8 1, ptr @isWorking, align 1, !tbaa !9
  tail call void @_ZN10SerialPort4sendEPKc(ptr noundef nonnull align 8 dereferenceable(292) %.011, ptr noundef nonnull @.str.2)
  %26 = tail call noundef zeroext i1 @_ZN10SerialPort11isReadErrorEv(ptr noundef nonnull align 8 dereferenceable(292) %.011)
  br i1 %26, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %23, %31
  call void @llvm.lifetime.start.p0(i64 5, ptr nonnull %3) #6
  %27 = load ptr, ptr @term, align 8, !tbaa !5
  call void @_ZN8Terminal3getEPc(ptr noundef nonnull align 8 dereferenceable(8) %27, ptr noundef nonnull %3)
  %28 = load i8, ptr %3, align 1, !tbaa !11
  %29 = icmp eq i8 %28, 17
  br i1 %29, label %.thread, label %31

.thread:                                          ; preds = %.lr.ph
  %30 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str)
  call void @llvm.lifetime.end.p0(i64 5, ptr nonnull %3) #6
  br label %.loopexit

31:                                               ; preds = %.lr.ph
  call void @_ZN10SerialPort4sendEPKc(ptr noundef nonnull align 8 dereferenceable(292) %.011, ptr noundef nonnull %3)
  call void @llvm.lifetime.end.p0(i64 5, ptr nonnull %3) #6
  %32 = call noundef zeroext i1 @_ZN10SerialPort11isReadErrorEv(ptr noundef nonnull align 8 dereferenceable(292) %.011)
  br i1 %32, label %.loopexit, label %.lr.ph

.loopexit:                                        ; preds = %31, %23, %.thread
  %33 = load ptr, ptr %.011, align 8, !tbaa !12
  %34 = getelementptr inbounds i8, ptr %33, i64 8
  %35 = load ptr, ptr %34, align 8
  call void %35(ptr noundef nonnull align 8 dereferenceable(292) %.011) #6
  %36 = load ptr, ptr @term, align 8, !tbaa !5
  %37 = icmp eq ptr %36, null
  br i1 %37, label %42, label %38

38:                                               ; preds = %.loopexit
  %39 = load ptr, ptr %36, align 8, !tbaa !12
  %40 = getelementptr inbounds i8, ptr %39, i64 8
  %41 = load ptr, ptr %40, align 8
  call void %41(ptr noundef nonnull align 8 dereferenceable(8) %36) #6
  br label %42

42:                                               ; preds = %.loopexit, %38, %20, %14, %7
  %.012 = phi i32 [ 1, %7 ], [ 1, %14 ], [ 1, %20 ], [ 0, %38 ], [ 0, %.loopexit ]
  ret i32 %.012
}

; Function Attrs: nofree nounwind
declare void @setbuf(ptr nocapture noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: inlinehint mustprogress nofree nounwind uwtable
declare noundef i32 @getchar() local_unnamed_addr #5

declare noundef ptr @_ZN8Terminal4openEv() local_unnamed_addr #3

declare noundef zeroext i1 @_ZN10SerialPort11isReadErrorEv(ptr noundef nonnull align 8 dereferenceable(292)) local_unnamed_addr #3

declare void @_ZN8Terminal3getEPc(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef) local_unnamed_addr #3

attributes #0 = { mustprogress 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 = { 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 = { mustprogress norecurse 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 = { inlinehint mustprogress 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 #6 = { 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 = !{!"bool", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"vtable pointer", !8, i64 0}


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