; LLVM IR from dynexcoin/Dynex :: src/Common/ConsoleHandler.ll (ZN6Common18AsyncConsoleReader9waitInputEv)

; 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.fd_set = type { [16 x i64] }
%struct.timeval = type { i64, i64 }
%"class.Common::AsyncConsoleReader" = type { %"struct.std::atomic", %"class.std::thread", %class.BlockingQueue }
%"struct.std::atomic" = type { %"struct.std::__atomic_base" }
%"struct.std::__atomic_base" = type { i8 }
%"class.std::thread" = type { %"class.std::thread::id" }
%"class.std::thread::id" = type { i64 }
%class.BlockingQueue = type { i64, %"class.std::deque", i8, %"class.std::mutex", %"class.std::condition_variable", %"class.std::condition_variable" }
%"class.std::deque" = type { %"class.std::_Deque_base" }
%"class.std::_Deque_base" = type { %"struct.std::_Deque_base<std::__cxx11::basic_string<char>, std::allocator<std::__cxx11::basic_string<char>>>::_Deque_impl" }
%"struct.std::_Deque_base<std::__cxx11::basic_string<char>, std::allocator<std::__cxx11::basic_string<char>>>::_Deque_impl" = type { %"struct.std::_Deque_base<std::__cxx11::basic_string<char>, std::allocator<std::__cxx11::basic_string<char>>>::_Deque_impl_data" }
%"struct.std::_Deque_base<std::__cxx11::basic_string<char>, std::allocator<std::__cxx11::basic_string<char>>>::_Deque_impl_data" = type { ptr, i64, %"struct.std::_Deque_iterator", %"struct.std::_Deque_iterator" }
%"struct.std::_Deque_iterator" = type { ptr, ptr, ptr, ptr }
%"class.std::mutex" = type { %"class.std::__mutex_base" }
%"class.std::__mutex_base" = type { %union.pthread_mutex_t }
%union.pthread_mutex_t = type { %struct.__pthread_mutex_s }
%struct.__pthread_mutex_s = type { i32, i32, i32, i32, i32, i16, i16, %struct.__pthread_internal_list }
%struct.__pthread_internal_list = type { ptr, ptr }
%"class.std::condition_variable" = type { %"class.std::__condvar" }
%"class.std::__condvar" = type { %union.pthread_cond_t }
%union.pthread_cond_t = type { %struct.__pthread_cond_s }
%struct.__pthread_cond_s = type { %union.__atomic_wide_counter, %union.__atomic_wide_counter, [2 x i32], [2 x i32], i32, i32, [2 x i32] }
%union.__atomic_wide_counter = type { i64 }

@stdin = external global ptr, align 8

; 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 nounwind uwtable
declare dso_local noundef zeroext i1 @_ZNKSt6atomicIbEcvbEv(ptr noundef nonnull align 1 dereferenceable(1)) #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_ZN6Common18AsyncConsoleReader9waitInputEv(ptr noundef nonnull align 8 dereferenceable(248) %0) #2 align 2 {
  %2 = alloca i1, align 1
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca %struct.fd_set, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca %struct.timeval, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  %11 = load ptr, ptr %3, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #6
  %12 = load ptr, ptr @stdin, align 8, !tbaa !5
  %13 = call i32 @fileno(ptr noundef %12) #6
  store i32 %13, ptr %4, align 4, !tbaa !9
  br label %14

14:                                               ; preds = %70, %68, %1
  %15 = getelementptr inbounds %"class.Common::AsyncConsoleReader", ptr %11, i32 0, i32 0
  %16 = call noundef zeroext i1 @_ZNKSt6atomicIbEcvbEv(ptr noundef nonnull align 1 dereferenceable(1) %15) #6
  %17 = xor i1 %16, true
  br i1 %17, label %18, label %71

18:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 128, ptr %5) #6
  br label %19

19:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #6
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #6
  store ptr %5, ptr %7, align 8, !tbaa !5
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %20

20:                                               ; preds = %30, %19
  %21 = load i32, ptr %6, align 4, !tbaa !9
  %22 = zext i32 %21 to i64
  %23 = icmp ult i64 %22, 16
  br i1 %23, label %24, label %33

24:                                               ; preds = %20
  %25 = load ptr, ptr %7, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.fd_set, ptr %25, i32 0, i32 0
  %27 = load i32, ptr %6, align 4, !tbaa !9
  %28 = zext i32 %27 to i64
  %29 = getelementptr inbounds [16 x i64], ptr %26, i64 0, i64 %28
  store i64 0, ptr %29, align 8, !tbaa !11
  br label %30

30:                                               ; preds = %24
  %31 = load i32, ptr %6, align 4, !tbaa !9
  %32 = add i32 %31, 1
  store i32 %32, ptr %6, align 4, !tbaa !9
  br label %20, !llvm.loop !13

33:                                               ; preds = %20
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #6
  br label %34

34:                                               ; preds = %33
  br label %35

35:                                               ; preds = %34
  %36 = load i32, ptr %4, align 4, !tbaa !9
  %37 = srem i32 %36, 64
  %38 = zext i32 %37 to i64
  %39 = shl i64 1, %38
  %40 = getelementptr inbounds %struct.fd_set, ptr %5, i32 0, i32 0
  %41 = load i32, ptr %4, align 4, !tbaa !9
  %42 = sdiv i32 %41, 64
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [16 x i64], ptr %40, i64 0, i64 %43
  %45 = load i64, ptr %44, align 8, !tbaa !11
  %46 = or i64 %45, %39
  store i64 %46, ptr %44, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 16, ptr %8) #6
  %47 = getelementptr inbounds %struct.timeval, ptr %8, i32 0, i32 0
  store i64 0, ptr %47, align 8, !tbaa !15
  %48 = getelementptr inbounds %struct.timeval, ptr %8, i32 0, i32 1
  store i64 100000, ptr %48, align 8, !tbaa !17
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #6
  %49 = load i32, ptr %4, align 4, !tbaa !9
  %50 = add nsw i32 %49, 1
  %51 = call i32 @select(i32 noundef %50, ptr noundef %5, ptr noundef null, ptr noundef null, ptr noundef %8)
  store i32 %51, ptr %9, align 4, !tbaa !9
  %52 = load i32, ptr %9, align 4, !tbaa !9
  %53 = icmp eq i32 %52, -1
  br i1 %53, label %54, label %59

54:                                               ; preds = %35
  %55 = call ptr @__errno_location() #7
  %56 = load i32, ptr %55, align 4, !tbaa !9
  %57 = icmp eq i32 %56, 4
  br i1 %57, label %58, label %59

58:                                               ; preds = %54
  store i32 2, ptr %10, align 4
  br label %68, !llvm.loop !18

59:                                               ; preds = %54, %35
  %60 = load i32, ptr %9, align 4, !tbaa !9
  %61 = icmp slt i32 %60, 0
  br i1 %61, label %62, label %63

62:                                               ; preds = %59
  store i1 false, ptr %2, align 1
  store i32 1, ptr %10, align 4
  br label %68

63:                                               ; preds = %59
  %64 = load i32, ptr %9, align 4, !tbaa !9
  %65 = icmp sgt i32 %64, 0
  br i1 %65, label %66, label %67

66:                                               ; preds = %63
  store i1 true, ptr %2, align 1
  store i32 1, ptr %10, align 4
  br label %68

67:                                               ; preds = %63
  store i32 0, ptr %10, align 4
  br label %68

68:                                               ; preds = %67, %66, %62, %58
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #6
  call void @llvm.lifetime.end.p0(i64 16, ptr %8) #6
  call void @llvm.lifetime.end.p0(i64 128, ptr %5) #6
  %69 = load i32, ptr %10, align 4
  switch i32 %69, label %75 [
    i32 0, label %70
    i32 2, label %14
  ]

70:                                               ; preds = %68
  br label %14, !llvm.loop !18

71:                                               ; preds = %14
  %72 = getelementptr inbounds %"class.Common::AsyncConsoleReader", ptr %11, i32 0, i32 0
  %73 = call noundef zeroext i1 @_ZNKSt6atomicIbEcvbEv(ptr noundef nonnull align 1 dereferenceable(1) %72) #6
  %74 = xor i1 %73, true
  store i1 %74, ptr %2, align 1
  store i32 1, ptr %10, align 4
  br label %75

75:                                               ; preds = %71, %68
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #6
  %76 = load i1, ptr %2, align 1
  ret i1 %76
}

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

declare i32 @select(i32 noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef) #4

; Function Attrs: nounwind willreturn memory(none)
declare ptr @__errno_location() #5

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 #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 = { "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 willreturn memory(none) "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 }
attributes #7 = { nounwind willreturn memory(none) }

!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, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!16, !12, i64 0}
!16 = !{!"_ZTS7timeval", !12, i64 0, !12, i64 8}
!17 = !{!16, !12, i64 8}
!18 = distinct !{!18, !14}


; 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.fd_set = type { [16 x i64] }
%struct.timeval = type { i64, i64 }

@stdin = external local_unnamed_addr global ptr, align 8

; 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 nounwind uwtable
declare dso_local noundef zeroext i1 @_ZNKSt6atomicIbEcvbEv(ptr noundef nonnull align 1 dereferenceable(1)) local_unnamed_addr #1 align 2

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_ZN6Common18AsyncConsoleReader9waitInputEv(ptr noundef nonnull align 8 dereferenceable(248) %0) local_unnamed_addr #2 align 2 {
  %2 = alloca %struct.fd_set, align 8
  %3 = alloca %struct.timeval, align 8
  %4 = load ptr, ptr @stdin, align 8, !tbaa !5
  %5 = tail call i32 @fileno(ptr noundef %4) #7
  %6 = tail call noundef zeroext i1 @_ZNKSt6atomicIbEcvbEv(ptr noundef nonnull align 1 dereferenceable(1) %0) #7
  br i1 %6, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %1
  %7 = srem i32 %5, 64
  %8 = zext nneg i32 %7 to i64
  %9 = shl nuw i64 1, %8
  %10 = sdiv i32 %5, 64
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [16 x i64], ptr %2, i64 0, i64 %11
  %13 = getelementptr inbounds i8, ptr %3, i64 8
  %14 = add nsw i32 %5, 1
  br label %15

15:                                               ; preds = %.lr.ph, %select.unfold
  call void @llvm.lifetime.start.p0(i64 128, ptr nonnull %2) #7
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(128) %2, i8 0, i64 128, i1 false), !tbaa !9
  %16 = load i64, ptr %12, align 8, !tbaa !9
  %17 = or i64 %16, %9
  store i64 %17, ptr %12, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %3) #7
  store i64 0, ptr %3, align 8, !tbaa !11
  store i64 100000, ptr %13, align 8, !tbaa !13
  %18 = call i32 @select(i32 noundef %14, ptr noundef nonnull %2, ptr noundef null, ptr noundef null, ptr noundef nonnull %3)
  %19 = icmp eq i32 %18, -1
  br i1 %19, label %20, label %24

20:                                               ; preds = %15
  %21 = tail call ptr @__errno_location() #8
  %22 = load i32, ptr %21, align 4, !tbaa !14
  %23 = icmp eq i32 %22, 4
  br i1 %23, label %select.unfold, label %.thread16, !llvm.loop !16

24:                                               ; preds = %15
  %25 = icmp slt i32 %18, 0
  br i1 %25, label %.thread16, label %26

26:                                               ; preds = %24
  %.not = icmp eq i32 %18, 0
  br i1 %.not, label %select.unfold, label %.thread16

.thread16:                                        ; preds = %24, %26, %20
  %.1.ph = phi i1 [ true, %26 ], [ false, %24 ], [ false, %20 ]
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %3) #7
  call void @llvm.lifetime.end.p0(i64 128, ptr nonnull %2) #7
  br label %30

select.unfold:                                    ; preds = %26, %20
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %3) #7
  call void @llvm.lifetime.end.p0(i64 128, ptr nonnull %2) #7
  %27 = call noundef zeroext i1 @_ZNKSt6atomicIbEcvbEv(ptr noundef nonnull align 1 dereferenceable(1) %0) #7
  br i1 %27, label %._crit_edge, label %15

._crit_edge:                                      ; preds = %select.unfold, %1
  %28 = call noundef zeroext i1 @_ZNKSt6atomicIbEcvbEv(ptr noundef nonnull align 1 dereferenceable(1) %0) #7
  %29 = xor i1 %28, true
  br label %30

30:                                               ; preds = %.thread16, %._crit_edge
  %.2 = phi i1 [ %29, %._crit_edge ], [ %.1.ph, %.thread16 ]
  ret i1 %.2
}

; Function Attrs: nofree nounwind
declare noundef i32 @fileno(ptr nocapture noundef) local_unnamed_addr #3

declare i32 @select(i32 noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #4

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none)
declare ptr @__errno_location() local_unnamed_addr #5

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #6

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 #3 = { 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 #4 = { "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 = { mustprogress nofree nosync nounwind willreturn memory(none) "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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #7 = { nounwind }
attributes #8 = { nounwind willreturn memory(none) }

!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 = !{!"long", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"_ZTS7timeval", !10, i64 0, !10, i64 8}
!13 = !{!12, !10, i64 8}
!14 = !{!15, !15, i64 0}
!15 = !{!"int", !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


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