#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*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int O_APPEND;
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ execlp(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(void) {
  printf("About to run who into a file\n");
  int pid = fork();
  if (pid == -1) {
    perror("fork");
    exit(EXIT_FAILURE);
  }
  if (pid == 0) {
    close(1);
    open("userlist", O_CREAT | O_WRONLY | O_APPEND, 0644);
    execlp("who", "who", NULL);
  }
  if (pid != 0) {
    wait(NULL);
    printf("Done running who. results in userlist\n");
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe6a(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11c9 <main+0x49>
lea    0xe68(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jne    121c <main+0x9c>
mov    $0x1,%edi
call   1040 <close@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
mov    0x2dd3(%rip),%rax
or     (%rax),%esi
mov    0x2dc2(%rip),%rax
or     (%rax),%esi
lea    0xe24(%rip),%rdi
mov    $0x1a4,%edx
call   1050 <open@plt>
lea    0xe1c(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
call   1080 <execlp@plt>
cmpl   $0x0,-0x8(%rbp)
je     123b <main+0xbb>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
lea    0xdfa(%rip),%rdi
call   1030 <printf@plt>
mov    0x2d8e(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xe81(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     11b2 <main+0x32>
cmp    $0xffffffff,%eax
je     1204 <main+0x84>
xor    %edi,%edi
call   1090 <wait@plt>
lea    0xe7b(%rip),%rdi
call   1030 <puts@plt>
jmp    11f9 <main+0x79>
mov    $0x1,%edi
call   1040 <close@plt>
mov    0x2e35(%rip),%rax
mov    0x2df6(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2de3(%rip),%rax
or     (%rax),%esi
lea    0xe65(%rip),%rdi
mov    $0x1a4,%edx
call   1050 <open@plt>
lea    0xe16(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   1080 <execlp@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
pop    %rcx
ret
lea    0xdf5(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [30 x i8] c"About to run who into a file\0A\00"
@.str.1 = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [9 x i8] c"userlist\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@.str.3 = [4 x i8] c"who\00"
@.str.4 = [39 x i8] c"Done running who. results in userlist\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%4 = call i32 (...) @fork()
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = call i32 @exit(i32 %9)
unreachable
11: ; preds = %0
%12 = load i32, i32* %2
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %23
14: ; preds = %11
%15 = call i32 @close(i32 1)
%16 = load i32, i32* @O_CREAT
%17 = load i32, i32* @O_WRONLY
%18 = or i32 %16, %17
%19 = load i32, i32* @O_APPEND
%20 = or i32 %18, %19
%21 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %20, i32 420)
%22 = 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)
br label %23
23: ; preds = %14, %11
%24 = load i32, i32* %2
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = call i32 @wait(i32* null)
%28 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0))
br label %29
29: ; preds = %26, %23
%30 = load i32, i32* @EXIT_SUCCESS
ret i32 %30
}

LLVM IR (-O2):
@.str.1 = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [9 x i8] c"userlist\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@.str.3 = [4 x i8] c"who\00"
@EXIT_SUCCESS = global i32 0
@str = [29 x i8] c"About to run who into a file\00"
@str.5 = [38 x i8] c"Done running who. results in userlist\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%2 = tail call i32 (...) @fork()
switch i32 %2, label %16 [
i32 -1, label %3
i32 0, label %7
]
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%5 = load i32, i32* @EXIT_FAILURE
%6 = tail call i32 @exit(i32 %5)
unreachable
7: ; preds = %0
%8 = tail call i32 @close(i32 1)
%9 = load i32, i32* @O_CREAT
%10 = load i32, i32* @O_WRONLY
%11 = or i32 %10, %9
%12 = load i32, i32* @O_APPEND
%13 = or i32 %11, %12
%14 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %13, i32 420)
%15 = 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)
br label %19
16: ; preds = %0
%17 = tail call i32 @wait(i32* null)
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.5, i64 0, i64 0))
br label %19
19: ; preds = %7, %16
%20 = load i32, i32* @EXIT_SUCCESS
ret i32 %20
}