; LLVM IR from uNetworking/uSockets :: src (socket)

; 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.us_socket_t = type { %struct.us_poll_t, i8, i8, i16, ptr, ptr, ptr }
%struct.us_poll_t = type { %struct.anon, [12 x i8] }
%struct.anon = type { i32 }
%struct.us_socket_context_t = type { ptr, i32, i8, i8, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, [8 x i8] }

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

declare i32 @us_poll_fd(ptr noundef) #1

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @us_socket_is_shut_down(i32 noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local i32 @us_socket_is_closed(i32 noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @us_socket_write2(i32 noundef %0, ptr noundef %1, ptr noundef %2, i32 noundef %3, ptr noundef %4, i32 noundef %5) #2 {
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca ptr, align 8
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  store i32 %0, ptr %8, align 4, !tbaa !5
  store ptr %1, ptr %9, align 8, !tbaa !9
  store ptr %2, ptr %10, align 8, !tbaa !9
  store i32 %3, ptr %11, align 4, !tbaa !5
  store ptr %4, ptr %12, align 8, !tbaa !9
  store i32 %5, ptr %13, align 4, !tbaa !5
  %15 = load i32, ptr %8, align 4, !tbaa !5
  %16 = load ptr, ptr %9, align 8, !tbaa !9
  %17 = call i32 @us_socket_is_closed(i32 noundef %15, ptr noundef %16)
  %18 = icmp ne i32 %17, 0
  br i1 %18, label %24, label %19

19:                                               ; preds = %6
  %20 = load i32, ptr %8, align 4, !tbaa !5
  %21 = load ptr, ptr %9, align 8, !tbaa !9
  %22 = call i32 @us_socket_is_shut_down(i32 noundef %20, ptr noundef %21)
  %23 = icmp ne i32 %22, 0
  br i1 %23, label %24, label %25

24:                                               ; preds = %19, %6
  store i32 0, ptr %7, align 4
  br label %55

25:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  %26 = load ptr, ptr %9, align 8, !tbaa !9
  %27 = getelementptr inbounds %struct.us_socket_t, ptr %26, i32 0, i32 0
  %28 = call i32 @us_poll_fd(ptr noundef %27)
  %29 = load ptr, ptr %10, align 8, !tbaa !9
  %30 = load i32, ptr %11, align 4, !tbaa !5
  %31 = load ptr, ptr %12, align 8, !tbaa !9
  %32 = load i32, ptr %13, align 4, !tbaa !5
  %33 = call i32 @bsd_write2(i32 noundef %28, ptr noundef %29, i32 noundef %30, ptr noundef %31, i32 noundef %32)
  store i32 %33, ptr %14, align 4, !tbaa !5
  %34 = load i32, ptr %14, align 4, !tbaa !5
  %35 = load i32, ptr %11, align 4, !tbaa !5
  %36 = load i32, ptr %13, align 4, !tbaa !5
  %37 = add nsw i32 %35, %36
  %38 = icmp ne i32 %34, %37
  br i1 %38, label %39, label %47

39:                                               ; preds = %25
  %40 = load ptr, ptr %9, align 8, !tbaa !9
  %41 = getelementptr inbounds %struct.us_socket_t, ptr %40, i32 0, i32 0
  %42 = load ptr, ptr %9, align 8, !tbaa !9
  %43 = getelementptr inbounds %struct.us_socket_t, ptr %42, i32 0, i32 4
  %44 = load ptr, ptr %43, align 8, !tbaa !11
  %45 = getelementptr inbounds %struct.us_socket_context_t, ptr %44, i32 0, i32 0
  %46 = load ptr, ptr %45, align 16, !tbaa !16
  call void @us_poll_change(ptr noundef %41, ptr noundef %46, i32 noundef 5)
  br label %47

47:                                               ; preds = %39, %25
  %48 = load i32, ptr %14, align 4, !tbaa !5
  %49 = icmp slt i32 %48, 0
  br i1 %49, label %50, label %51

50:                                               ; preds = %47
  br label %53

51:                                               ; preds = %47
  %52 = load i32, ptr %14, align 4, !tbaa !5
  br label %53

53:                                               ; preds = %51, %50
  %54 = phi i32 [ 0, %50 ], [ %52, %51 ]
  store i32 %54, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  br label %55

55:                                               ; preds = %53, %24
  %56 = load i32, ptr %7, align 4
  ret i32 %56
}

declare i32 @bsd_write2(i32 noundef, ptr noundef, i32 noundef, ptr noundef, i32 noundef) #1

declare void @us_poll_change(ptr noundef, ptr noundef, i32 noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 = { 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 #3 = { 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !10, i64 24}
!12 = !{!"us_socket_t", !13, i64 0, !7, i64 16, !7, i64 17, !15, i64 18, !10, i64 24, !10, i64 32, !10, i64 40}
!13 = !{!"us_poll_t", !14, i64 0}
!14 = !{!"", !6, i64 0, !6, i64 3}
!15 = !{!"short", !7, i64 0}
!16 = !{!17, !10, i64 0}
!17 = !{!"us_socket_context_t", !10, i64 0, !6, i64 8, !7, i64 12, !7, i64 13, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !10, i64 48, !10, i64 56, !10, i64 64, !10, i64 72, !10, i64 80, !10, i64 88, !10, i64 96, !10, i64 104, !10, i64 112, !10, i64 120, !10, i64 128}


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

declare i32 @us_poll_fd(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @us_socket_is_shut_down(i32 noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @us_socket_is_closed(i32 noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i32 @us_socket_write2(i32 noundef %0, ptr noundef %1, ptr noundef %2, i32 noundef %3, ptr noundef %4, i32 noundef %5) local_unnamed_addr #1 {
  %7 = tail call i32 @us_socket_is_closed(i32 noundef %0, ptr noundef %1)
  %.not = icmp eq i32 %7, 0
  br i1 %.not, label %8, label %20

8:                                                ; preds = %6
  %9 = tail call i32 @us_socket_is_shut_down(i32 noundef %0, ptr noundef %1)
  %.not16 = icmp eq i32 %9, 0
  br i1 %.not16, label %10, label %20

10:                                               ; preds = %8
  %11 = tail call i32 @us_poll_fd(ptr noundef %1) #3
  %12 = tail call i32 @bsd_write2(i32 noundef %11, ptr noundef %2, i32 noundef %3, ptr noundef %4, i32 noundef %5) #3
  %13 = add nsw i32 %5, %3
  %.not17 = icmp eq i32 %12, %13
  br i1 %.not17, label %18, label %14

14:                                               ; preds = %10
  %15 = getelementptr inbounds i8, ptr %1, i64 24
  %16 = load ptr, ptr %15, align 8, !tbaa !5
  %17 = load ptr, ptr %16, align 16, !tbaa !14
  tail call void @us_poll_change(ptr noundef %1, ptr noundef %17, i32 noundef 5) #3
  br label %18

18:                                               ; preds = %14, %10
  %19 = tail call i32 @llvm.smax.i32(i32 %12, i32 0)
  br label %20

20:                                               ; preds = %6, %8, %18
  %.0 = phi i32 [ %19, %18 ], [ 0, %8 ], [ 0, %6 ]
  ret i32 %.0
}

declare i32 @bsd_write2(i32 noundef, ptr noundef, i32 noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

declare void @us_poll_change(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #2

attributes #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 = { 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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #3 = { 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 24}
!6 = !{!"us_socket_t", !7, i64 0, !10, i64 16, !10, i64 17, !12, i64 18, !13, i64 24, !13, i64 32, !13, i64 40}
!7 = !{!"us_poll_t", !8, i64 0}
!8 = !{!"", !9, i64 0, !9, i64 3}
!9 = !{!"int", !10, i64 0}
!10 = !{!"omnipotent char", !11, i64 0}
!11 = !{!"Simple C/C++ TBAA"}
!12 = !{!"short", !10, i64 0}
!13 = !{!"any pointer", !10, i64 0}
!14 = !{!15, !13, i64 0}
!15 = !{!"us_socket_context_t", !13, i64 0, !9, i64 8, !10, i64 12, !10, i64 13, !13, i64 16, !13, i64 24, !13, i64 32, !13, i64 40, !13, i64 48, !13, i64 56, !13, i64 64, !13, i64 72, !13, i64 80, !13, i64 88, !13, i64 96, !13, i64 104, !13, i64 112, !13, i64 120, !13, i64 128}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass