; LLVM IR from kala13x/libxutils :: src/net (sock)

; 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.XSocket = type { i32, %union.xsock_addr_t, i32, i32, i16, i32, i32, i32, i32, ptr }
%union.xsock_addr_t = type { %struct.sockaddr_in, [96 x i8] }
%struct.sockaddr_in = type { i16, i16, %struct.in_addr, [8 x i8] }
%struct.in_addr = type { i32 }

; 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 uwtable
declare dso_local i32 @XSock_Check(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @XSock_Close(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @XSock_NonBlock(ptr noundef %0, i8 noundef zeroext %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i8, align 1
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i8 %1, ptr %5, align 1, !tbaa !9
  %8 = load ptr, ptr %4, align 8, !tbaa !5
  %9 = call i32 @XSock_Check(ptr noundef %8)
  %10 = icmp ne i32 %9, 0
  br i1 %10, label %12, label %11

11:                                               ; preds = %2
  store i32 -1, ptr %3, align 4
  br label %72

12:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.XSocket, ptr %13, i32 0, i32 5
  %15 = load i32, ptr %14, align 8, !tbaa !10
  %16 = call i32 (i32, i32, ...) @fcntl(i32 noundef %15, i32 noundef 3)
  store i32 %16, ptr %6, align 4, !tbaa !14
  %17 = load i32, ptr %6, align 4, !tbaa !14
  %18 = icmp slt i32 %17, 0
  br i1 %18, label %19, label %23

19:                                               ; preds = %12
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.XSocket, ptr %20, i32 0, i32 0
  store i32 17, ptr %21, align 8, !tbaa !15
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  call void @XSock_Close(ptr noundef %22)
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %71

23:                                               ; preds = %12
  %24 = load i8, ptr %5, align 1, !tbaa !9
  %25 = icmp ne i8 %24, 0
  br i1 %25, label %26, label %40

26:                                               ; preds = %23
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.XSocket, ptr %27, i32 0, i32 5
  %29 = load i32, ptr %28, align 8, !tbaa !10
  %30 = load i32, ptr %6, align 4, !tbaa !14
  %31 = or i32 %30, 2048
  %32 = call i32 (i32, i32, ...) @fcntl(i32 noundef %29, i32 noundef 4, i32 noundef %31)
  store i32 %32, ptr %6, align 4, !tbaa !14
  %33 = load i32, ptr %6, align 4, !tbaa !14
  %34 = icmp slt i32 %33, 0
  br i1 %34, label %35, label %39

35:                                               ; preds = %26
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.XSocket, ptr %36, i32 0, i32 0
  store i32 16, ptr %37, align 8, !tbaa !15
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  call void @XSock_Close(ptr noundef %38)
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %71

39:                                               ; preds = %26
  br label %54

40:                                               ; preds = %23
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.XSocket, ptr %41, i32 0, i32 5
  %43 = load i32, ptr %42, align 8, !tbaa !10
  %44 = load i32, ptr %6, align 4, !tbaa !14
  %45 = and i32 %44, -2049
  %46 = call i32 (i32, i32, ...) @fcntl(i32 noundef %43, i32 noundef 4, i32 noundef %45)
  store i32 %46, ptr %6, align 4, !tbaa !14
  %47 = load i32, ptr %6, align 4, !tbaa !14
  %48 = icmp slt i32 %47, 0
  br i1 %48, label %49, label %53

49:                                               ; preds = %40
  %50 = load ptr, ptr %4, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.XSocket, ptr %50, i32 0, i32 0
  store i32 16, ptr %51, align 8, !tbaa !15
  %52 = load ptr, ptr %4, align 8, !tbaa !5
  call void @XSock_Close(ptr noundef %52)
  store i32 -1, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %71

53:                                               ; preds = %40
  br label %54

54:                                               ; preds = %53, %39
  %55 = load i8, ptr %5, align 1, !tbaa !9
  %56 = icmp ne i8 %55, 0
  br i1 %56, label %57, label %62

57:                                               ; preds = %54
  %58 = load ptr, ptr %4, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.XSocket, ptr %58, i32 0, i32 2
  %60 = load i32, ptr %59, align 4, !tbaa !16
  %61 = or i32 %60, 16384
  store i32 %61, ptr %59, align 4, !tbaa !16
  br label %67

62:                                               ; preds = %54
  %63 = load ptr, ptr %4, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.XSocket, ptr %63, i32 0, i32 2
  %65 = load i32, ptr %64, align 4, !tbaa !16
  %66 = and i32 %65, -16385
  store i32 %66, ptr %64, align 4, !tbaa !16
  br label %67

67:                                               ; preds = %62, %57
  %68 = load ptr, ptr %4, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.XSocket, ptr %68, i32 0, i32 5
  %70 = load i32, ptr %69, align 8, !tbaa !10
  store i32 %70, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %71

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

72:                                               ; preds = %71, %11
  %73 = load i32, ptr %3, align 4
  ret i32 %73
}

declare i32 @fcntl(i32 noundef, i32 noundef, ...) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { "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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!11, !12, i64 128}
!11 = !{!"XSocket", !12, i64 0, !7, i64 4, !12, i64 116, !12, i64 120, !13, i64 124, !12, i64 128, !12, i64 132, !12, i64 136, !12, i64 140, !6, i64 144}
!12 = !{!"int", !7, i64 0}
!13 = !{!"short", !7, i64 0}
!14 = !{!12, !12, i64 0}
!15 = !{!11, !12, i64 0}
!16 = !{!11, !12, i64 116}


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

; Function Attrs: nounwind uwtable
declare dso_local i32 @XSock_Check(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @XSock_Close(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @XSock_NonBlock(ptr noundef %0, i8 noundef zeroext %1) local_unnamed_addr #0 {
  %3 = tail call i32 @XSock_Check(ptr noundef %0)
  %.not = icmp eq i32 %3, 0
  br i1 %.not, label %32, label %4

4:                                                ; preds = %2
  %5 = getelementptr inbounds i8, ptr %0, i64 128
  %6 = load i32, ptr %5, align 8, !tbaa !5
  %7 = tail call i32 (i32, i32, ...) @fcntl(i32 noundef %6, i32 noundef 3) #2
  %8 = icmp slt i32 %7, 0
  br i1 %8, label %9, label %10

9:                                                ; preds = %4
  store i32 17, ptr %0, align 8, !tbaa !12
  tail call void @XSock_Close(ptr noundef nonnull %0)
  br label %32

10:                                               ; preds = %4
  %.not21 = icmp eq i8 %1, 0
  %11 = load i32, ptr %5, align 8, !tbaa !5
  br i1 %.not21, label %17, label %12

12:                                               ; preds = %10
  %13 = or i32 %7, 2048
  %14 = tail call i32 (i32, i32, ...) @fcntl(i32 noundef %11, i32 noundef 4, i32 noundef %13) #2
  %15 = icmp slt i32 %14, 0
  br i1 %15, label %16, label %22

16:                                               ; preds = %12
  store i32 16, ptr %0, align 8, !tbaa !12
  tail call void @XSock_Close(ptr noundef nonnull %0)
  br label %32

17:                                               ; preds = %10
  %18 = and i32 %7, 2147481599
  %19 = tail call i32 (i32, i32, ...) @fcntl(i32 noundef %11, i32 noundef 4, i32 noundef %18) #2
  %20 = icmp slt i32 %19, 0
  br i1 %20, label %21, label %26

21:                                               ; preds = %17
  store i32 16, ptr %0, align 8, !tbaa !12
  tail call void @XSock_Close(ptr noundef nonnull %0)
  br label %32

22:                                               ; preds = %12
  %23 = getelementptr inbounds i8, ptr %0, i64 116
  %24 = load i32, ptr %23, align 4, !tbaa !13
  %25 = or i32 %24, 16384
  store i32 %25, ptr %23, align 4, !tbaa !13
  br label %30

26:                                               ; preds = %17
  %27 = getelementptr inbounds i8, ptr %0, i64 116
  %28 = load i32, ptr %27, align 4, !tbaa !13
  %29 = and i32 %28, -16385
  store i32 %29, ptr %27, align 4, !tbaa !13
  br label %30

30:                                               ; preds = %26, %22
  %31 = load i32, ptr %5, align 8, !tbaa !5
  br label %32

32:                                               ; preds = %9, %16, %21, %30, %2
  %.1 = phi i32 [ -1, %2 ], [ -1, %9 ], [ -1, %16 ], [ %31, %30 ], [ -1, %21 ]
  ret i32 %.1
}

declare i32 @fcntl(i32 noundef, i32 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 = { "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 }

!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 128}
!6 = !{!"XSocket", !7, i64 0, !8, i64 4, !7, i64 116, !7, i64 120, !10, i64 124, !7, i64 128, !7, i64 132, !7, i64 136, !7, i64 140, !11, i64 144}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"short", !8, i64 0}
!11 = !{!"any pointer", !8, i64 0}
!12 = !{!6, !7, i64 0}
!13 = !{!6, !7, i64 116}


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