#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int STDOUT_FILENO;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int read(int, char *, int);
char *reverseinplace(char **, int /*<<< orphan*/);
int strlen(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int pfd[2];
  pid_t cpid;
  char *rev;
  int sz;
  int rsz;
  assert(argc == 2);
  sz = strlen(argv[1]);
  char buff[sz + 1];
  memset(buff, 0, sz + 1);
  if (pipe(pfd) == -1) {
    perror("pipe");
    exit(EXIT_FAILURE);
  }
  cpid = fork();
  if (cpid == -1) {
    perror("fork");
    exit(EXIT_FAILURE);
  }
  if (cpid == 0) {
    close(pfd[1]);
    rsz = read(pfd[0], buff, sz);
    rev = reverseinplace(&buff, 0);
    write(STDOUT_FILENO, rev, sz);
    write(STDOUT_FILENO, "\n", 1);
    close(pfd[0]);
    _exit(EXIT_SUCCESS);
  } else {
    close(pfd[0]);
    write(pfd[1], argv[1], strlen(argv[1]));
    close(pfd[1]);
    wait(NULL);
    exit(EXIT_SUCCESS);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%eax
sub    $0x2,%eax
sete   %al
movzbl %al,%edi
call   10a0 <assert@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x48(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x40(%rbp)
mov    -0x2c(%rbp),%edx
add    $0x1,%edx
xor    %esi,%esi
call   1060 <memset@plt>
lea    -0x18(%rbp),%rdi
call   1080 <pipe@plt>
cmp    $0xffffffff,%eax
jne    1269 <main+0x99>
lea    0xdaa(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1297 <main+0xc7>
lea    0xd81(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    130f <main+0x13f>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x48(%rbp),%rsi
mov    -0x18(%rbp),%edi
mov    -0x2c(%rbp),%edx
call   1090 <read@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,-0x30(%rbp)
xor    %esi,%esi
call   10c0 <reverseinplace@plt>
mov    %rax,-0x28(%rbp)
mov    0x2d1f(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1040 <write@plt>
mov    0x2d0a(%rip),%rax
mov    (%rax),%edi
lea    0xd1b(%rip),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
mov    0x2cc8(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <strlen@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10e0 <wait@plt>
mov    0x2c74(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
xor    %eax,%eax
cmp    $0x2,%edi
sete   %al
mov    %eax,%edi
call   10a0 <assert@plt>
mov    0x8(%r14),%rdi
call   1050 <strlen@plt>
mov    %eax,%r15d
lea    0x1(%r15),%edx
mov    %rsp,%rbx
lea    0xf(%rdx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
lea    -0x20(%rbp),%rdi
call   1080 <pipe@plt>
cmp    $0xffffffff,%eax
jne    122e <main+0x5e>
lea    0xdd4(%rip),%rdi
jmp    1245 <main+0x75>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %eax,%eax
je     1258 <main+0x88>
cmp    $0xffffffff,%eax
jne    12b5 <main+0xe5>
lea    0xdc0(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1090 <read@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   10c0 <reverseinplace@plt>
mov    0x2d71(%rip),%rbx
mov    (%rbx),%edi
mov    %rax,%rsi
mov    %r15d,%edx
call   1040 <write@plt>
mov    (%rbx),%edi
lea    0xd75(%rip),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
mov    0x2d22(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
mov    -0x1c(%rbp),%r15d
mov    0x8(%r14),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10e0 <wait@plt>
mov    0x2ce0(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"pipe\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [5 x i8] c"fork\00"
@STDOUT_FILENO = global i32 0
@.str.2 = [2 x i8] c"\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [2 x i32]
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, 2
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @strlen(i8* %19)
store i32 %20, i32* %9
%21 = load i32, i32* %9
%22 = add nsw i32 %21, 1
%23 = zext i32 %22 to i64
%24 = call i8* @llvm.stacksave()
store i8* %24, i8** %11
%25 = alloca i8, i64 %23
store i64 %23, i64* %12
%26 = load i32, i32* %9
%27 = add nsw i32 %26, 1
%28 = call i32 @memset(i8* %25, i32 0, i32 %27)
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%30 = call i32 @pipe(i32* %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %36
32: ; preds = %2
%33 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %2
%37 = call i32 (...) @fork()
store i32 %37, i32* %7
%38 = load i32, i32* %7
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %44
40: ; preds = %36
%41 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %36
%45 = load i32, i32* %7
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %68
47: ; preds = %44
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%49 = load i32, i32* %48
%50 = call i32 @close(i32 %49)
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%52 = load i32, i32* %51
%53 = load i32, i32* %9
%54 = call i32 @read(i32 %52, i8* %25, i32 %53)
store i32 %54, i32* %10
%55 = bitcast i8* %25 to i8**
%56 = call i8* @reverseinplace(i8** %55, i32 0)
store i8* %56, i8** %8
%57 = load i32, i32* @STDOUT_FILENO
%58 = load i8*, i8** %8
%59 = load i32, i32* %9
%60 = call i32 @write(i32 %57, i8* %58, i32 %59)
%61 = load i32, i32* @STDOUT_FILENO
%62 = call i32 @write(i32 %61, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%63 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%64 = load i32, i32* %63
%65 = call i32 @close(i32 %64)
%66 = load i32, i32* @EXIT_SUCCESS
%67 = call i32 @_exit(i32 %66)
unreachable
68: ; preds = %44
%69 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%70 = load i32, i32* %69
%71 = call i32 @close(i32 %70)
%72 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%73 = load i32, i32* %72
%74 = load i8**, i8*** %5
%75 = getelementptr inbounds i8*, i8** %74, i64 1
%76 = load i8*, i8** %75
%77 = load i8**, i8*** %5
%78 = getelementptr inbounds i8*, i8** %77, i64 1
%79 = load i8*, i8** %78
%80 = call i32 @strlen(i8* %79)
%81 = call i32 @write(i32 %73, i8* %76, i32 %80)
%82 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%83 = load i32, i32* %82
%84 = call i32 @close(i32 %83)
%85 = call i32 @wait(i32* null)
%86 = load i32, i32* @EXIT_SUCCESS
%87 = call i32 @exit(i32 %86)
unreachable
}

LLVM IR (-O2):
@.str = [5 x i8] c"pipe\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [5 x i8] c"fork\00"
@STDOUT_FILENO = global i32 0
@.str.2 = [2 x i8] c"\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2 x i32]
%4 = bitcast [2 x i32]* %3 to i8*
%5 = icmp eq i32 %0, 2
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 @strlen(i8* %9)
%11 = add nsw i32 %10, 1
%12 = zext i32 %11 to i64
%13 = alloca i8, i64 %12
%14 = call i32 @memset(i8* %13, i32 0, i32 %11)
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%16 = call i32 @pipe(i32* %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %22
18: ; preds = %2
%19 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %2
%23 = call i32 (...) @fork()
switch i32 %23, label %44 [
i32 -1, label %24
i32 0, label %28
]
24: ; preds = %22
%25 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %22
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%30 = load i32, i32* %29
%31 = call i32 @close(i32 %30)
%32 = load i32, i32* %15
%33 = call i32 @read(i32 %32, i8* %13, i32 %10)
%34 = bitcast i8* %13 to i8**
%35 = call i8* @reverseinplace(i8** %34, i32 0)
%36 = load i32, i32* @STDOUT_FILENO
%37 = call i32 @write(i32 %36, i8* %35, i32 %10)
%38 = load i32, i32* @STDOUT_FILENO
%39 = call i32 @write(i32 %38, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%40 = load i32, i32* %15
%41 = call i32 @close(i32 %40)
%42 = load i32, i32* @EXIT_SUCCESS
%43 = call i32 @_exit(i32 %42)
unreachable
44: ; preds = %22
%45 = load i32, i32* %15
%46 = call i32 @close(i32 %45)
%47 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%48 = load i32, i32* %47
%49 = load i8*, i8** %8
%50 = call i32 @strlen(i8* %49)
%51 = call i32 @write(i32 %48, i8* %49, i32 %50)
%52 = load i32, i32* %47
%53 = call i32 @close(i32 %52)
%54 = call i32 @wait(i32* null)
%55 = load i32, i32* @EXIT_SUCCESS
%56 = call i32 @exit(i32 %55)
unreachable
}