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

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ assert(int);
int read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int fd_flush(int fd, struct timeval *timeout) {
  char buffer[1];
  fd_set set_read;
  FD_ZERO(&set_read);
  FD_SET(fd, &set_read);
  int bytes_read = 0;
  while (1) {
    int s = select(fd + 1, &set_read, NULL, NULL, timeout);
    if (s != 1) {
      break;
    }
    int ret = read(fd, buffer, 1);
    assert(ret != -1);
    bytes_read++;
  }
  return bytes_read;
}


x86 assembly (clang, -O0):
<fd_flush>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1070 <FD_SET@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
add    $0x1,%edi
mov    -0x10(%rbp),%r8
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1060 <select@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x1,-0x20(%rbp)
je     11a7 <fd_flush+0x57>
jmp    11da <fd_flush+0x8a>
mov    -0x4(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    117b <fd_flush+0x2b>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd_flush>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%r15d
lea    0x4(%rsp),%r12
mov    %r12,%rdi
call   1050 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %r12,%rsi
call   1070 <FD_SET@plt>
lea    0x1(%r15),%ebp
xor    %ebx,%ebx
mov    %ebp,%edi
mov    %r12,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r14,%r8
call   1060 <select@plt>
cmp    $0x1,%eax
jne    11e7 <fd_flush+0x97>
xor    %ebx,%ebx
lea    0x3(%rsp),%r12
lea    0x4(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
xor    %edi,%edi
cmp    $0xffffffff,%eax
setne  %dil
call   1040 <assert@plt>
add    $0x1,%ebx
mov    %ebp,%edi
mov    %r13,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r14,%r8
call   1060 <select@plt>
cmp    $0x1,%eax
je     11b0 <fd_flush+0x60>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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