#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 WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
int /*<<< orphan*/ dup2(int, int);
int execlp(char *, char *, int /*<<< orphan*/ *);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stderr;
int waitpid(int, int *, int /*<<< orphan*/);
int spawn_who(int outfd) {
  pid_t pid;
  int status;
  pid = fork();
  switch (pid) {
  case -1:
    return -1;
  case 0:
    dup2(outfd, 1);
    if (execlp("who", "who", NULL) == -1)
      return -1;
  default:
    if (waitpid(pid, &status, 0) == pid) {
      int rc = WIFEXITED(status) ? WEXITSTATUS(status) : 0;
      if (rc)
        fprintf(stderr, "who failed with rc=%d\n", rc);
      return rc;
    } else {
      fprintf(stderr, "ut_wrapper: waitpid failed to wait for %d\n", pid);
      return -1;
    }
  }
}


x86 assembly (clang, -O0):
<spawn_who>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
sub    $0xffffffff,%eax
je     11a9 <spawn_who+0x39>
jmp    1199 <spawn_who+0x29>
mov    -0x18(%rbp),%eax
test   %eax,%eax
je     11b5 <spawn_who+0x45>
jmp    11ef <spawn_who+0x7f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1283 <spawn_who+0x113>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
lea    0xe37(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
call   1080 <execlp@plt>
cmp    $0xffffffff,%eax
jne    11ea <spawn_who+0x7a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1283 <spawn_who+0x113>
jmp    11ef <spawn_who+0x7f>
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    -0xc(%rbp),%eax
jne    1264 <spawn_who+0xf4>
mov    -0x10(%rbp),%edi
call   1070 <WIFEXITED@plt>
cmp    $0x0,%eax
je     1227 <spawn_who+0xb7>
mov    -0x10(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,-0x1c(%rbp)
jmp    1231 <spawn_who+0xc1>
xor    %eax,%eax
mov    %eax,-0x1c(%rbp)
jmp    1231 <spawn_who+0xc1>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1259 <spawn_who+0xe9>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xdb0(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1283 <spawn_who+0x113>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
lea    0xda4(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawn_who>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11f0 <spawn_who+0x80>
test   %ebx,%ebx
jne    11a9 <spawn_who+0x39>
mov    %ebp,%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
lea    0xe66(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   1080 <execlp@plt>
cmp    $0xffffffff,%eax
je     120b <spawn_who+0x9b>
lea    0x4(%rsp),%rsi
xor    %ebp,%ebp
mov    %ebx,%edi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    %ebx,%eax
jne    11f4 <spawn_who+0x84>
mov    0x4(%rsp),%edi
call   1070 <WIFEXITED@plt>
test   %eax,%eax
je     1210 <spawn_who+0xa0>
mov    0x4(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
test   %eax,%eax
je     1210 <spawn_who+0xa0>
mov    %eax,%ebx
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
lea    0xe1b(%rip),%rsi
mov    %ebx,%edx
call   1040 <fprintf@plt>
mov    %ebx,%ebp
jmp    1210 <spawn_who+0xa0>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
lea    0xe17(%rip),%rsi
mov    %ebx,%edx
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"who\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"who failed with rc=%d\0A\00"
@.str.2 = [43 x i8] c"ut_wrapper: waitpid failed to wait for %d\0A\00"
define i32 @spawn_who(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
%7 = call i32 (...) @fork()
store i32 %7, i32* %4
%8 = load i32, i32* %4
switch i32 %8, label %17 [
i32 -1, label %9
i32 0, label %10
]
9: ; preds = %1
store i32 -1, i32* %2
br label %44
10: ; preds = %1
%11 = load i32, i32* %3
%12 = call i32 @dup2(i32 %11, i32 1)
%13 = call i32 @execlp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* null)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %10
store i32 -1, i32* %2
br label %44
16: ; preds = %10
br label %17
17: ; preds = %1, %16
%18 = load i32, i32* %4
%19 = call i32 @waitpid(i32 %18, i32* %5, i32 0)
%20 = load i32, i32* %4
%21 = icmp eq i32 %19, %20
br i1 %21, label %22, label %40
22: ; preds = %17
%23 = load i32, i32* %5
%24 = call i32 @WIFEXITED(i32 %23)
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = load i32, i32* %5
%28 = call i32 @WEXITSTATUS(i32 %27)
br label %30
29: ; preds = %22
br label %30
30: ; preds = %29, %26
%31 = phi i32 [ %28, %26 ], [ 0, %29 ]
store i32 %31, i32* %6
%32 = load i32, i32* %6
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = load i32, i32* %6
%37 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %36)
br label %38
38: ; preds = %34, %30
%39 = load i32, i32* %6
store i32 %39, i32* %2
br label %44
40: ; preds = %17
%41 = load i32, i32* @stderr
%42 = load i32, i32* %4
%43 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %42)
store i32 -1, i32* %2
br label %44
44: ; preds = %40, %38, %15, %9
%45 = load i32, i32* %2
ret i32 %45
}

LLVM IR (-O2):
@.str = [4 x i8] c"who\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"who failed with rc=%d\0A\00"
@.str.2 = [43 x i8] c"ut_wrapper: waitpid failed to wait for %d\0A\00"
define i32 @spawn_who(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i32 (...) @fork()
switch i32 %4, label %9 [
i32 -1, label %26
i32 0, label %5
]
5: ; preds = %1
%6 = tail call i32 @dup2(i32 %0, i32 1)
%7 = tail call i32 @execlp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* null)
%8 = icmp eq i32 %7, -1
br i1 %8, label %26, label %9
9: ; preds = %5, %1
%10 = call i32 @waitpid(i32 %4, i32* %2, i32 0)
%11 = icmp eq i32 %10, %4
br i1 %11, label %12, label %23
12: ; preds = %9
%13 = load i32, i32* %2
%14 = call i32 @WIFEXITED(i32 %13)
%15 = icmp eq i32 %14, 0
br i1 %15, label %26, label %16
16: ; preds = %12
%17 = load i32, i32* %2
%18 = call i32 @WEXITSTATUS(i32 %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %26, label %20
20: ; preds = %16
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %18)
br label %26
23: ; preds = %9
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %4)
br label %26
26: ; preds = %12, %16, %20, %5, %1, %23
%27 = phi i32 [ -1, %23 ], [ %4, %1 ], [ -1, %5 ], [ %18, %20 ], [ 0, %16 ], [ 0, %12 ]
ret i32 %27
}