; 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_SSLWrite(ptr noundef, ptr noundef, i64 noundef) #1

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

; Function Attrs: nounwind uwtable
define dso_local i32 @XSock_Send(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 ptr, align 8
  %9 = alloca i32, align 4
  %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_SSLWrite(ptr noundef %17, ptr noundef %18, i64 noundef %19)
  store i32 %20, ptr %4, align 4
  br label %73

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 %73

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 %73

33:                                               ; preds = %29
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = call ptr @XSock_GetSockAddr(ptr noundef %34)
  store ptr %35, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = call i32 @XSock_GetAddrLen(ptr noundef %36)
  store i32 %37, ptr %9, align 4, !tbaa !15
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store i32 0, 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 @send(i32 noundef %45, ptr noundef %46, i64 noundef %49, i32 noundef 16384)
  %51 = trunc i64 %50 to i32
  store i32 %51, ptr %10, align 4, !tbaa !15
  br label %64

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 %8, align 8, !tbaa !5
  %61 = load i32, ptr %9, align 4, !tbaa !15
  %62 = call i64 @sendto(i32 noundef %55, ptr noundef %56, i64 noundef %59, i32 noundef 16384, ptr noundef %60, i32 noundef %61)
  %63 = trunc i64 %62 to i32
  store i32 %63, ptr %10, align 4, !tbaa !15
  br label %64

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

67:                                               ; preds = %64
  %68 = load ptr, ptr %5, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.XSocket, ptr %68, i32 0, i32 0
  store i32 11, ptr %69, align 8, !tbaa !18
  %70 = load ptr, ptr %5, align 8, !tbaa !5
  call void @XSock_Close(ptr noundef %70)
  br label %71

71:                                               ; preds = %67, %64
  %72 = load i32, ptr %10, align 4, !tbaa !15
  store i32 %72, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  br label %73

73:                                               ; preds = %71, %32, %25, %16
  %74 = load i32, ptr %4, align 4
  ret i32 %74
}

declare i64 @sendto(i32 noundef, ptr noundef, i64 noundef, i32 noundef, ptr 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 = !{!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: nounwind uwtable
declare dso_local i32 @XSock_Check(ptr noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
declare dso_local ptr @XSock_GetSockAddr(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
declare dso_local i32 @XSock_SSLWrite(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #0

declare i64 @send(i32 noundef, ptr noundef, i64 noundef, i32 noundef) local_unnamed_addr #1

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

7:                                                ; preds = %3
  %8 = tail call i32 @XSock_SSLWrite(ptr noundef nonnull %0, ptr noundef %1, i64 noundef %2)
  br label %29

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

11:                                               ; preds = %9
  %12 = icmp eq i64 %2, 0
  %13 = icmp eq ptr %1, null
  %or.cond = or i1 %13, %12
  br i1 %or.cond, label %29, label %14

14:                                               ; preds = %11
  %15 = tail call ptr @XSock_GetSockAddr(ptr noundef nonnull %0)
  %16 = tail call i32 @XSock_GetAddrLen(ptr noundef nonnull %0)
  %17 = getelementptr inbounds i8, ptr %0, i64 140
  %18 = load i32, ptr %17, align 4, !tbaa !12
  %.not27 = icmp eq i32 %18, 2
  %19 = getelementptr inbounds i8, ptr %0, i64 128
  %20 = load i32, ptr %19, align 8, !tbaa !13
  %sext = shl i64 %2, 32
  %21 = ashr exact i64 %sext, 32
  br i1 %.not27, label %24, label %22

22:                                               ; preds = %14
  %23 = tail call i64 @send(i32 noundef %20, ptr noundef nonnull %1, i64 noundef %21, i32 noundef 16384) #2
  br label %26

24:                                               ; preds = %14
  %25 = tail call i64 @sendto(i32 noundef %20, ptr noundef nonnull %1, i64 noundef %21, i32 noundef 16384, ptr noundef %15, i32 noundef %16) #2
  br label %26

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

28:                                               ; preds = %26
  store i32 11, ptr %0, align 8, !tbaa !14
  tail call void @XSock_Close(ptr noundef nonnull %0)
  br label %29

29:                                               ; preds = %26, %28, %11, %9, %7
  %.023 = phi i32 [ %8, %7 ], [ -1, %9 ], [ 0, %11 ], [ %.0, %28 ], [ %.0, %26 ]
  ret i32 %.023
}

declare i64 @sendto(i32 noundef, ptr noundef, i64 noundef, i32 noundef, ptr 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 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 = !{!6, !7, i64 140}
!13 = !{!6, !7, i64 128}
!14 = !{!6, !7, i64 0}


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