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

/* Variables and functions */
int /*<<< orphan*/ F_OK;
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int S_IRUSR;
int S_IWUSR;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int const);
int /*<<< orphan*/ dup2(int const, int);
int /*<<< orphan*/ execlp(char *, char *, char *, char *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fork();
int open(char *, int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
int /*<<< orphan*/ waitpid(scalar_t__, int *, int /*<<< orphan*/);
FILE *createTempManPage(char *command) {
  const char const *tempLocation = "/tmp/exp";
  char tmpFile[strlen(tempLocation) + strlen(command) + 1];
  strcpy(tmpFile, tempLocation);
  strcat(tmpFile, command);
  pid_t pID = fork();
  if (pID == 0) {
    if (access(tmpFile, F_OK) != 0) {
      const int file =
          open(tmpFile, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
      if (file < 0) {
        printf("Error creating temp file. Aborting.\n");
        return NULL;
      }
      dup2(file, 1);
      close(file);
      execlp("man", "man", command, (char *)NULL);
    }
    return NULL;
  } else {
    int status;
    waitpid(pID, &status, 0);
    FILE *fp;
    fp = fopen(tmpFile, "r");
    if (fp == NULL) {
      printf("failure opening tmp file for reading.\n");
    }
    return fp;
  }
}


x86 assembly (clang, -O0):
<createTempManPage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe2d(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x58(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x58(%rbp),%rcx
mov    %rax,%rdx
lea    0x1(%rcx,%rdx,1),%rax
mov    %rsp,%rsi
mov    %rsi,-0x20(%rbp)
lea    0x10(%rcx,%rdx,1),%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x50(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10c0 <strcat@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1305 <createTempManPage+0x145>
mov    -0x50(%rbp),%rdi
mov    0x2d5f(%rip),%rax
mov    (%rax),%esi
call   10a0 <access@plt>
cmp    $0x0,%rax
je     12f1 <createTempManPage+0x131>
mov    -0x50(%rbp),%rdi
mov    0x2d8b(%rip),%rax
mov    (%rax),%esi
mov    0x2d62(%rip),%rax
or     (%rax),%esi
mov    0x2d71(%rip),%rax
or     (%rax),%esi
mov    0x2d38(%rip),%rax
mov    (%rax),%edx
mov    0x2d3f(%rip),%rax
or     (%rax),%edx
call   1090 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    12c5 <createTempManPage+0x105>
lea    0xd5d(%rip),%rdi
call   1060 <printf@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    134e <createTempManPage+0x18e>
mov    -0x34(%rbp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
mov    -0x34(%rbp),%edi
call   1070 <close@plt>
mov    -0x10(%rbp),%rdx
lea    0xd49(%rip),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
call   10d0 <execlp@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    134e <createTempManPage+0x18e>
mov    -0x30(%rbp),%rdi
lea    -0x3c(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    -0x50(%rbp),%rdi
lea    0xd13(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    133f <createTempManPage+0x17f>
lea    0xcfa(%rip),%rdi
call   1060 <printf@plt>
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createTempManPage>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
call   1050 <strlen@plt>
mov    %rsp,%rbx
add    $0x18,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe15(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   10c0 <strcat@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %rax,%rax
je     1244 <createTempManPage+0x84>
lea    -0x14(%rbp),%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   1080 <waitpid@plt>
lea    0xdee(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    12ce <createTempManPage+0x10e>
lea    0xdd5(%rip),%rdi
call   1040 <puts@plt>
jmp    12ce <createTempManPage+0x10e>
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10a0 <access@plt>
test   %rax,%rax
je     12cc <createTempManPage+0x10c>
mov    0x2d97(%rip),%rax
mov    0x2d70(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d7d(%rip),%rax
or     (%rax),%esi
mov    0x2d44(%rip),%rax
mov    0x2d4d(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     12c0 <createTempManPage+0x100>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x1,%esi
call   1060 <dup2@plt>
mov    %ebx,%edi
call   1070 <close@plt>
lea    0xd5a(%rip),%rdi
xor    %ebx,%ebx
mov    %rdi,%rsi
mov    %r14,%rdx
xor    %ecx,%ecx
call   10d0 <execlp@plt>
jmp    12ce <createTempManPage+0x10e>
lea    0xd6e(%rip),%rdi
call   1040 <puts@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/tmp/exp\00"
@F_OK = global i32 0
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [37 x i8] c"Error creating temp file. Aborting.\0A\00"
@.str.2 = [4 x i8] c"man\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [39 x i8] c"failure opening tmp file for reading.\0A\00"
define i32* @createTempManPage(i8* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
store i8* %0, i8** %3
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %4
%12 = load i8*, i8** %4
%13 = call i64 @strlen(i8* %12)
%14 = load i8*, i8** %3
%15 = call i64 @strlen(i8* %14)
%16 = add nsw i64 %13, %15
%17 = add nsw i64 %16, 1
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %5
%19 = alloca i8, i64 %17
store i64 %17, i64* %6
%20 = load i8*, i8** %4
%21 = call i32 @strcpy(i8* %19, i8* %20)
%22 = load i8*, i8** %3
%23 = call i32 @strcat(i8* %19, i8* %22)
%24 = call i64 (...) @fork()
store i64 %24, i64* %7
%25 = load i64, i64* %7
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %53
27: ; preds = %1
%28 = load i32, i32* @F_OK
%29 = call i64 @access(i8* %19, i32 %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %52
31: ; preds = %27
%32 = load i32, i32* @O_CREAT
%33 = load i32, i32* @O_RDWR
%34 = or i32 %32, %33
%35 = load i32, i32* @O_TRUNC
%36 = or i32 %34, %35
%37 = load i32, i32* @S_IRUSR
%38 = load i32, i32* @S_IWUSR
%39 = or i32 %37, %38
%40 = call i32 @open(i8* %19, i32 %36, i32 %39)
store i32 %40, i32* %8
%41 = load i32, i32* %8
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %31
%44 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
store i32* null, i32** %2
store i32 1, i32* %9
br label %63
45: ; preds = %31
%46 = load i32, i32* %8
%47 = call i32 @dup2(i32 %46, i32 1)
%48 = load i32, i32* %8
%49 = call i32 @close(i32 %48)
%50 = load i8*, i8** %3
%51 = call i32 @execlp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %50, i8* null)
br label %52
52: ; preds = %45, %27
store i32* null, i32** %2
store i32 1, i32* %9
br label %63
53: ; preds = %1
%54 = load i64, i64* %7
%55 = call i32 @waitpid(i64 %54, i32* %10, i32 0)
%56 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %56, i32** %11
%57 = load i32*, i32** %11
%58 = icmp eq i32* %57, null
br i1 %58, label %59, label %61
59: ; preds = %53
%60 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0))
br label %61
61: ; preds = %59, %53
%62 = load i32*, i32** %11
store i32* %62, i32** %2
store i32 1, i32* %9
br label %63
63: ; preds = %61, %52, %43
%64 = load i8*, i8** %5
%65 = load i32*, i32** %2
ret i32* %65
}

LLVM IR (-O2):
@.str = [9 x i8] c"/tmp/exp\00"
@F_OK = global i32 0
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.2 = [4 x i8] c"man\00"
@.str.3 = [2 x i8] c"r\00"
@str = [38 x i8] c"failure opening tmp file for reading.\00"
@str.5 = [36 x i8] c"Error creating temp file. Aborting.\00"
define noalias i32* @createTempManPage(i8* %0) {
%2 = alloca i32
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = add nsw i64 %3, 9
%5 = alloca i8, i64 %4
%6 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @strcat(i8* %5, i8* %0)
%8 = call i64 (...) @fork()
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %31
10: ; preds = %1
%11 = load i32, i32* @F_OK
%12 = call i64 @access(i8* %5, i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %39, label %14
14: ; preds = %10
%15 = load i32, i32* @O_CREAT
%16 = load i32, i32* @O_RDWR
%17 = or i32 %16, %15
%18 = load i32, i32* @O_TRUNC
%19 = or i32 %17, %18
%20 = load i32, i32* @S_IRUSR
%21 = load i32, i32* @S_IWUSR
%22 = or i32 %21, %20
%23 = call i32 @open(i8* %5, i32 %19, i32 %22)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %14
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.5, i64 0, i64 0))
br label %39
27: ; preds = %14
%28 = call i32 @dup2(i32 %23, i32 1)
%29 = call i32 @close(i32 %23)
%30 = call i32 @execlp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* null)
br label %39
31: ; preds = %1
%32 = bitcast i32* %2 to i8*
%33 = call i32 @waitpid(i64 %8, i32* %2, i32 0)
%34 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%35 = icmp eq i32* %34, null
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
br label %38
38: ; preds = %36, %31
br label %39
39: ; preds = %25, %27, %10, %38
%40 = phi i32* [ %34, %38 ], [ null, %10 ], [ null, %27 ], [ null, %25 ]
ret i32* %40
}