#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*/ EINVAL;
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int fork();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *);
scalar_t__ read(int, char *, int);
int strlen(char *);
int /*<<< orphan*/ wait();
int write(int, char *, int);
int main(int argc, char **argv) {
  int pipefd[2];
  pid_t pid;
  int ret;
  char buf[1];
  if (argc != 2 || argv[1] == NULL) {
    errno = EINVAL;
    perror("one arg string is needed");
    return -1;
  }
  ret = pipe(pipefd);
  if (ret == -1) {
    perror("pipe");
    return -1;
  }
  pid = fork();
  if (pid == -1) {
    perror("fork");
    return -1;
  }
  if (pid == 0) {
    close(pipefd[1]);
    printf("child start\n");
    while (read(pipefd[0], buf, 1) > 0) {
      ret = write(STDOUT_FILENO, buf, 1);
      if (ret == -1) {
        perror("write error in child");
        return -1;
      }
    }
    close(pipefd[0]);
    printf("\nchild end\n");
    return 0;
  }
  close(pipefd[0]);
  ret = write(pipefd[1], argv[1], strlen(argv[1]));
  if (ret == -1) {
    perror("write error in parent");
    return -1;
  }
  close(pipefd[1]);
  wait();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    11bf <main+0x2f>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    11e9 <main+0x59>
mov    0x2e0a(%rip),%rax
mov    (%rax),%ecx
mov    0x2e09(%rip),%rax
mov    %ecx,(%rax)
lea    0xe28(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134d <main+0x1bd>
lea    -0x18(%rbp),%rdi
call   1070 <pipe@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1217 <main+0x87>
lea    0xe13(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134d <main+0x1bd>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1243 <main+0xb3>
lea    0xdec(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134d <main+0x1bd>
cmpl   $0x0,-0x1c(%rbp)
jne    12dd <main+0x14d>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
lea    0xdc7(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1080 <read@plt>
cmp    $0x0,%rax
jle    12bd <main+0x12d>
mov    0x2d6d(%rip),%rax
mov    (%rax),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    12b8 <main+0x128>
lea    0xd89(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134d <main+0x1bd>
jmp    1261 <main+0xd1>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
lea    0xd79(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    134d <main+0x1bd>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1337 <main+0x1a7>
lea    0xd2b(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134d <main+0x1bd>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    $0x0,%al
call   10a0 <wait@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jne    128d <main+0xfd>
mov    %rsi,%rbx
cmpq   $0x0,0x8(%rsi)
je     128d <main+0xfd>
lea    0x10(%rsp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
je     12a8 <main+0x118>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     1219 <main+0x89>
cmp    $0xffffffff,%eax
je     12b1 <main+0x121>
mov    0x10(%rsp),%edi
call   1060 <close@plt>
mov    0x14(%rsp),%ebp
mov    0x8(%rbx),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     12ba <main+0x12a>
mov    0x14(%rsp),%edi
call   1060 <close@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   10a0 <wait@plt>
jmp    12cb <main+0x13b>
mov    0x14(%rsp),%edi
call   1060 <close@plt>
lea    0xe25(%rip),%rdi
call   1030 <puts@plt>
lea    0xf(%rsp),%rbx
mov    0x2db6(%rip),%rbp
nopw   0x0(%rax,%rax,1)
mov    0x10(%rsp),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1080 <read@plt>
test   %rax,%rax
jle    1274 <main+0xe4>
mov    0x0(%rbp),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    1240 <main+0xb0>
lea    0xdb1(%rip),%rdi
jmp    12c1 <main+0x131>
mov    0x10(%rsp),%edi
call   1060 <close@plt>
lea    0xdd6(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
jmp    12cb <main+0x13b>
mov    0x2d3c(%rip),%rax
mov    (%rax),%eax
mov    0x2d3b(%rip),%rcx
mov    %eax,(%rcx)
lea    0xd5a(%rip),%rdi
jmp    12c1 <main+0x131>
lea    0xd6a(%rip),%rdi
jmp    12c1 <main+0x131>
lea    0xd66(%rip),%rdi
jmp    12c1 <main+0x131>
lea    0xd77(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
@.str = [25 x i8] c"one arg string is needed\00"
@.str.1 = [5 x i8] c"pipe\00"
@.str.2 = [5 x i8] c"fork\00"
@.str.3 = [13 x i8] c"child start\0A\00"
@STDOUT_FILENO = global i32 0
@.str.4 = [21 x i8] c"write error in child\00"
@.str.5 = [12 x i8] c"\0Achild end\0A\00"
@.str.6 = [22 x i8] c"write error in parent\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [2 x i32]
%7 = alloca i32
%8 = alloca i32
%9 = alloca [1 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %17, label %12
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %12, %2
%18 = load i32, i32* @EINVAL
store i32 %18, i32* @errno
%19 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %84
20: ; preds = %12
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%22 = call i32 @pipe(i32* %21)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %84
27: ; preds = %20
%28 = call i32 (...) @fork()
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %84
33: ; preds = %27
%34 = load i32, i32* %7
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %61
36: ; preds = %33
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%38 = load i32, i32* %37
%39 = call i32 @close(i32 %38)
%40 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %41
41: ; preds = %55, %36
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%43 = load i32, i32* %42
%44 = getelementptr inbounds [1 x i8], [1 x i8]* %9, i64 0, i64 0
%45 = call i64 @read(i32 %43, i8* %44, i32 1)
%46 = icmp sgt i64 %45, 0
br i1 %46, label %47, label %56
47: ; preds = %41
%48 = load i32, i32* @STDOUT_FILENO
%49 = getelementptr inbounds [1 x i8], [1 x i8]* %9, i64 0, i64 0
%50 = call i32 @write(i32 %48, i8* %49, i32 1)
store i32 %50, i32* %8
%51 = load i32, i32* %8
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %55
53: ; preds = %47
%54 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %84
55: ; preds = %47
br label %41
56: ; preds = %41
%57 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%58 = load i32, i32* %57
%59 = call i32 @close(i32 %58)
%60 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %3
br label %84
61: ; preds = %33
%62 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%63 = load i32, i32* %62
%64 = call i32 @close(i32 %63)
%65 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%66 = load i32, i32* %65
%67 = load i8**, i8*** %5
%68 = getelementptr inbounds i8*, i8** %67, i64 1
%69 = load i8*, i8** %68
%70 = load i8**, i8*** %5
%71 = getelementptr inbounds i8*, i8** %70, i64 1
%72 = load i8*, i8** %71
%73 = call i32 @strlen(i8* %72)
%74 = call i32 @write(i32 %66, i8* %69, i32 %73)
store i32 %74, i32* %8
%75 = load i32, i32* %8
%76 = icmp eq i32 %75, -1
br i1 %76, label %77, label %79
77: ; preds = %61
%78 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %3
br label %84
79: ; preds = %61
%80 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%81 = load i32, i32* %80
%82 = call i32 @close(i32 %81)
%83 = call i32 (...) @wait()
store i32 0, i32* %3
br label %84
84: ; preds = %79, %77, %56, %53, %31, %25, %17
%85 = load i32, i32* %3
ret i32 %85
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@.str = [25 x i8] c"one arg string is needed\00"
@.str.1 = [5 x i8] c"pipe\00"
@.str.2 = [5 x i8] c"fork\00"
@STDOUT_FILENO = global i32 0
@.str.4 = [21 x i8] c"write error in child\00"
@.str.6 = [22 x i8] c"write error in parent\00"
@str = [12 x i8] c"child start\00"
@str.7 = [11 x i8] c"\0Achild end\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2 x i32]
%4 = alloca [1 x i8]
%5 = bitcast [2 x i32]* %3 to i8*
%6 = getelementptr inbounds [1 x i8], [1 x i8]* %4, i64 0, i64 0
%7 = icmp eq i32 %0, 2
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %8, %2
%13 = load i32, i32* @EINVAL
store i32 %13, i32* @errno
%14 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %59
15: ; preds = %8
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%17 = call i32 @pipe(i32* %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %59
21: ; preds = %15
%22 = call i32 (...) @fork()
switch i32 %22, label %44 [
i32 -1, label %23
i32 0, label %25
]
23: ; preds = %21
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %59
25: ; preds = %21
%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)
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %30
30: ; preds = %34, %25
%31 = load i32, i32* %16
%32 = call i64 @read(i32 %31, i8* %6, i32 1)
%33 = icmp sgt i64 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %30
%35 = load i32, i32* @STDOUT_FILENO
%36 = call i32 @write(i32 %35, i8* %6, i32 1)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %30
38: ; preds = %34
%39 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %59
40: ; preds = %30
%41 = load i32, i32* %16
%42 = call i32 @close(i32 %41)
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.7, i64 0, i64 0))
br label %59
44: ; preds = %21
%45 = load i32, i32* %16
%46 = call i32 @close(i32 %45)
%47 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%48 = load i32, i32* %47
%49 = load i8*, i8** %9
%50 = call i32 @strlen(i8* %49)
%51 = call i32 @write(i32 %48, i8* %49, i32 %50)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %55
53: ; preds = %44
%54 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0))
br label %59
55: ; preds = %44
%56 = load i32, i32* %47
%57 = call i32 @close(i32 %56)
%58 = call i32 (...) @wait()
br label %59
59: ; preds = %55, %53, %40, %38, %23, %19, %12
%60 = phi i32 [ -1, %12 ], [ -1, %19 ], [ -1, %23 ], [ -1, %38 ], [ 0, %40 ], [ -1, %53 ], [ 0, %55 ]
ret i32 %60
}