#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 */

/* Variables and functions */
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/, ...);
int fork();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ write_frame(int, char *, int);
int main() {
  char buf[64 * 1024];
  int i, flags, fd[2];
  printf("1..1\n");
  if (pipe(fd) < 0) {
    perror("pipe");
    exit(1);
  }
  flags = fcntl(fd[1], F_GETFL);
  if (flags == -1 || fcntl(fd[1], F_SETFL, flags | O_NONBLOCK) == -1) {
    perror("fcntl");
    exit(1);
  }
  switch (fork()) {
  case -1:
    perror("fork");
    exit(1);
  case 0:
    close(fd[1]);
    for (;;) {
      i = read(fd[0], buf, 256);
      if (i == 0)
        break;
      if (i < 0) {
        perror("read");
        exit(1);
      }
    }
    exit(0);
  default:
    break;
  }
  close(fd[0]);
  memset(buf, 0, sizeof buf);
  for (i = 0; i < 1000; i++)
    write_frame(fd[1], buf, sizeof buf);
  printf("ok 1\n");
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10030,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe47(%rip),%rdi
call   1040 <printf@plt>
lea    -0x10020(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0x0,%rax
jge    11ea <main+0x4a>
lea    0xe2b(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x1001c(%rbp),%edi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x10018(%rbp)
cmpl   $0xffffffff,-0x10018(%rbp)
je     1241 <main+0xa1>
mov    -0x1001c(%rbp),%edi
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
mov    -0x10018(%rbp),%edx
mov    0x2db9(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1257 <main+0xb7>
lea    0xdc3(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x10024(%rbp)
sub    $0xffffffff,%eax
je     1287 <main+0xe7>
jmp    1274 <main+0xd4>
mov    -0x10024(%rbp),%eax
test   %eax,%eax
je     129d <main+0xfd>
jmp    1306 <main+0x166>
lea    0xd83(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x1001c(%rbp),%edi
call   1060 <close@plt>
mov    -0x10020(%rbp),%edi
lea    -0x10010(%rbp),%rsi
mov    $0x100,%edx
call   1080 <read@plt>
mov    %eax,-0x10014(%rbp)
cmpl   $0x0,-0x10014(%rbp)
jne    12d7 <main+0x137>
jmp    12ff <main+0x15f>
cmpl   $0x0,-0x10014(%rbp)
jge    12fa <main+0x15a>
lea    0xd2b(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    12a8 <main+0x108>
xor    %edi,%edi
call   10a0 <exit@plt>
jmp    130b <main+0x16b>
mov    -0x10020(%rbp),%edi
call   1060 <close@plt>
lea    -0x10010(%rbp),%rdi
xor    %esi,%esi
mov    $0x10000,%edx
call   1050 <memset@plt>
movl   $0x0,-0x10014(%rbp)
cmpl   $0x3e8,-0x10014(%rbp)
jge    136e <main+0x1ce>
mov    -0x1001c(%rbp),%edi
lea    -0x10010(%rbp),%rsi
mov    $0x10000,%edx
call   10b0 <write_frame@plt>
mov    -0x10014(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10014(%rbp)
jmp    1333 <main+0x193>
lea    0xca6(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x10018,%rsp
lea    0xe65(%rip),%rdi
call   1030 <puts@plt>
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
test   %rax,%rax
jns    11cd <main+0x2d>
lea    0xe35(%rip),%rdi
jmp    1211 <main+0x71>
mov    0xc(%rsp),%edi
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     120a <main+0x6a>
mov    0xc(%rsp),%edi
mov    0x2de7(%rip),%rcx
mov    (%rcx),%esi
mov    0x2dee(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1220 <main+0x80>
lea    0xdf4(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
je     1239 <main+0x99>
cmp    $0xffffffff,%eax
jne    1270 <main+0xd0>
lea    0xdd4(%rip),%rdi
jmp    1211 <main+0x71>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
lea    0x10(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x100,%edx
call   1080 <read@plt>
test   %eax,%eax
je     12c2 <main+0x122>
jns    1250 <main+0xb0>
lea    0xda2(%rip),%rdi
jmp    1211 <main+0x71>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10000,%edx
call   1050 <memset@plt>
mov    $0x3e8,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0x10000,%edx
call   10b0 <write_frame@plt>
add    $0xffffffff,%ebp
jne    12a0 <main+0x100>
lea    0xd5d(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"1..1\0A\00"
@.str.1 = [5 x i8] c"pipe\00"
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [6 x i8] c"fcntl\00"
@.str.3 = [5 x i8] c"fork\00"
@.str.4 = [5 x i8] c"read\00"
@.str.5 = [6 x i8] c"ok 1\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [65536 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca [2 x i32]
store i32 0, i32* %1
%6 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%8 = call i64 @pipe(i32* %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%15 = load i32, i32* %14
%16 = load i32, i32* @F_GETFL
%17 = call i32 (i32, i32, ...) @fcntl(i32 %15, i32 %16)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = icmp eq i32 %18, -1
br i1 %19, label %29, label %20
20: ; preds = %13
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%22 = load i32, i32* %21
%23 = load i32, i32* @F_SETFL
%24 = load i32, i32* %4
%25 = load i32, i32* @O_NONBLOCK
%26 = or i32 %24, %25
%27 = call i32 (i32, i32, ...) @fcntl(i32 %22, i32 %23, i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %20, %13
%30 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %20
%33 = call i32 (...) @fork()
switch i32 %33, label %58 [
i32 -1, label %34
i32 0, label %37
]
34: ; preds = %32
%35 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %32
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%39 = load i32, i32* %38
%40 = call i32 @close(i32 %39)
br label %41
41: ; preds = %55, %37
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%43 = load i32, i32* %42
%44 = getelementptr inbounds [65536 x i8], [65536 x i8]* %2, i64 0, i64 0
%45 = call i32 @read(i32 %43, i8* %44, i32 256)
store i32 %45, i32* %3
%46 = load i32, i32* %3
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %41
br label %56
49: ; preds = %41
%50 = load i32, i32* %3
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %49
%53 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %49
br label %41
56: ; preds = %48
%57 = call i32 @exit(i32 0)
unreachable
58: ; preds = %32
br label %59
59: ; preds = %58
%60 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%61 = load i32, i32* %60
%62 = call i32 @close(i32 %61)
%63 = getelementptr inbounds [65536 x i8], [65536 x i8]* %2, i64 0, i64 0
%64 = call i32 @memset(i8* %63, i32 0, i32 65536)
store i32 0, i32* %3
br label %65
65: ; preds = %73, %59
%66 = load i32, i32* %3
%67 = icmp slt i32 %66, 1000
br i1 %67, label %68, label %76
68: ; preds = %65
%69 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%70 = load i32, i32* %69
%71 = getelementptr inbounds [65536 x i8], [65536 x i8]* %2, i64 0, i64 0
%72 = call i32 @write_frame(i32 %70, i8* %71, i32 65536)
br label %73
73: ; preds = %68
%74 = load i32, i32* %3
%75 = add nsw i32 %74, 1
store i32 %75, i32* %3
br label %65
76: ; preds = %65
%77 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%78 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str.1 = [5 x i8] c"pipe\00"
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [6 x i8] c"fcntl\00"
@.str.3 = [5 x i8] c"fork\00"
@.str.4 = [5 x i8] c"read\00"
@str = [5 x i8] c"1..1\00"
@str.6 = [5 x i8] c"ok 1\00"
define i32 @main() {
%1 = alloca [65536 x i8]
%2 = alloca [2 x i32]
%3 = getelementptr inbounds [65536 x i8], [65536 x i8]* %1, i64 0, i64 0
%4 = bitcast [2 x i32]* %2 to i8*
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%7 = call i64 @pipe(i32* %6)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%14 = load i32, i32* %13
%15 = load i32, i32* @F_GETFL
%16 = call i32 (i32, i32, ...) @fcntl(i32 %14, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %25, label %18
18: ; preds = %12
%19 = load i32, i32* %13
%20 = load i32, i32* @F_SETFL
%21 = load i32, i32* @O_NONBLOCK
%22 = or i32 %21, %16
%23 = call i32 (i32, i32, ...) @fcntl(i32 %19, i32 %20, i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %18, %12
%26 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %18
%29 = call i32 (...) @fork()
switch i32 %29, label %47 [
i32 -1, label %30
i32 0, label %33
]
30: ; preds = %28
%31 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %28
%34 = load i32, i32* %13
%35 = call i32 @close(i32 %34)
br label %36
36: ; preds = %40, %33
%37 = load i32, i32* %6
%38 = call i32 @read(i32 %37, i8* %3, i32 256)
%39 = icmp eq i32 %38, 0
br i1 %39, label %45, label %40
40: ; preds = %36
%41 = icmp slt i32 %38, 0
br i1 %41, label %42, label %36
42: ; preds = %40
%43 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %36
%46 = call i32 @exit(i32 0)
unreachable
47: ; preds = %28
%48 = load i32, i32* %6
%49 = call i32 @close(i32 %48)
%50 = call i32 @memset(i8* %3, i32 0, i32 65536)
br label %51
51: ; preds = %47, %51
%52 = phi i32 [ 0, %47 ], [ %55, %51 ]
%53 = load i32, i32* %13
%54 = call i32 @write_frame(i32 %53, i8* %3, i32 65536)
%55 = add nuw nsw i32 %52, 1
%56 = icmp eq i32 %55, 1000
br i1 %56, label %57, label %51
57: ; preds = %51
%58 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.6, i64 0, i64 0))
%59 = call i32 @exit(i32 0)
unreachable
}