#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*/ close(int);
int creat(char *, int);
int /*<<< orphan*/ execlp(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  pid_t pid;
  int fd;
  printf("About to run who into a file\n");
  if ((pid = fork()) == -1) {
    perror("fork() failed");
    exit(1);
  }
  if (pid == 0) {
    close(1);
    fd = creat("list", 0644);
    execlp("who", "who", NULL);
    perror("execlp() failed");
    exit(1);
  }
  wait(NULL);
  printf("Done\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe63(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11cb <main+0x4b>
lea    0xe62(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    121c <main+0x9c>
mov    $0x1,%edi
call   1040 <close@plt>
lea    0xe46(%rip),%rdi
mov    $0x1a4,%esi
call   1060 <creat@plt>
mov    %eax,-0x18(%rbp)
lea    0xe37(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
call   1080 <execlp@plt>
lea    0xe28(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
lea    0xe19(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xe9f(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     11b4 <main+0x34>
cmp    $0xffffffff,%eax
je     11e9 <main+0x69>
xor    %edi,%edi
call   1090 <wait@plt>
lea    0xe99(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rcx
ret
mov    $0x1,%edi
call   1040 <close@plt>
lea    0xe49(%rip),%rdi
mov    $0x1a4,%esi
call   1060 <creat@plt>
lea    0xe3d(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   1080 <execlp@plt>
lea    0xe30(%rip),%rdi
jmp    11f0 <main+0x70>
lea    0xe10(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [30 x i8] c"About to run who into a file\0A\00"
@.str.1 = [14 x i8] c"fork() failed\00"
@.str.2 = [5 x i8] c"list\00"
@.str.3 = [4 x i8] c"who\00"
@.str.4 = [16 x i8] c"execlp() failed\00"
@.str.5 = [6 x i8] c"Done\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%9 = call i32 (...) @fork()
store i32 %9, i32* %6
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = load i32, i32* %6
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %14
%18 = call i32 @close(i32 1)
%19 = call i32 @creat(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 420)
store i32 %19, i32* %7
%20 = call i32 @execlp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32* null)
%21 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %14
%24 = call i32 @wait(i32* null)
%25 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [14 x i8] c"fork() failed\00"
@.str.2 = [5 x i8] c"list\00"
@.str.3 = [4 x i8] c"who\00"
@.str.4 = [16 x i8] c"execlp() failed\00"
@str = [29 x i8] c"About to run who into a file\00"
@str.6 = [5 x i8] c"Done\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 (...) @fork()
switch i32 %4, label %14 [
i32 -1, label %5
i32 0, label %8
]
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = tail call i32 @close(i32 1)
%10 = tail call i32 @creat(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 420)
%11 = tail call i32 @execlp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32* null)
%12 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = tail call i32 @wait(i32* null)
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.6, i64 0, i64 0))
ret i32 0
}