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

/* Variables and functions */
int POLLIN;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ poll(struct pollfd *, int, long);
int /*<<< orphan*/ printf(char *);
scalar_t__ read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  ssize_t n = 0;
  char buf[1024] = "";
  struct pollfd fds[1];
  while (1) {
    printf("new iteration\n");
    fds[0].fd = STDIN_FILENO;
    fds[0].events = POLLIN;
    poll(fds, 1, 1000L);
    if (fds[0].revents & POLLIN) {
      n = read(fds[0].fd, buf, 1024);
      if (!n) {
        printf("stdin closed\n");
        return 0;
      }
      fprintf(stdout, "--> %s\n", buf);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
lea    0xe75(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x428(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x430(%rbp)
lea    -0x430(%rbp),%rdi
mov    $0x1,%esi
mov    $0x3e8,%edx
call   1070 <poll@plt>
mov    -0x42c(%rbp),%eax
mov    0x2e17(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1235 <main+0xe5>
mov    -0x428(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1219 <main+0xc9>
lea    0xe06(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
lea    -0x420(%rbp),%rdx
lea    0xded(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1184 <main+0x34>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
lea    0x10(%rsp),%rbx
mov    $0x400,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
lea    0xe8c(%rip),%r14
mov    0x2e4d(%rip),%r13
mov    0x2e5e(%rip),%rbp
mov    %rsp,%r15
lea    0xe6c(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14,%rdi
call   1030 <puts@plt>
mov    0x0(%r13),%eax
mov    %eax,0x8(%rsp)
mov    0x0(%rbp),%eax
mov    %eax,(%rsp)
mov    $0x3e8,%edx
mov    %r15,%rdi
mov    $0x1,%esi
call   1070 <poll@plt>
mov    0x0(%rbp),%eax
test   %eax,0x4(%rsp)
je     11a0 <main+0x50>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
test   %rax,%rax
je     11fd <main+0xad>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1060 <fprintf@plt>
jmp    11a0 <main+0x50>
lea    0xe12(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@.str = [15 x i8] c"new iteration\0A\00"
@STDIN_FILENO = global i32 0
@POLLIN = global i32 0
@.str.1 = [14 x i8] c"stdin closed\0A\00"
@stdout = global i32 0
@.str.2 = [8 x i8] c"--> %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca [1024 x i8]
%8 = alloca [1 x %struct.pollfd]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 0, i64* %6
%9 = bitcast [1024 x i8]* %7 to i8*
br label %10
10: ; preds = %2, %40
%11 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @STDIN_FILENO
%13 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %8, i64 0, i64 0
%14 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %13, i32 0, i32 2
store i32 %12, i32* %14
%15 = load i32, i32* @POLLIN
%16 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %8, i64 0, i64 0
%17 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %16, i32 0, i32 0
store i32 %15, i32* %17
%18 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %8, i64 0, i64 0
%19 = call i32 @poll(%struct.pollfd* %18, i32 1, i64 1000)
%20 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %8, i64 0, i64 0
%21 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %20, i32 0, i32 1
%22 = load i32, i32* %21
%23 = load i32, i32* @POLLIN
%24 = and i32 %22, %23
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %40
26: ; preds = %10
%27 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %8, i64 0, i64 0
%28 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %27, i32 0, i32 2
%29 = load i32, i32* %28
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%31 = call i64 @read(i32 %29, i8* %30, i32 1024)
store i64 %31, i64* %6
%32 = load i64, i64* %6
%33 = icmp ne i64 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %26
%35 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
ret i32 0
36: ; preds = %26
%37 = load i32, i32* @stdout
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%39 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %38)
br label %40
40: ; preds = %36, %10
br label %10
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@STDIN_FILENO = global i32 0
@POLLIN = global i32 0
@stdout = global i32 0
@.str.2 = [8 x i8] c"--> %s\0A\00"
@str = [14 x i8] c"new iteration\00"
@str.3 = [13 x i8] c"stdin closed\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [1024 x i8]
%4 = alloca [1 x %struct.pollfd]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = bitcast [1 x %struct.pollfd]* %4 to i8*
%7 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %4, i64 0, i64 0
%8 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %4, i64 0, i64 0, i32 2
%9 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %4, i64 0, i64 0, i32 0
%10 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %4, i64 0, i64 0, i32 1
br label %11
11: ; preds = %29, %2
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%13 = load i32, i32* @STDIN_FILENO
store i32 %13, i32* %8
%14 = load i32, i32* @POLLIN
store i32 %14, i32* %9
%15 = call i32 @poll(%struct.pollfd* %7, i32 1, i64 1000)
%16 = load i32, i32* %10
%17 = load i32, i32* @POLLIN
%18 = and i32 %17, %16
%19 = icmp eq i32 %18, 0
br i1 %19, label %29, label %20
20: ; preds = %11
%21 = load i32, i32* %8
%22 = call i64 @read(i32 %21, i8* %5, i32 1024)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.3, i64 0, i64 0))
ret i32 0
26: ; preds = %20
%27 = load i32, i32* @stdout
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %5)
br label %29
29: ; preds = %26, %11
br label %11
}