#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*/ err(int, char *, ...);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ errx(int, char *);
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/, ...);
int fork();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ write_frame(int, char *, int);
int main(void) {
  char buf[64 * 1024];
  int i, flags, fd[2];
  if (pipe(fd) < 0)
    errx(1, "pipe failed");
  flags = fcntl(fd[1], F_GETFL);
  if (flags == -1 || fcntl(fd[1], F_SETFL, flags | O_NONBLOCK) == -1) {
    printf("fcntl failed: %s\n", strerror(errno));
    exit(1);
  }
  switch (fork()) {
  case -1:
    err(1, "fork failed: %s\n", strerror(errno));
    break;
  case 0:
    close(fd[1]);
    for (;;) {
      i = read(fd[0], buf, 256);
      if (i == 0)
        break;
      if (i < 0)
        err(1, "read");
    }
    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\n");
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10030,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x10020(%rbp),%rdi
call   1080 <pipe@plt>
cmp    $0x0,%rax
jge    11f9 <main+0x39>
mov    $0x1,%edi
lea    0xe0c(%rip),%rsi
call   1040 <errx@plt>
mov    -0x1001c(%rbp),%edi
mov    0x2dc2(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x10018(%rbp)
cmpl   $0xffffffff,-0x10018(%rbp)
je     1250 <main+0x90>
mov    -0x1001c(%rbp),%edi
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
mov    -0x10018(%rbp),%edx
mov    0x2daa(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1279 <main+0xb9>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xda4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x10024(%rbp)
sub    $0xffffffff,%eax
je     12a9 <main+0xe9>
jmp    1296 <main+0xd6>
mov    -0x10024(%rbp),%eax
test   %eax,%eax
je     12d2 <main+0x112>
jmp    1338 <main+0x178>
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rdx
mov    $0x1,%edi
lea    0xd58(%rip),%rsi
mov    $0x0,%al
call   10a0 <err@plt>
jmp    133d <main+0x17d>
mov    -0x1001c(%rbp),%edi
call   1070 <close@plt>
mov    -0x10020(%rbp),%edi
lea    -0x10010(%rbp),%rsi
mov    $0x100,%edx
call   1090 <read@plt>
mov    %eax,-0x10014(%rbp)
cmpl   $0x0,-0x10014(%rbp)
jne    130c <main+0x14c>
jmp    1331 <main+0x171>
cmpl   $0x0,-0x10014(%rbp)
jge    132c <main+0x16c>
mov    $0x1,%edi
lea    0xd0a(%rip),%rsi
mov    $0x0,%al
call   10a0 <err@plt>
jmp    12dd <main+0x11d>
xor    %edi,%edi
call   10b0 <exit@plt>
jmp    133d <main+0x17d>
mov    -0x10020(%rbp),%edi
call   1070 <close@plt>
lea    -0x10010(%rbp),%rdi
xor    %esi,%esi
mov    $0x10000,%edx
call   1060 <memset@plt>
movl   $0x0,-0x10014(%rbp)
cmpl   $0x3e8,-0x10014(%rbp)
jge    13a0 <main+0x1e0>
mov    -0x1001c(%rbp),%edi
lea    -0x10010(%rbp),%rsi
mov    $0x10000,%edx
call   10d0 <write_frame@plt>
mov    -0x10014(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10014(%rbp)
jmp    1365 <main+0x1a5>
lea    0xc8d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10010,%rsp
lea    0x8(%rsp),%rdi
call   1090 <pipe@plt>
test   %rax,%rax
jns    11fb <main+0x2b>
lea    0xe0f(%rip),%rsi
mov    $0x1,%edi
call   1050 <errx@plt>
mov    0xc(%rsp),%edi
mov    0x2dc2(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1238 <main+0x68>
mov    0xc(%rsp),%edi
mov    0x2db9(%rip),%rcx
mov    (%rcx),%esi
mov    0x2dc0(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1261 <main+0x91>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xdbf(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %eax,%eax
je     12e9 <main+0x119>
cmp    $0xffffffff,%eax
jne    1295 <main+0xc5>
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd98(%rip),%rsi
mov    $0x1,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   10b0 <err@plt>
mov    0x8(%rsp),%edi
call   1080 <close@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10000,%edx
call   1070 <memset@plt>
mov    $0x3e8,%ebp
nopw   0x0(%rax,%rax,1)
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0x10000,%edx
call   10e0 <write_frame@plt>
add    $0xffffffff,%ebp
jne    12c0 <main+0xf0>
lea    0xd57(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    0xc(%rsp),%edi
call   1080 <close@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x100,%edx
call   10a0 <read@plt>
test   %eax,%eax
jne    1310 <main+0x140>
xor    %edi,%edi
call   10c0 <exit@plt>
lea    0xd18(%rip),%r14
lea    0x10(%rsp),%rbx
jmp    1335 <main+0x165>
xchg   %ax,%ax
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x100,%edx
call   10a0 <read@plt>
test   %eax,%eax
je     1309 <main+0x139>
test   %eax,%eax
jns    1320 <main+0x150>
mov    $0x1,%edi
mov    %r14,%rsi
xor    %eax,%eax
call   10b0 <err@plt>
jmp    1320 <main+0x150>

LLVM IR (-O0):
@.str = [12 x i8] c"pipe failed\00"
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [18 x i8] c"fcntl failed: %s\0A\00"
@errno = global i32 0
@.str.2 = [17 x i8] c"fork failed: %s\0A\00"
@.str.3 = [5 x i8] c"read\00"
@.str.4 = [4 x i8] c"ok\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 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%7 = call i64 @pipe(i32* %6)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @errx(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %0
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%13 = load i32, i32* %12
%14 = load i32, i32* @F_GETFL
%15 = call i32 (i32, i32, ...) @fcntl(i32 %13, i32 %14)
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = icmp eq i32 %16, -1
br i1 %17, label %27, label %18
18: ; preds = %11
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%20 = load i32, i32* %19
%21 = load i32, i32* @F_SETFL
%22 = load i32, i32* %4
%23 = load i32, i32* @O_NONBLOCK
%24 = or i32 %22, %23
%25 = call i32 (i32, i32, ...) @fcntl(i32 %20, i32 %21, i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %32
27: ; preds = %18, %11
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %29)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %18
%33 = call i32 (...) @fork()
switch i32 %33, label %58 [
i32 -1, label %34
i32 0, label %38
]
34: ; preds = %32
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %36)
br label %59
38: ; preds = %32
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%40 = load i32, i32* %39
%41 = call i32 @close(i32 %40)
br label %42
42: ; preds = %55, %38
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%44 = load i32, i32* %43
%45 = getelementptr inbounds [65536 x i8], [65536 x i8]* %2, i64 0, i64 0
%46 = call i32 @read(i32 %44, i8* %45, i32 256)
store i32 %46, i32* %3
%47 = load i32, i32* %3
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %42
br label %56
50: ; preds = %42
%51 = load i32, i32* %3
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %50
%54 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %55
55: ; preds = %53, %50
br label %42
56: ; preds = %49
%57 = call i32 @exit(i32 0)
unreachable
58: ; preds = %32
br label %59
59: ; preds = %58, %34
%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 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
%78 = call i32 @exit(i32 0)
unreachable
}

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