#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 events;
  int revents;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int POLLHUP;
int POLLIN;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int tty_init(int);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  struct pollfd pfd;
  int fd;
  int ret = 0;
  char buf[16];
  ssize_t val;
retry:
  fd = open(argv[1], O_RDWR);
  if (fd < 0) {
    perror(argv[1]);
    ret = fd;
    goto out1;
  }
  ret = tty_init(fd);
  if (ret < 0)
    goto out2;
  pfd.fd = fd;
  pfd.events = POLLIN | POLLHUP;
  pfd.revents = 0;
  while (1) {
    ret = poll(&pfd, 1, -1);
    if (ret < 0)
      goto out2;
    if (pfd.revents & POLLHUP) {
      close(fd);
      goto retry;
    }
    val = read(fd, buf, sizeof(buf));
    if (val < 0) {
      ret = val;
      goto out2;
    }
    printf("%s\n", buf);
    ret = write(fd, buf, val);
    if (ret < 0)
      goto out2;
  }
out2:
  close(fd);
out1:
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11d8 <main+0x58>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <perror@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    12c2 <main+0x142>
mov    -0x24(%rbp),%edi
call   1050 <tty_init@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11f2 <main+0x72>
jmp    12ba <main+0x13a>
mov    -0x24(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dd9(%rip),%rax
mov    (%rax),%eax
mov    0x2dd8(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1080 <poll@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1239 <main+0xb9>
jmp    12ba <main+0x13a>
mov    -0x18(%rbp),%eax
mov    0x2d9d(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     125b <main+0xdb>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
jmp    119d <main+0x1d>
mov    -0x24(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x10,%edx
call   1070 <read@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    1284 <main+0x104>
mov    -0x44(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    12ba <main+0x13a>
lea    -0x40(%rbp),%rsi
lea    0xd71(%rip),%rdi
call   1040 <printf@plt>
mov    -0x24(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    -0x44(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    12b5 <main+0x135>
jmp    12ba <main+0x13a>
jmp    1214 <main+0x94>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    -0x28(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rsi,0x18(%rsp)
mov    0x8(%rsi),%rdi
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1288 <main+0x108>
mov    0x2e2a(%rip),%r14
lea    0x8(%rsp),%r15
lea    0x20(%rsp),%r12
lea    0xe39(%rip),%r13
mov    %ebx,%edi
call   1050 <tty_init@plt>
test   %eax,%eax
js     129a <main+0x11a>
mov    %ebx,0x8(%rsp)
mov    (%r14),%eax
mov    0x2df4(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0xc(%rsp)
movl   $0x0,0x10(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1080 <poll@plt>
test   %eax,%eax
js     129a <main+0x11a>
mov    (%r14),%eax
test   %eax,0x10(%rsp)
jne    1260 <main+0xe0>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x10,%edx
call   1070 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
js     129c <main+0x11c>
mov    %r13,%rdi
mov    %r12,%rsi
call   1040 <printf@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    1200 <main+0x80>
jmp    129a <main+0x11a>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%edi
call   1060 <close@plt>
mov    0x18(%rsp),%rax
mov    0x8(%rax),%rdi
mov    0x2d71(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11c7 <main+0x47>
mov    0x18(%rsp),%rax
mov    0x8(%rax),%rdi
call   10a0 <perror@plt>
mov    %ebx,%ebp
jmp    12a3 <main+0x123>
mov    %eax,%ebp
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@O_RDWR = global i32 0
@POLLIN = global i32 0
@POLLHUP = global i32 0
@.str = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.pollfd
%7 = alloca i32
%8 = alloca i32
%9 = alloca [16 x i8]
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %8
br label %11
11: ; preds = %50, %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = load i32, i32* @O_RDWR
%16 = call i32 @open(i8* %14, i32 %15)
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %11
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @perror(i8* %22)
%24 = load i32, i32* %7
store i32 %24, i32* %8
br label %75
25: ; preds = %11
%26 = load i32, i32* %7
%27 = call i32 @tty_init(i32 %26)
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %25
br label %72
31: ; preds = %25
%32 = load i32, i32* %7
%33 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i32 0, i32 0
store i32 %32, i32* %33
%34 = load i32, i32* @POLLIN
%35 = load i32, i32* @POLLHUP
%36 = or i32 %34, %35
%37 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i32 0, i32 1
store i32 %36, i32* %37
%38 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i32 0, i32 2
store i32 0, i32* %38
br label %39
39: ; preds = %31, %71
%40 = call i32 @poll(%struct.pollfd* %6, i32 1, i32 -1)
store i32 %40, i32* %8
%41 = load i32, i32* %8
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %39
br label %72
44: ; preds = %39
%45 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i32 0, i32 2
%46 = load i32, i32* %45
%47 = load i32, i32* @POLLHUP
%48 = and i32 %46, %47
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %44
%51 = load i32, i32* %7
%52 = call i32 @close(i32 %51)
br label %11
53: ; preds = %44
%54 = load i32, i32* %7
%55 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%56 = call i32 @read(i32 %54, i8* %55, i32 16)
store i32 %56, i32* %10
%57 = load i32, i32* %10
%58 = icmp slt i32 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %53
%60 = load i32, i32* %10
store i32 %60, i32* %8
br label %72
61: ; preds = %53
%62 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%63 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %62)
%64 = load i32, i32* %7
%65 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%66 = load i32, i32* %10
%67 = call i32 @write(i32 %64, i8* %65, i32 %66)
store i32 %67, i32* %8
%68 = load i32, i32* %8
%69 = icmp slt i32 %68, 0
br i1 %69, label %70, label %71
70: ; preds = %61
br label %72
71: ; preds = %61
br label %39
72: ; preds = %70, %59, %43, %30
%73 = load i32, i32* %7
%74 = call i32 @close(i32 %73)
br label %75
75: ; preds = %72, %19
%76 = load i32, i32* %8
ret i32 %76
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@O_RDWR = global i32 0
@POLLIN = global i32 0
@POLLHUP = global i32 0
@.str = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.pollfd
%4 = alloca [16 x i8]
%5 = bitcast %struct.pollfd* %3 to i8*
%6 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = load i32, i32* @O_RDWR
%10 = call i32 @open(i8* %8, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %16, label %12
12: ; preds = %2
%13 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i64 0, i32 0
%14 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i64 0, i32 1
%15 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i64 0, i32 2
br label %20
16: ; preds = %36, %2
%17 = phi i32 [ %10, %2 ], [ %40, %36 ]
%18 = load i8*, i8** %7
%19 = call i32 @perror(i8* %18)
br label %52
20: ; preds = %12, %36
%21 = phi i32 [ %10, %12 ], [ %40, %36 ]
%22 = call i32 @tty_init(i32 %21)
%23 = icmp slt i32 %22, 0
br i1 %23, label %49, label %24
24: ; preds = %20
store i32 %21, i32* %13
%25 = load i32, i32* @POLLIN
%26 = load i32, i32* @POLLHUP
%27 = or i32 %26, %25
store i32 %27, i32* %14
store i32 0, i32* %15
br label %28
28: ; preds = %45, %24
%29 = call i32 @poll(%struct.pollfd* %3, i32 1, i32 -1)
%30 = icmp slt i32 %29, 0
br i1 %30, label %49, label %31
31: ; preds = %28
%32 = load i32, i32* %15
%33 = load i32, i32* @POLLHUP
%34 = and i32 %33, %32
%35 = icmp eq i32 %34, 0
br i1 %35, label %42, label %36
36: ; preds = %31
%37 = call i32 @close(i32 %21)
%38 = load i8*, i8** %7
%39 = load i32, i32* @O_RDWR
%40 = call i32 @open(i8* %38, i32 %39)
%41 = icmp slt i32 %40, 0
br i1 %41, label %16, label %20
42: ; preds = %31
%43 = call i32 @read(i32 %21, i8* %6, i32 16)
%44 = icmp slt i32 %43, 0
br i1 %44, label %49, label %45
45: ; preds = %42
%46 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %6)
%47 = call i32 @write(i32 %21, i8* %6, i32 %43)
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %28
49: ; preds = %20, %42, %45, %28
%50 = phi i32 [ %43, %42 ], [ %47, %45 ], [ %29, %28 ], [ %22, %20 ]
%51 = call i32 @close(i32 %21)
br label %52
52: ; preds = %49, %16
%53 = phi i32 [ %17, %16 ], [ %50, %49 ]
ret i32 %53
}