#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);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execlp(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *);
int strlen(char const *const);
int /*<<< orphan*/ wait(int *);
int /*<<< orphan*/ write(int, char const *const, int);
int main(int argc, char *argv[]) {
  int pipefd[2];
  pid_t cpid;
  pipe(pipefd);
  cpid = fork();
  if (cpid < 0) {
    perror("Fork");
  } else if (cpid == 0) {
    printf("This is child process\n");
    close(pipefd[1]);
    close(0);
    dup2(pipefd[0], 0);
    execlp("sort", "sort", NULL);
    exit(0);
  } else {
    printf("This is parent process\n");
    close(pipefd[0]);
    static const char *const sort_array[] = {"hello", "amigos", "gracias",
                                             "hola"};
    int i;
    for (i = 0; i < 4; i++) {
      write(pipefd[1], sort_array[i], strlen(sort_array[i]));
      write(pipefd[1], "\n", 1);
    }
    close(pipefd[1]);
    int cstatus;
    wait(&cstatus);
  }
}


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)
lea    -0x18(%rbp),%rdi
call   1080 <pipe@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11f6 <main+0x46>
lea    0xe14(%rip),%rdi
call   1090 <perror@plt>
jmp    12d8 <main+0x128>
cmpq   $0x0,-0x20(%rbp)
jne    1240 <main+0x90>
lea    0xdfd(%rip),%rdi
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%edi
xor    %esi,%esi
call   1050 <dup2@plt>
lea    0xdef(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
call   10b0 <execlp@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
lea    0xdda(%rip),%rdi
call   1060 <printf@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x24(%rbp)
cmpl   $0x4,-0x24(%rbp)
jge    12c2 <main+0x112>
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
movslq -0x24(%rbp),%rcx
lea    0x2b8a(%rip),%rax
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x30(%rbp)
movslq -0x24(%rbp),%rcx
lea    0x2b77(%rip),%rax
mov    (%rax,%rcx,8),%rdi
call   1040 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
lea    0xd70(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    125b <main+0xab>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
lea    -0x28(%rbp),%rdi
call   10c0 <wait@plt>
jmp    12d8 <main+0x128>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0x4(%rsp),%rdi
call   1080 <pipe@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     12d2 <main+0x122>
je     12e0 <main+0x130>
lea    0xe47(%rip),%rdi
call   1030 <puts@plt>
mov    0x4(%rsp),%edi
call   1070 <close@plt>
mov    0x8(%rsp),%ebx
lea    0xe12(%rip),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    0x8(%rsp),%edi
lea    0xe0d(%rip),%r14
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    0x8(%rsp),%ebp
lea    0xde1(%rip),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    0x8(%rsp),%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    0x8(%rsp),%ebx
lea    0xdb8(%rip),%rbp
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    0x8(%rsp),%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    0x8(%rsp),%ebx
lea    0xd90(%rip),%rbp
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    0x8(%rsp),%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
lea    0xc(%rsp),%rdi
call   10c0 <wait@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd27(%rip),%rdi
call   1090 <perror@plt>
jmp    12c7 <main+0x117>
lea    0xd56(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   1070 <close@plt>
mov    0x4(%rsp),%edi
xor    %esi,%esi
call   1060 <dup2@plt>
lea    0xcf7(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   10b0 <execlp@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"Fork\00"
@.str.1 = [23 x i8] c"This is child process\0A\00"
@.str.2 = [5 x i8] c"sort\00"
@.str.3 = [24 x i8] c"This is parent process\0A\00"
@main.sort_array = internal constant [4 x i8*] [i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i32 0, i32 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i32 0, i32 0)]
@.str.4 = [6 x i8] c"hello\00"
@.str.5 = [7 x i8] c"amigos\00"
@.str.6 = [8 x i8] c"gracias\00"
@.str.7 = [5 x i8] c"hola\00"
@.str.8 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [2 x i32]
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%11 = call i32 @pipe(i32* %10)
%12 = call i64 (...) @fork()
store i64 %12, i64* %7
%13 = load i64, i64* %7
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %64
17: ; preds = %2
%18 = load i64, i64* %7
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %31
20: ; preds = %17
%21 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%23 = load i32, i32* %22
%24 = call i32 @close(i32 %23)
%25 = call i32 @close(i32 0)
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%27 = load i32, i32* %26
%28 = call i32 @dup2(i32 %27, i32 0)
%29 = call i32 @execlp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32* null)
%30 = call i32 @exit(i32 0)
unreachable
31: ; preds = %17
%32 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%34 = load i32, i32* %33
%35 = call i32 @close(i32 %34)
store i32 0, i32* %8
br label %36
36: ; preds = %55, %31
%37 = load i32, i32* %8
%38 = icmp slt i32 %37, 4
br i1 %38, label %39, label %58
39: ; preds = %36
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%41 = load i32, i32* %40
%42 = load i32, i32* %8
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [4 x i8*], [4 x i8*]* @main.sort_array, i64 0, i64 %43
%45 = load i8*, i8** %44
%46 = load i32, i32* %8
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds [4 x i8*], [4 x i8*]* @main.sort_array, i64 0, i64 %47
%49 = load i8*, i8** %48
%50 = call i32 @strlen(i8* %49)
%51 = call i32 @write(i32 %41, i8* %45, i32 %50)
%52 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%53 = load i32, i32* %52
%54 = call i32 @write(i32 %53, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0), i32 1)
br label %55
55: ; preds = %39
%56 = load i32, i32* %8
%57 = add nsw i32 %56, 1
store i32 %57, i32* %8
br label %36
58: ; preds = %36
%59 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%60 = load i32, i32* %59
%61 = call i32 @close(i32 %60)
%62 = call i32 @wait(i32* %9)
br label %63
63: ; preds = %58
br label %64
64: ; preds = %63, %15
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
@.str = [5 x i8] c"Fork\00"
@.str.2 = [5 x i8] c"sort\00"
@.str.4 = [6 x i8] c"hello\00"
@.str.5 = [7 x i8] c"amigos\00"
@.str.6 = [8 x i8] c"gracias\00"
@.str.7 = [5 x i8] c"hola\00"
@.str.8 = [2 x i8] c"\0A\00"
@str = [23 x i8] c"This is parent process\00"
@str.9 = [22 x i8] c"This is child process\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = bitcast [2 x i32]* %3 to i8*
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%7 = call i32 @pipe(i32* %6)
%8 = call i64 (...) @fork()
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %53
12: ; preds = %2
%13 = icmp eq i64 %8, 0
br i1 %13, label %14, label %24
14: ; preds = %12
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.9, i64 0, i64 0))
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%17 = load i32, i32* %16
%18 = call i32 @close(i32 %17)
%19 = call i32 @close(i32 0)
%20 = load i32, i32* %6
%21 = call i32 @dup2(i32 %20, i32 0)
%22 = call i32 @execlp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32* null)
%23 = call i32 @exit(i32 0)
unreachable
24: ; preds = %12
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%26 = load i32, i32* %6
%27 = call i32 @close(i32 %26)
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%29 = load i32, i32* %28
%30 = call i32 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%31 = call i32 @write(i32 %29, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %30)
%32 = load i32, i32* %28
%33 = call i32 @write(i32 %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0), i32 1)
%34 = load i32, i32* %28
%35 = call i32 @strlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%36 = call i32 @write(i32 %34, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i32 %35)
%37 = load i32, i32* %28
%38 = call i32 @write(i32 %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0), i32 1)
%39 = load i32, i32* %28
%40 = call i32 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
%41 = call i32 @write(i32 %39, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i32 %40)
%42 = load i32, i32* %28
%43 = call i32 @write(i32 %42, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0), i32 1)
%44 = load i32, i32* %28
%45 = call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%46 = call i32 @write(i32 %44, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), i32 %45)
%47 = load i32, i32* %28
%48 = call i32 @write(i32 %47, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0), i32 1)
%49 = load i32, i32* %28
%50 = call i32 @close(i32 %49)
%51 = bitcast i32* %4 to i8*
%52 = call i32 @wait(i32* %4)
br label %53
53: ; preds = %24, %10
ret i32 0
}