#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 */
scalar_t__ EINTR;
int do_login(char **);
scalar_t__ errno;
int perror(char *);
int waitpid(int, int *, int /*<<< orphan*/);
int do_guest(char *path, char *username) {
  char *args[3];
  pid_t pid, reaped;
  int status;
  args[0] = path;
  args[1] = username;
  args[2] = NULL;
  pid = do_login(args);
  if (pid == -1)
    return -1;
  for (;;) {
    reaped = waitpid(-1, &status, 0);
    if (reaped == -1) {
      if (errno != EINTR)
        return perror("waitpid"), -1;
    } else if (reaped == pid)
      return status ? -1 : 0;
  }
}


x86 assembly (clang, -O0):
<do_guest>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
call   1040 <do_login@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    117a <do_guest+0x4a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1201 <do_guest+0xd1>
jmp    117f <do_guest+0x4f>
mov    $0xffffffff,%edi
lea    -0x3c(%rbp),%rsi
xor    %edx,%edx
call   1030 <waitpid@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
jne    11d3 <do_guest+0xa3>
mov    0x2e3d(%rip),%rax
mov    (%rax),%rax
mov    0x2e2b(%rip),%rcx
cmp    (%rcx),%rax
je     11ce <do_guest+0x9e>
lea    0xe43(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1201 <do_guest+0xd1>
jmp    11fc <do_guest+0xcc>
mov    -0x38(%rbp),%eax
cmp    -0x34(%rbp),%eax
jne    11f7 <do_guest+0xc7>
mov    -0x3c(%rbp),%edx
xor    %eax,%eax
mov    $0xffffffff,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    1201 <do_guest+0xd1>
jmp    11fc <do_guest+0xcc>
jmp    117f <do_guest+0x4f>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_guest>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,0x10(%rsp)
mov    %rsi,0x18(%rsp)
movq   $0x0,0x20(%rsp)
lea    0x10(%rsp),%rdi
call   1040 <do_login@plt>
cmp    $0xffffffff,%eax
je     11b7 <do_guest+0x87>
mov    %eax,%ebx
lea    0xc(%rsp),%r14
mov    0x2e75(%rip),%r15
mov    0x2e66(%rip),%r12
jmp    1189 <do_guest+0x59>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r15),%rax
cmp    (%r12),%rax
jne    11ab <do_guest+0x7b>
mov    $0xffffffff,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1030 <waitpid@plt>
cmp    $0xffffffff,%eax
je     1180 <do_guest+0x50>
cmp    %ebx,%eax
jne    1189 <do_guest+0x59>
xor    %eax,%eax
cmp    0xc(%rsp),%eax
sbb    %eax,%eax
jmp    11bc <do_guest+0x8c>
lea    0xe4e(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@.str = [8 x i8] c"waitpid\00"
define i32 @do_guest(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [3 x i8*]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %4
%11 = getelementptr inbounds [3 x i8*], [3 x i8*]* %6, i64 0, i64 0
store i8* %10, i8** %11
%12 = load i8*, i8** %5
%13 = getelementptr inbounds [3 x i8*], [3 x i8*]* %6, i64 0, i64 1
store i8* %12, i8** %13
%14 = getelementptr inbounds [3 x i8*], [3 x i8*]* %6, i64 0, i64 2
store i8* null, i8** %14
%15 = getelementptr inbounds [3 x i8*], [3 x i8*]* %6, i64 0, i64 0
%16 = call i32 @do_login(i8** %15)
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %2
store i32 -1, i32* %3
br label %43
20: ; preds = %2
br label %21
21: ; preds = %42, %20
%22 = call i32 @waitpid(i32 -1, i32* %9, i32 0)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %32
25: ; preds = %21
%26 = load i64, i64* @errno
%27 = load i64, i64* @EINTR
%28 = icmp ne i64 %26, %27
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %43
31: ; preds = %25
br label %42
32: ; preds = %21
%33 = load i32, i32* %8
%34 = load i32, i32* %7
%35 = icmp eq i32 %33, %34
br i1 %35, label %36, label %41
36: ; preds = %32
%37 = load i32, i32* %9
%38 = icmp ne i32 %37, 0
%39 = zext i1 %38 to i64
%40 = select i1 %38, i32 -1, i32 0
store i32 %40, i32* %3
br label %43
41: ; preds = %32
br label %42
42: ; preds = %41, %31
br label %21
43: ; preds = %36, %29, %19
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [8 x i8] c"waitpid\00"
define i32 @do_guest(i8* %0, i8* %1) {
%3 = alloca [3 x i8*]
%4 = alloca i32
%5 = bitcast [3 x i8*]* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 0
store i8* %0, i8** %7
%8 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 1
store i8* %1, i8** %8
%9 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 2
store i8* null, i8** %9
%10 = call i32 @do_login(i8** %7)
%11 = icmp eq i32 %10, -1
br i1 %11, label %28, label %12
12: ; preds = %2, %23
%13 = call i32 @waitpid(i32 -1, i32* %4, i32 0)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %21
15: ; preds = %12
%16 = load i64, i64* @errno
%17 = load i64, i64* @EINTR
%18 = icmp eq i64 %16, %17
br i1 %18, label %23, label %19
19: ; preds = %15
%20 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %28
21: ; preds = %12
%22 = icmp eq i32 %13, %10
br i1 %22, label %24, label %23
23: ; preds = %21, %15
br label %12
24: ; preds = %21
%25 = load i32, i32* %4
%26 = icmp ne i32 %25, 0
%27 = sext i1 %26 to i32
br label %28
28: ; preds = %2, %24, %19
%29 = phi i32 [ -1, %19 ], [ %27, %24 ], [ -1, %2 ]
ret i32 %29
}