; 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
declare dso_local i32 @XSock_SSLRead(ptr noundef, ptr noundef, i64 noundef, i8 noundef zeroext) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @XSock_RecvChunk(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
  %11 = alloca i32, align 4
  %12 = 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
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.XSocket, ptr %13, i32 0, i32 2
  %15 = load i32, ptr %14, align 4, !tbaa !11
  %16 = and i32 %15, 16
  %17 = icmp eq i32 %16, 16
  br i1 %17, label %18, label %23

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

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

27:                                               ; preds = %23
  store i32 -1, ptr %4, align 4
  br label %90

28:                                               ; preds = %23
  %29 = load i64, ptr %7, align 8, !tbaa !9
  %30 = icmp ne i64 %29, 0
  br i1 %30, label %31, label %34

31:                                               ; preds = %28
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = icmp eq ptr %32, null
  br i1 %33, label %34, label %35

34:                                               ; preds = %31, %28
  store i32 0, ptr %4, align 4
  br label %90

35:                                               ; preds = %31
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %36 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %36, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 0, ptr %9, align 4, !tbaa !15
  br label %37

37:                                               ; preds = %86, %35
  %38 = load i32, ptr %9, align 4, !tbaa !15
  %39 = sext i32 %38 to i64
  %40 = load i64, ptr %7, align 8, !tbaa !9
  %41 = icmp ult i64 %39, %40
  br i1 %41, label %42, label %87

42:                                               ; preds = %37
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %43 = load i64, ptr %7, align 8, !tbaa !9
  %44 = trunc i64 %43 to i32
  %45 = load i32, ptr %9, align 4, !tbaa !15
  %46 = sub nsw i32 %44, %45
  %47 = icmp slt i32 %46, 32768
  br i1 %47, label %48, label %53

48:                                               ; preds = %42
  %49 = load i64, ptr %7, align 8, !tbaa !9
  %50 = trunc i64 %49 to i32
  %51 = load i32, ptr %9, align 4, !tbaa !15
  %52 = sub nsw i32 %50, %51
  br label %54

53:                                               ; preds = %42
  br label %54

54:                                               ; preds = %53, %48
  %55 = phi i32 [ %52, %48 ], [ 32768, %53 ]
  store i32 %55, ptr %10, align 4, !tbaa !15
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  %56 = load ptr, ptr %5, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.XSocket, ptr %56, i32 0, i32 5
  %58 = load i32, ptr %57, align 8, !tbaa !16
  %59 = load ptr, ptr %8, align 8, !tbaa !5
  %60 = load i32, ptr %9, align 4, !tbaa !15
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds i8, ptr %59, i64 %61
  %63 = load i32, ptr %10, align 4, !tbaa !15
  %64 = sext i32 %63 to i64
  %65 = call i64 @recv(i32 noundef %58, ptr noundef %62, i64 noundef %64, i32 noundef 16384)
  %66 = trunc i64 %65 to i32
  store i32 %66, ptr %11, align 4, !tbaa !15
  %67 = load i32, ptr %11, align 4, !tbaa !15
  %68 = icmp sle i32 %67, 0
  br i1 %68, label %69, label %80

69:                                               ; preds = %54
  %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 !17
  %72 = load ptr, ptr %5, align 8, !tbaa !5
  call void @XSock_Close(ptr noundef %72)
  %73 = load i32, ptr %11, align 4, !tbaa !15
  %74 = icmp slt i32 %73, 0
  br i1 %74, label %75, label %78

75:                                               ; preds = %69
  %76 = load ptr, ptr %5, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.XSocket, ptr %76, i32 0, i32 0
  store i32 12, ptr %77, align 8, !tbaa !17
  store i32 -1, ptr %9, align 4, !tbaa !15
  br label %78

78:                                               ; preds = %75, %69
  %79 = load i32, ptr %9, align 4, !tbaa !15
  store i32 %79, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %84

80:                                               ; preds = %54
  %81 = load i32, ptr %11, align 4, !tbaa !15
  %82 = load i32, ptr %9, align 4, !tbaa !15
  %83 = add nsw i32 %82, %81
  store i32 %83, ptr %9, align 4, !tbaa !15
  store i32 0, ptr %12, align 4
  br label %84

84:                                               ; preds = %80, %78
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  %85 = load i32, ptr %12, align 4
  switch i32 %85, label %89 [
    i32 0, label %86
  ]

86:                                               ; preds = %84
  br label %37, !llvm.loop !18

87:                                               ; preds = %37
  %88 = load i32, ptr %9, align 4, !tbaa !15
  store i32 %88, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %89

89:                                               ; preds = %87, %84
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  br label %90

90:                                               ; preds = %89, %34, %27, %18
  %91 = load i32, ptr %4, align 4
  ret i32 %91
}

declare i64 @recv(i32 noundef, ptr noundef, i64 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 128}
!17 = !{!12, !13, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}


; 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
declare dso_local i32 @XSock_SSLRead(ptr noundef, ptr noundef, i64 noundef, i8 noundef zeroext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @XSock_RecvChunk(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_SSLRead(ptr noundef nonnull %0, ptr noundef %1, i64 noundef %2, i8 noundef zeroext 1)
  br label %.thread

9:                                                ; preds = %3
  %10 = tail call i32 @XSock_Check(ptr noundef nonnull %0)
  %.not38 = icmp eq i32 %10, 0
  br i1 %.not38, label %.thread, 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 %.thread, label %.preheader

.preheader:                                       ; preds = %11
  %14 = trunc i64 %2 to i32
  %15 = getelementptr inbounds i8, ptr %0, i64 128
  br label %16

16:                                               ; preds = %.preheader, %29
  %17 = phi i64 [ 0, %.preheader ], [ %31, %29 ]
  %.03042 = phi i32 [ 0, %.preheader ], [ %30, %29 ]
  %18 = sub nsw i32 %14, %.03042
  %19 = tail call i32 @llvm.smin.i32(i32 %18, i32 32768)
  %20 = load i32, ptr %15, align 8, !tbaa !12
  %21 = getelementptr inbounds i8, ptr %1, i64 %17
  %22 = sext i32 %19 to i64
  %23 = tail call i64 @recv(i32 noundef %20, ptr noundef %21, i64 noundef %22, i32 noundef 16384) #3
  %24 = trunc i64 %23 to i32
  %25 = icmp sgt i32 %24, 0
  br i1 %25, label %29, label %26

26:                                               ; preds = %16
  store i32 38, ptr %0, align 8, !tbaa !13
  tail call void @XSock_Close(ptr noundef nonnull %0)
  %27 = icmp slt i32 %24, 0
  br i1 %27, label %28, label %.thread

28:                                               ; preds = %26
  store i32 12, ptr %0, align 8, !tbaa !13
  br label %.thread

29:                                               ; preds = %16
  %30 = add nuw nsw i32 %.03042, %24
  %31 = zext nneg i32 %30 to i64
  %32 = icmp ult i64 %31, %2
  br i1 %32, label %16, label %.thread, !llvm.loop !14

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

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

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

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 = { 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, !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 128}
!13 = !{!6, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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