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

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int st_getbytes(int devfd, char *buf, int len) {
  fd_set s;
  struct timeval tv;
  int ret;
  FD_ZERO(&s);
  FD_SET(devfd, &s);
  while (len) {
    tv.tv_sec = 4000000 / 1000000;
    tv.tv_usec = 4000000 % 1000000;
    if (select(devfd + 1, &s, NULL, NULL, &tv) != 1)
      return -1;
    ret = read(devfd, buf, len);
    if (ret <= 0)
      return -1;
    len -= ret;
  }
  return 0;
}


x86 assembly (clang, -O0):
<st_getbytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1060 <FD_SET@plt>
cmpl   $0x0,-0x14(%rbp)
je     11e6 <st_getbytes+0xa6>
movl   $0x4,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x20(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
cmp    $0x1,%eax
je     11ae <st_getbytes+0x6e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ed <st_getbytes+0xad>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jg     11d6 <st_getbytes+0x96>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ed <st_getbytes+0xad>
mov    -0x24(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    1167 <st_getbytes+0x27>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<st_getbytes>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r15d
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1040 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
call   1060 <FD_SET@plt>
test   %ebx,%ebx
je     11c6 <st_getbytes+0x86>
lea    0x1(%r15),%ebp
lea    0x10(%rsp),%r13
mov    $0xffffffff,%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movq   $0x4,0x10(%rsp)
mov    %ebp,%edi
lea    0xc(%rsp),%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r13,%r8
call   1050 <select@plt>
cmp    $0x1,%eax
jne    11c9 <st_getbytes+0x89>
mov    %r15d,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <read@plt>
test   %eax,%eax
jle    11c9 <st_getbytes+0x89>
sub    %eax,%ebx
jne    1190 <st_getbytes+0x50>
xor    %r12d,%r12d
mov    %r12d,%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 @st_getbytes(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.timeval
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = call i32 @FD_ZERO(i32* %8)
%12 = load i32, i32* %5
%13 = call i32 @FD_SET(i32 %12, i32* %8)
br label %14
14: ; preds = %33, %3
%15 = load i32, i32* %7
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %37
17: ; preds = %14
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 4, i32* %18
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
store i32 0, i32* %19
%20 = load i32, i32* %5
%21 = add nsw i32 %20, 1
%22 = call i32 @select(i32 %21, i32* %8, i32* null, i32* null, %struct.timeval* %9)
%23 = icmp ne i32 %22, 1
br i1 %23, label %24, label %25
24: ; preds = %17
store i32 -1, i32* %4
br label %38
25: ; preds = %17
%26 = load i32, i32* %5
%27 = load i8*, i8** %6
%28 = load i32, i32* %7
%29 = call i32 @read(i32 %26, i8* %27, i32 %28)
store i32 %29, i32* %10
%30 = load i32, i32* %10
%31 = icmp sle i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %25
store i32 -1, i32* %4
br label %38
33: ; preds = %25
%34 = load i32, i32* %10
%35 = load i32, i32* %7
%36 = sub nsw i32 %35, %34
store i32 %36, i32* %7
br label %14
37: ; preds = %14
store i32 0, i32* %4
br label %38
38: ; preds = %37, %32, %24
%39 = load i32, i32* %4
ret i32 %39
}

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