#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 timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ server_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EAGAIN;
scalar_t__ EWOULDBLOCK;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVTIMEO;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ logcat(char *, ...);
int recvfrom(int, unsigned char *, int, int /*<<< orphan*/, struct sockaddr *,
             int *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(scalar_t__);
int socket_recv_udp(int localport, unsigned char *buf, int len, int timeout) {
  int ret;
  int serverSocket;
  socklen_t sin_len;
  struct sockaddr_in server_addr;
  struct timeval tv;
  serverSocket = socket(AF_INET, SOCK_DGRAM, 0);
  tv.tv_sec = timeout;
  tv.tv_usec = 0;
  ret = setsockopt(serverSocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,
                   sizeof(struct timeval));
  if (ret < 0) {
    logcat("setsockopt: %s\n", strerror(errno));
    return -1;
  }
  server_addr.sin_family = AF_INET;
  server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  server_addr.sin_port = htons(localport);
  sin_len = sizeof(server_addr);
  ret =
      bind(serverSocket, (struct sockaddr *)&server_addr, sizeof(server_addr));
  if (ret != 0) {
    logcat("[tcp server] bind error!\n ");
    return -1;
  }
  ret = recvfrom(serverSocket, buf, len, 0, (struct sockaddr *)&server_addr,
                 &sin_len);
  if ((errno == EWOULDBLOCK) || (errno == EAGAIN))
    logcat("Socket UDP: Receive timeout.\n");
  close(serverSocket);
  return ret;
}


x86 assembly (clang, -O0):
<socket_recv_udp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x40(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x20(%rbp),%edi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
mov    0x2dde(%rip),%rax
mov    (%rax),%edx
lea    -0x40(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    122b <socket_recv_udp+0x9b>
mov    0x2db1(%rip),%rax
mov    (%rax),%rdi
call   10a0 <strerror@plt>
mov    %eax,%esi
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1070 <logcat@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fe <socket_recv_udp+0x16e>
mov    0x2dae(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x30(%rbp)
movl   $0xc,-0x24(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1292 <socket_recv_udp+0x102>
lea    0xd91(%rip),%rdi
mov    $0x0,%al
call   1070 <logcat@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fe <socket_recv_udp+0x16e>
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    -0x30(%rbp),%r8
xor    %ecx,%ecx
lea    -0x24(%rbp),%r9
call   1080 <recvfrom@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d03(%rip),%rax
mov    (%rax),%rax
mov    0x2cf1(%rip),%rcx
cmp    (%rcx),%rax
je     12e2 <socket_recv_udp+0x152>
mov    0x2ce9(%rip),%rax
mov    (%rax),%rax
mov    0x2cc7(%rip),%rcx
cmp    (%rcx),%rax
jne    12f0 <socket_recv_udp+0x160>
lea    0xd42(%rip),%rdi
mov    $0x0,%al
call   1070 <logcat@plt>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_recv_udp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %ecx,%ebp
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%r12d
mov    0x2e30(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%ebx
mov    %ebp,0x18(%rsp)
movq   $0x0,0x20(%rsp)
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
mov    0x2ddd(%rip),%rax
mov    (%rax),%edx
lea    0x18(%rsp),%rcx
mov    %ebx,%edi
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
js     1253 <socket_recv_udp+0xc3>
mov    0x0(%r13),%eax
mov    %eax,0x10(%rsp)
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0xc(%rsp)
mov    %r12d,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
movl   $0xc,0x4(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %eax,%eax
je     1279 <socket_recv_udp+0xe9>
lea    0xdcb(%rip),%rdi
xor    %eax,%eax
call   1070 <logcat@plt>
mov    $0xffffffff,%ebp
jmp    12cb <socket_recv_udp+0x13b>
mov    0x2d5e(%rip),%rax
mov    (%rax),%rdi
call   10a0 <strerror@plt>
lea    0xd97(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <logcat@plt>
mov    $0xffffffff,%ebp
jmp    12cb <socket_recv_udp+0x13b>
lea    0x8(%rsp),%r8
lea    0x4(%rsp),%r9
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14d,%edx
xor    %ecx,%ecx
call   1080 <recvfrom@plt>
mov    %eax,%ebp
mov    0x2d1d(%rip),%rax
mov    (%rax),%rax
mov    0x2d0b(%rip),%rcx
cmp    (%rcx),%rax
je     12b6 <socket_recv_udp+0x126>
mov    0x2cef(%rip),%rcx
cmp    (%rcx),%rax
jne    12c4 <socket_recv_udp+0x134>
lea    0xd6e(%rip),%rdi
xor    %eax,%eax
call   1070 <logcat@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.timeval = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@.str = [16 x i8] c"setsockopt: %s\0A\00"
@errno = global i64 0
@INADDR_ANY = global i32 0
@.str.1 = [27 x i8] c"[tcp server] bind error!\0A \00"
@EWOULDBLOCK = global i64 0
@EAGAIN = global i64 0
@.str.2 = [30 x i8] c"Socket UDP: Receive timeout.\0A\00"
define i32 @socket_recv_udp(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca %struct.sockaddr_in
%14 = alloca %struct.timeval
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%15 = load i32, i32* @AF_INET
%16 = load i32, i32* @SOCK_DGRAM
%17 = call i32 @socket(i32 %15, i32 %16, i32 0)
store i32 %17, i32* %11
%18 = load i32, i32* %9
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %14, i32 0, i32 0
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %14, i32 0, i32 1
store i64 0, i64* %20
%21 = load i32, i32* %11
%22 = load i32, i32* @SOL_SOCKET
%23 = load i32, i32* @SO_RCVTIMEO
%24 = bitcast %struct.timeval* %14 to i8*
%25 = call i32 @setsockopt(i32 %21, i32 %22, i32 %23, i8* %24, i32 16)
store i32 %25, i32* %10
%26 = load i32, i32* %10
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %4
%29 = load i64, i64* @errno
%30 = call i32 @strerror(i64 %29)
%31 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %30)
store i32 -1, i32* %5
br label %68
32: ; preds = %4
%33 = load i32, i32* @AF_INET
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 2
store i32 %33, i32* %34
%35 = load i32, i32* @INADDR_ANY
%36 = call i32 @htonl(i32 %35)
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 1
%38 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %37, i32 0, i32 0
store i32 %36, i32* %38
%39 = load i32, i32* %6
%40 = call i32 @htons(i32 %39)
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 0
store i32 %40, i32* %41
store i32 12, i32* %12
%42 = load i32, i32* %11
%43 = bitcast %struct.sockaddr_in* %13 to %struct.sockaddr*
%44 = call i32 @bind(i32 %42, %struct.sockaddr* %43, i32 12)
store i32 %44, i32* %10
%45 = load i32, i32* %10
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %32
%48 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
br label %68
49: ; preds = %32
%50 = load i32, i32* %11
%51 = load i8*, i8** %7
%52 = load i32, i32* %8
%53 = bitcast %struct.sockaddr_in* %13 to %struct.sockaddr*
%54 = call i32 @recvfrom(i32 %50, i8* %51, i32 %52, i32 0, %struct.sockaddr* %53, i32* %12)
store i32 %54, i32* %10
%55 = load i64, i64* @errno
%56 = load i64, i64* @EWOULDBLOCK
%57 = icmp eq i64 %55, %56
br i1 %57, label %62, label %58
58: ; preds = %49
%59 = load i64, i64* @errno
%60 = load i64, i64* @EAGAIN
%61 = icmp eq i64 %59, %60
br i1 %61, label %62, label %64
62: ; preds = %58, %49
%63 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %64
64: ; preds = %62, %58
%65 = load i32, i32* %11
%66 = call i32 @close(i32 %65)
%67 = load i32, i32* %10
store i32 %67, i32* %5
br label %68
68: ; preds = %64, %47, %28
%69 = load i32, i32* %5
ret i32 %69
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.timeval = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@.str = [16 x i8] c"setsockopt: %s\0A\00"
@errno = global i64 0
@INADDR_ANY = global i32 0
@.str.1 = [27 x i8] c"[tcp server] bind error!\0A \00"
@EWOULDBLOCK = global i64 0
@EAGAIN = global i64 0
@.str.2 = [30 x i8] c"Socket UDP: Receive timeout.\0A\00"
define i32 @socket_recv_udp(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.timeval
%8 = bitcast i32* %5 to i8*
%9 = bitcast %struct.sockaddr_in* %6 to i8*
%10 = bitcast %struct.timeval* %7 to i8*
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_DGRAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 0
store i32 %3, i32* %14
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 1
store i64 0, i64* %15
%16 = load i32, i32* @SOL_SOCKET
%17 = load i32, i32* @SO_RCVTIMEO
%18 = call i32 @setsockopt(i32 %13, i32 %16, i32 %17, i8* %10, i32 16)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %4
%21 = load i64, i64* @errno
%22 = call i32 @strerror(i64 %21)
%23 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %22)
br label %49
24: ; preds = %4
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 2
store i32 %25, i32* %26
%27 = load i32, i32* @INADDR_ANY
%28 = call i32 @htonl(i32 %27)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1, i32 0
store i32 %28, i32* %29
%30 = call i32 @htons(i32 %0)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0
store i32 %30, i32* %31
store i32 12, i32* %5
%32 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%33 = call i32 @bind(i32 %13, %struct.sockaddr* %32, i32 12)
%34 = icmp eq i32 %33, 0
br i1 %34, label %37, label %35
35: ; preds = %24
%36 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %49
37: ; preds = %24
%38 = call i32 @recvfrom(i32 %13, i8* %1, i32 %2, i32 0, %struct.sockaddr* %32, i32* %5)
%39 = load i64, i64* @errno
%40 = load i64, i64* @EWOULDBLOCK
%41 = icmp eq i64 %39, %40
%42 = load i64, i64* @EAGAIN
%43 = icmp eq i64 %39, %42
%44 = select i1 %41, i1 true, i1 %43
br i1 %44, label %45, label %47
45: ; preds = %37
%46 = call i32 (i8*, ...) @logcat(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %47
47: ; preds = %37, %45
%48 = call i32 @close(i32 %13)
br label %49
49: ; preds = %47, %35, %20
%50 = phi i32 [ -1, %20 ], [ -1, %35 ], [ %38, %47 ]
ret i32 %50
}