#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 */

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ err(int, char *);
int execl(char *, char *, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main() {
  int pid;
  int pd[2];
  char *message = "3\n2\n1\n4\n";
  pipe(pd);
  pid = fork();
  if (pid < 0) {
    close(pd[0]);
    close(pd[1]);
    err(1, "error creating the child process");
  }
  if (pid > 0) {
    close(pd[0]);
    write(pd[1], message, strlen(message));
    close(pd[1]);
  } else {
    close(pd[1]);
    dup2(pd[0], 0);
    if (-1 == execl("/usr/bin/sort", "sort", (char *)NULL)) {
      err(1, "failed to execute sort");
    }
    exit(0);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe5a(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    -0x10(%rbp),%rdi
call   1070 <pipe@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11e8 <main+0x58>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    $0x1,%edi
lea    0xe26(%rip),%rsi
call   1080 <err@plt>
cmpl   $0x0,-0x8(%rbp)
jle    122c <main+0x9c>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
jmp    127c <main+0xec>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%edi
xor    %esi,%esi
call   1050 <dup2@plt>
lea    0xde5(%rip),%rdi
lea    0xdfe(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   10a0 <execl@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1275 <main+0xe5>
mov    $0x1,%edi
lea    0xdc8(%rip),%rsi
call   1080 <err@plt>
xor    %edi,%edi
call   1090 <exit@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsp,%rdi
call   1070 <pipe@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
js     11e3 <main+0x53>
je     1205 <main+0x75>
mov    (%rsp),%edi
call   1060 <close@plt>
mov    0x4(%rsp),%ebx
lea    0xe44(%rip),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    0x4(%rsp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    (%rsp),%edi
call   1060 <close@plt>
mov    0x4(%rsp),%edi
call   1060 <close@plt>
lea    0xe0e(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
mov    0x4(%rsp),%edi
call   1060 <close@plt>
mov    (%rsp),%edi
xor    %esi,%esi
call   1050 <dup2@plt>
lea    0xe0b(%rip),%rdi
lea    0xe24(%rip),%rsi
xor    %edx,%edx
call   10a0 <execl@plt>
cmp    $0xffffffff,%eax
jne    1243 <main+0xb3>
lea    0xdff(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"3\0A2\0A1\0A4\0A\00"
@.str.1 = [33 x i8] c"error creating the child process\00"
@.str.2 = [14 x i8] c"/usr/bin/sort\00"
@.str.3 = [5 x i8] c"sort\00"
@.str.4 = [23 x i8] c"failed to execute sort\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [2 x i32]
%4 = alloca i8*
store i32 0, i32* %1
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %4
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = call i32 (...) @fork()
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %18
10: ; preds = %0
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%12 = load i32, i32* %11
%13 = call i32 @close(i32 %12)
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%15 = load i32, i32* %14
%16 = call i32 @close(i32 %15)
%17 = call i32 @err(i32 1, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %10, %0
%19 = load i32, i32* %2
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %34
21: ; preds = %18
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%23 = load i32, i32* %22
%24 = call i32 @close(i32 %23)
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%26 = load i32, i32* %25
%27 = load i8*, i8** %4
%28 = load i8*, i8** %4
%29 = call i32 @strlen(i8* %28)
%30 = call i32 @write(i32 %26, i8* %27, i32 %29)
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%32 = load i32, i32* %31
%33 = call i32 @close(i32 %32)
br label %47
34: ; preds = %18
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%36 = load i32, i32* %35
%37 = call i32 @close(i32 %36)
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%39 = load i32, i32* %38
%40 = call i32 @dup2(i32 %39, i32 0)
%41 = call i32 @execl(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* null)
%42 = icmp eq i32 -1, %41
br i1 %42, label %43, label %45
43: ; preds = %34
%44 = call i32 @err(i32 1, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
br label %45
45: ; preds = %43, %34
%46 = call i32 @exit(i32 0)
unreachable
47: ; preds = %21
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"3\0A2\0A1\0A4\0A\00"
@.str.1 = [33 x i8] c"error creating the child process\00"
@.str.2 = [14 x i8] c"/usr/bin/sort\00"
@.str.3 = [5 x i8] c"sort\00"
@.str.4 = [23 x i8] c"failed to execute sort\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = bitcast [2 x i32]* %1 to i8*
%3 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%4 = call i32 @pipe(i32* %3)
%5 = call i32 (...) @fork()
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %14
7: ; preds = %0
%8 = load i32, i32* %3
%9 = call i32 @close(i32 %8)
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%11 = load i32, i32* %10
%12 = call i32 @close(i32 %11)
%13 = call i32 @err(i32 1, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %25
14: ; preds = %0
%15 = icmp eq i32 %5, 0
br i1 %15, label %25, label %16
16: ; preds = %14
%17 = load i32, i32* %3
%18 = call i32 @close(i32 %17)
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%20 = load i32, i32* %19
%21 = call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%22 = call i32 @write(i32 %20, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = load i32, i32* %19
%24 = call i32 @close(i32 %23)
ret i32 0
25: ; preds = %7, %14
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%27 = load i32, i32* %26
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* %3
%30 = call i32 @dup2(i32 %29, i32 0)
%31 = call i32 @execl(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* null)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %25
%34 = call i32 @err(i32 1, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
br label %35
35: ; preds = %33, %25
%36 = call i32 @exit(i32 0)
unreachable
}