#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;

/* Variables and functions */
int /*<<< orphan*/ EPROTO;
int /*<<< orphan*/ byte_copy(unsigned char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ byte_zero(struct sockaddr_in *, int);
int /*<<< orphan*/ errno;
int recvfrom(int, unsigned char *, long long, int /*<<< orphan*/,
             struct sockaddr *, int *);
long long socket_recv(int fd, unsigned char *x, long long xlen,
                      unsigned char *ip, unsigned char *port) {
  struct sockaddr_in sa;
  socklen_t salen;
  int r;
  if (xlen < 0) {
    errno = EPROTO;
    return -1;
  }
  if (xlen > 1048576)
    xlen = 1048576;
  byte_zero(&sa, sizeof sa);
  salen = sizeof sa;
  r = recvfrom(fd, x, xlen, 0, (struct sockaddr *)&sa, &salen);
  byte_copy(ip, 4, &sa.sin_addr);
  byte_copy(port, 2, &sa.sin_port);
  return r;
}


x86 assembly (clang, -O0):
<socket_recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    1175 <socket_recv+0x45>
mov    0x2e83(%rip),%rax
mov    (%rax),%ecx
mov    0x2e72(%rip),%rax
mov    %ecx,(%rax)
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11ed <socket_recv+0xbd>
cmpq   $0x100000,-0x20(%rbp)
jle    118b <socket_recv+0x5b>
movq   $0x100000,-0x20(%rbp)
lea    -0x38(%rbp),%rdi
mov    $0x8,%esi
call   1030 <byte_zero@plt>
movl   $0x8,-0x3c(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    -0x38(%rbp),%r8
xor    %ecx,%ecx
lea    -0x3c(%rbp),%r9
call   1040 <recvfrom@plt>
mov    %eax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x38(%rbp),%rdx
add    $0x4,%rdx
mov    $0x4,%esi
call   1050 <byte_copy@plt>
mov    -0x30(%rbp),%rdi
lea    -0x38(%rbp),%rdx
mov    $0x2,%esi
call   1050 <byte_copy@plt>
movslq -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_recv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %rdx,%rdx
js     11b8 <socket_recv+0x88>
mov    %r8,%r14
mov    %rcx,%r12
mov    %rsi,%r13
mov    %edi,%ebp
cmp    $0x100000,%rdx
mov    $0x100000,%ebx
cmovb  %rdx,%rbx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0x8,%esi
call   1030 <byte_zero@plt>
movl   $0x8,0xc(%rsp)
lea    0xc(%rsp),%r9
mov    %ebp,%edi
mov    %r13,%rsi
mov    %rbx,%rdx
xor    %ecx,%ecx
mov    %r15,%r8
call   1040 <recvfrom@plt>
mov    %eax,%ebx
lea    0x14(%rsp),%rdx
mov    %r12,%rdi
mov    $0x4,%esi
call   1050 <byte_copy@plt>
mov    %r14,%rdi
mov    $0x2,%esi
mov    %r15,%rdx
call   1050 <byte_copy@plt>
movslq %ebx,%rax
jmp    11d1 <socket_recv+0xa1>
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    0x2e10(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffffffffffff,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@EPROTO = global i32 0
@errno = global i32 0
define i64 @socket_recv(i32 %0, i8* %1, i64 %2, i8* %3, i8* %4) {
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca %struct.sockaddr_in
%13 = alloca i32
%14 = alloca i32
store i32 %0, i32* %7
store i8* %1, i8** %8
store i64 %2, i64* %9
store i8* %3, i8** %10
store i8* %4, i8** %11
%15 = load i64, i64* %9
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %5
%18 = load i32, i32* @EPROTO
store i32 %18, i32* @errno
store i64 -1, i64* %6
br label %38
19: ; preds = %5
%20 = load i64, i64* %9
%21 = icmp sgt i64 %20, 1048576
br i1 %21, label %22, label %23
22: ; preds = %19
store i64 1048576, i64* %9
br label %23
23: ; preds = %22, %19
%24 = call i32 @byte_zero(%struct.sockaddr_in* %12, i32 8)
store i32 8, i32* %13
%25 = load i32, i32* %7
%26 = load i8*, i8** %8
%27 = load i64, i64* %9
%28 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%29 = call i32 @recvfrom(i32 %25, i8* %26, i64 %27, i32 0, %struct.sockaddr* %28, i32* %13)
store i32 %29, i32* %14
%30 = load i8*, i8** %10
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 1
%32 = call i32 @byte_copy(i8* %30, i32 4, i32* %31)
%33 = load i8*, i8** %11
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
%35 = call i32 @byte_copy(i8* %33, i32 2, i32* %34)
%36 = load i32, i32* %14
%37 = sext i32 %36 to i64
store i64 %37, i64* %6
br label %38
38: ; preds = %23, %17
%39 = load i64, i64* %6
ret i64 %39
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@EPROTO = global i32 0
@errno = global i32 0
define i64 @socket_recv(i32 %0, i8* %1, i64 %2, i8* %3, i8* %4) {
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = bitcast %struct.sockaddr_in* %6 to i8*
%9 = bitcast i32* %7 to i8*
%10 = icmp slt i64 %2, 0
br i1 %10, label %11, label %13
11: ; preds = %5
%12 = load i32, i32* @EPROTO
store i32 %12, i32* @errno
br label %24
13: ; preds = %5
%14 = icmp ult i64 %2, 1048576
%15 = select i1 %14, i64 %2, i64 1048576
%16 = call i32 @byte_zero(%struct.sockaddr_in* %6, i32 8)
store i32 8, i32* %7
%17 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%18 = call i32 @recvfrom(i32 %0, i8* %1, i64 %15, i32 0, %struct.sockaddr* %17, i32* %7)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1
%20 = call i32 @byte_copy(i8* %3, i32 4, i32* %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0
%22 = call i32 @byte_copy(i8* %4, i32 2, i32* %21)
%23 = sext i32 %18 to i64
br label %24
24: ; preds = %13, %11
%25 = phi i64 [ -1, %11 ], [ %23, %13 ]
ret i64 %25
}