#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 pollfd {
  int fd;
  int /*<<< orphan*/ events;
};
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int EAGAIN;
int EINTR;
int LINE_MAX;
int /*<<< orphan*/ POLLIN;
int errno;
int poll(struct pollfd *, int, int /*<<< orphan*/);
scalar_t__ read(int, char *, int);
int flush_fd(int fd) {
  struct pollfd pollfd = {
      .fd = fd,
      .events = POLLIN,
  };
  for (;;) {
    char buf[LINE_MAX];
    ssize_t l;
    int r;
    r = poll(&pollfd, 1, 0);
    if (r < 0) {
      if (errno == EINTR)
        continue;
      return -errno;
    } else if (r == 0)
      return 0;
    l = read(fd, buf, sizeof(buf));
    if (l < 0) {
      if (errno == EINTR)
        continue;
      if (errno == EAGAIN)
        return 0;
      return -errno;
    } else if (l == 0)
      return 0;
  }
}


x86 assembly (clang, -O0):
<flush_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2ea8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2ea4(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x40(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
lea    -0x10(%rbp),%rdi
mov    $0x1,%esi
xor    %edx,%edx
call   1040 <poll@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11c8 <flush_fd+0xa8>
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    0x2e2e(%rip),%rcx
cmp    (%rcx),%eax
jne    11ae <flush_fd+0x8e>
movl   $0x3,-0x30(%rbp)
jmp    129e <flush_fd+0x17e>
xor    %eax,%eax
mov    0x2e19(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    129e <flush_fd+0x17e>
cmpl   $0x0,-0x2c(%rbp)
jne    11e5 <flush_fd+0xc5>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    129e <flush_fd+0x17e>
jmp    11ea <flush_fd+0xca>
mov    -0x38(%rbp),%rsi
mov    -0x40(%rbp),%rax
mov    -0x8(%rbp),%edi
mov    %eax,%edx
call   1030 <read@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1274 <flush_fd+0x154>
mov    0x2dbe(%rip),%rax
mov    (%rax),%eax
mov    0x2dad(%rip),%rcx
cmp    (%rcx),%eax
jne    122f <flush_fd+0x10f>
movl   $0x3,-0x30(%rbp)
jmp    129e <flush_fd+0x17e>
mov    0x2d9a(%rip),%rax
mov    (%rax),%eax
mov    0x2d79(%rip),%rcx
cmp    (%rcx),%eax
jne    125a <flush_fd+0x13a>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    129e <flush_fd+0x17e>
xor    %eax,%eax
mov    0x2d6d(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    129e <flush_fd+0x17e>
cmpq   $0x0,-0x28(%rbp)
jne    1292 <flush_fd+0x172>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x30(%rbp)
jmp    129e <flush_fd+0x17e>
jmp    1297 <flush_fd+0x177>
movl   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x30(%rbp),%eax
mov    %eax,-0x44(%rbp)
test   %eax,%eax
je     12ce <flush_fd+0x1ae>
jmp    12b8 <flush_fd+0x198>
mov    -0x44(%rbp),%eax
sub    $0x1,%eax
je     12d3 <flush_fd+0x1b3>
jmp    12c9 <flush_fd+0x1a9>
jmp    113d <flush_fd+0x1d>
jmp    113d <flush_fd+0x1d>
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flush_fd>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r15d
mov    %edi,-0x30(%rbp)
mov    0x2ea5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    116d <flush_fd+0x4d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x2e79(%rip),%rax
mov    (%rax),%r13d
mov    0x2e67(%rip),%rax
cmp    (%rax),%r13d
jne    11ea <flush_fd+0xca>
mov    %r14,%rsp
mov    %rsp,%r14
mov    0x2e71(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%rbx
lea    0xf(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    -0x30(%rbp),%rdi
mov    $0x1,%esi
xor    %edx,%edx
call   1040 <poll@plt>
test   %eax,%eax
js     1150 <flush_fd+0x30>
xor    %r13d,%r13d
test   %eax,%eax
je     11ed <flush_fd+0xcd>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1030 <read@plt>
test   %rax,%rax
js     11c0 <flush_fd+0xa0>
jne    116a <flush_fd+0x4a>
jmp    11ed <flush_fd+0xcd>
xchg   %ax,%ax
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    0x2df8(%rip),%rcx
cmp    (%rcx),%eax
je     116a <flush_fd+0x4a>
mov    0x2ddd(%rip),%rcx
mov    %eax,%edx
neg    %edx
xor    %r13d,%r13d
cmp    (%rcx),%eax
cmovne %edx,%r13d
jmp    11ed <flush_fd+0xcd>
neg    %r13d
mov    %r14,%rsp
mov    %r13d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
@LINE_MAX = global i32 0
@errno = global i32 0
@EINTR = global i32 0
@EAGAIN = global i32 0
define i32 @flush_fd(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.pollfd
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %3
%10 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i32 0, i32 0
%11 = load i32, i32* %3
store i32 %11, i32* %10
%12 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i32 0, i32 1
%13 = load i32, i32* @POLLIN
store i32 %13, i32* %12
br label %14
14: ; preds = %63, %60, %1
%15 = load i32, i32* @LINE_MAX
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %5
%18 = alloca i8, i64 %16
store i64 %16, i64* %6
%19 = call i32 @poll(%struct.pollfd* %4, i32 1, i32 0)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %30
22: ; preds = %14
%23 = load i32, i32* @errno
%24 = load i32, i32* @EINTR
%25 = icmp eq i32 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %22
store i32 3, i32* %9
br label %60
27: ; preds = %22
%28 = load i32, i32* @errno
%29 = sub nsw i32 0, %28
store i32 %29, i32* %2
store i32 1, i32* %9
br label %60
30: ; preds = %14
%31 = load i32, i32* %8
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %30
store i32 0, i32* %2
store i32 1, i32* %9
br label %60
34: ; preds = %30
br label %35
35: ; preds = %34
%36 = load i32, i32* %3
%37 = trunc i64 %16 to i32
%38 = call i64 @read(i32 %36, i8* %18, i32 %37)
store i64 %38, i64* %7
%39 = load i64, i64* %7
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %54
41: ; preds = %35
%42 = load i32, i32* @errno
%43 = load i32, i32* @EINTR
%44 = icmp eq i32 %42, %43
br i1 %44, label %45, label %46
45: ; preds = %41
store i32 3, i32* %9
br label %60
46: ; preds = %41
%47 = load i32, i32* @errno
%48 = load i32, i32* @EAGAIN
%49 = icmp eq i32 %47, %48
br i1 %49, label %50, label %51
50: ; preds = %46
store i32 0, i32* %2
store i32 1, i32* %9
br label %60
51: ; preds = %46
%52 = load i32, i32* @errno
%53 = sub nsw i32 0, %52
store i32 %53, i32* %2
store i32 1, i32* %9
br label %60
54: ; preds = %35
%55 = load i64, i64* %7
%56 = icmp eq i64 %55, 0
br i1 %56, label %57, label %58
57: ; preds = %54
store i32 0, i32* %2
store i32 1, i32* %9
br label %60
58: ; preds = %54
br label %59
59: ; preds = %58
store i32 0, i32* %9
br label %60
60: ; preds = %59, %57, %51, %50, %45, %33, %27, %26
%61 = load i8*, i8** %5
%62 = load i32, i32* %9
switch i32 %62, label %66 [
i32 0, label %63
i32 3, label %14
i32 1, label %64
]
63: ; preds = %60
br label %14
64: ; preds = %60
%65 = load i32, i32* %2
ret i32 %65
66: ; preds = %60
unreachable
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
@LINE_MAX = global i32 0
@errno = global i32 0
@EINTR = global i32 0
@EAGAIN = global i32 0
define i32 @flush_fd(i32 %0) {
%2 = alloca %struct.pollfd
%3 = bitcast %struct.pollfd* %2 to i8*
%4 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i64 0, i32 0
store i32 %0, i32* %4
%5 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i64 0, i32 1
%6 = load i32, i32* @POLLIN
store i32 %6, i32* %5
br label %7
7: ; preds = %34, %1
%8 = load i32, i32* @LINE_MAX
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
%11 = alloca i8, i64 %9
%12 = call i32 @poll(%struct.pollfd* %2, i32 1, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %7
%15 = load i32, i32* @errno
%16 = load i32, i32* @EINTR
%17 = icmp eq i32 %15, %16
br i1 %17, label %34, label %35
18: ; preds = %7
%19 = icmp eq i32 %12, 0
br i1 %19, label %37, label %20
20: ; preds = %18
%21 = call i64 @read(i32 %0, i8* %11, i32 %8)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %32
23: ; preds = %20
%24 = load i32, i32* @errno
%25 = load i32, i32* @EINTR
%26 = icmp eq i32 %24, %25
br i1 %26, label %34, label %27
27: ; preds = %23
%28 = load i32, i32* @EAGAIN
%29 = icmp eq i32 %24, %28
%30 = sub nsw i32 0, %24
%31 = select i1 %29, i32 0, i32 %30
br label %37
32: ; preds = %20
%33 = icmp eq i64 %21, 0
br i1 %33, label %37, label %34
34: ; preds = %14, %32, %23
br label %7
35: ; preds = %14
%36 = sub nsw i32 0, %15
br label %37
37: ; preds = %18, %32, %35, %27
%38 = phi i32 [ %31, %27 ], [ %36, %35 ], [ 0, %32 ], [ 0, %18 ]
ret i32 %38
}