#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct stat {
  int dummy;
};
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef scalar_t__ ssize_t;
typedef int socklen_t;
typedef int /*<<< orphan*/ sa;
typedef int /*<<< orphan*/ in_port_t;
typedef int /*<<< orphan*/ in_addr_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ MSG_DONTWAIT;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ fstat(int, struct stat *);
int /*<<< orphan*/ ntohl(int /*<<< orphan*/);
int /*<<< orphan*/ ntohs(int /*<<< orphan*/);
scalar_t__ recvfrom(int, void *, size_t, int /*<<< orphan*/, struct sockaddr *,
                    int *);
int /*<<< orphan*/ stderr;
int udt_recv(int udt, void *buff, size_t nbytes, in_addr_t *addr,
             in_port_t *port) {
  struct stat info;
  if (fstat(udt, &info) != 0) {
    fprintf(stderr,
            "UDT: descriptor is invalid, probably udt_init not called.\n");
    exit(EXIT_FAILURE);
  }
  struct sockaddr_in sa;
  bzero(&sa, sizeof(sa));
  socklen_t salen = sizeof(sa);
  ssize_t nrecv =
      recvfrom(udt, buff, nbytes, MSG_DONTWAIT, (struct sockaddr *)&sa, &salen);
  if (addr != NULL)
    (*addr) = ntohl(sa.sin_addr.s_addr);
  if (port != NULL)
    (*port) = ntohs(sa.sin_port);
  if (nrecv < 0)
    nrecv = 0;
  return nrecv;
}


x86 assembly (clang, -O0):
<udt_recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0x0,%rax
je     11c4 <udt_recv+0x54>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
lea    -0x38(%rbp),%rdi
mov    $0x8,%esi
call   1060 <bzero@plt>
movl   $0x8,-0x3c(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    0x2ded(%rip),%rax
mov    (%rax),%ecx
lea    -0x38(%rbp),%r8
lea    -0x3c(%rbp),%r9
call   1040 <recvfrom@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1219 <udt_recv+0xa9>
mov    -0x34(%rbp),%edi
call   1080 <ntohl@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
cmpq   $0x0,-0x28(%rbp)
je     1234 <udt_recv+0xc4>
mov    -0x38(%rbp),%edi
call   1050 <ntohs@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
cmpq   $0x0,-0x48(%rbp)
jge    1247 <udt_recv+0xd7>
movq   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udt_recv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,%r14
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0x10(%rsp),%rsi
call   1090 <fstat@plt>
test   %rax,%rax
jne    1211 <udt_recv+0xa1>
lea    0x8(%rsp),%r13
mov    %r13,%rdi
mov    $0x8,%esi
call   1060 <bzero@plt>
movl   $0x8,0x4(%rsp)
mov    0x2e1c(%rip),%rax
mov    (%rax),%ecx
lea    0x4(%rsp),%r9
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
mov    %r13,%r8
call   1040 <recvfrom@plt>
mov    %rax,%rbx
test   %r15,%r15
je     11e7 <udt_recv+0x77>
mov    0xc(%rsp),%edi
call   1080 <ntohl@plt>
mov    %eax,(%r15)
test   %r14,%r14
je     11f8 <udt_recv+0x88>
mov    0x8(%rsp),%edi
call   1050 <ntohs@plt>
mov    %eax,(%r14)
xor    %eax,%eax
test   %rbx,%rbx
cmovle %eax,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
lea    0xddf(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
%struct.sockaddr_in = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [59 x i8] c"UDT: descriptor is invalid, probably udt_init not called.\0A\00"
@EXIT_FAILURE = global i32 0
@MSG_DONTWAIT = global i32 0
define i32 @udt_recv(i32 %0, i8* %1, i64 %2, i32* %3, i32* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca %struct.stat
%12 = alloca %struct.sockaddr_in
%13 = alloca i32
%14 = alloca i64
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32* %3, i32** %9
store i32* %4, i32** %10
%15 = load i32, i32* %6
%16 = call i64 @fstat(i32 %15, %struct.stat* %11)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %5
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %5
%24 = call i32 @bzero(%struct.sockaddr_in* %12, i32 8)
store i32 8, i32* %13
%25 = load i32, i32* %6
%26 = load i8*, i8** %7
%27 = load i64, i64* %8
%28 = load i32, i32* @MSG_DONTWAIT
%29 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%30 = call i64 @recvfrom(i32 %25, i8* %26, i64 %27, i32 %28, %struct.sockaddr* %29, i32* %13)
store i64 %30, i64* %14
%31 = load i32*, i32** %9
%32 = icmp ne i32* %31, null
br i1 %32, label %33, label %39
33: ; preds = %23
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 1
%35 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %34, i32 0, i32 0
%36 = load i32, i32* %35
%37 = call i32 @ntohl(i32 %36)
%38 = load i32*, i32** %9
store i32 %37, i32* %38
br label %39
39: ; preds = %33, %23
%40 = load i32*, i32** %10
%41 = icmp ne i32* %40, null
br i1 %41, label %42, label %47
42: ; preds = %39
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
%44 = load i32, i32* %43
%45 = call i32 @ntohs(i32 %44)
%46 = load i32*, i32** %10
store i32 %45, i32* %46
br label %47
47: ; preds = %42, %39
%48 = load i64, i64* %14
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %47
store i64 0, i64* %14
br label %51
51: ; preds = %50, %47
%52 = load i64, i64* %14
%53 = trunc i64 %52 to i32
ret i32 %53
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.sockaddr_in = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [59 x i8] c"UDT: descriptor is invalid, probably udt_init not called.\0A\00"
@EXIT_FAILURE = global i32 0
@MSG_DONTWAIT = global i32 0
define i32 @udt_recv(i32 %0, i8* %1, i64 %2, i32* writeonly %3, i32* writeonly %4) {
%6 = alloca %struct.stat
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = bitcast %struct.stat* %6 to i8*
%10 = call i64 @fstat(i32 %0, %struct.stat* %6)
%11 = icmp eq i64 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %5
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %5
%18 = bitcast %struct.sockaddr_in* %7 to i8*
%19 = call i32 @bzero(%struct.sockaddr_in* %7, i32 8)
%20 = bitcast i32* %8 to i8*
store i32 8, i32* %8
%21 = load i32, i32* @MSG_DONTWAIT
%22 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%23 = call i64 @recvfrom(i32 %0, i8* %1, i64 %2, i32 %21, %struct.sockaddr* %22, i32* %8)
%24 = icmp eq i32* %3, null
br i1 %24, label %29, label %25
25: ; preds = %17
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 1, i32 0
%27 = load i32, i32* %26
%28 = call i32 @ntohl(i32 %27)
store i32 %28, i32* %3
br label %29
29: ; preds = %25, %17
%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* %7, 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 = %31, %29
%36 = icmp sgt i64 %23, 0
%37 = select i1 %36, i64 %23, i64 0
%38 = trunc i64 %37 to i32
ret i32 %38
}