#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*/ sender_si;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EWOULDBLOCK;
int /*<<< orphan*/ MSG_DONTWAIT;
int /*<<< orphan*/ diep(char *);
scalar_t__ errno;
char *inet_ntoa(int /*<<< orphan*/);
int ntohs(int /*<<< orphan*/);
int recvfrom(int, void *, int, int /*<<< orphan*/, struct sockaddr *, int *);
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, buf, 65535, MSG_DONTWAIT, (struct sockaddr *)&sender_si,
                &slen);
  if (nr == -1) {
    if (errno == EAGAIN || errno == EWOULDBLOCK)
      return 0;
    diep("recvfrom");
  }
  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    0x2e61(%rip),%rax
mov    (%rax),%ecx
lea    -0x30(%rbp),%r8
mov    $0xffff,%edx
lea    -0x34(%rbp),%r9
call   1050 <recvfrom@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
jne    11dc <receive_udp_datagram+0x9c>
mov    0x2e49(%rip),%rax
mov    (%rax),%rax
mov    0x2e1f(%rip),%rcx
cmp    (%rcx),%rax
je     11c4 <receive_udp_datagram+0x84>
mov    0x2e2f(%rip),%rax
mov    (%rax),%rax
mov    0x2e1d(%rip),%rcx
cmp    (%rcx),%rax
jne    11d0 <receive_udp_datagram+0x90>
movl   $0x0,-0x4(%rbp)
jmp    121a <receive_udp_datagram+0xda>
lea    0xe29(%rip),%rdi
call   1040 <diep@plt>
cmpq   $0x0,-0x20(%rbp)
je     11f9 <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     1214 <receive_udp_datagram+0xd4>
mov    -0x30(%rbp),%edi
call   1060 <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    0x2e70(%rip),%rax
mov    (%rax),%ecx
lea    0x8(%rsp),%r8
lea    0x4(%rsp),%r9
mov    %edx,%edi
mov    $0xffff,%edx
call   1050 <recvfrom@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     11ac <receive_udp_datagram+0x6c>
test   %rbx,%rbx
je     1190 <receive_udp_datagram+0x50>
mov    0xc(%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,(%rbx)
test   %r14,%r14
je     11a1 <receive_udp_datagram+0x61>
mov    0x8(%rsp),%edi
call   1060 <ntohs@plt>
mov    %eax,(%r14)
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e2d(%rip),%rax
mov    (%rax),%rcx
mov    0x2e03(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
je     11a3 <receive_udp_datagram+0x63>
mov    0x2e0d(%rip),%rdx
cmp    (%rdx),%rcx
je     11a3 <receive_udp_datagram+0x63>
lea    0xe29(%rip),%rdi
call   1040 <diep@plt>
test   %rbx,%rbx
jne    1184 <receive_udp_datagram+0x44>
jmp    1190 <receive_udp_datagram+0x50>

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
@.str = [9 x i8] c"recvfrom\00"
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 %33
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 %51
31: ; preds = %26
%32 = call i32 @diep(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %33
33: ; preds = %31, %5
%34 = load i8**, i8*** %10
%35 = icmp ne i8** %34, null
br i1 %35, label %36, label %41
36: ; preds = %33
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 1
%38 = load i32, i32* %37
%39 = call i8* @inet_ntoa(i32 %38)
%40 = load i8**, i8*** %10
store i8* %39, i8** %40
br label %41
41: ; preds = %36, %33
%42 = load i32*, i32** %11
%43 = icmp ne i32* %42, null
br i1 %43, label %44, label %49
44: ; preds = %41
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
%46 = load i32, i32* %45
%47 = call i32 @ntohs(i32 %46)
%48 = load i32*, i32** %11
store i32 %47, i32* %48
br label %49
49: ; preds = %44, %41
%50 = load i32, i32* %14
store i32 %50, i32* %6
br label %51
51: ; preds = %49, %30
%52 = load i32, i32* %6
ret i32 %52
}

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
@.str = [9 x i8] c"recvfrom\00"
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 %23
14: ; preds = %5
%15 = load i64, i64* @errno
%16 = load i64, i64* @EAGAIN
%17 = icmp eq i64 %15, %16
%18 = load i64, i64* @EWOULDBLOCK
%19 = icmp eq i64 %15, %18
%20 = select i1 %17, i1 true, i1 %19
br i1 %20, label %35, label %21
21: ; preds = %14
%22 = call i32 @diep(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %21, %5
%24 = icmp eq i8** %3, null
br i1 %24, label %29, label %25
25: ; preds = %23
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1
%27 = load i32, i32* %26
%28 = call i8* @inet_ntoa(i32 %27)
store i8* %28, i8** %3
br label %29
29: ; preds = %25, %23
%30 = icmp eq i32* %4, null
br i1 %30, label %35, label %31
31: ; preds = %29
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @ntohs(i32 %33)
store i32 %34, i32* %4
br label %35
35: ; preds = %29, %31, %14
%36 = phi i32 [ 0, %14 ], [ %12, %31 ], [ %12, %29 ]
ret i32 %36
}