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

/* Variables and functions */
int EOF;
int EXIT_FAILURE;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int fgetc(int /*<<< orphan*/ *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  pid_t pid;
  int mypipe[2];
  int status;
  if (pipe(mypipe)) {
    fprintf(stderr, "Pipe failed.\n");
    return EXIT_FAILURE;
  }
  pid = fork();
  if (pid < 0) {
    printf("fork error %d\n", pid);
    return -1;
  } else if (pid == 0) {
    int nChars = 0;
    close(mypipe[1]);
    FILE *stream;
    int c;
    stream = fdopen(mypipe[0], "r");
    while ((c = fgetc(stream)) != EOF) {
      nChars++;
    }
    fclose(stream);
    return nChars;
  } else {
    int nChars = 0;
    FILE *stream;
    stream = fdopen(mypipe[1], "w");
    if (argc > 1) {
      for (int i = 1; i < argc; i++) {
        fprintf(stream, "%s", argv[i]);
      }
    }
    fclose(stream);
    if (waitpid(pid, &status, 0) > 0) {
      if (WIFEXITED(status)) {
        nChars = WEXITSTATUS(status);
      }
    }
    if (nChars == 1)
      printf("child counted %d character\n", nChars);
    else
      printf("child counted %d characters\n", nChars);
    return 0;
  }
}


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)
lea    -0x20(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0x0,%rax
je     1202 <main+0x52>
mov    0x2df8(%rip),%rax
movslq (%rax),%rdi
lea    0xe16(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2dd8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1380 <main+0x1d0>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1236 <main+0x86>
mov    -0x18(%rbp),%rax
mov    %eax,%esi
lea    0xde9(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1380 <main+0x1d0>
cmpq   $0x0,-0x18(%rbp)
jne    12a0 <main+0xf0>
movl   $0x0,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%edi
lea    0xdc3(%rip),%rsi
call   1090 <fdopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x34(%rbp)
mov    0x2d72(%rip),%rcx
cmp    (%rcx),%eax
je     128c <main+0xdc>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1263 <main+0xb3>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1380 <main+0x1d0>
movl   $0x0,-0x38(%rbp)
mov    -0x1c(%rbp),%edi
lea    0xd6e(%rip),%rsi
call   1090 <fdopen@plt>
mov    %rax,-0x40(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    1308 <main+0x158>
movl   $0x1,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1303 <main+0x153>
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x44(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xd33(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    12cb <main+0x11b>
jmp    1308 <main+0x158>
mov    -0x40(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rdi
lea    -0x24(%rbp),%rsi
xor    %edx,%edx
call   10b0 <waitpid@plt>
cmp    $0x0,%rax
jle    134c <main+0x19c>
mov    -0x24(%rbp),%edi
call   10c0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1347 <main+0x197>
mov    -0x24(%rbp),%edi
call   10a0 <WEXITSTATUS@plt>
mov    %eax,-0x38(%rbp)
jmp    134c <main+0x19c>
cmpl   $0x1,-0x38(%rbp)
jne    136a <main+0x1ba>
mov    -0x38(%rbp),%esi
lea    0xcc4(%rip),%rdi
call   1040 <printf@plt>
jmp    1379 <main+0x1c9>
mov    -0x38(%rbp),%esi
lea    0xccc(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%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    $0x18,%rsp
mov    %rsi,%r12
mov    %edi,%r15d
lea    0x10(%rsp),%rdi
call   1080 <pipe@plt>
test   %rax,%rax
je     121d <main+0x5d>
mov    0x2dee(%rip),%rax
movslq (%rax),%rcx
lea    0xe0c(%rip),%rdi
mov    $0xd,%esi
mov    $0x1,%edx
call   10d0 <fwrite@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   10e0 <fork@plt>
mov    %rax,%r14
test   %rax,%rax
js     12c9 <main+0x109>
mov    0x14(%rsp),%edi
je     12f3 <main+0x133>
lea    0xdde(%rip),%rsi
call   1090 <fdopen@plt>
mov    %rax,%r13
cmp    $0x2,%r15d
jl     1275 <main+0xb5>
mov    %r15d,%ebx
mov    $0x1,%ebp
nopw   0x0(%rax,%rax,1)
mov    (%r12,%rbp,8),%rdi
mov    %r13,%rsi
call   1050 <fputs@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1260 <main+0xa0>
mov    %r13,%rdi
call   1030 <fclose@plt>
lea    0xc(%rsp),%rsi
xor    %ebx,%ebx
mov    %r14,%rdi
xor    %edx,%edx
call   10b0 <waitpid@plt>
mov    $0x0,%esi
test   %rax,%rax
jle    12e2 <main+0x122>
mov    0xc(%rsp),%edi
call   10c0 <WIFEXITED@plt>
mov    $0x0,%esi
test   %rax,%rax
je     12e2 <main+0x122>
mov    0xc(%rsp),%edi
call   10a0 <WEXITSTATUS@plt>
mov    %eax,%esi
cmp    $0x1,%eax
jne    12e2 <main+0x122>
lea    0xd5f(%rip),%rdi
mov    $0x1,%esi
jmp    12e9 <main+0x129>
lea    0xd3e(%rip),%rdi
mov    %r14d,%esi
call   1040 <printf@plt>
mov    $0xffffffff,%ebx
jmp    120c <main+0x4c>
lea    0xd54(%rip),%rdi
call   1040 <printf@plt>
jmp    120c <main+0x4c>
call   1070 <close@plt>
mov    0x10(%rsp),%edi
lea    0xd1a(%rip),%rsi
call   1090 <fdopen@plt>
mov    %rax,%rbp
mov    $0xffffffff,%ebx
mov    0x2cd1(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1060 <fgetc@plt>
add    $0x1,%ebx
cmp    (%r14),%eax
jne    1320 <main+0x160>
mov    %rbp,%rdi
call   1030 <fclose@plt>
jmp    120c <main+0x4c>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [14 x i8] c"Pipe failed.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [15 x i8] c"fork error %d\0A\00"
@.str.2 = [2 x i8] c"r\00"
@EOF = global i32 0
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [28 x i8] c"child counted %d character\0A\00"
@.str.6 = [29 x i8] c"child counted %d characters\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca [2 x i32]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32*
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%16 = call i64 @pipe(i32* %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = sext i32 %19 to i64
%21 = inttoptr i64 %20 to i32*
%22 = call i32 (i32*, i8*, ...) @fprintf(i32* %21, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
store i32 %23, i32* %3
br label %101
24: ; preds = %2
%25 = call i64 (...) @fork()
store i64 %25, i64* %6
%26 = load i64, i64* %6
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = load i64, i64* %6
%30 = trunc i64 %29 to i32
%31 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %30)
store i32 -1, i32* %3
br label %101
32: ; preds = %24
%33 = load i64, i64* %6
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %54
35: ; preds = %32
store i32 0, i32* %9
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%37 = load i32, i32* %36
%38 = call i32 @close(i32 %37)
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%40 = load i32, i32* %39
%41 = call i32* @fdopen(i32 %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %41, i32** %10
br label %42
42: ; preds = %47, %35
%43 = load i32*, i32** %10
%44 = call i32 @fgetc(i32* %43)
store i32 %44, i32* %11
%45 = load i32, i32* @EOF
%46 = icmp ne i32 %44, %45
br i1 %46, label %47, label %50
47: ; preds = %42
%48 = load i32, i32* %9
%49 = add nsw i32 %48, 1
store i32 %49, i32* %9
br label %42
50: ; preds = %42
%51 = load i32*, i32** %10
%52 = call i32 @fclose(i32* %51)
%53 = load i32, i32* %9
store i32 %53, i32* %3
br label %101
54: ; preds = %32
store i32 0, i32* %12
%55 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%56 = load i32, i32* %55
%57 = call i32* @fdopen(i32 %56, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %57, i32** %13
%58 = load i32, i32* %4
%59 = icmp sgt i32 %58, 1
br i1 %59, label %60, label %77
60: ; preds = %54
store i32 1, i32* %14
br label %61
61: ; preds = %73, %60
%62 = load i32, i32* %14
%63 = load i32, i32* %4
%64 = icmp slt i32 %62, %63
br i1 %64, label %65, label %76
65: ; preds = %61
%66 = load i32*, i32** %13
%67 = load i8**, i8*** %5
%68 = load i32, i32* %14
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds i8*, i8** %67, i64 %69
%71 = load i8*, i8** %70
%72 = call i32 (i32*, i8*, ...) @fprintf(i32* %66, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %71)
br label %73
73: ; preds = %65
%74 = load i32, i32* %14
%75 = add nsw i32 %74, 1
store i32 %75, i32* %14
br label %61
76: ; preds = %61
br label %77
77: ; preds = %76, %54
%78 = load i32*, i32** %13
%79 = call i32 @fclose(i32* %78)
%80 = load i64, i64* %6
%81 = call i64 @waitpid(i64 %80, i32* %8, i32 0)
%82 = icmp sgt i64 %81, 0
br i1 %82, label %83, label %91
83: ; preds = %77
%84 = load i32, i32* %8
%85 = call i64 @WIFEXITED(i32 %84)
%86 = icmp ne i64 %85, 0
br i1 %86, label %87, label %90
87: ; preds = %83
%88 = load i32, i32* %8
%89 = call i32 @WEXITSTATUS(i32 %88)
store i32 %89, i32* %12
br label %90
90: ; preds = %87, %83
br label %91
91: ; preds = %90, %77
%92 = load i32, i32* %12
%93 = icmp eq i32 %92, 1
br i1 %93, label %94, label %97
94: ; preds = %91
%95 = load i32, i32* %12
%96 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i32 %95)
br label %100
97: ; preds = %91
%98 = load i32, i32* %12
%99 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i32 %98)
br label %100
100: ; preds = %97, %94
store i32 0, i32* %3
br label %101
101: ; preds = %100, %50, %28, %18
%102 = load i32, i32* %3
ret i32 %102
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [14 x i8] c"Pipe failed.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [15 x i8] c"fork error %d\0A\00"
@.str.2 = [2 x i8] c"r\00"
@EOF = global i32 0
@.str.3 = [2 x i8] c"w\00"
@.str.5 = [28 x i8] c"child counted %d character\0A\00"
@.str.6 = [29 x i8] c"child counted %d characters\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = bitcast [2 x i32]* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%8 = call i64 @pipe(i32* %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %16, label %10
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = sext i32 %11 to i64
%13 = inttoptr i64 %12 to i32*
%14 = call i64 @fwrite(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i64 13, i64 1, i32* %13)
%15 = load i32, i32* @EXIT_FAILURE
br label %67
16: ; preds = %2
%17 = call i64 (...) @fork()
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = trunc i64 %17 to i32
%21 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %20)
br label %67
22: ; preds = %16
%23 = icmp eq i64 %17, 0
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%25 = load i32, i32* %24
br i1 %23, label %26, label %38
26: ; preds = %22
%27 = call i32 @close(i32 %25)
%28 = load i32, i32* %7
%29 = call i32* @fdopen(i32 %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %30, %26
%31 = phi i32 [ 0, %26 ], [ %35, %30 ]
%32 = call i32 @fgetc(i32* %29)
%33 = load i32, i32* @EOF
%34 = icmp eq i32 %32, %33
%35 = add nuw nsw i32 %31, 1
br i1 %34, label %36, label %30
36: ; preds = %30
%37 = call i32 @fclose(i32* %29)
br label %67
38: ; preds = %22
%39 = call i32* @fdopen(i32 %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%40 = icmp sgt i32 %0, 1
br i1 %40, label %41, label %50
41: ; preds = %38
%42 = zext i32 %0 to i64
br label %43
43: ; preds = %41, %43
%44 = phi i64 [ 1, %41 ], [ %48, %43 ]
%45 = getelementptr inbounds i8*, i8** %1, i64 %44
%46 = load i8*, i8** %45
%47 = call i32 @fputs(i8* %46, i32* %39)
%48 = add nuw nsw i64 %44, 1
%49 = icmp eq i64 %48, %42
br i1 %49, label %50, label %43
50: ; preds = %43, %38
%51 = call i32 @fclose(i32* %39)
%52 = call i64 @waitpid(i64 %17, i32* %4, i32 0)
%53 = icmp sgt i64 %52, 0
br i1 %53, label %54, label %64
54: ; preds = %50
%55 = load i32, i32* %4
%56 = call i64 @WIFEXITED(i32 %55)
%57 = icmp eq i64 %56, 0
br i1 %57, label %64, label %58
58: ; preds = %54
%59 = load i32, i32* %4
%60 = call i32 @WEXITSTATUS(i32 %59)
%61 = icmp eq i32 %60, 1
br i1 %61, label %62, label %64
62: ; preds = %58
%63 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i32 1)
br label %67
64: ; preds = %50, %54, %58
%65 = phi i32 [ %60, %58 ], [ 0, %54 ], [ 0, %50 ]
%66 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i32 %65)
br label %67
67: ; preds = %62, %64, %36, %19, %10
%68 = phi i32 [ %15, %10 ], [ -1, %19 ], [ %31, %36 ], [ 0, %64 ], [ 0, %62 ]
ret i32 %68
}