#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 */
typedef int /*<<< orphan*/ uint8_t;
struct timeval {
  scalar_t__ tv_usec;
  scalar_t__ tv_sec;
};
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ clientaddr;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ MSG_DONTWAIT;
scalar_t__ errno;
int recvfrom(int, int /*<<< orphan*/ *, size_t, int /*<<< orphan*/,
             struct sockaddr *, int *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int recvfrom_u(int sock, uint8_t *buf, size_t buflen, struct timeval wait,
               struct sockaddr_in *from) {
  int bytes = 0;
  int ret = -1;
  fd_set rset;
  struct timeval tv;
  static struct sockaddr_in clientaddr;
  static socklen_t clilen = sizeof(clientaddr);
  tv.tv_sec = wait.tv_sec;
  tv.tv_usec = wait.tv_usec;
RE_RECV:
  if (tv.tv_sec || tv.tv_usec) {
    FD_ZERO(&rset);
    FD_SET(sock, &rset);
    ret = select(sock + 1, &rset, (fd_set *)0, (fd_set *)0, &tv);
    if (ret == -1) {
      if (errno == EINTR) {
        goto RE_RECV;
      }
      return -1;
    } else if (ret == 0) {
      return 0;
    }
    if (!FD_ISSET(sock, &rset)) {
      return -1;
    }
    bytes = recvfrom(sock, buf, buflen, MSG_DONTWAIT,
                     from ? (struct sockaddr *)from
                          : (struct sockaddr *)&clientaddr,
                     &clilen);
    if (bytes <= 0) {
      return -1;
    }
    return bytes;
  }
  bytes = recvfrom(
      sock, buf, buflen, 0,
      from ? (struct sockaddr *)from : (struct sockaddr *)&clientaddr, &clilen);
  if (bytes <= 0) {
    return -1;
  }
  return bytes;
}


x86 assembly (clang, -O0):
<recvfrom_u>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rcx,-0x18(%rbp)
mov    %r8,-0x10(%rbp)
mov    %edi,-0x1c(%rbp)
mov    %rsi,-0x28(%rbp)
mov    %rdx,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0xffffffff,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    11a6 <recvfrom_u+0x56>
cmpq   $0x0,-0x58(%rbp)
je     12d5 <recvfrom_u+0x185>
lea    -0x44(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x1c(%rbp),%edi
lea    -0x44(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
lea    -0x44(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x58(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0xffffffff,-0x40(%rbp)
jne    120d <recvfrom_u+0xbd>
mov    0x2df7(%rip),%rax
mov    (%rax),%rax
mov    0x2ddd(%rip),%rcx
cmp    (%rcx),%rax
jne    1201 <recvfrom_u+0xb1>
jmp    1190 <recvfrom_u+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    136a <recvfrom_u+0x21a>
cmpl   $0x0,-0x40(%rbp)
jne    1223 <recvfrom_u+0xd3>
movl   $0x0,-0x4(%rbp)
jmp    136a <recvfrom_u+0x21a>
jmp    1228 <recvfrom_u+0xd8>
mov    -0x1c(%rbp),%edi
lea    -0x44(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%eax
jne    1249 <recvfrom_u+0xf9>
movl   $0xffffffff,-0x4(%rbp)
jmp    136a <recvfrom_u+0x21a>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x74(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    0x2d72(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1283 <recvfrom_u+0x133>
mov    -0x38(%rbp),%rax
mov    %rax,-0x80(%rbp)
jmp    1293 <recvfrom_u+0x143>
lea    0x2dce(%rip),%rax
mov    %rax,-0x80(%rbp)
jmp    1293 <recvfrom_u+0x143>
mov    -0x5c(%rbp),%ecx
mov    -0x68(%rbp),%rdx
mov    -0x70(%rbp),%rsi
mov    -0x74(%rbp),%edi
mov    -0x80(%rbp),%r8
lea    0x2d9c(%rip),%r9
call   1060 <recvfrom@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jg     12ca <recvfrom_u+0x17a>
movl   $0xffffffff,-0x4(%rbp)
jmp    136a <recvfrom_u+0x21a>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    136a <recvfrom_u+0x21a>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x94(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x90(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x88(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     130f <recvfrom_u+0x1bf>
mov    -0x38(%rbp),%rax
mov    %rax,-0xa0(%rbp)
jmp    1322 <recvfrom_u+0x1d2>
lea    0x2d42(%rip),%rax
mov    %rax,-0xa0(%rbp)
jmp    1322 <recvfrom_u+0x1d2>
mov    -0x88(%rbp),%rdx
mov    -0x90(%rbp),%rsi
mov    -0x94(%rbp),%edi
mov    -0xa0(%rbp),%r8
xor    %ecx,%ecx
lea    0x2d02(%rip),%r9
call   1060 <recvfrom@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jg     1364 <recvfrom_u+0x214>
movl   $0xffffffff,-0x4(%rbp)
jmp    136a <recvfrom_u+0x21a>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recvfrom_u>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r9,0x10(%rsp)
mov    %rdx,0x20(%rsp)
mov    %rsi,0x18(%rsp)
mov    %edi,%r13d
mov    %r8,0x30(%rsp)
mov    %rcx,0x28(%rsp)
or     %rcx,%r8
je     11e2 <recvfrom_u+0x92>
lea    0x1(%r13),%ebp
lea    0xc(%rsp),%rbx
lea    0x28(%rsp),%r14
mov    0x2e4c(%rip),%r15
mov    0x2e35(%rip),%r12
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1040 <FD_ZERO@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
call   1070 <FD_SET@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r14,%r8
call   1050 <select@plt>
cmp    $0xffffffff,%eax
jne    122b <recvfrom_u+0xdb>
mov    (%r15),%rax
cmp    (%r12),%rax
jne    1272 <recvfrom_u+0x122>
mov    0x30(%rsp),%rax
or     0x28(%rsp),%rax
jne    11a0 <recvfrom_u+0x50>
mov    0x10(%rsp),%rax
test   %rax,%rax
lea    0x2e67(%rip),%r8
cmovne %rax,%r8
lea    0x2e4c(%rip),%r9
mov    %r13d,%edi
mov    0x18(%rsp),%rsi
mov    0x20(%rsp),%rdx
xor    %ecx,%ecx
call   1060 <recvfrom@plt>
mov    %eax,%ecx
test   %eax,%eax
mov    $0xffffffff,%eax
cmovg  %ecx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
test   %eax,%eax
je     121c <recvfrom_u+0xcc>
lea    0xc(%rsp),%rsi
mov    %r13d,%edi
call   1030 <FD_ISSET@plt>
test   %eax,%eax
je     1272 <recvfrom_u+0x122>
mov    0x2d91(%rip),%rax
mov    (%rax),%ecx
mov    0x10(%rsp),%rax
test   %rax,%rax
lea    0x2e00(%rip),%r8
cmovne %rax,%r8
lea    0x2de5(%rip),%r9
mov    %r13d,%edi
mov    0x18(%rsp),%rsi
mov    0x20(%rsp),%rdx
jmp    120b <recvfrom_u+0xbb>
mov    $0xffffffff,%eax
jmp    121c <recvfrom_u+0xcc>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.timeval = type { i64, i64 }
%struct.sockaddr = type { i32 }
@recvfrom_u.clientaddr = internal global %struct.sockaddr_in zeroinitializer
@recvfrom_u.clilen = internal global i32 4
@errno = global i64 0
@EINTR = global i64 0
@MSG_DONTWAIT = global i32 0
define i32 @recvfrom_u(i32 %0, i32* %1, i64 %2, i64 %3, i64 %4, %struct.sockaddr_in* %5) {
%7 = alloca i32
%8 = alloca %struct.timeval
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i64
%12 = alloca %struct.sockaddr_in*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca %struct.timeval
%17 = bitcast %struct.timeval* %8 to { i64, i64 }*
%18 = getelementptr inbounds { i64, i64 }, { i64, i64 }* %17, i32 0, i32 0
store i64 %3, i64* %18
%19 = getelementptr inbounds { i64, i64 }, { i64, i64 }* %17, i32 0, i32 1
store i64 %4, i64* %19
store i32 %0, i32* %9
store i32* %1, i32** %10
store i64 %2, i64* %11
store %struct.sockaddr_in* %5, %struct.sockaddr_in** %12
store i32 0, i32* %13
store i32 -1, i32* %14
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
%21 = load i64, i64* %20
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %16, i32 0, i32 1
store i64 %21, i64* %22
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
%24 = load i64, i64* %23
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %16, i32 0, i32 0
store i64 %24, i64* %25
br label %26
26: ; preds = %47, %6
%27 = getelementptr inbounds %struct.timeval, %struct.timeval* %16, i32 0, i32 1
%28 = load i64, i64* %27
%29 = icmp ne i64 %28, 0
br i1 %29, label %34, label %30
30: ; preds = %26
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %16, i32 0, i32 0
%32 = load i64, i64* %31
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %78
34: ; preds = %30, %26
%35 = call i32 @FD_ZERO(i32* %15)
%36 = load i32, i32* %9
%37 = call i32 @FD_SET(i32 %36, i32* %15)
%38 = load i32, i32* %9
%39 = add nsw i32 %38, 1
%40 = call i32 @select(i32 %39, i32* %15, i32* null, i32* null, %struct.timeval* %16)
store i32 %40, i32* %14
%41 = load i32, i32* %14
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %49
43: ; preds = %34
%44 = load i64, i64* @errno
%45 = load i64, i64* @EINTR
%46 = icmp eq i64 %44, %45
br i1 %46, label %47, label %48
47: ; preds = %43
br label %26
48: ; preds = %43
store i32 -1, i32* %7
br label %96
49: ; preds = %34
%50 = load i32, i32* %14
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %49
store i32 0, i32* %7
br label %96
53: ; preds = %49
br label %54
54: ; preds = %53
%55 = load i32, i32* %9
%56 = call i32 @FD_ISSET(i32 %55, i32* %15)
%57 = icmp ne i32 %56, 0
br i1 %57, label %59, label %58
58: ; preds = %54
store i32 -1, i32* %7
br label %96
59: ; preds = %54
%60 = load i32, i32* %9
%61 = load i32*, i32** %10
%62 = load i64, i64* %11
%63 = load i32, i32* @MSG_DONTWAIT
%64 = load %struct.sockaddr_in*, %struct.sockaddr_in** %12
%65 = icmp ne %struct.sockaddr_in* %64, null
br i1 %65, label %66, label %69
66: ; preds = %59
%67 = load %struct.sockaddr_in*, %struct.sockaddr_in** %12
%68 = bitcast %struct.sockaddr_in* %67 to %struct.sockaddr*
br label %70
69: ; preds = %59
br label %70
70: ; preds = %69, %66
%71 = phi %struct.sockaddr* [ %68, %66 ], [ bitcast (%struct.sockaddr_in* @recvfrom_u.clientaddr to %struct.sockaddr*), %69 ]
%72 = call i32 @recvfrom(i32 %60, i32* %61, i64 %62, i32 %63, %struct.sockaddr* %71, i32* @recvfrom_u.clilen)
store i32 %72, i32* %13
%73 = load i32, i32* %13
%74 = icmp sle i32 %73, 0
br i1 %74, label %75, label %76
75: ; preds = %70
store i32 -1, i32* %7
br label %96
76: ; preds = %70
%77 = load i32, i32* %13
store i32 %77, i32* %7
br label %96
78: ; preds = %30
%79 = load i32, i32* %9
%80 = load i32*, i32** %10
%81 = load i64, i64* %11
%82 = load %struct.sockaddr_in*, %struct.sockaddr_in** %12
%83 = icmp ne %struct.sockaddr_in* %82, null
br i1 %83, label %84, label %87
84: ; preds = %78
%85 = load %struct.sockaddr_in*, %struct.sockaddr_in** %12
%86 = bitcast %struct.sockaddr_in* %85 to %struct.sockaddr*
br label %88
87: ; preds = %78
br label %88
88: ; preds = %87, %84
%89 = phi %struct.sockaddr* [ %86, %84 ], [ bitcast (%struct.sockaddr_in* @recvfrom_u.clientaddr to %struct.sockaddr*), %87 ]
%90 = call i32 @recvfrom(i32 %79, i32* %80, i64 %81, i32 0, %struct.sockaddr* %89, i32* @recvfrom_u.clilen)
store i32 %90, i32* %13
%91 = load i32, i32* %13
%92 = icmp sle i32 %91, 0
br i1 %92, label %93, label %94
93: ; preds = %88
store i32 -1, i32* %7
br label %96
94: ; preds = %88
%95 = load i32, i32* %13
store i32 %95, i32* %7
br label %96
96: ; preds = %94, %93, %76, %75, %58, %52, %48
%97 = load i32, i32* %7
ret i32 %97
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.timeval = type { i64, i64 }
%struct.sockaddr = type { i32 }
@recvfrom_u.clientaddr = internal global %struct.sockaddr_in zeroinitializer
@recvfrom_u.clilen = internal global i32 4
@errno = global i64 0
@EINTR = global i64 0
@MSG_DONTWAIT = global i32 0
define i32 @recvfrom_u(i32 %0, i32* %1, i64 %2, i64 %3, i64 %4, %struct.sockaddr_in* %5) {
%7 = alloca i32
%8 = alloca %struct.timeval
%9 = bitcast i32* %7 to i8*
%10 = bitcast %struct.timeval* %8 to i8*
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i64 0, i32 1
store i64 %4, i64* %11
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i64 0, i32 0
store i64 %3, i64* %12
%13 = add nsw i32 %0, 1
%14 = icmp ne i64 %4, 0
%15 = icmp ne i64 %3, 0
%16 = select i1 %14, i1 true, i1 %15
br i1 %16, label %17, label %42
17: ; preds = %6, %25
%18 = call i32 @FD_ZERO(i32* %7)
%19 = call i32 @FD_SET(i32 %0, i32* %7)
%20 = call i32 @select(i32 %13, i32* %7, i32* null, i32* null, %struct.timeval* %8)
switch i32 %20, label %31 [
i32 -1, label %21
i32 0, label %49
]
21: ; preds = %17
%22 = load i64, i64* @errno
%23 = load i64, i64* @EINTR
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %49
25: ; preds = %21
%26 = load i64, i64* %11
%27 = load i64, i64* %12
%28 = icmp ne i64 %26, 0
%29 = icmp ne i64 %27, 0
%30 = select i1 %28, i1 true, i1 %29
br i1 %30, label %17, label %42
31: ; preds = %17
%32 = call i32 @FD_ISSET(i32 %0, i32* %7)
%33 = icmp eq i32 %32, 0
br i1 %33, label %49, label %34
34: ; preds = %31
%35 = load i32, i32* @MSG_DONTWAIT
%36 = icmp eq %struct.sockaddr_in* %5, null
%37 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%38 = select i1 %36, %struct.sockaddr* bitcast (%struct.sockaddr_in* @recvfrom_u.clientaddr to %struct.sockaddr*), %struct.sockaddr* %37
%39 = call i32 @recvfrom(i32 %0, i32* %1, i64 %2, i32 %35, %struct.sockaddr* %38, i32* @recvfrom_u.clilen)
%40 = icmp slt i32 %39, 1
%41 = select i1 %40, i32 -1, i32 %39
br label %49
42: ; preds = %25, %6
%43 = icmp eq %struct.sockaddr_in* %5, null
%44 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%45 = select i1 %43, %struct.sockaddr* bitcast (%struct.sockaddr_in* @recvfrom_u.clientaddr to %struct.sockaddr*), %struct.sockaddr* %44
%46 = call i32 @recvfrom(i32 %0, i32* %1, i64 %2, i32 0, %struct.sockaddr* %45, i32* @recvfrom_u.clilen)
%47 = icmp slt i32 %46, 1
%48 = select i1 %47, i32 -1, i32 %46
br label %49
49: ; preds = %17, %21, %42, %34, %31
%50 = phi i32 [ -1, %31 ], [ %41, %34 ], [ %48, %42 ], [ %20, %17 ], [ -1, %21 ]
ret i32 %50
}