#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 fork();
int getpid();
int pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ readAndPrint();
int /*<<< orphan*/ replace(int, int);
int substitute(int, int);
int /*<<< orphan*/ syserr(char *);
int wait(int /*<<< orphan*/);
int main() {
  pid_t pid;
  char msg[] = "123";
  int pipe_dsc[2];
  if (pipe(pipe_dsc) == -1) {
    syserr("Error in pipe");
  }
  switch (pid = fork()) {
  case -1:
    syserr("Error in fork\n");
    break;
  case 0:
    printf("I am a child and my pid is %d\n", getpid());
    printf("I am a child and fork() return value is %d\n", pid);
    int b = substitute(pipe_dsc[0], 0);
    readAndPrint();
    replace(b, 0);
    break;
  default:
    printf("I am a parent and my pid is %d\n", getpid());
    printf("I am a parent and fork() return value is %d\n", pid);
    int out = substitute(pipe_dsc[1], 1);
    printf("%s\n", msg);
    printf("%s\n", msg);
    printf("\n");
    puts("OKK");
    replace(out, 1);
    puts("OK");
    if (wait(0) == -1)
      syserr("Error in wait\n");
    return 0;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe4b(%rip),%eax
mov    %eax,-0xc(%rbp)
lea    -0x14(%rbp),%rdi
call   1080 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11d6 <main+0x36>
lea    0xe33(%rip),%rdi
call   1070 <syserr@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,-0x8(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x20(%rbp)
sub    $0xffffffff,%eax
je     1203 <main+0x63>
jmp    11f3 <main+0x53>
mov    -0x20(%rbp),%eax
test   %eax,%eax
je     1214 <main+0x74>
jmp    125f <main+0xbf>
lea    0xe08(%rip),%rdi
call   1070 <syserr@plt>
jmp    1311 <main+0x171>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%esi
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   10a0 <substitute@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1050 <readAndPrint@plt>
mov    -0x18(%rbp),%edi
xor    %esi,%esi
call   1090 <replace@plt>
jmp    1311 <main+0x171>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%esi
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%edi
mov    $0x1,%esi
call   10a0 <substitute@plt>
mov    %eax,-0x1c(%rbp)
lea    -0xc(%rbp),%rsi
lea    0xe17(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0xc(%rbp),%rsi
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xd7c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xded(%rip),%rdi
call   1030 <puts@plt>
mov    -0x1c(%rbp),%edi
mov    $0x1,%esi
call   1090 <replace@plt>
lea    0xdd8(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10b0 <wait@plt>
cmp    $0xffffffff,%eax
jne    130a <main+0x16a>
lea    0xdbf(%rip),%rdi
call   1070 <syserr@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
movl   $0x333231,0xc(%rsp)
lea    0x10(%rsp),%rdi
call   1090 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11d9 <main+0x29>
lea    0xe2c(%rip),%rdi
call   1080 <syserr@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
je     11f7 <main+0x47>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1240 <main+0x90>
lea    0xe1c(%rip),%rdi
jmp    12cd <main+0x11d>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe18(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xe27(%rip),%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x10(%rsp),%edi
xor    %esi,%esi
call   10b0 <substitute@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <readAndPrint@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   10a0 <replace@plt>
jmp    12d2 <main+0x122>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe1a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xe2a(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x14(%rsp),%edi
mov    $0x1,%esi
call   10b0 <substitute@plt>
mov    %eax,%ebp
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1040 <puts@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xe18(%rip),%rdi
call   1040 <puts@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   10a0 <replace@plt>
lea    0xe04(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10c0 <wait@plt>
cmp    $0xffffffff,%eax
jne    12d2 <main+0x122>
lea    0xdef(%rip),%rdi
call   1080 <syserr@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.msg = [4 x i8] c"123\00"
@.str = [14 x i8] c"Error in pipe\00"
@.str.1 = [15 x i8] c"Error in fork\0A\00"
@.str.2 = [31 x i8] c"I am a child and my pid is %d\0A\00"
@.str.3 = [44 x i8] c"I am a child and fork() return value is %d\0A\00"
@.str.4 = [32 x i8] c"I am a parent and my pid is %d\0A\00"
@.str.5 = [45 x i8] c"I am a parent and fork() return value is %d\0A\00"
@.str.6 = [4 x i8] c"%s\0A\00"
@.str.7 = [2 x i8] c"\0A\00"
@.str.8 = [4 x i8] c"OKK\00"
@.str.9 = [3 x i8] c"OK\00"
@.str.10 = [15 x i8] c"Error in wait\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [4 x i8]
%4 = alloca [2 x i32]
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = bitcast [4 x i8]* %3 to i8*
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%9 = call i32 @pipe(i32* %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @syserr(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %0
%14 = call i32 (...) @fork()
store i32 %14, i32* %2
switch i32 %14, label %28 [
i32 -1, label %15
i32 0, label %17
]
15: ; preds = %13
%16 = call i32 @syserr(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %50
17: ; preds = %13
%18 = call i32 (...) @getpid()
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%20 = load i32, i32* %2
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i32 %20)
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%23 = load i32, i32* %22
%24 = call i32 @substitute(i32 %23, i32 0)
store i32 %24, i32* %5
%25 = call i32 (...) @readAndPrint()
%26 = load i32, i32* %5
%27 = call i32 @replace(i32 %26, i32 0)
br label %50
28: ; preds = %13
%29 = call i32 (...) @getpid()
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %29)
%31 = load i32, i32* %2
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0), i32 %31)
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%34 = load i32, i32* %33
%35 = call i32 @substitute(i32 %34, i32 1)
store i32 %35, i32* %6
%36 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %36)
%38 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %38)
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%41 = call i32 @puts(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0))
%42 = load i32, i32* %6
%43 = call i32 @replace(i32 %42, i32 1)
%44 = call i32 @puts(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i64 0, i64 0))
%45 = call i32 @wait(i32 0)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %49
47: ; preds = %28
%48 = call i32 @syserr(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.10, i64 0, i64 0))
br label %49
49: ; preds = %47, %28
store i32 0, i32* %1
br label %50
50: ; preds = %49, %17, %15
%51 = load i32, i32* %1
ret i32 %51
}

LLVM IR (-O2):
@.str = [14 x i8] c"Error in pipe\00"
@.str.1 = [15 x i8] c"Error in fork\0A\00"
@.str.2 = [31 x i8] c"I am a child and my pid is %d\0A\00"
@.str.3 = [44 x i8] c"I am a child and fork() return value is %d\0A\00"
@.str.4 = [32 x i8] c"I am a parent and my pid is %d\0A\00"
@.str.5 = [45 x i8] c"I am a parent and fork() return value is %d\0A\00"
@.str.8 = [4 x i8] c"OKK\00"
@.str.9 = [3 x i8] c"OK\00"
@.str.10 = [15 x i8] c"Error in wait\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = bitcast i32* %1 to i8*
store i32 3355185, i32* %1
%4 = bitcast [2 x i32]* %2 to i8*
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @syserr(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %0
%11 = call i32 (...) @fork()
switch i32 %11, label %22 [
i32 -1, label %12
i32 0, label %14
]
12: ; preds = %10
%13 = call i32 @syserr(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %39
14: ; preds = %10
%15 = call i32 (...) @getpid()
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i32 0)
%18 = load i32, i32* %5
%19 = call i32 @substitute(i32 %18, i32 0)
%20 = call i32 (...) @readAndPrint()
%21 = call i32 @replace(i32 %19, i32 0)
br label %39
22: ; preds = %10
%23 = call i32 (...) @getpid()
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %23)
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0), i32 %11)
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%27 = load i32, i32* %26
%28 = call i32 @substitute(i32 %27, i32 1)
%29 = call i32 @puts(i8* %3)
%30 = call i32 @puts(i8* %3)
%31 = call i32 @putchar(i32 10)
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0))
%33 = call i32 @replace(i32 %28, i32 1)
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i64 0, i64 0))
%35 = call i32 @wait(i32 0)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %22
%38 = call i32 @syserr(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.10, i64 0, i64 0))
br label %39
39: ; preds = %12, %14, %22, %37
ret i32 0
}