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

/* Variables and functions */
int /*<<< orphan*/ DEBUGF(char *, ...);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *,
             unsigned int *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int udp_read_timed(int fd, char *buf, int len, struct sockaddr_in *from,
                   int timeout) {
  int bytes_recd = 0;
  unsigned int fromlen = 0;
  fd_set fdvar;
  int sel_ret;
  struct timeval tv;
  DEBUGF("Entered udp_read\n");
  tv.tv_sec = timeout;
  tv.tv_usec = 0;
  FD_ZERO(&fdvar);
  FD_SET(fd, &fdvar);
  sel_ret = select(32, &fdvar, (fd_set *)0, (fd_set *)0, &tv);
  if (sel_ret < 0) {
    DEBUGF("select call failed; system error\n");
    return -1;
  } else if (sel_ret == 0) {
    DEBUGF("select call timed out\n");
    return -1;
  }
  fromlen = sizeof(struct sockaddr_in);
  bytes_recd = recvfrom(fd, buf, len, 0, (struct sockaddr *)from, &fromlen);
  if (bytes_recd < 0) {
    DEBUGF("recvfrom on socket %d failed\n", fd);
    return -1;
  }
  DEBUGF("Read %d bytes\n", bytes_recd);
  return bytes_recd;
}


x86 assembly (clang, -O0):
<udp_read_timed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
lea    0xe81(%rip),%rdi
mov    $0x0,%al
call   1030 <DEBUGF@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x48(%rbp)
movq   $0x0,-0x40(%rbp)
lea    -0x30(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    $0x20,%edi
lea    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x48(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    11e9 <udp_read_timed+0x99>
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1030 <DEBUGF@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1273 <udp_read_timed+0x123>
cmpl   $0x0,-0x34(%rbp)
jne    120d <udp_read_timed+0xbd>
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1030 <DEBUGF@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1273 <udp_read_timed+0x123>
jmp    1212 <udp_read_timed+0xc2>
movl   $0x4,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%r8
xor    %ecx,%ecx
lea    -0x2c(%rbp),%r9
call   1060 <recvfrom@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    125c <udp_read_timed+0x10c>
mov    -0x8(%rbp),%esi
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1030 <DEBUGF@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1273 <udp_read_timed+0x123>
mov    -0x28(%rbp),%esi
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1030 <DEBUGF@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp_read_timed>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %r8d,%ebp
mov    %rcx,%r14
mov    %edx,%r15d
mov    %rsi,%r12
mov    %edi,%ebx
lea    0xe8f(%rip),%rdi
xor    %eax,%eax
call   1030 <DEBUGF@plt>
mov    %ebp,0x10(%rsp)
movq   $0x0,0x18(%rsp)
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1070 <FD_SET@plt>
lea    0x10(%rsp),%r8
mov    $0x20,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
test   %eax,%eax
js     11ef <udp_read_timed+0x9f>
je     11f8 <udp_read_timed+0xa8>
movl   $0x4,0x8(%rsp)
lea    0x8(%rsp),%r9
mov    %ebx,%edi
mov    %r12,%rsi
mov    %r15d,%edx
xor    %ecx,%ecx
mov    %r14,%r8
call   1060 <recvfrom@plt>
test   %eax,%eax
js     1208 <udp_read_timed+0xb8>
mov    %eax,%ebp
lea    0xe85(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <DEBUGF@plt>
jmp    121d <udp_read_timed+0xcd>
lea    0xe1c(%rip),%rdi
jmp    11ff <udp_read_timed+0xaf>
lea    0xe35(%rip),%rdi
xor    %eax,%eax
call   1030 <DEBUGF@plt>
jmp    1218 <udp_read_timed+0xc8>
lea    0xe3c(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <DEBUGF@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.timeval = type { i32, i64 }
%struct.sockaddr = type { i32 }
@.str = [18 x i8] c"Entered udp_read\0A\00"
@.str.1 = [34 x i8] c"select call failed; system error\0A\00"
@.str.2 = [23 x i8] c"select call timed out\0A\00"
@.str.3 = [30 x i8] c"recvfrom on socket %d failed\0A\00"
@.str.4 = [15 x i8] c"Read %d bytes\0A\00"
define i32 @udp_read_timed(i32 %0, i8* %1, i32 %2, %struct.sockaddr_in* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca %struct.sockaddr_in*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca %struct.timeval
store i32 %0, i32* %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store %struct.sockaddr_in* %3, %struct.sockaddr_in** %10
store i32 %4, i32* %11
store i32 0, i32* %12
store i32 0, i32* %13
%17 = call i32 (i8*, ...) @DEBUGF(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* %11
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %16, i32 0, i32 0
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %16, i32 0, i32 1
store i64 0, i64* %20
%21 = call i32 @FD_ZERO(i32* %14)
%22 = load i32, i32* %7
%23 = call i32 @FD_SET(i32 %22, i32* %14)
%24 = call i32 @select(i32 32, i32* %14, i32* null, i32* null, %struct.timeval* %16)
store i32 %24, i32* %15
%25 = load i32, i32* %15
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %5
%28 = call i32 (i8*, ...) @DEBUGF(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %6
br label %51
29: ; preds = %5
%30 = load i32, i32* %15
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = call i32 (i8*, ...) @DEBUGF(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %6
br label %51
34: ; preds = %29
br label %35
35: ; preds = %34
store i32 4, i32* %13
%36 = load i32, i32* %7
%37 = load i8*, i8** %8
%38 = load i32, i32* %9
%39 = load %struct.sockaddr_in*, %struct.sockaddr_in** %10
%40 = bitcast %struct.sockaddr_in* %39 to %struct.sockaddr*
%41 = call i32 @recvfrom(i32 %36, i8* %37, i32 %38, i32 0, %struct.sockaddr* %40, i32* %13)
store i32 %41, i32* %12
%42 = load i32, i32* %12
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %35
%45 = load i32, i32* %7
%46 = call i32 (i8*, ...) @DEBUGF(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %45)
store i32 -1, i32* %6
br label %51
47: ; preds = %35
%48 = load i32, i32* %12
%49 = call i32 (i8*, ...) @DEBUGF(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %48)
%50 = load i32, i32* %12
store i32 %50, i32* %6
br label %51
51: ; preds = %47, %44, %32, %27
%52 = load i32, i32* %6
ret i32 %52
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.timeval = type { i32, i64 }
%struct.sockaddr = type { i32 }
@.str = [18 x i8] c"Entered udp_read\0A\00"
@.str.1 = [34 x i8] c"select call failed; system error\0A\00"
@.str.2 = [23 x i8] c"select call timed out\0A\00"
@.str.3 = [30 x i8] c"recvfrom on socket %d failed\0A\00"
@.str.4 = [15 x i8] c"Read %d bytes\0A\00"
define i32 @udp_read_timed(i32 %0, i8* %1, i32 %2, %struct.sockaddr_in* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.timeval
%9 = bitcast i32* %6 to i8*
%10 = bitcast i32* %7 to i8*
%11 = bitcast %struct.timeval* %8 to i8*
%12 = tail call i32 (i8*, ...) @DEBUGF(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i64 0, i32 0
store i32 %4, i32* %13
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i64 0, i32 1
store i64 0, i64* %14
%15 = call i32 @FD_ZERO(i32* %7)
%16 = call i32 @FD_SET(i32 %0, i32* %7)
%17 = call i32 @select(i32 32, i32* %7, i32* null, i32* null, %struct.timeval* %8)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %5
%20 = call i32 (i8*, ...) @DEBUGF(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
br label %33
21: ; preds = %5
%22 = icmp eq i32 %17, 0
br i1 %22, label %23, label %25
23: ; preds = %21
%24 = call i32 (i8*, ...) @DEBUGF(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %33
25: ; preds = %21
store i32 4, i32* %6
%26 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%27 = call i32 @recvfrom(i32 %0, i8* %1, i32 %2, i32 0, %struct.sockaddr* %26, i32* %6)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 (i8*, ...) @DEBUGF(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %0)
br label %33
31: ; preds = %25
%32 = call i32 (i8*, ...) @DEBUGF(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %27)
br label %33
33: ; preds = %31, %29, %23, %19
%34 = phi i32 [ -1, %19 ], [ -1, %23 ], [ -1, %29 ], [ %27, %31 ]
ret i32 %34
}