#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 member_0;
  int /*<<< orphan*/ member_1;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int read(int, void *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int recv_buf(int fd, void *buf, int len, int tmo) {
  fd_set set;
  int rv;
  struct timeval timeout = {tmo, 0};
  struct timeval *ptmo = tmo ? &timeout : NULL;
  while (len) {
    FD_ZERO(&set);
    FD_SET(fd, &set);
    rv = select(fd + 1, &set, NULL, NULL, ptmo);
    if (rv <= 0)
      return -1;
    rv = read(fd, buf, len);
    if (rv <= 0)
      return -1;
    len -= rv;
    buf += rv;
  }
  return 0;
}


x86 assembly (clang, -O0):
<recv_buf>:
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    -0x18(%rbp),%eax
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1179 <recv_buf+0x39>
lea    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1184 <recv_buf+0x44>
xor    %eax,%eax
mov    %rax,-0x38(%rbp)
jmp    1184 <recv_buf+0x44>
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1227 <recv_buf+0xe7>
lea    -0x1c(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
mov    -0x30(%rbp),%r8
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jg     11de <recv_buf+0x9e>
movl   $0xffffffff,-0x4(%rbp)
jmp    122e <recv_buf+0xee>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jg     1206 <recv_buf+0xc6>
movl   $0xffffffff,-0x4(%rbp)
jmp    122e <recv_buf+0xee>
mov    -0x20(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    118c <recv_buf+0x4c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_buf>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,0x8(%rsp)
movl   $0x0,0xc(%rsp)
xor    %r13d,%r13d
test   %ecx,%ecx
lea    0x8(%rsp),%r12
cmove  %r13,%r12
test   %edx,%edx
je     11dd <recv_buf+0x9d>
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r15d
lea    0x1(%r15),%eax
mov    %eax,0x4(%rsp)
lea    0x14(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1040 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
call   1060 <FD_SET@plt>
mov    0x4(%rsp),%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r12,%r8
call   1050 <select@plt>
mov    $0xffffffff,%r13d
test   %eax,%eax
jle    11dd <recv_buf+0x9d>
mov    %r15d,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <read@plt>
test   %eax,%eax
jle    11dd <recv_buf+0x9d>
mov    %eax,%ecx
add    %rcx,%r14
sub    %eax,%ebx
jne    1190 <recv_buf+0x50>
xor    %r13d,%r13d
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
define i32 @recv_buf(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 %struct.timeval
%13 = alloca %struct.timeval*
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 0
%15 = load i32, i32* %9
store i32 %15, i32* %14
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 1
store i32 0, i32* %16
%17 = load i32, i32* %9
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %4
br label %21
20: ; preds = %4
br label %21
21: ; preds = %20, %19
%22 = phi %struct.timeval* [ %12, %19 ], [ null, %20 ]
store %struct.timeval* %22, %struct.timeval** %13
br label %23
23: ; preds = %45, %21
%24 = load i32, i32* %8
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %53
26: ; preds = %23
%27 = call i32 @FD_ZERO(i32* %10)
%28 = load i32, i32* %6
%29 = call i32 @FD_SET(i32 %28, i32* %10)
%30 = load i32, i32* %6
%31 = add nsw i32 %30, 1
%32 = load %struct.timeval*, %struct.timeval** %13
%33 = call i32 @select(i32 %31, i32* %10, i32* null, i32* null, %struct.timeval* %32)
store i32 %33, i32* %11
%34 = load i32, i32* %11
%35 = icmp sle i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %26
store i32 -1, i32* %5
br label %54
37: ; preds = %26
%38 = load i32, i32* %6
%39 = load i8*, i8** %7
%40 = load i32, i32* %8
%41 = call i32 @read(i32 %38, i8* %39, i32 %40)
store i32 %41, i32* %11
%42 = load i32, i32* %11
%43 = icmp sle i32 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %37
store i32 -1, i32* %5
br label %54
45: ; preds = %37
%46 = load i32, i32* %11
%47 = load i32, i32* %8
%48 = sub nsw i32 %47, %46
store i32 %48, i32* %8
%49 = load i32, i32* %11
%50 = load i8*, i8** %7
%51 = sext i32 %49 to i64
%52 = getelementptr i8, i8* %50, i64 %51
store i8* %52, i8** %7
br label %23
53: ; preds = %23
store i32 0, i32* %5
br label %54
54: ; preds = %53, %44, %36
%55 = load i32, i32* %5
ret i32 %55
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
define i32 @recv_buf(i32 %0, i8* nocapture %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = bitcast i32* %5 to i8*
%8 = bitcast %struct.timeval* %6 to i8*
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
store i32 %3, i32* %9
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
store i32 0, i32* %10
%11 = icmp eq i32 %3, 0
%12 = select i1 %11, %struct.timeval* null, %struct.timeval* %6
%13 = icmp eq i32 %2, 0
br i1 %13, label %31, label %14
14: ; preds = %4
%15 = add nsw i32 %0, 1
br label %16
16: ; preds = %14, %26
%17 = phi i8* [ %1, %14 ], [ %29, %26 ]
%18 = phi i32 [ %2, %14 ], [ %27, %26 ]
%19 = call i32 @FD_ZERO(i32* %5)
%20 = call i32 @FD_SET(i32 %0, i32* %5)
%21 = call i32 @select(i32 %15, i32* %5, i32* null, i32* null, %struct.timeval* %12)
%22 = icmp slt i32 %21, 1
br i1 %22, label %31, label %23
23: ; preds = %16
%24 = call i32 @read(i32 %0, i8* %17, i32 %18)
%25 = icmp slt i32 %24, 1
br i1 %25, label %31, label %26
26: ; preds = %23
%27 = sub nsw i32 %18, %24
%28 = zext i32 %24 to i64
%29 = getelementptr i8, i8* %17, i64 %28
%30 = icmp eq i32 %27, 0
br i1 %30, label %31, label %16
31: ; preds = %16, %23, %26, %4
%32 = phi i32 [ 0, %4 ], [ 0, %26 ], [ -1, %23 ], [ -1, %16 ]
ret i32 %32
}