#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 */
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ read(int, unsigned char *, int);
int /*<<< orphan*/ write(int, ...);
int main(void) {
  unsigned char c = 32;
  int fds[2];
  pipe(fds);
  pid_t child = fork();
  switch (child) {
  case -1:
    perror("fork failed");
    return 1;
  case 0:
    close(fds[0]);
    while (c < 128) {
      write(fds[1], &c, 1);
      c++;
    }
    close(fds[1]);
    break;
  default:
    close(fds[1]);
    while (read(fds[0], &c, 1)) {
      write(1, &c, 1);
    }
    close(fds[0]);
    write(1, "\n", 1);
    break;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movb   $0x20,-0x5(%rbp)
lea    -0x10(%rbp),%rdi
call   1050 <pipe@plt>
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
sub    $0xffffffff,%eax
je     11aa <main+0x4a>
jmp    119a <main+0x3a>
mov    -0x18(%rbp),%eax
test   %eax,%eax
je     11c2 <main+0x62>
jmp    1206 <main+0xa6>
lea    0xe4f(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1263 <main+0x103>
mov    -0x10(%rbp),%edi
call   1040 <close@plt>
movzbl -0x5(%rbp),%eax
cmp    $0x80,%eax
jge    11f9 <main+0x99>
mov    -0xc(%rbp),%edi
lea    -0x5(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1030 <write@plt>
mov    -0x5(%rbp),%al
add    $0x1,%al
mov    %al,-0x5(%rbp)
jmp    11ca <main+0x6a>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
jmp    125c <main+0xfc>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%edi
lea    -0x5(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
cmp    $0x0,%eax
je     123f <main+0xdf>
mov    $0x1,%edx
lea    -0x5(%rbp),%rsi
mov    %edx,%edi
mov    $0x0,%al
call   1030 <write@plt>
jmp    120e <main+0xae>
mov    -0x10(%rbp),%edi
call   1040 <close@plt>
mov    $0x1,%edx
lea    0xdb9(%rip),%rsi
mov    %edx,%edi
mov    $0x0,%al
call   1030 <write@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
movb   $0x20,0x7(%rsp)
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11fe <main+0x9e>
cmp    $0xffffffff,%eax
je     123d <main+0xdd>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
test   %eax,%eax
je     11d9 <main+0x79>
lea    0x7(%rsp),%rbx
nopl   (%rax)
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
xor    %eax,%eax
call   1030 <write@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
test   %eax,%eax
jne    11b0 <main+0x50>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
lea    0xe23(%rip),%rsi
xor    %ebx,%ebx
mov    $0x1,%edi
mov    $0x1,%edx
xor    %eax,%eax
call   1030 <write@plt>
jmp    1235 <main+0xd5>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
lea    0x7(%rsp),%rbx
nopl   0x0(%rax)
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
xor    %eax,%eax
call   1030 <write@plt>
addb   $0x1,0x7(%rsp)
jns    1210 <main+0xb0>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xdbc(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%ebx
jmp    1235 <main+0xd5>

LLVM IR (-O0):
@.str = [12 x i8] c"fork failed\00"
@.str.1 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8
%3 = alloca [2 x i32]
%4 = alloca i32
store i32 0, i32* %1
store i8 32, i8* %2
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = call i32 (...) @fork()
store i32 %7, i32* %4
%8 = load i32, i32* %4
switch i32 %8, label %29 [
i32 -1, label %9
i32 0, label %11
]
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %46
11: ; preds = %0
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%13 = load i32, i32* %12
%14 = call i32 @close(i32 %13)
br label %15
15: ; preds = %19, %11
%16 = load i8, i8* %2
%17 = zext i8 %16 to i32
%18 = icmp slt i32 %17, 128
br i1 %18, label %19, label %25
19: ; preds = %15
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%21 = load i32, i32* %20
%22 = call i32 (i32, ...) @write(i32 %21, i8* %2, i32 1)
%23 = load i8, i8* %2
%24 = add i8 %23, 1
store i8 %24, i8* %2
br label %15
25: ; preds = %15
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%27 = load i32, i32* %26
%28 = call i32 @close(i32 %27)
br label %45
29: ; preds = %0
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%31 = load i32, i32* %30
%32 = call i32 @close(i32 %31)
br label %33
33: ; preds = %38, %29
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%35 = load i32, i32* %34
%36 = call i32 @read(i32 %35, i8* %2, i32 1)
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %33
%39 = call i32 (i32, ...) @write(i32 1, i8* %2, i32 1)
br label %33
40: ; preds = %33
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%42 = load i32, i32* %41
%43 = call i32 @close(i32 %42)
%44 = call i32 (i32, ...) @write(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
br label %45
45: ; preds = %40, %25
store i32 0, i32* %1
br label %46
46: ; preds = %45, %9
%47 = load i32, i32* %1
ret i32 %47
}

LLVM IR (-O2):
@.str = [12 x i8] c"fork failed\00"
@.str.1 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca i8
%2 = alloca [2 x i32]
store i8 32, i8* %1
%3 = bitcast [2 x i32]* %2 to i8*
%4 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%5 = call i32 @pipe(i32* %4)
%6 = call i32 (...) @fork()
switch i32 %6, label %23 [
i32 -1, label %7
i32 0, label %9
]
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %39
9: ; preds = %0
%10 = load i32, i32* %4
%11 = call i32 @close(i32 %10)
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
br label %13
13: ; preds = %9, %13
%14 = load i32, i32* %12
%15 = call i32 (i32, ...) @write(i32 %14, i8* %1, i32 1)
%16 = load i8, i8* %1
%17 = add i8 %16, 1
store i8 %17, i8* %1
%18 = icmp sgt i8 %17, -1
br i1 %18, label %13, label %19
19: ; preds = %13
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%21 = load i32, i32* %20
%22 = call i32 @close(i32 %21)
br label %39
23: ; preds = %0
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%25 = load i32, i32* %24
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* %4
%28 = call i32 @read(i32 %27, i8* %1, i32 1)
%29 = icmp eq i32 %28, 0
br i1 %29, label %35, label %30
30: ; preds = %23, %30
%31 = call i32 (i32, ...) @write(i32 1, i8* %1, i32 1)
%32 = load i32, i32* %4
%33 = call i32 @read(i32 %32, i8* %1, i32 1)
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %30
35: ; preds = %30, %23
%36 = load i32, i32* %4
%37 = call i32 @close(i32 %36)
%38 = call i32 (i32, ...) @write(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
br label %39
39: ; preds = %19, %35, %7
%40 = phi i32 [ 1, %7 ], [ 0, %35 ], [ 0, %19 ]
ret i32 %40
}