#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 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*/ *, int /*<<< orphan*/ *);
int udp_read(int fd, char *buf, int len, struct sockaddr_in *from) {
  int bytes_recd = 0;
  unsigned int fromlen = 0;
  fd_set fdvar;
  int sel_ret;
  DEBUGF("Entered udp_read\n");
  FD_ZERO(&fdvar);
  FD_SET(fd, &fdvar);
  sel_ret = select(32, &fdvar, (fd_set *)0, (fd_set *)0, NULL);
  if (sel_ret < 0) {
    DEBUGF("select call failed; system error\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>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
lea    0xe85(%rip),%rdi
mov    $0x0,%al
call   1030 <DEBUGF@plt>
lea    -0x2c(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    $0x20,%edi
lea    -0x2c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1050 <select@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    11d7 <udp_read+0x87>
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1030 <DEBUGF@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1238 <udp_read+0xe8>
movl   $0x4,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%r8
xor    %ecx,%ecx
lea    -0x28(%rbp),%r9
call   1060 <recvfrom@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1221 <udp_read+0xd1>
mov    -0x8(%rbp),%esi
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1030 <DEBUGF@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1238 <udp_read+0xe8>
mov    -0x24(%rbp),%esi
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1030 <DEBUGF@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udp_read>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r14
mov    %edx,%r15d
mov    %rsi,%r12
mov    %edi,%ebx
lea    0xe92(%rip),%rdi
xor    %eax,%eax
call   1030 <DEBUGF@plt>
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1070 <FD_SET@plt>
mov    $0x20,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1050 <select@plt>
test   %eax,%eax
js     11db <udp_read+0x8b>
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     11eb <udp_read+0x9b>
mov    %eax,%ebp
lea    0xe82(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <DEBUGF@plt>
jmp    1200 <udp_read+0xb0>
lea    0xe30(%rip),%rdi
xor    %eax,%eax
call   1030 <DEBUGF@plt>
jmp    11fb <udp_read+0xab>
lea    0xe42(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <DEBUGF@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%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 = [30 x i8] c"recvfrom on socket %d failed\0A\00"
@.str.3 = [15 x i8] c"Read %d bytes\0A\00"
define i32 @udp_read(i32 %0, i8* %1, i32 %2, %struct.sockaddr_in* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca %struct.sockaddr_in*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store %struct.sockaddr_in* %3, %struct.sockaddr_in** %9
store i32 0, i32* %10
store i32 0, i32* %11
%14 = call i32 (i8*, ...) @DEBUGF(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @FD_ZERO(i32* %12)
%16 = load i32, i32* %6
%17 = call i32 @FD_SET(i32 %16, i32* %12)
%18 = call i32 @select(i32 32, i32* %12, i32* null, i32* null, i32* null)
store i32 %18, i32* %13
%19 = load i32, i32* %13
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %4
%22 = call i32 (i8*, ...) @DEBUGF(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
br label %39
23: ; preds = %4
store i32 4, i32* %11
%24 = load i32, i32* %6
%25 = load i8*, i8** %7
%26 = load i32, i32* %8
%27 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%28 = bitcast %struct.sockaddr_in* %27 to %struct.sockaddr*
%29 = call i32 @recvfrom(i32 %24, i8* %25, i32 %26, i32 0, %struct.sockaddr* %28, i32* %11)
store i32 %29, i32* %10
%30 = load i32, i32* %10
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %23
%33 = load i32, i32* %6
%34 = call i32 (i8*, ...) @DEBUGF(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %33)
store i32 -1, i32* %5
br label %39
35: ; preds = %23
%36 = load i32, i32* %10
%37 = call i32 (i8*, ...) @DEBUGF(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %36)
%38 = load i32, i32* %10
store i32 %38, i32* %5
br label %39
39: ; preds = %35, %32, %21
%40 = load i32, i32* %5
ret i32 %40
}

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