#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 /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ sender_si;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EWOULDBLOCK;
int /*<<< orphan*/ MSG_DONTWAIT;
scalar_t__ errno;
char *inet_ntoa(int /*<<< orphan*/);
int ntohs(int /*<<< orphan*/);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *,
             int /*<<< orphan*/ *);
int receive_udp_datagram(void *buf, int max, int socket, char **sender_host,
                         int *sender_port) {
  struct sockaddr_in sender_si;
  int slen = sizeof(sender_si);
  int nr;
  nr = recvfrom(socket, (char *)buf, 65535, MSG_DONTWAIT,
                (struct sockaddr *)&sender_si, (socklen_t *)&slen);
  if (nr == -1) {
    if (errno == EAGAIN || errno == EWOULDBLOCK)
      return 0;
    return (-1);
  }
  if (sender_host)
    *sender_host = inet_ntoa(sender_si.sin_addr);
  if (sender_port)
    *sender_port = ntohs(sender_si.sin_port);
  return nr;
}


x86 assembly (clang, -O0):
<receive_udp_datagram>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
movl   $0x8,-0x34(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e71(%rip),%rax
mov    (%rax),%ecx
lea    -0x30(%rbp),%r8
mov    $0xffff,%edx
lea    -0x34(%rbp),%r9
call   1040 <recvfrom@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
jne    11cc <receive_udp_datagram+0x9c>
mov    0x2e59(%rip),%rax
mov    (%rax),%rax
mov    0x2e2f(%rip),%rcx
cmp    (%rcx),%rax
je     11b4 <receive_udp_datagram+0x84>
mov    0x2e3f(%rip),%rax
mov    (%rax),%rax
mov    0x2e2d(%rip),%rcx
cmp    (%rcx),%rax
jne    11c0 <receive_udp_datagram+0x90>
movl   $0x0,-0x4(%rbp)
jmp    120a <receive_udp_datagram+0xda>
movl   $0xffffffff,-0x4(%rbp)
jmp    120a <receive_udp_datagram+0xda>
cmpq   $0x0,-0x20(%rbp)
je     11e9 <receive_udp_datagram+0xb9>
mov    -0x2c(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x28(%rbp)
je     1204 <receive_udp_datagram+0xd4>
mov    -0x30(%rbp),%edi
call   1050 <ntohs@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receive_udp_datagram>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %r8,%r14
mov    %rcx,%rbx
mov    %rdi,%rsi
movl   $0x8,0x4(%rsp)
mov    0x2e80(%rip),%rax
mov    (%rax),%ecx
lea    0x8(%rsp),%r8
lea    0x4(%rsp),%r9
mov    %edx,%edi
mov    $0xffff,%edx
call   1040 <recvfrom@plt>
cmp    $0xffffffff,%eax
je     1193 <receive_udp_datagram+0x63>
mov    %eax,%ebp
test   %rbx,%rbx
je     1180 <receive_udp_datagram+0x50>
mov    0xc(%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,(%rbx)
test   %r14,%r14
je     11be <receive_udp_datagram+0x8e>
mov    0x8(%rsp),%edi
call   1050 <ntohs@plt>
mov    %eax,(%r14)
jmp    11be <receive_udp_datagram+0x8e>
mov    0x2e46(%rip),%rax
mov    (%rax),%rax
mov    0x2e1c(%rip),%rcx
cmp    (%rcx),%rax
setne  %cl
mov    0x2e27(%rip),%rdx
cmp    (%rdx),%rax
setne  %al
and    %cl,%al
movzbl %al,%ebp
neg    %ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@MSG_DONTWAIT = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
define i32 @receive_udp_datagram(i8* %0, i32 %1, i32 %2, i8** %3, i32* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8**
%11 = alloca i32*
%12 = alloca %struct.sockaddr_in
%13 = alloca i32
%14 = alloca i32
store i8* %0, i8** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i8** %3, i8*** %10
store i32* %4, i32** %11
store i32 8, i32* %13
%15 = load i32, i32* %9
%16 = load i8*, i8** %7
%17 = load i32, i32* @MSG_DONTWAIT
%18 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%19 = call i32 @recvfrom(i32 %15, i8* %16, i32 65535, i32 %17, %struct.sockaddr* %18, i32* %13)
store i32 %19, i32* %14
%20 = load i32, i32* %14
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %32
22: ; preds = %5
%23 = load i64, i64* @errno
%24 = load i64, i64* @EAGAIN
%25 = icmp eq i64 %23, %24
br i1 %25, label %30, label %26
26: ; preds = %22
%27 = load i64, i64* @errno
%28 = load i64, i64* @EWOULDBLOCK
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %31
30: ; preds = %26, %22
store i32 0, i32* %6
br label %50
31: ; preds = %26
store i32 -1, i32* %6
br label %50
32: ; preds = %5
%33 = load i8**, i8*** %10
%34 = icmp ne i8** %33, null
br i1 %34, label %35, label %40
35: ; preds = %32
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 1
%37 = load i32, i32* %36
%38 = call i8* @inet_ntoa(i32 %37)
%39 = load i8**, i8*** %10
store i8* %38, i8** %39
br label %40
40: ; preds = %35, %32
%41 = load i32*, i32** %11
%42 = icmp ne i32* %41, null
br i1 %42, label %43, label %48
43: ; preds = %40
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
%45 = load i32, i32* %44
%46 = call i32 @ntohs(i32 %45)
%47 = load i32*, i32** %11
store i32 %46, i32* %47
br label %48
48: ; preds = %43, %40
%49 = load i32, i32* %14
store i32 %49, i32* %6
br label %50
50: ; preds = %48, %31, %30
%51 = load i32, i32* %6
ret i32 %51
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@MSG_DONTWAIT = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
define i32 @receive_udp_datagram(i8* %0, i32 %1, i32 %2, i8** writeonly %3, i32* writeonly %4) {
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = bitcast %struct.sockaddr_in* %6 to i8*
%9 = bitcast i32* %7 to i8*
store i32 8, i32* %7
%10 = load i32, i32* @MSG_DONTWAIT
%11 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%12 = call i32 @recvfrom(i32 %2, i8* %0, i32 65535, i32 %10, %struct.sockaddr* %11, i32* %7)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %22
14: ; preds = %5
%15 = load i64, i64* @errno
%16 = load i64, i64* @EAGAIN
%17 = icmp ne i64 %15, %16
%18 = load i64, i64* @EWOULDBLOCK
%19 = icmp ne i64 %15, %18
%20 = select i1 %17, i1 %19, i1 false
%21 = sext i1 %20 to i32
br label %34
22: ; preds = %5
%23 = icmp eq i8** %3, null
br i1 %23, label %28, label %24
24: ; preds = %22
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1
%26 = load i32, i32* %25
%27 = call i8* @inet_ntoa(i32 %26)
store i8* %27, i8** %3
br label %28
28: ; preds = %24, %22
%29 = icmp eq i32* %4, null
br i1 %29, label %34, label %30
30: ; preds = %28
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @ntohs(i32 %32)
store i32 %33, i32* %4
br label %34
34: ; preds = %14, %28, %30
%35 = phi i32 [ %12, %30 ], [ %12, %28 ], [ %21, %14 ]
ret i32 %35
}