#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 ssize_t;
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ close(long);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *getenv(char *);
int /*<<< orphan*/ perror(char *);
int read(long, char *, int);
int /*<<< orphan*/ stderr;
void *strtol(char *, char **, int);
int waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int write(long, char *, int);
pid_t chrootme() {
  long int fd = -1;
  char *sbxdesc;
  char *helper_pid_desc;
  char msg = 'C';
  ssize_t cnt;
  pid_t helper = -1, ret;
  sbxdesc = getenv("SBX_D");
  if (sbxdesc == NULL)
    return -1;
  errno = 0;
  fd = strtol(sbxdesc, (char **)NULL, 10);
  if (errno || (fd == -1))
    return -1;
  helper_pid_desc = getenv("SBX_HELPER_PID");
  if (helper_pid_desc != NULL) {
    errno = 0;
    helper = strtol(helper_pid_desc, (char **)NULL, 10);
    if (errno || (helper == -1))
      return -1;
  }
  cnt = write(fd, &msg, 1);
  if (cnt != 1)
    return -1;
  cnt = read(fd, &msg, 1);
  if ((cnt != 1) || (msg != 'O')) {
    fprintf(stderr, "Error reading confirmation message\n");
    return -1;
  }
  close(fd);
  ret = waitpid(helper, NULL, 0);
  if (ret != -1 && ((helper == -1) || (ret == helper)))
    return ret;
  else {
    perror("waitpid");
    return -1;
  }
}


x86 assembly (clang, -O0):
<chrootme>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movq   $0xffffffffffffffff,-0x10(%rbp)
ff
movb   $0x43,-0x21(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
lea    0xe5e(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c2 <chrootme+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    1348 <chrootme+0x1c8>
mov    0x2e0f(%rip),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %rax,-0x10(%rbp)
mov    0x2deb(%rip),%rax
cmpq   $0x0,(%rax)
jne    1202 <chrootme+0x82>
cmpq   $0xffffffffffffffff,-0x10(%rbp)
jne    120e <chrootme+0x8e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1348 <chrootme+0x1c8>
lea    0xdf1(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1278 <chrootme+0xf8>
mov    0x2da8(%rip),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2d85(%rip),%rax
cmpq   $0x0,(%rax)
jne    1267 <chrootme+0xe7>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1273 <chrootme+0xf3>
movl   $0xffffffff,-0x4(%rbp)
jmp    1348 <chrootme+0x1c8>
jmp    1278 <chrootme+0xf8>
mov    -0x10(%rbp),%rdi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x1,-0x28(%rbp)
je     12a3 <chrootme+0x123>
movl   $0xffffffff,-0x4(%rbp)
jmp    1348 <chrootme+0x1c8>
mov    -0x10(%rbp),%rdi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x1,-0x28(%rbp)
jne    12cf <chrootme+0x14f>
movsbl -0x21(%rbp),%eax
cmp    $0x4f,%eax
je     12f0 <chrootme+0x170>
mov    0x2d0a(%rip),%rax
mov    (%rax),%edi
lea    0xd36(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1348 <chrootme+0x1c8>
mov    -0x10(%rbp),%rdi
call   1050 <close@plt>
mov    -0x2c(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <waitpid@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
je     1335 <chrootme+0x1b5>
cmpl   $0xffffffff,-0x2c(%rbp)
je     132a <chrootme+0x1aa>
mov    -0x30(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jne    1335 <chrootme+0x1b5>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1348 <chrootme+0x1c8>
lea    0xcfd(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chrootme>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
movb   $0x43,0x7(%rsp)
lea    0xe6d(%rip),%rdi
call   1030 <getenv@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     12a6 <chrootme+0x126>
mov    0x2e2b(%rip),%rbx
movq   $0x0,(%rbx)
mov    %rax,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1080 <strtol@plt>
cmpq   $0x0,(%rbx)
jne    12a6 <chrootme+0x126>
mov    %rax,%r15
cmp    $0xffffffffffffffff,%rax
je     12a6 <chrootme+0x126>
lea    0xe25(%rip),%rdi
call   1030 <getenv@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     1223 <chrootme+0xa3>
movq   $0x0,(%rbx)
mov    %rax,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1080 <strtol@plt>
cmpq   $0x0,(%rbx)
mov    $0xffffffff,%ebp
jne    12a6 <chrootme+0x126>
mov    %rax,%r14
cmp    $0xffffffff,%r14d
je     12a6 <chrootme+0x126>
lea    0x7(%rsp),%rsi
mov    %r15,%rdi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
jne    12a1 <chrootme+0x121>
lea    0x7(%rsp),%rsi
mov    %r15,%rdi
mov    $0x1,%edx
call   1060 <read@plt>
cmp    $0x1,%eax
jne    128c <chrootme+0x10c>
cmpb   $0x4f,0x7(%rsp)
jne    128c <chrootme+0x10c>
mov    %r15,%rdi
call   1050 <close@plt>
mov    %r14d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <waitpid@plt>
cmp    $0xffffffff,%eax
je     127e <chrootme+0xfe>
mov    %eax,%ebp
cmp    $0xffffffff,%r14d
je     12a6 <chrootme+0x126>
cmp    %r14d,%ebp
je     12a6 <chrootme+0x126>
lea    0xdb4(%rip),%rdi
call   10a0 <perror@plt>
jmp    12a1 <chrootme+0x121>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
lea    0xd79(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"SBX_D\00"
@errno = global i64 0
@.str.1 = [15 x i8] c"SBX_HELPER_PID\00"
@stderr = global i32 0
@.str.2 = [36 x i8] c"Error reading confirmation message\0A\00"
@.str.3 = [8 x i8] c"waitpid\00"
define i32 @chrootme() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i64 -1, i64* %2
store i8 67, i8* %5
store i32 -1, i32* %7
%9 = call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i8* %9, i8** %3
%10 = load i8*, i8** %3
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %13
12: ; preds = %0
store i32 -1, i32* %1
br label %74
13: ; preds = %0
store i64 0, i64* @errno
%14 = load i8*, i8** %3
%15 = call i8* @strtol(i8* %14, i8** null, i32 10)
%16 = ptrtoint i8* %15 to i64
store i64 %16, i64* %2
%17 = load i64, i64* @errno
%18 = icmp ne i64 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %13
%20 = load i64, i64* %2
%21 = icmp eq i64 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %19, %13
store i32 -1, i32* %1
br label %74
23: ; preds = %19
%24 = call i8* @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i8* %24, i8** %4
%25 = load i8*, i8** %4
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %38
27: ; preds = %23
store i64 0, i64* @errno
%28 = load i8*, i8** %4
%29 = call i8* @strtol(i8* %28, i8** null, i32 10)
%30 = ptrtoint i8* %29 to i32
store i32 %30, i32* %7
%31 = load i64, i64* @errno
%32 = icmp ne i64 %31, 0
br i1 %32, label %36, label %33
33: ; preds = %27
%34 = load i32, i32* %7
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %37
36: ; preds = %33, %27
store i32 -1, i32* %1
br label %74
37: ; preds = %33
br label %38
38: ; preds = %37, %23
%39 = load i64, i64* %2
%40 = call i32 @write(i64 %39, i8* %5, i32 1)
store i32 %40, i32* %6
%41 = load i32, i32* %6
%42 = icmp ne i32 %41, 1
br i1 %42, label %43, label %44
43: ; preds = %38
store i32 -1, i32* %1
br label %74
44: ; preds = %38
%45 = load i64, i64* %2
%46 = call i32 @read(i64 %45, i8* %5, i32 1)
store i32 %46, i32* %6
%47 = load i32, i32* %6
%48 = icmp ne i32 %47, 1
br i1 %48, label %53, label %49
49: ; preds = %44
%50 = load i8, i8* %5
%51 = sext i8 %50 to i32
%52 = icmp ne i32 %51, 79
br i1 %52, label %53, label %56
53: ; preds = %49, %44
%54 = load i32, i32* @stderr
%55 = call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %74
56: ; preds = %49
%57 = load i64, i64* %2
%58 = call i32 @close(i64 %57)
%59 = load i32, i32* %7
%60 = call i32 @waitpid(i32 %59, i32* null, i32 0)
store i32 %60, i32* %8
%61 = load i32, i32* %8
%62 = icmp ne i32 %61, -1
br i1 %62, label %63, label %72
63: ; preds = %56
%64 = load i32, i32* %7
%65 = icmp eq i32 %64, -1
br i1 %65, label %70, label %66
66: ; preds = %63
%67 = load i32, i32* %8
%68 = load i32, i32* %7
%69 = icmp eq i32 %67, %68
br i1 %69, label %70, label %72
70: ; preds = %66, %63
%71 = load i32, i32* %8
store i32 %71, i32* %1
br label %74
72: ; preds = %66, %56
%73 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %1
br label %74
74: ; preds = %72, %70, %53, %43, %36, %22, %12
%75 = load i32, i32* %1
ret i32 %75
}

LLVM IR (-O2):
@.str = [6 x i8] c"SBX_D\00"
@errno = global i64 0
@.str.1 = [15 x i8] c"SBX_HELPER_PID\00"
@stderr = global i32 0
@.str.2 = [36 x i8] c"Error reading confirmation message\0A\00"
@.str.3 = [8 x i8] c"waitpid\00"
define i32 @chrootme() {
%1 = alloca i8
store i8 67, i8* %1
%2 = tail call i8* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i8* %2, null
br i1 %3, label %45, label %4
4: ; preds = %0
store i64 0, i64* @errno
%5 = tail call i8* @strtol(i8* nocapture %2, i8** null, i32 10)
%6 = ptrtoint i8* %5 to i64
%7 = load i64, i64* @errno
%8 = icmp ne i64 %7, 0
%9 = icmp eq i8* %5, inttoptr (i64 -1 to i8*)
%10 = select i1 %8, i1 true, i1 %9
br i1 %10, label %45, label %11
11: ; preds = %4
%12 = tail call i8* @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i8* %12, null
br i1 %13, label %22, label %14
14: ; preds = %11
store i64 0, i64* @errno
%15 = tail call i8* @strtol(i8* nocapture %12, i8** null, i32 10)
%16 = ptrtoint i8* %15 to i64
%17 = trunc i64 %16 to i32
%18 = load i64, i64* @errno
%19 = icmp ne i64 %18, 0
%20 = icmp eq i32 %17, -1
%21 = select i1 %19, i1 true, i1 %20
br i1 %21, label %45, label %22
22: ; preds = %14, %11
%23 = phi i32 [ %17, %14 ], [ -1, %11 ]
%24 = call i32 @write(i64 %6, i8* %1, i32 1)
%25 = icmp eq i32 %24, 1
br i1 %25, label %26, label %45
26: ; preds = %22
%27 = call i32 @read(i64 %6, i8* %1, i32 1)
%28 = icmp ne i32 %27, 1
%29 = load i8, i8* %1
%30 = icmp ne i8 %29, 79
%31 = select i1 %28, i1 true, i1 %30
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = load i32, i32* @stderr
%34 = tail call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
br label %45
35: ; preds = %26
%36 = tail call i32 @close(i64 %6)
%37 = tail call i32 @waitpid(i32 %23, i32* null, i32 0)
%38 = icmp eq i32 %37, -1
br i1 %38, label %43, label %39
39: ; preds = %35
%40 = icmp eq i32 %23, -1
%41 = icmp eq i32 %37, %23
%42 = select i1 %40, i1 true, i1 %41
br i1 %42, label %45, label %43
43: ; preds = %39, %35
%44 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %45
45: ; preds = %39, %22, %14, %4, %0, %43, %32
%46 = phi i32 [ -1, %32 ], [ -1, %43 ], [ -1, %0 ], [ -1, %4 ], [ -1, %14 ], [ -1, %22 ], [ %37, %39 ]
ret i32 %46
}