#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;

/* Variables and functions */
int /*<<< orphan*/ close(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ write(int, char *, scalar_t__);
int main(void) {
  char write_msg[25];
  pid_t pid;
  int fd[2];
  if (pipe(fd) == -1) {
    fprintf(stderr, "Pipe failed");
    return 1;
  }
  pid = fork();
  if (pid < 0) {
    fprintf(stderr, "Fork failed");
    return 1;
  }
  if (pid > 0) {
    close(fd[0]);
    strcpy(write_msg, "parent process assigned");
    write(fd[1], write_msg, strlen(write_msg) + 1);
    close(fd[1]);
  } else {
    close(fd[1]);
    read(fd[0], write_msg, 25);
    printf("child read: %s\n", write_msg);
    close(fd[0]);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x30(%rbp),%rdi
call   1080 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11d2 <main+0x42>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
lea    0xe3f(%rip),%rsi
call   10a0 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    129b <main+0x10b>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1209 <main+0x79>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
lea    0xe14(%rip),%rsi
call   10a0 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    129b <main+0x10b>
cmpq   $0x0,-0x28(%rbp)
jle    1263 <main+0xd3>
mov    -0x30(%rbp),%edi
call   1070 <close@plt>
lea    -0x20(%rbp),%rdi
lea    0xdf1(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1040 <write@plt>
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
jmp    1294 <main+0x104>
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
mov    -0x30(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x19,%edx
call   1090 <read@plt>
lea    -0x20(%rbp),%rsi
lea    0xda9(%rip),%rdi
call   1060 <printf@plt>
mov    -0x30(%rbp),%edi
call   1070 <close@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    $0x38,%rsp
lea    0x8(%rsp),%rdi
call   1080 <pipe@plt>
cmp    $0xffffffff,%eax
je     11f0 <main+0x60>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     1202 <main+0x72>
je     121e <main+0x8e>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
lea    0xe55(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    0xc(%rsp),%ebp
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <write@plt>
mov    0xc(%rsp),%edi
jmp    1250 <main+0xc0>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
lea    0xe00(%rip),%rsi
jmp    1212 <main+0x82>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
lea    0xdfa(%rip),%rsi
call   10a0 <fprintf@plt>
mov    $0x1,%eax
jmp    1257 <main+0xc7>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x19,%edx
call   1090 <read@plt>
lea    0xdec(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [12 x i8] c"Pipe failed\00"
@.str.1 = [12 x i8] c"Fork failed\00"
@.str.2 = [24 x i8] c"parent process assigned\00"
@.str.3 = [16 x i8] c"child read: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [25 x i8]
%3 = alloca i64
%4 = alloca [2 x i32]
store i32 0, i32* %1
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %51
11: ; preds = %0
%12 = call i64 (...) @fork()
store i64 %12, i64* %3
%13 = load i64, i64* %3
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %51
18: ; preds = %11
%19 = load i64, i64* %3
%20 = icmp sgt i64 %19, 0
br i1 %20, label %21, label %37
21: ; preds = %18
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%23 = load i32, i32* %22
%24 = call i32 @close(i32 %23)
%25 = getelementptr inbounds [25 x i8], [25 x i8]* %2, i64 0, i64 0
%26 = call i32 @strcpy(i8* %25, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%28 = load i32, i32* %27
%29 = getelementptr inbounds [25 x i8], [25 x i8]* %2, i64 0, i64 0
%30 = getelementptr inbounds [25 x i8], [25 x i8]* %2, i64 0, i64 0
%31 = call i64 @strlen(i8* %30)
%32 = add nsw i64 %31, 1
%33 = call i32 @write(i32 %28, i8* %29, i64 %32)
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%35 = load i32, i32* %34
%36 = call i32 @close(i32 %35)
br label %50
37: ; preds = %18
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%39 = load i32, i32* %38
%40 = call i32 @close(i32 %39)
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%42 = load i32, i32* %41
%43 = getelementptr inbounds [25 x i8], [25 x i8]* %2, i64 0, i64 0
%44 = call i32 @read(i32 %42, i8* %43, i32 25)
%45 = getelementptr inbounds [25 x i8], [25 x i8]* %2, i64 0, i64 0
%46 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %45)
%47 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%48 = load i32, i32* %47
%49 = call i32 @close(i32 %48)
br label %50
50: ; preds = %37, %21
store i32 0, i32* %1
br label %51
51: ; preds = %50, %15, %8
%52 = load i32, i32* %1
ret i32 %52
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [12 x i8] c"Pipe failed\00"
@.str.1 = [12 x i8] c"Fork failed\00"
@.str.2 = [24 x i8] c"parent process assigned\00"
@.str.3 = [16 x i8] c"child read: %s\0A\00"
define i32 @main() {
%1 = alloca [25 x i8]
%2 = alloca [2 x i32]
%3 = getelementptr inbounds [25 x i8], [25 x i8]* %1, i64 0, i64 0
%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 %11
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %39
11: ; preds = %0
%12 = call i64 (...) @fork()
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %39
17: ; preds = %11
%18 = icmp eq i64 %12, 0
br i1 %18, label %30, label %19
19: ; preds = %17
%20 = load i32, i32* %5
%21 = call i32 @close(i32 %20)
%22 = call i32 @strcpy(i8* %3, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%24 = load i32, i32* %23
%25 = call i64 @strlen(i8* %3)
%26 = add nsw i64 %25, 1
%27 = call i32 @write(i32 %24, i8* %3, i64 %26)
%28 = load i32, i32* %23
%29 = call i32 @close(i32 %28)
br label %39
30: ; preds = %17
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%32 = load i32, i32* %31
%33 = call i32 @close(i32 %32)
%34 = load i32, i32* %5
%35 = call i32 @read(i32 %34, i8* %3, i32 25)
%36 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %3)
%37 = load i32, i32* %5
%38 = call i32 @close(i32 %37)
br label %39
39: ; preds = %19, %30, %14, %8
%40 = phi i32 [ 1, %8 ], [ 1, %14 ], [ 0, %30 ], [ 0, %19 ]
ret i32 %40
}