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


x86 assembly (clang, -O0):
<spawn_utempter>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
sub    $0xffffffff,%eax
je     11ad <spawn_utempter+0x3d>
jmp    119d <spawn_utempter+0x2d>
mov    -0x20(%rbp),%eax
test   %eax,%eax
je     11b9 <spawn_utempter+0x49>
jmp    11f4 <spawn_utempter+0x84>
movl   $0xffffffff,-0x4(%rbp)
jmp    1290 <spawn_utempter+0x120>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x10(%rbp),%rdx
lea    0xe32(%rip),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
call   1080 <execlp@plt>
cmp    $0xffffffff,%eax
jne    11ef <spawn_utempter+0x7f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1290 <spawn_utempter+0x120>
jmp    11f4 <spawn_utempter+0x84>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    -0x14(%rbp),%eax
jne    126f <spawn_utempter+0xff>
mov    -0x18(%rbp),%edi
call   1070 <WIFEXITED@plt>
cmp    $0x0,%eax
je     122c <spawn_utempter+0xbc>
mov    -0x18(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,-0x24(%rbp)
jmp    1236 <spawn_utempter+0xc6>
xor    %eax,%eax
mov    %eax,-0x24(%rbp)
jmp    1236 <spawn_utempter+0xc6>
mov    -0x24(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1264 <spawn_utempter+0xf4>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
lea    0xdac(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1290 <spawn_utempter+0x120>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xda6(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawn_utempter>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     11fd <spawn_utempter+0x8d>
test   %ebp,%ebp
jne    11b1 <spawn_utempter+0x41>
mov    %ebx,%edi
xor    %esi,%esi
call   1030 <dup2@plt>
lea    0xe61(%rip),%rdi
mov    %rdi,%rsi
mov    %r14,%rdx
xor    %ecx,%ecx
call   1080 <execlp@plt>
cmp    $0xffffffff,%eax
je     121a <spawn_utempter+0xaa>
lea    0xc(%rsp),%rsi
xor    %ebx,%ebx
mov    %ebp,%edi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    %ebp,%eax
jne    1201 <spawn_utempter+0x91>
mov    0xc(%rsp),%edi
call   1070 <WIFEXITED@plt>
test   %eax,%eax
je     121f <spawn_utempter+0xaf>
mov    0xc(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
test   %eax,%eax
je     121f <spawn_utempter+0xaf>
mov    %eax,%ebp
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    0xe18(%rip),%rsi
mov    %r14,%rdx
mov    %ebp,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %ebp,%ebx
jmp    121f <spawn_utempter+0xaf>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
lea    0xe17(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"utempter\00"
@stderr = global i32 0
@.str.1 = [31 x i8] c"utempter %s 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_utempter(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%9 = call i32 (...) @fork()
store i32 %9, i32* %6
%10 = load i32, i32* %6
switch i32 %10, label %20 [
i32 -1, label %11
i32 0, label %12
]
11: ; preds = %2
store i32 -1, i32* %3
br label %48
12: ; preds = %2
%13 = load i32, i32* %4
%14 = call i32 @dup2(i32 %13, i32 0)
%15 = load i8*, i8** %5
%16 = call i32 @execlp(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %15, i32* null)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %12
store i32 -1, i32* %3
br label %48
19: ; preds = %12
br label %20
20: ; preds = %2, %19
%21 = load i32, i32* %6
%22 = call i32 @waitpid(i32 %21, i32* %7, i32 0)
%23 = load i32, i32* %6
%24 = icmp eq i32 %22, %23
br i1 %24, label %25, label %44
25: ; preds = %20
%26 = load i32, i32* %7
%27 = call i32 @WIFEXITED(i32 %26)
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = load i32, i32* %7
%31 = call i32 @WEXITSTATUS(i32 %30)
br label %33
32: ; preds = %25
br label %33
33: ; preds = %32, %29
%34 = phi i32 [ %31, %29 ], [ 0, %32 ]
store i32 %34, i32* %8
%35 = load i32, i32* %8
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %33
%38 = load i32, i32* @stderr
%39 = load i8*, i8** %5
%40 = load i32, i32* %8
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %39, i32 %40)
br label %42
42: ; preds = %37, %33
%43 = load i32, i32* %8
store i32 %43, i32* %3
br label %48
44: ; preds = %20
%45 = load i32, i32* @stderr
%46 = load i32, i32* %6
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %46)
store i32 -1, i32* %3
br label %48
48: ; preds = %44, %42, %18, %11
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
@.str = [9 x i8] c"utempter\00"
@stderr = global i32 0
@.str.1 = [31 x i8] c"utempter %s 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_utempter(i32 %0, i8* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 (...) @fork()
switch i32 %5, label %10 [
i32 -1, label %27
i32 0, label %6
]
6: ; preds = %2
%7 = tail call i32 @dup2(i32 %0, i32 0)
%8 = tail call i32 @execlp(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %1, i32* null)
%9 = icmp eq i32 %8, -1
br i1 %9, label %27, label %10
10: ; preds = %6, %2
%11 = call i32 @waitpid(i32 %5, i32* %3, i32 0)
%12 = icmp eq i32 %11, %5
br i1 %12, label %13, label %24
13: ; preds = %10
%14 = load i32, i32* %3
%15 = call i32 @WIFEXITED(i32 %14)
%16 = icmp eq i32 %15, 0
br i1 %16, label %27, label %17
17: ; preds = %13
%18 = load i32, i32* %3
%19 = call i32 @WEXITSTATUS(i32 %18)
%20 = icmp eq i32 %19, 0
br i1 %20, label %27, label %21
21: ; preds = %17
%22 = load i32, i32* @stderr
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %1, i32 %19)
br label %27
24: ; preds = %10
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %5)
br label %27
27: ; preds = %13, %17, %21, %6, %2, %24
%28 = phi i32 [ -1, %24 ], [ %5, %2 ], [ -1, %6 ], [ %19, %21 ], [ 0, %17 ], [ 0, %13 ]
ret i32 %28
}