#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 dummy;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, scalar_t__);
int recvfrom(int, void *, int /*<<< orphan*/, int /*<<< orphan*/,
             struct sockaddr *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ udp_socket_error();
int udp_socket_receive(int s, int dstport, void *data, socklen_t len) {
  int rc;
  struct sockaddr_in from;
  rc = recvfrom(s, data, len, 0, (struct sockaddr *)&from, &len);
  if ((rc < 0) && (errno != EAGAIN)) {
    fprintf(stderr, "could not receive, errno=%d\n", errno);
    udp_socket_error();
  }
  return rc;
}


x86 assembly (clang, -O0):
<udp_socket_receive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    -0x20(%rbp),%r8
xor    %ecx,%ecx
lea    -0x14(%rbp),%r9
call   1050 <recvfrom@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11ab <udp_socket_receive+0x7b>
mov    0x2e66(%rip),%rax
mov    (%rax),%rax
mov    0x2e4c(%rip),%rcx
cmp    (%rcx),%rax
je     11ab <udp_socket_receive+0x7b>
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
mov    0x2e43(%rip),%rax
mov    (%rax),%rdx
lea    0xe61(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x0,%al
call   1040 <udp_socket_error@plt>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp_socket_receive>:
push   %rbx
sub    $0x10,%rsp
mov    %ecx,0x4(%rsp)
lea    0x8(%rsp),%r8
lea    0x4(%rsp),%r9
mov    %rdx,%rsi
mov    %ecx,%edx
xor    %ecx,%ecx
call   1050 <recvfrom@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1187 <udp_socket_receive+0x57>
mov    0x2e7c(%rip),%rax
mov    (%rax),%rdx
mov    0x2e62(%rip),%rax
cmp    (%rax),%rdx
je     1187 <udp_socket_receive+0x57>
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
lea    0xe85(%rip),%rsi
call   1030 <fprintf@plt>
xor    %eax,%eax
call   1040 <udp_socket_error@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EAGAIN = global i64 0
@stderr = global i32 0
@.str = [29 x i8] c"could not receive, errno=%d\0A\00"
define i32 @udp_socket_receive(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%11 = load i32, i32* %5
%12 = load i8*, i8** %7
%13 = load i32, i32* %8
%14 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%15 = call i32 @recvfrom(i32 %11, i8* %12, i32 %13, i32 0, %struct.sockaddr* %14, i32* %8)
store i32 %15, i32* %9
%16 = load i32, i32* %9
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %27
18: ; preds = %4
%19 = load i64, i64* @errno
%20 = load i64, i64* @EAGAIN
%21 = icmp ne i64 %19, %20
br i1 %21, label %22, label %27
22: ; preds = %18
%23 = load i32, i32* @stderr
%24 = load i64, i64* @errno
%25 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i64 %24)
%26 = call i32 (...) @udp_socket_error()
br label %27
27: ; preds = %22, %18, %4
%28 = load i32, i32* %9
ret i32 %28
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EAGAIN = global i64 0
@stderr = global i32 0
@.str = [29 x i8] c"could not receive, errno=%d\0A\00"
define i32 @udp_socket_receive(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
store i32 %3, i32* %5
%7 = bitcast %struct.sockaddr_in* %6 to i8*
%8 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%9 = call i32 @recvfrom(i32 %0, i8* %2, i32 %3, i32 0, %struct.sockaddr* %8, i32* %5)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %19
11: ; preds = %4
%12 = load i64, i64* @errno
%13 = load i64, i64* @EAGAIN
%14 = icmp eq i64 %12, %13
br i1 %14, label %19, label %15
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i64 %12)
%18 = call i32 (...) @udp_socket_error()
br label %19
19: ; preds = %15, %11, %4
ret i32 %9
}