#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 /*<<< orphan*/ close(int);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int write(int, char *, int);
int main() {
  int fd[2];
  if (pipe(fd) < 0) {
    perror("failed pipe.");
    return 1;
  }
  pid_t pid = fork();
  if (pid < 0) {
    perror("failed fork.");
    return 1;
  } else if (pid > 0) {
    close(fd[0]);
    sleep(1);
    int write_ret = write(fd[1], "hello sonico!", 12);
    printf("write return %d.\n", write_ret);
  } else {
    close(fd[1]);
    char line[12];
    int n = read(fd[0], line, 200);
    printf("read return %d, %s.\n", n, line);
    n = read(fd[0], line, 200);
    printf("read return %d, %s.\n", n, line);
    FILE *fpin = popen("echo \"hello sonico!\"", "r");
    while (fgets(line, 200, fpin) != NULL) {
      printf("fgets: %s\n", line);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
jge    11da <main+0x3a>
lea    0xe37(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1307 <main+0x167>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1208 <main+0x68>
lea    0xe16(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1307 <main+0x167>
cmpq   $0x0,-0x18(%rbp)
jle    1252 <main+0xb2>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    -0x8(%rbp),%edi
lea    0xdeb(%rip),%rsi
mov    $0xc,%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xde2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12fb <main+0x15b>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc8,%edx
call   1070 <read@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%esi
lea    -0x28(%rbp),%rdx
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc8,%edx
call   1070 <read@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%esi
lea    -0x28(%rbp),%rdx
lea    0xd95(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd9c(%rip),%rdi
lea    0xdaa(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,-0x38(%rbp)
lea    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    $0xc8,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     12f6 <main+0x156>
lea    -0x28(%rbp),%rsi
lea    0xd7c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12c3 <main+0x123>
jmp    12fb <main+0x15b>
jmp    1300 <main+0x160>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0x4(%rsp),%rdi
call   1060 <pipe@plt>
test   %rax,%rax
js     12c4 <main+0x124>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     12cd <main+0x12d>
je     120f <main+0x6f>
mov    0x4(%rsp),%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    0x8(%rsp),%edi
lea    0xe2d(%rip),%rsi
mov    $0xc,%edx
call   1030 <write@plt>
lea    0xe2a(%rip),%rdi
xor    %r15d,%r15d
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    12df <main+0x13f>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0x4(%rsp),%edi
lea    0xc(%rsp),%r14
mov    %r14,%rsi
mov    $0xc8,%edx
call   1070 <read@plt>
lea    0xe05(%rip),%rbx
xor    %r15d,%r15d
mov    %rbx,%rdi
mov    %eax,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x4(%rsp),%edi
mov    %r14,%rsi
mov    $0xc8,%edx
call   1070 <read@plt>
mov    %rbx,%rdi
mov    %eax,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xde1(%rip),%rdi
lea    0xdef(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0xc8,%esi
mov    %rax,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     12df <main+0x13f>
lea    0xdcd(%rip),%r14
lea    0xc(%rsp),%rbp
xchg   %ax,%ax
mov    %r14,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbp,%rdi
mov    $0xc8,%esi
mov    %rbx,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
jne    12a0 <main+0x100>
jmp    12df <main+0x13f>
lea    0xd35(%rip),%rdi
jmp    12d4 <main+0x134>
lea    0xd39(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%r15d
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"failed pipe.\00"
@.str.1 = [13 x i8] c"failed fork.\00"
@.str.2 = [14 x i8] c"hello sonico!\00"
@.str.3 = [18 x i8] c"write return %d.\0A\00"
@.str.4 = [21 x i8] c"read return %d, %s.\0A\00"
@.str.5 = [21 x i8] c"echo \22hello sonico!\22\00"
@.str.6 = [2 x i8] c"r\00"
@.str.7 = [11 x i8] c"fgets: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i64
%4 = alloca i32
%5 = alloca [12 x i8]
%6 = alloca i32
%7 = alloca i32*
store i32 0, i32* %1
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%9 = call i64 @pipe(i32* %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %62
13: ; preds = %0
%14 = call i64 (...) @fork()
store i64 %14, i64* %3
%15 = load i64, i64* %3
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %62
19: ; preds = %13
%20 = load i64, i64* %3
%21 = icmp sgt i64 %20, 0
br i1 %21, label %22, label %32
22: ; preds = %19
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%24 = load i32, i32* %23
%25 = call i32 @close(i32 %24)
%26 = call i32 @sleep(i32 1)
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%28 = load i32, i32* %27
%29 = call i32 @write(i32 %28, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 12)
store i32 %29, i32* %4
%30 = load i32, i32* %4
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %30)
br label %60
32: ; preds = %19
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%34 = load i32, i32* %33
%35 = call i32 @close(i32 %34)
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%37 = load i32, i32* %36
%38 = getelementptr inbounds [12 x i8], [12 x i8]* %5, i64 0, i64 0
%39 = call i32 @read(i32 %37, i8* %38, i32 200)
store i32 %39, i32* %6
%40 = load i32, i32* %6
%41 = getelementptr inbounds [12 x i8], [12 x i8]* %5, i64 0, i64 0
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %40, i8* %41)
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%44 = load i32, i32* %43
%45 = getelementptr inbounds [12 x i8], [12 x i8]* %5, i64 0, i64 0
%46 = call i32 @read(i32 %44, i8* %45, i32 200)
store i32 %46, i32* %6
%47 = load i32, i32* %6
%48 = getelementptr inbounds [12 x i8], [12 x i8]* %5, i64 0, i64 0
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %47, i8* %48)
%50 = call i32* @popen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
store i32* %50, i32** %7
br label %51
51: ; preds = %56, %32
%52 = getelementptr inbounds [12 x i8], [12 x i8]* %5, i64 0, i64 0
%53 = load i32*, i32** %7
%54 = call i32* @fgets(i8* %52, i32 200, i32* %53)
%55 = icmp ne i32* %54, null
br i1 %55, label %56, label %59
56: ; preds = %51
%57 = getelementptr inbounds [12 x i8], [12 x i8]* %5, i64 0, i64 0
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i8* %57)
br label %51
59: ; preds = %51
br label %60
60: ; preds = %59, %22
br label %61
61: ; preds = %60
store i32 0, i32* %1
br label %62
62: ; preds = %61, %17, %11
%63 = load i32, i32* %1
ret i32 %63
}

LLVM IR (-O2):
@.str = [13 x i8] c"failed pipe.\00"
@.str.1 = [13 x i8] c"failed fork.\00"
@.str.2 = [14 x i8] c"hello sonico!\00"
@.str.3 = [18 x i8] c"write return %d.\0A\00"
@.str.4 = [21 x i8] c"read return %d, %s.\0A\00"
@.str.5 = [21 x i8] c"echo \22hello sonico!\22\00"
@.str.6 = [2 x i8] c"r\00"
@.str.7 = [11 x i8] c"fgets: %s\0A\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [12 x i8]
%3 = bitcast [2 x i32]* %1 to i8*
%4 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%5 = call i64 @pipe(i32* %4)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %43
9: ; preds = %0
%10 = call i64 (...) @fork()
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %43
14: ; preds = %9
%15 = icmp eq i64 %10, 0
br i1 %15, label %24, label %16
16: ; preds = %14
%17 = load i32, i32* %4
%18 = call i32 @close(i32 %17)
%19 = call i32 @sleep(i32 1)
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%21 = load i32, i32* %20
%22 = call i32 @write(i32 %21, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 12)
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %22)
br label %43
24: ; preds = %14
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%26 = load i32, i32* %25
%27 = call i32 @close(i32 %26)
%28 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%29 = load i32, i32* %4
%30 = call i32 @read(i32 %29, i8* %28, i32 200)
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %30, i8* %28)
%32 = load i32, i32* %4
%33 = call i32 @read(i32 %32, i8* %28, i32 200)
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %33, i8* %28)
%35 = call i32* @popen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%36 = call i32* @fgets(i8* %28, i32 200, i32* %35)
%37 = icmp eq i32* %36, null
br i1 %37, label %42, label %38
38: ; preds = %24, %38
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i8* %28)
%40 = call i32* @fgets(i8* %28, i32 200, i32* %35)
%41 = icmp eq i32* %40, null
br i1 %41, label %42, label %38
42: ; preds = %38, %24
br label %43
43: ; preds = %12, %16, %42, %7
%44 = phi i32 [ 1, %7 ], [ 1, %12 ], [ 0, %16 ], [ 0, %42 ]
ret i32 %44
}