; LLVM IR from mkazhdan/DMG :: Util/MultiStreamIO.ll (ZN19MultiStreamIOClient7advanceEv)

; 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.IOClient = type { ptr, %"class.boost::recursive_timed_mutex", ptr }
%"class.boost::recursive_timed_mutex" = type <{ %union.pthread_mutex_t, %union.pthread_cond_t, i8, [7 x i8], i64, i32, [4 x i8] }>
%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 }
%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 }
%class.MultiStreamIOClient = type { %class.StreamingGrid, ptr, %class.FixedIOClientStream }
%class.StreamingGrid = type { %class.IOClient }
%class.FixedIOClientStream = type { %class.IOClient, i32, ptr, ptr, i32, i32, i32, i32, ptr, i8, i32, i32, i32 }

@stderr = external global ptr, align 8
@.str.29 = external hidden unnamed_addr constant [67 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: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #1

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) #2

; Function Attrs: mustprogress uwtable
declare dso_local noundef zeroext i1 @_ZN19MultiStreamIOClient7AdvanceEv(ptr noundef nonnull align 8 dereferenceable(328)) #3 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN19MultiStreamIOClient7advanceEv(ptr noundef nonnull align 8 dereferenceable(328) %0) unnamed_addr #3 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %4 = load ptr, ptr %2, align 8
  %5 = getelementptr inbounds %class.IOClient, ptr %4, i32 0, i32 2
  %6 = load ptr, ptr %5, align 8, !tbaa !9
  %7 = icmp ne ptr %6, null
  br i1 %7, label %8, label %20

8:                                                ; preds = %1
  br label %9

9:                                                ; preds = %19, %8
  %10 = call noundef zeroext i1 @_ZN19MultiStreamIOClient7AdvanceEv(ptr noundef nonnull align 8 dereferenceable(328) %4)
  br i1 %10, label %11, label %12

11:                                               ; preds = %9
  br label %56

12:                                               ; preds = %9
  %13 = getelementptr inbounds %class.IOClient, ptr %4, i32 0, i32 2
  %14 = load ptr, ptr %13, align 8, !tbaa !9
  %15 = load ptr, ptr %14, align 8, !tbaa !15
  %16 = getelementptr inbounds ptr, ptr %15, i64 0
  %17 = load ptr, ptr %16, align 8
  %18 = call noundef zeroext i1 %17(ptr noundef nonnull align 8 dereferenceable(273) %14, ptr noundef %4)
  br label %19

19:                                               ; preds = %12
  br label %9, !llvm.loop !17

20:                                               ; preds = %1
  %21 = call noundef zeroext i1 @_ZN19MultiStreamIOClient7AdvanceEv(ptr noundef nonnull align 8 dereferenceable(328) %4)
  br i1 %21, label %50, label %22

22:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  %23 = load ptr, ptr %4, align 8, !tbaa !15
  %24 = getelementptr inbounds ptr, ptr %23, i64 0
  %25 = load ptr, ptr %24, align 8
  %26 = call noundef i32 %25(ptr noundef nonnull align 8 dereferenceable(328) %4)
  store i32 %26, ptr %3, align 4, !tbaa !19
  br label %27

27:                                               ; preds = %30, %22
  %28 = load i32, ptr %3, align 4, !tbaa !19
  %29 = icmp eq i32 %28, 1
  br i1 %29, label %30, label %35

30:                                               ; preds = %27
  %31 = load ptr, ptr %4, align 8, !tbaa !15
  %32 = getelementptr inbounds ptr, ptr %31, i64 0
  %33 = load ptr, ptr %32, align 8
  %34 = call noundef i32 %33(ptr noundef nonnull align 8 dereferenceable(328) %4)
  store i32 %34, ptr %3, align 4, !tbaa !19
  br label %27, !llvm.loop !20

35:                                               ; preds = %27
  %36 = call noundef zeroext i1 @_ZN19MultiStreamIOClient7AdvanceEv(ptr noundef nonnull align 8 dereferenceable(328) %4)
  br i1 %36, label %49, label %37

37:                                               ; preds = %35
  %38 = load ptr, ptr @stderr, align 8, !tbaa !5
  %39 = getelementptr inbounds %class.MultiStreamIOClient, ptr %4, i32 0, i32 2
  %40 = getelementptr inbounds %class.FixedIOClientStream, ptr %39, i32 0, i32 10
  %41 = load i32, ptr %40, align 4, !tbaa !21
  %42 = getelementptr inbounds %class.MultiStreamIOClient, ptr %4, i32 0, i32 2
  %43 = getelementptr inbounds %class.FixedIOClientStream, ptr %42, i32 0, i32 11
  %44 = load i32, ptr %43, align 8, !tbaa !25
  %45 = getelementptr inbounds %class.MultiStreamIOClient, ptr %4, i32 0, i32 2
  %46 = getelementptr inbounds %class.FixedIOClientStream, ptr %45, i32 0, i32 4
  %47 = load i32, ptr %46, align 8, !tbaa !26
  %48 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %38, ptr noundef @.str.29, i32 noundef %41, i32 noundef %44, i32 noundef %47) #4
  call void @exit(i32 noundef 0) #5
  unreachable

49:                                               ; preds = %35
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  br label %55

50:                                               ; preds = %20
  %51 = load ptr, ptr %4, align 8, !tbaa !15
  %52 = getelementptr inbounds ptr, ptr %51, i64 0
  %53 = load ptr, ptr %52, align 8
  %54 = call noundef i32 %53(ptr noundef nonnull align 8 dereferenceable(328) %4)
  br label %55

55:                                               ; preds = %50, %49
  br label %56

56:                                               ; preds = %55, %11
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = { noreturn 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 = { 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 #4 = { nounwind }
attributes #5 = { noreturn 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, !6, i64 120}
!10 = !{!"_ZTS8IOClient", !11, i64 8, !6, i64 120}
!11 = !{!"_ZTSN5boost21recursive_timed_mutexE", !7, i64 0, !7, i64 40, !12, i64 88, !13, i64 96, !14, i64 104}
!12 = !{!"bool", !7, i64 0}
!13 = !{!"long", !7, i64 0}
!14 = !{!"int", !7, i64 0}
!15 = !{!16, !16, i64 0}
!16 = !{!"vtable pointer", !8, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!14, !14, i64 0}
!20 = distinct !{!20, !18}
!21 = !{!22, !14, i64 316}
!22 = !{!"_ZTS19MultiStreamIOClient", !23, i64 0, !6, i64 128, !24, i64 136}
!23 = !{!"_ZTS13StreamingGrid", !10, i64 0}
!24 = !{!"_ZTS19FixedIOClientStream", !10, i64 0, !14, i64 128, !6, i64 136, !6, i64 144, !14, i64 152, !14, i64 156, !14, i64 160, !14, i64 164, !6, i64 168, !12, i64 176, !14, i64 180, !14, i64 184, !14, i64 188}
!25 = !{!22, !14, i64 320}
!26 = !{!22, !14, i64 288}


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

@stderr = external local_unnamed_addr global ptr, align 8
@.str.29 = external hidden unnamed_addr constant [67 x i8], align 1

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

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
declare dso_local noundef zeroext i1 @_ZN19MultiStreamIOClient7AdvanceEv(ptr noundef nonnull align 8 dereferenceable(328)) local_unnamed_addr #2 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN19MultiStreamIOClient7advanceEv(ptr noundef nonnull align 8 dereferenceable(328) %0) unnamed_addr #2 align 2 {
  %2 = getelementptr inbounds i8, ptr %0, i64 120
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %.not = icmp eq ptr %3, null
  %4 = tail call noundef zeroext i1 @_ZN19MultiStreamIOClient7AdvanceEv(ptr noundef nonnull align 8 dereferenceable(328) %0)
  br i1 %.not, label %10, label %.preheader

.preheader:                                       ; preds = %1
  br i1 %4, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  %6 = load ptr, ptr %5, align 8, !tbaa !14
  %7 = load ptr, ptr %6, align 8
  %8 = tail call noundef zeroext i1 %7(ptr noundef nonnull align 8 dereferenceable(273) %5, ptr noundef nonnull %0)
  %9 = tail call noundef zeroext i1 @_ZN19MultiStreamIOClient7AdvanceEv(ptr noundef nonnull align 8 dereferenceable(328) %0)
  br i1 %9, label %.loopexit, label %.lr.ph, !llvm.loop !16

10:                                               ; preds = %1
  %11 = load ptr, ptr %0, align 8, !tbaa !14
  %12 = load ptr, ptr %11, align 8
  %13 = tail call noundef i32 %12(ptr noundef nonnull align 8 dereferenceable(328) %0)
  br i1 %4, label %.loopexit, label %14

14:                                               ; preds = %10
  %15 = icmp eq i32 %13, 1
  br i1 %15, label %.lr.ph1, label %._crit_edge

.lr.ph1:                                          ; preds = %14, %.lr.ph1
  %16 = load ptr, ptr %0, align 8, !tbaa !14
  %17 = load ptr, ptr %16, align 8
  %18 = tail call noundef i32 %17(ptr noundef nonnull align 8 dereferenceable(328) %0)
  %19 = icmp eq i32 %18, 1
  br i1 %19, label %.lr.ph1, label %._crit_edge, !llvm.loop !18

._crit_edge:                                      ; preds = %.lr.ph1, %14
  %20 = tail call noundef zeroext i1 @_ZN19MultiStreamIOClient7AdvanceEv(ptr noundef nonnull align 8 dereferenceable(328) %0)
  br i1 %20, label %.loopexit, label %21

21:                                               ; preds = %._crit_edge
  %22 = load ptr, ptr @stderr, align 8, !tbaa !19
  %23 = getelementptr inbounds i8, ptr %0, i64 316
  %24 = load i32, ptr %23, align 4, !tbaa !20
  %25 = getelementptr inbounds i8, ptr %0, i64 320
  %26 = load i32, ptr %25, align 8, !tbaa !24
  %27 = getelementptr inbounds i8, ptr %0, i64 288
  %28 = load i32, ptr %27, align 8, !tbaa !25
  %29 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %22, ptr noundef nonnull @.str.29, i32 noundef %24, i32 noundef %26, i32 noundef %28) #3
  tail call void @exit(i32 noundef 0) #4
  unreachable

.loopexit:                                        ; preds = %.lr.ph, %10, %.preheader, %._crit_edge
  ret void
}

attributes #0 = { 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 #1 = { noreturn 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 = { 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 = { cold nounwind }
attributes #4 = { noreturn 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, !13, i64 120}
!6 = !{!"_ZTS8IOClient", !7, i64 8, !13, i64 120}
!7 = !{!"_ZTSN5boost21recursive_timed_mutexE", !8, i64 0, !8, i64 40, !10, i64 88, !11, i64 96, !12, i64 104}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"bool", !8, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!"int", !8, i64 0}
!13 = !{!"any pointer", !8, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"vtable pointer", !9, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = distinct !{!18, !17}
!19 = !{!13, !13, i64 0}
!20 = !{!21, !12, i64 316}
!21 = !{!"_ZTS19MultiStreamIOClient", !22, i64 0, !13, i64 128, !23, i64 136}
!22 = !{!"_ZTS13StreamingGrid", !6, i64 0}
!23 = !{!"_ZTS19FixedIOClientStream", !6, i64 0, !12, i64 128, !13, i64 136, !13, i64 144, !12, i64 152, !12, i64 156, !12, i64 160, !12, i64 164, !13, i64 168, !10, i64 176, !12, i64 180, !12, i64 184, !12, i64 188}
!24 = !{!21, !12, i64 320}
!25 = !{!21, !12, i64 288}


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