#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 /*<<< orphan*/ close(int);
int /*<<< orphan*/ creat(char *, int);
int /*<<< orphan*/ execlp(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
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);
    creat("userlist", 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   1050 <perror@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jne    1201 <main+0x81>
mov    $0x1,%edi
call   1040 <close@plt>
lea    0xe3f(%rip),%rdi
mov    $0x1a4,%esi
call   1060 <creat@plt>
lea    0xe37(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
call   1080 <execlp@plt>
cmpl   $0x0,-0x8(%rbp)
je     1220 <main+0xa0>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
lea    0xe15(%rip),%rdi
call   1030 <printf@plt>
mov    0x2db1(%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     11e9 <main+0x69>
xor    %edi,%edi
call   1090 <wait@plt>
lea    0xe7b(%rip),%rdi
call   1030 <puts@plt>
jmp    11de <main+0x5e>
mov    $0x1,%edi
call   1040 <close@plt>
lea    0xe80(%rip),%rdi
mov    $0x1a4,%esi
call   1060 <creat@plt>
lea    0xe31(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   1080 <execlp@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
pop    %rcx
ret
lea    0xe10(%rip),%rdi
call   1050 <perror@plt>
mov    0x2de4(%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"
@.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 %18
14: ; preds = %11
%15 = call i32 @close(i32 1)
%16 = call i32 @creat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 420)
%17 = 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 %18
18: ; preds = %14, %11
%19 = load i32, i32* %2
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = call i32 @wait(i32* null)
%23 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0))
br label %24
24: ; preds = %21, %18
%25 = load i32, i32* @EXIT_SUCCESS
ret i32 %25
}

LLVM IR (-O2):
@.str.1 = [5 x i8] c"fork\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [9 x i8] c"userlist\00"
@.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 %11 [
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 = tail call i32 @creat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 420)
%10 = 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 %14
11: ; preds = %0
%12 = tail call i32 @wait(i32* null)
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.5, i64 0, i64 0))
br label %14
14: ; preds = %7, %11
%15 = load i32, i32* @EXIT_SUCCESS
ret i32 %15
}