#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;
typedef int /*<<< orphan*/ buff;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ execl(char *, char *, char *, char *, char *,
                         int /*<<< orphan*/ *);
scalar_t__ fork();
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ scanf(char *, char *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main() {
  int fdes[2];
  if (pipe(fdes) == 0) {
    pid_t pid = fork();
    if (pid == 0) {
      char temp[20];
      close(fdes[0]);
      printf("Enter the command for which man page is required\n");
      scanf("%s", temp);
      write(fdes[1], temp, strlen(temp));
    } else {
      wait(0);
      char buff[20] = {'\0'};
      close(fdes[1]);
      int length = read(fdes[0], buff, sizeof(buff));
      execl("/usr/bin/xterm", "/usr/bin/xterm", "-e", "man", buff, NULL);
    }
  } else
    printf("Pipe error\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1080 <pipe@plt>
cmp    $0x0,%rax
jne    1294 <main+0xe4>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1236 <main+0x86>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
lea    0xe09(%rip),%rdi
call   1050 <printf@plt>
lea    -0x30(%rbp),%rsi
lea    0xe2b(%rip),%rdi
call   10a0 <scanf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x64(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x60(%rbp)
lea    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    128f <main+0xdf>
xor    %edi,%edi
call   10c0 <wait@plt>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1060 <memset@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    -0xc(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x14,%edx
call   1090 <read@plt>
mov    %eax,-0x54(%rbp)
lea    -0x50(%rbp),%r8
lea    0xdc1(%rip),%rsi
lea    0xdc9(%rip),%rdx
lea    0xdc5(%rip),%rcx
xor    %eax,%eax
mov    %eax,%r9d
mov    %rsi,%rdi
call   10b0 <execl@plt>
jmp    12a0 <main+0xf0>
lea    0xdb0(%rip),%rdi
call   1050 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
test   %rax,%rax
je     11c6 <main+0x26>
lea    0xe5d(%rip),%rdi
call   1030 <puts@plt>
jmp    126e <main+0xce>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
je     122d <main+0x8d>
xor    %edi,%edi
call   10b0 <wait@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0x0,0x20(%rsp)
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x14,%edx
call   1080 <read@plt>
lea    0xdf4(%rip),%rdi
lea    0xdfc(%rip),%rdx
lea    0xdf8(%rip),%rcx
mov    %rdi,%rsi
mov    %rbx,%r8
xor    %r9d,%r9d
call   10a0 <execl@plt>
jmp    126e <main+0xce>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
lea    0xde7(%rip),%rdi
call   1030 <puts@plt>
lea    0xdb7(%rip),%rdi
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
call   1090 <scanf@plt>
mov    0xc(%rsp),%ebp
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [50 x i8] c"Enter the command for which man page is required\0A\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [15 x i8] c"/usr/bin/xterm\00"
@.str.3 = [3 x i8] c"-e\00"
@.str.4 = [4 x i8] c"man\00"
@.str.5 = [12 x i8] c"Pipe error\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i64
%4 = alloca [20 x i8]
%5 = alloca [20 x i8]
%6 = alloca i32
store i32 0, i32* %1
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%8 = call i64 @pipe(i32* %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %40
10: ; preds = %0
%11 = call i64 (...) @fork()
store i64 %11, i64* %3
%12 = load i64, i64* %3
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %27
14: ; preds = %10
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%16 = load i32, i32* %15
%17 = call i32 @close(i32 %16)
%18 = call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
%19 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%20 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%22 = load i32, i32* %21
%23 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%24 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%25 = call i32 @strlen(i8* %24)
%26 = call i32 @write(i32 %22, i8* %23, i32 %25)
br label %39
27: ; preds = %10
%28 = call i32 @wait(i32 0)
%29 = bitcast [20 x i8]* %5 to i8*
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%31 = load i32, i32* %30
%32 = call i32 @close(i32 %31)
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%34 = load i32, i32* %33
%35 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%36 = call i32 @read(i32 %34, i8* %35, i32 20)
store i32 %36, i32* %6
%37 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%38 = call i32 @execl(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %37, i32* null)
br label %39
39: ; preds = %27, %14
br label %42
40: ; preds = %0
%41 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
br label %42
42: ; preds = %40, %39
%43 = load i32, i32* %1
ret i32 %43
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [15 x i8] c"/usr/bin/xterm\00"
@.str.3 = [3 x i8] c"-e\00"
@.str.4 = [4 x i8] c"man\00"
@str = [11 x i8] c"Pipe error\00"
@str.6 = [49 x i8] c"Enter the command for which man page is required\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [20 x i8]
%3 = alloca [20 x i8]
%4 = bitcast [2 x i32]* %1 to i8*
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%6 = call i64 @pipe(i32* %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %30
8: ; preds = %0
%9 = call i64 (...) @fork()
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %21
11: ; preds = %8
%12 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%13 = load i32, i32* %5
%14 = call i32 @close(i32 %13)
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str.6, i64 0, i64 0))
%16 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %12)
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%18 = load i32, i32* %17
%19 = call i32 @strlen(i8* %12)
%20 = call i32 @write(i32 %18, i8* %12, i32 %19)
br label %32
21: ; preds = %8
%22 = call i32 @wait(i32 0)
%23 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%25 = load i32, i32* %24
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* %5
%28 = call i32 @read(i32 %27, i8* %23, i32 20)
%29 = call i32 @execl(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %23, i32* null)
br label %32
30: ; preds = %0
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
br label %32
32: ; preds = %11, %21, %30
ret i32 0
}