; 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 i32 @XSock_GetAddrLen(ptr noundef) #1

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

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @XSock_SSLRead(ptr noundef, ptr noundef, i64 noundef, i8 noundef zeroext) #1

declare i64 @recv(i32 noundef, ptr noundef, i64 noundef, i32 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @XSock_Recv(ptr noundef %0, ptr noundef %1, i64 noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.XSocket, ptr %11, i32 0, i32 2
  %13 = load i32, ptr %12, align 4, !tbaa !11
  %14 = and i32 %13, 16
  %15 = icmp eq i32 %14, 16
  br i1 %15, label %16, label %21

16:                                               ; preds = %3
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  %19 = load i64, ptr %7, align 8, !tbaa !9
  %20 = call i32 @XSock_SSLRead(ptr noundef %17, ptr noundef %18, i64 noundef %19, i8 noundef zeroext 0)
  store i32 %20, ptr %4, align 4
  br label %79

21:                                               ; preds = %3
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = call i32 @XSock_Check(ptr noundef %22)
  %24 = icmp ne i32 %23, 0
  br i1 %24, label %26, label %25

25:                                               ; preds = %21
  store i32 -1, ptr %4, align 4
  br label %79

26:                                               ; preds = %21
  %27 = load i64, ptr %7, align 8, !tbaa !9
  %28 = icmp ne i64 %27, 0
  br i1 %28, label %29, label %32

29:                                               ; preds = %26
  %30 = load ptr, ptr %6, align 8, !tbaa !5
  %31 = icmp eq ptr %30, null
  br i1 %31, label %32, label %33

32:                                               ; preds = %29, %26
  store i32 0, ptr %4, align 4
  br label %79

33:                                               ; preds = %29
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 0, ptr %8, align 4, !tbaa !15
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = call ptr @XSock_GetSockAddr(ptr noundef %34)
  store ptr %35, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = call i32 @XSock_GetAddrLen(ptr noundef %36)
  store i32 %37, ptr %10, align 4, !tbaa !15
  %38 = load ptr, ptr %5, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.XSocket, ptr %38, i32 0, i32 8
  %40 = load i32, ptr %39, align 4, !tbaa !16
  %41 = icmp ne i32 %40, 2
  br i1 %41, label %42, label %52

42:                                               ; preds = %33
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.XSocket, ptr %43, i32 0, i32 5
  %45 = load i32, ptr %44, align 8, !tbaa !17
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = load i64, ptr %7, align 8, !tbaa !9
  %48 = trunc i64 %47 to i32
  %49 = sext i32 %48 to i64
  %50 = call i64 @recv(i32 noundef %45, ptr noundef %46, i64 noundef %49, i32 noundef 16384)
  %51 = trunc i64 %50 to i32
  store i32 %51, ptr %8, align 4, !tbaa !15
  br label %63

52:                                               ; preds = %33
  %53 = load ptr, ptr %5, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.XSocket, ptr %53, i32 0, i32 5
  %55 = load i32, ptr %54, align 8, !tbaa !17
  %56 = load ptr, ptr %6, align 8, !tbaa !5
  %57 = load i64, ptr %7, align 8, !tbaa !9
  %58 = trunc i64 %57 to i32
  %59 = sext i32 %58 to i64
  %60 = load ptr, ptr %9, align 8, !tbaa !5
  %61 = call i64 @recvfrom(i32 noundef %55, ptr noundef %56, i64 noundef %59, i32 noundef 0, ptr noundef %60, ptr noundef %10)
  %62 = trunc i64 %61 to i32
  store i32 %62, ptr %8, align 4, !tbaa !15
  br label %63

63:                                               ; preds = %52, %42
  %64 = load i32, ptr %8, align 4, !tbaa !15
  %65 = icmp sle i32 %64, 0
  br i1 %65, label %66, label %77

66:                                               ; preds = %63
  %67 = load i32, ptr %8, align 4, !tbaa !15
  %68 = icmp ne i32 %67, 0
  br i1 %68, label %72, label %69

69:                                               ; preds = %66
  %70 = load ptr, ptr %5, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.XSocket, ptr %70, i32 0, i32 0
  store i32 38, ptr %71, align 8, !tbaa !18
  br label %75

72:                                               ; preds = %66
  %73 = load ptr, ptr %5, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.XSocket, ptr %73, i32 0, i32 0
  store i32 12, ptr %74, align 8, !tbaa !18
  br label %75

75:                                               ; preds = %72, %69
  %76 = load ptr, ptr %5, align 8, !tbaa !5
  call void @XSock_Close(ptr noundef %76)
  br label %77

77:                                               ; preds = %75, %63
  %78 = load i32, ptr %8, align 4, !tbaa !15
  store i32 %78, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %79

79:                                               ; preds = %77, %32, %25, %16
  %80 = load i32, ptr %4, align 4
  ret i32 %80
}

declare i64 @recvfrom(i32 noundef, ptr noundef, i64 noundef, i32 noundef, ptr noundef, ptr 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 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !13, i64 116}
!12 = !{!"XSocket", !13, i64 0, !7, i64 4, !13, i64 116, !13, i64 120, !14, i64 124, !13, i64 128, !13, i64 132, !13, i64 136, !13, i64 140, !6, i64 144}
!13 = !{!"int", !7, i64 0}
!14 = !{!"short", !7, i64 0}
!15 = !{!13, !13, i64 0}
!16 = !{!12, !13, i64 140}
!17 = !{!12, !13, i64 128}
!18 = !{!12, !13, i64 0}


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

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

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

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @XSock_SSLRead(ptr noundef, ptr noundef, i64 noundef, i8 noundef zeroext) local_unnamed_addr #1

declare i64 @recv(i32 noundef, ptr noundef, i64 noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local i32 @XSock_Recv(ptr noundef %0, ptr noundef %1, i64 noundef %2) local_unnamed_addr #1 {
  %4 = alloca i32, align 4
  %5 = getelementptr inbounds i8, ptr %0, i64 116
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %7 = and i32 %6, 16
  %.not = icmp eq i32 %7, 0
  br i1 %.not, label %10, label %8

8:                                                ; preds = %3
  %9 = tail call i32 @XSock_SSLRead(ptr noundef nonnull %0, ptr noundef %1, i64 noundef %2, i8 noundef zeroext 0)
  br label %31

10:                                               ; preds = %3
  %11 = tail call i32 @XSock_Check(ptr noundef nonnull %0)
  %.not27 = icmp eq i32 %11, 0
  br i1 %.not27, label %31, label %12

12:                                               ; preds = %10
  %13 = icmp eq i64 %2, 0
  %14 = icmp eq ptr %1, null
  %or.cond = or i1 %14, %13
  br i1 %or.cond, label %31, label %15

15:                                               ; preds = %12
  %16 = tail call ptr @XSock_GetSockAddr(ptr noundef nonnull %0)
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %4) #3
  %17 = tail call i32 @XSock_GetAddrLen(ptr noundef nonnull %0)
  store i32 %17, ptr %4, align 4, !tbaa !12
  %18 = getelementptr inbounds i8, ptr %0, i64 140
  %19 = load i32, ptr %18, align 4, !tbaa !13
  %.not28 = icmp eq i32 %19, 2
  %20 = getelementptr inbounds i8, ptr %0, i64 128
  %21 = load i32, ptr %20, align 8, !tbaa !14
  %sext = shl i64 %2, 32
  %22 = ashr exact i64 %sext, 32
  br i1 %.not28, label %25, label %23

23:                                               ; preds = %15
  %24 = tail call i64 @recv(i32 noundef %21, ptr noundef nonnull %1, i64 noundef %22, i32 noundef 16384) #3
  br label %27

25:                                               ; preds = %15
  %26 = call i64 @recvfrom(i32 noundef %21, ptr noundef nonnull %1, i64 noundef %22, i32 noundef 0, ptr noundef %16, ptr noundef nonnull %4) #3
  br label %27

27:                                               ; preds = %25, %23
  %.024.in = phi i64 [ %24, %23 ], [ %26, %25 ]
  %.024 = trunc i64 %.024.in to i32
  %28 = icmp slt i32 %.024, 1
  br i1 %28, label %29, label %30

29:                                               ; preds = %27
  %.not30 = icmp eq i32 %.024, 0
  %. = select i1 %.not30, i32 38, i32 12
  store i32 %., ptr %0, align 8, !tbaa !15
  call void @XSock_Close(ptr noundef nonnull %0)
  br label %30

30:                                               ; preds = %29, %27
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %4) #3
  br label %31

31:                                               ; preds = %12, %10, %30, %8
  %.0 = phi i32 [ %9, %8 ], [ %.024, %30 ], [ -1, %10 ], [ 0, %12 ]
  ret i32 %.0
}

declare i64 @recvfrom(i32 noundef, ptr noundef, i64 noundef, i32 noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

attributes #0 = { mustprogress 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, !7, i64 116}
!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 = !{!7, !7, i64 0}
!13 = !{!6, !7, i64 140}
!14 = !{!6, !7, i64 128}
!15 = !{!6, !7, i64 0}


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