#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;
};

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
int /*<<< orphan*/ POLLIN;
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *, scalar_t__, ...);
int read(int, char *, int);
char *strerror(scalar_t__);
scalar_t__ write(int, char *, int);
void dump_file(int read_fd, int write_fd) {
  int rc;
  char *buf = malloc(4096);
  struct pollfd pollfd;
  do {
    pollfd.fd = read_fd;
    pollfd.events = POLLIN;
    rc = poll(&pollfd, 1, 1);
    if (rc < 0) {
      if (errno != EINTR) {
        printf("poll error: %s\n", strerror(errno));
        break;
      }
      printf("poll warning: %s\n", strerror(errno));
    }
    rc = read(read_fd, buf, 4096);
    if (!rc)
      continue;
    if (rc < 0) {
      if (errno == EAGAIN)
        continue;
      perror("read");
      break;
    }
    if (write(write_fd, buf, rc) < 0) {
      printf("Couldn't write to output file: errcode = %d: %s\n", errno,
             strerror(errno));
      break;
    }
  } while (1);
  free(buf);
}


x86 assembly (clang, -O0):
<dump_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    $0x1000,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1080 <poll@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    122a <dump_file+0xaa>
mov    0x2e06(%rip),%rax
mov    (%rax),%rax
mov    0x2df4(%rip),%rcx
cmp    (%rcx),%rax
je     120a <dump_file+0x8a>
mov    0x2dec(%rip),%rax
mov    (%rax),%rdi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12eb <dump_file+0x16b>
mov    0x2dc7(%rip),%rax
mov    (%rax),%rdi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    124d <dump_file+0xcd>
jmp    12dc <dump_file+0x15c>
cmpl   $0x0,-0xc(%rbp)
jge    1287 <dump_file+0x107>
mov    0x2d7a(%rip),%rax
mov    (%rax),%rax
mov    0x2d58(%rip),%rcx
cmp    (%rcx),%rax
jne    1276 <dump_file+0xf6>
jmp    12dc <dump_file+0x15c>
lea    0xda5(%rip),%rdi
call   1090 <perror@plt>
jmp    12eb <dump_file+0x16b>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1040 <write@plt>
cmp    $0x0,%rax
jge    12d7 <dump_file+0x157>
mov    0x2d31(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2d23(%rip),%rax
mov    (%rax),%rdi
call   10a0 <strerror@plt>
mov    -0x28(%rbp),%rsi
mov    %rax,%rdx
lea    0xd5c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12eb <dump_file+0x16b>
jmp    12dc <dump_file+0x15c>
mov    $0x1,%al
test   $0x1,%al
jne    119c <dump_file+0x1c>
jmp    12eb <dump_file+0x16b>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,0xc(%rsp)
mov    %edi,%ebp
mov    $0x1000,%edi
call   1070 <malloc@plt>
mov    %rax,%r13
mov    0x2e40(%rip),%r12
lea    0x10(%rsp),%r15
jmp    11ca <dump_file+0x4a>
nop
mov    0x2e21(%rip),%rax
mov    (%rax),%rax
mov    0x2dff(%rip),%rcx
cmp    (%rcx),%rax
jne    129a <dump_file+0x11a>
mov    %ebp,0x10(%rsp)
mov    (%r12),%eax
mov    %eax,0x14(%rsp)
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
call   1080 <poll@plt>
test   %eax,%eax
jns    121e <dump_file+0x9e>
mov    0x2de5(%rip),%rax
mov    (%rax),%rbx
mov    0x2dd3(%rip),%rax
mov    (%rax),%r14
mov    %rbx,%rdi
call   10a0 <strerror@plt>
cmp    %r14,%rbx
jne    1272 <dump_file+0xf2>
lea    0xdfc(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
mov    %r13,%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
test   %eax,%eax
je     11ca <dump_file+0x4a>
js     11b0 <dump_file+0x30>
mov    0xc(%rsp),%edi
mov    %r13,%rsi
mov    %eax,%edx
call   1040 <write@plt>
test   %rax,%rax
jns    11ca <dump_file+0x4a>
mov    0x2d87(%rip),%rax
mov    (%rax),%rbx
mov    %rbx,%rdi
call   10a0 <strerror@plt>
lea    0xdc4(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1283 <dump_file+0x103>
lea    0xd87(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r13,%rdi
call   1030 <free@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd81(%rip),%rdi
call   1090 <perror@plt>
jmp    1283 <dump_file+0x103>

LLVM IR (-O0):
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str = [16 x i8] c"poll error: %s\0A\00"
@.str.1 = [18 x i8] c"poll warning: %s\0A\00"
@EAGAIN = global i64 0
@.str.2 = [5 x i8] c"read\00"
@.str.3 = [49 x i8] c"Couldn't write to output file: errcode = %d: %s\0A\00"
define void @dump_file(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca %struct.pollfd
store i32 %0, i32* %3
store i32 %1, i32* %4
%8 = call i8* @malloc(i32 4096)
store i8* %8, i8** %6
br label %9
9: ; preds = %60, %2
%10 = load i32, i32* %3
%11 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i32 0, i32 0
store i32 %10, i32* %11
%12 = load i32, i32* @POLLIN
%13 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i32 0, i32 1
store i32 %12, i32* %13
%14 = call i32 @poll(%struct.pollfd* %7, i32 1, i32 1)
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %31
17: ; preds = %9
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINTR
%20 = icmp ne i64 %18, %19
br i1 %20, label %21, label %26
21: ; preds = %17
%22 = load i64, i64* @errno
%23 = call i8* @strerror(i64 %22)
%24 = ptrtoint i8* %23 to i64
%25 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i64 %24)
br label %61
26: ; preds = %17
%27 = load i64, i64* @errno
%28 = call i8* @strerror(i64 %27)
%29 = ptrtoint i8* %28 to i64
%30 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i64 %29)
br label %31
31: ; preds = %26, %9
%32 = load i32, i32* %3
%33 = load i8*, i8** %6
%34 = call i32 @read(i32 %32, i8* %33, i32 4096)
store i32 %34, i32* %5
%35 = load i32, i32* %5
%36 = icmp ne i32 %35, 0
br i1 %36, label %38, label %37
37: ; preds = %31
br label %60
38: ; preds = %31
%39 = load i32, i32* %5
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %48
41: ; preds = %38
%42 = load i64, i64* @errno
%43 = load i64, i64* @EAGAIN
%44 = icmp eq i64 %42, %43
br i1 %44, label %45, label %46
45: ; preds = %41
br label %60
46: ; preds = %41
%47 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %61
48: ; preds = %38
%49 = load i32, i32* %4
%50 = load i8*, i8** %6
%51 = load i32, i32* %5
%52 = call i64 @write(i32 %49, i8* %50, i32 %51)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %59
54: ; preds = %48
%55 = load i64, i64* @errno
%56 = load i64, i64* @errno
%57 = call i8* @strerror(i64 %56)
%58 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), i64 %55, i8* %57)
br label %61
59: ; preds = %48
br label %60
60: ; preds = %59, %45, %37
br i1 true, label %9, label %61
61: ; preds = %60, %54, %46, %21
%62 = load i8*, i8** %6
%63 = call i32 @free(i8* %62)
ret void
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str = [16 x i8] c"poll error: %s\0A\00"
@.str.1 = [18 x i8] c"poll warning: %s\0A\00"
@EAGAIN = global i64 0
@.str.2 = [5 x i8] c"read\00"
@.str.3 = [49 x i8] c"Couldn't write to output file: errcode = %d: %s\0A\00"
define void @dump_file(i32 %0, i32 %1) {
%3 = alloca %struct.pollfd
%4 = tail call dereferenceable_or_null(4096) i8* @malloc(i32 4096)
%5 = bitcast %struct.pollfd* %3 to i8*
%6 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i64 0, i32 0
%7 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i64 0, i32 1
br label %8
8: ; preds = %37, %2
store i32 %0, i32* %6
%9 = load i32, i32* @POLLIN
store i32 %9, i32* %7
%10 = call i32 @poll(%struct.pollfd* %3, i32 1, i32 1)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %23
12: ; preds = %8
%13 = load i64, i64* @errno
%14 = load i64, i64* @EINTR
%15 = icmp eq i64 %13, %14
%16 = call i8* @strerror(i64 %13)
%17 = ptrtoint i8* %16 to i64
br i1 %15, label %21, label %18
18: ; preds = %12
%19 = ptrtoint i8* %16 to i64
%20 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i64 %19)
br label %42
21: ; preds = %12
%22 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i64 %17)
br label %23
23: ; preds = %21, %8
%24 = call i32 @read(i32 %0, i8* %4, i32 4096)
%25 = icmp eq i32 %24, 0
br i1 %25, label %37, label %26
26: ; preds = %23
%27 = icmp slt i32 %24, 0
br i1 %27, label %28, label %34
28: ; preds = %26
%29 = load i64, i64* @errno
%30 = load i64, i64* @EAGAIN
%31 = icmp eq i64 %29, %30
br i1 %31, label %37, label %32
32: ; preds = %28
%33 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %42
34: ; preds = %26
%35 = call i64 @write(i32 %1, i8* %4, i32 %24)
%36 = icmp slt i64 %35, 0
br i1 %36, label %38, label %37
37: ; preds = %34, %28, %23
br label %8
38: ; preds = %34
%39 = load i64, i64* @errno
%40 = call i8* @strerror(i64 %39)
%41 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), i64 %39, i8* %40)
br label %42
42: ; preds = %38, %32, %18
%43 = call i32 @free(i8* %4)
ret void
}