; LLVM IR from openssl/openssl :: ssl/rio (poll_builder)

; 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.rio_poll_builder_st = type { ptr, [32 x %struct.pollfd], i64, i64 }
%struct.pollfd = type { i32, i16, i16 }

@.str = external hidden unnamed_addr constant [72 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [52 x i8], align 1
@__PRETTY_FUNCTION__.ossl_rio_poll_builder_add_fd = external hidden unnamed_addr constant [68 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @ossl_rio_poll_builder_add_fd(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) #0 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i64, align 8
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !9
  store i32 %2, ptr %8, align 4, !tbaa !9
  store i32 %3, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %14 = load ptr, ptr %6, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.rio_poll_builder_st, ptr %14, i32 0, i32 0
  %16 = load ptr, ptr %15, align 8, !tbaa !11
  %17 = icmp ne ptr %16, null
  br i1 %17, label %18, label %22

18:                                               ; preds = %4
  %19 = load ptr, ptr %6, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.rio_poll_builder_st, ptr %19, i32 0, i32 0
  %21 = load ptr, ptr %20, align 8, !tbaa !11
  br label %26

22:                                               ; preds = %4
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.rio_poll_builder_st, ptr %23, i32 0, i32 1
  %25 = getelementptr inbounds [32 x %struct.pollfd], ptr %24, i64 0, i64 0
  br label %26

26:                                               ; preds = %22, %18
  %27 = phi ptr [ %21, %18 ], [ %25, %22 ]
  store ptr %27, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %28 = load i32, ptr %7, align 4, !tbaa !9
  %29 = icmp slt i32 %28, 0
  br i1 %29, label %30, label %31

30:                                               ; preds = %26
  store i32 0, ptr %5, align 4
  store i32 1, ptr %13, align 4
  br label %134

31:                                               ; preds = %26
  store i64 0, ptr %10, align 8, !tbaa !14
  br label %32

32:                                               ; preds = %54, %31
  %33 = load i64, ptr %10, align 8, !tbaa !14
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.rio_poll_builder_st, ptr %34, i32 0, i32 2
  %36 = load i64, ptr %35, align 8, !tbaa !15
  %37 = icmp ult i64 %33, %36
  br i1 %37, label %38, label %57

38:                                               ; preds = %32
  %39 = load ptr, ptr %11, align 8, !tbaa !5
  %40 = load i64, ptr %10, align 8, !tbaa !14
  %41 = getelementptr inbounds %struct.pollfd, ptr %39, i64 %40
  store ptr %41, ptr %12, align 8, !tbaa !5
  %42 = load ptr, ptr %12, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.pollfd, ptr %42, i32 0, i32 0
  %44 = load i32, ptr %43, align 4, !tbaa !16
  %45 = icmp eq i32 %44, -1
  br i1 %45, label %52, label %46

46:                                               ; preds = %38
  %47 = load ptr, ptr %12, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.pollfd, ptr %47, i32 0, i32 0
  %49 = load i32, ptr %48, align 4, !tbaa !16
  %50 = load i32, ptr %7, align 4, !tbaa !9
  %51 = icmp eq i32 %49, %50
  br i1 %51, label %52, label %53

52:                                               ; preds = %46, %38
  br label %57

53:                                               ; preds = %46
  br label %54

54:                                               ; preds = %53
  %55 = load i64, ptr %10, align 8, !tbaa !14
  %56 = add i64 %55, 1
  store i64 %56, ptr %10, align 8, !tbaa !14
  br label %32, !llvm.loop !19

57:                                               ; preds = %52, %32
  %58 = load i64, ptr %10, align 8, !tbaa !14
  %59 = load ptr, ptr %6, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.rio_poll_builder_st, ptr %59, i32 0, i32 3
  %61 = load i64, ptr %60, align 8, !tbaa !21
  %62 = icmp uge i64 %58, %61
  br i1 %62, label %63, label %73

63:                                               ; preds = %57
  %64 = load ptr, ptr %6, align 8, !tbaa !5
  %65 = load ptr, ptr %6, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.rio_poll_builder_st, ptr %65, i32 0, i32 3
  %67 = load i64, ptr %66, align 8, !tbaa !21
  %68 = mul i64 %67, 2
  %69 = call i32 @rpb_ensure_alloc(ptr noundef %64, i64 noundef %68)
  %70 = icmp ne i32 %69, 0
  br i1 %70, label %72, label %71

71:                                               ; preds = %63
  store i32 0, ptr %5, align 4
  store i32 1, ptr %13, align 4
  br label %134

72:                                               ; preds = %63
  br label %73

73:                                               ; preds = %72, %57
  %74 = load i64, ptr %10, align 8, !tbaa !14
  %75 = load ptr, ptr %6, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.rio_poll_builder_st, ptr %75, i32 0, i32 2
  %77 = load i64, ptr %76, align 8, !tbaa !15
  %78 = icmp ule i64 %74, %77
  br i1 %78, label %79, label %88

79:                                               ; preds = %73
  %80 = load ptr, ptr %6, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.rio_poll_builder_st, ptr %80, i32 0, i32 2
  %82 = load i64, ptr %81, align 8, !tbaa !15
  %83 = load ptr, ptr %6, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.rio_poll_builder_st, ptr %83, i32 0, i32 3
  %85 = load i64, ptr %84, align 8, !tbaa !21
  %86 = icmp ule i64 %82, %85
  br i1 %86, label %87, label %88

87:                                               ; preds = %79
  br label %89

88:                                               ; preds = %79, %73
  call void @__assert_fail(ptr noundef @.str.1, ptr noundef @.str, i32 noundef 120, ptr noundef @__PRETTY_FUNCTION__.ossl_rio_poll_builder_add_fd) #4
  unreachable

89:                                               ; preds = %87
  %90 = load i32, ptr %7, align 4, !tbaa !9
  %91 = load ptr, ptr %11, align 8, !tbaa !5
  %92 = load i64, ptr %10, align 8, !tbaa !14
  %93 = getelementptr inbounds %struct.pollfd, ptr %91, i64 %92
  %94 = getelementptr inbounds %struct.pollfd, ptr %93, i32 0, i32 0
  store i32 %90, ptr %94, align 4, !tbaa !16
  %95 = load ptr, ptr %11, align 8, !tbaa !5
  %96 = load i64, ptr %10, align 8, !tbaa !14
  %97 = getelementptr inbounds %struct.pollfd, ptr %95, i64 %96
  %98 = getelementptr inbounds %struct.pollfd, ptr %97, i32 0, i32 1
  store i16 0, ptr %98, align 4, !tbaa !22
  %99 = load i32, ptr %8, align 4, !tbaa !9
  %100 = icmp ne i32 %99, 0
  br i1 %100, label %101, label %110

101:                                              ; preds = %89
  %102 = load ptr, ptr %11, align 8, !tbaa !5
  %103 = load i64, ptr %10, align 8, !tbaa !14
  %104 = getelementptr inbounds %struct.pollfd, ptr %102, i64 %103
  %105 = getelementptr inbounds %struct.pollfd, ptr %104, i32 0, i32 1
  %106 = load i16, ptr %105, align 4, !tbaa !22
  %107 = sext i16 %106 to i32
  %108 = or i32 %107, 1
  %109 = trunc i32 %108 to i16
  store i16 %109, ptr %105, align 4, !tbaa !22
  br label %110

110:                                              ; preds = %101, %89
  %111 = load i32, ptr %9, align 4, !tbaa !9
  %112 = icmp ne i32 %111, 0
  br i1 %112, label %113, label %122

113:                                              ; preds = %110
  %114 = load ptr, ptr %11, align 8, !tbaa !5
  %115 = load i64, ptr %10, align 8, !tbaa !14
  %116 = getelementptr inbounds %struct.pollfd, ptr %114, i64 %115
  %117 = getelementptr inbounds %struct.pollfd, ptr %116, i32 0, i32 1
  %118 = load i16, ptr %117, align 4, !tbaa !22
  %119 = sext i16 %118 to i32
  %120 = or i32 %119, 4
  %121 = trunc i32 %120 to i16
  store i16 %121, ptr %117, align 4, !tbaa !22
  br label %122

122:                                              ; preds = %113, %110
  %123 = load i64, ptr %10, align 8, !tbaa !14
  %124 = load ptr, ptr %6, align 8, !tbaa !5
  %125 = getelementptr inbounds %struct.rio_poll_builder_st, ptr %124, i32 0, i32 2
  %126 = load i64, ptr %125, align 8, !tbaa !15
  %127 = icmp eq i64 %123, %126
  br i1 %127, label %128, label %133

128:                                              ; preds = %122
  %129 = load ptr, ptr %6, align 8, !tbaa !5
  %130 = getelementptr inbounds %struct.rio_poll_builder_st, ptr %129, i32 0, i32 2
  %131 = load i64, ptr %130, align 8, !tbaa !15
  %132 = add i64 %131, 1
  store i64 %132, ptr %130, align 8, !tbaa !15
  br label %133

133:                                              ; preds = %128, %122
  store i32 1, ptr %5, align 4
  store i32 1, ptr %13, align 4
  br label %134

134:                                              ; preds = %133, %71, %30
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  %135 = load i32, ptr %5, align 4
  ret i32 %135
}

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

; Function Attrs: nounwind uwtable
declare hidden i32 @rpb_ensure_alloc(ptr noundef, i64 noundef) #0

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #2

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

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"rio_poll_builder_st", !6, i64 0, !7, i64 8, !13, i64 264, !13, i64 272}
!13 = !{!"long", !7, i64 0}
!14 = !{!13, !13, i64 0}
!15 = !{!12, !13, i64 264}
!16 = !{!17, !10, i64 0}
!17 = !{!"pollfd", !10, i64 0, !18, i64 4, !18, i64 6}
!18 = !{!"short", !7, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = !{!12, !13, i64 272}
!22 = !{!17, !18, i64 4}


; 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.pollfd = type { i32, i16, i16 }

@.str = external hidden unnamed_addr constant [72 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [52 x i8], align 1
@__PRETTY_FUNCTION__.ossl_rio_poll_builder_add_fd = external hidden unnamed_addr constant [68 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @ossl_rio_poll_builder_add_fd(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %5 = load ptr, ptr %0, align 8, !tbaa !5
  %.not = icmp eq ptr %5, null
  %6 = getelementptr inbounds i8, ptr %0, i64 8
  %7 = select i1 %.not, ptr %6, ptr %5
  %8 = icmp slt i32 %1, 0
  br i1 %8, label %34, label %.preheader

.preheader:                                       ; preds = %4
  %9 = getelementptr inbounds i8, ptr %0, i64 264
  %10 = load i64, ptr %9, align 8, !tbaa !11
  %.not51 = icmp eq i64 %10, 0
  br i1 %.not51, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %15
  %.03448 = phi i64 [ %16, %15 ], [ 0, %.preheader ]
  %11 = getelementptr inbounds %struct.pollfd, ptr %7, i64 %.03448
  %12 = load i32, ptr %11, align 4, !tbaa !12
  %13 = icmp eq i32 %12, -1
  %14 = icmp eq i32 %12, %1
  %or.cond = or i1 %13, %14
  br i1 %or.cond, label %._crit_edge, label %15

15:                                               ; preds = %.lr.ph
  %16 = add nuw i64 %.03448, 1
  %exitcond.not = icmp eq i64 %16, %10
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !16

._crit_edge:                                      ; preds = %15, %.lr.ph, %.preheader
  %.034.lcssa = phi i64 [ 0, %.preheader ], [ %.03448, %.lr.ph ], [ %10, %15 ]
  %17 = getelementptr inbounds i8, ptr %0, i64 272
  %18 = load i64, ptr %17, align 8, !tbaa !18
  %.not41 = icmp ult i64 %.034.lcssa, %18
  br i1 %.not41, label %22, label %19

19:                                               ; preds = %._crit_edge
  %20 = shl i64 %18, 1
  %21 = tail call i32 @rpb_ensure_alloc(ptr noundef nonnull %0, i64 noundef %20)
  %.not42 = icmp eq i32 %21, 0
  br i1 %.not42, label %34, label %._crit_edge52

._crit_edge52:                                    ; preds = %19
  %.pre = load i64, ptr %9, align 8, !tbaa !11
  br label %22

22:                                               ; preds = %._crit_edge52, %._crit_edge
  %23 = phi i64 [ %.pre, %._crit_edge52 ], [ %10, %._crit_edge ]
  %.not43 = icmp ugt i64 %.034.lcssa, %23
  br i1 %.not43, label %26, label %24

24:                                               ; preds = %22
  %25 = load i64, ptr %17, align 8, !tbaa !18
  %.not44 = icmp ugt i64 %23, %25
  br i1 %.not44, label %26, label %27

26:                                               ; preds = %24, %22
  tail call void @__assert_fail(ptr noundef nonnull @.str.1, ptr noundef nonnull @.str, i32 noundef 120, ptr noundef nonnull @__PRETTY_FUNCTION__.ossl_rio_poll_builder_add_fd) #2
  unreachable

27:                                               ; preds = %24
  %28 = getelementptr inbounds %struct.pollfd, ptr %7, i64 %.034.lcssa
  store i32 %1, ptr %28, align 4, !tbaa !12
  %29 = getelementptr inbounds i8, ptr %28, i64 4
  %.not45 = icmp ne i32 %2, 0
  %spec.select = zext i1 %.not45 to i16
  %.not46 = icmp eq i32 %3, 0
  %30 = or disjoint i16 %spec.select, 4
  %spec.select47 = select i1 %.not46, i16 %spec.select, i16 %30
  store i16 %spec.select47, ptr %29, align 4, !tbaa !19
  %31 = icmp eq i64 %.034.lcssa, %23
  br i1 %31, label %32, label %34

32:                                               ; preds = %27
  %33 = add i64 %.034.lcssa, 1
  store i64 %33, ptr %9, align 8, !tbaa !11
  br label %34

34:                                               ; preds = %27, %32, %19, %4
  %.0 = phi i32 [ 0, %4 ], [ 0, %19 ], [ 1, %32 ], [ 1, %27 ]
  ret i32 %.0
}

; Function Attrs: nounwind uwtable
declare hidden i32 @rpb_ensure_alloc(ptr noundef, i64 noundef) local_unnamed_addr #0

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

attributes #0 = { 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 #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 = { 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, !7, i64 0}
!6 = !{!"rio_poll_builder_st", !7, i64 0, !8, i64 8, !10, i64 264, !10, i64 272}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!6, !10, i64 264}
!12 = !{!13, !14, i64 0}
!13 = !{!"pollfd", !14, i64 0, !15, i64 4, !15, i64 6}
!14 = !{!"int", !8, i64 0}
!15 = !{!"short", !8, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!6, !10, i64 272}
!19 = !{!13, !15, i64 4}


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