#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 */
struct stat {
  scalar_t__ st_dev;
  scalar_t__ st_ino;
};

/* Variables and functions */
scalar_t__ geteuid();
scalar_t__ lstat(char *, struct stat *);
scalar_t__ stat(char *, struct stat *);
int ischroot() {
  struct stat st1, st2;
  if (stat("/", &st1))
    return 2;
  if (stat("/proc/1/root", &st2)) {
    if (lstat("/proc/1/root", &st2))
      return 2;
    if (geteuid() != 0)
      return 2;
    return 0;
  } else if ((st1.st_dev == st2.st_dev) && (st1.st_ino == st2.st_ino))
    return 1;
  else
    return 0;
}


x86 assembly (clang, -O0):
<ischroot>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xec1(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
je     115e <ischroot+0x2e>
movl   $0x2,-0x4(%rbp)
jmp    11f6 <ischroot+0xc6>
lea    0xe9d(%rip),%rdi
lea    -0x28(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
je     11c7 <ischroot+0x97>
lea    0xe83(%rip),%rdi
lea    -0x28(%rbp),%rsi
call   1040 <lstat@plt>
cmp    $0x0,%rax
je     119e <ischroot+0x6e>
movl   $0x2,-0x4(%rbp)
jmp    11f6 <ischroot+0xc6>
mov    $0x0,%al
call   1030 <geteuid@plt>
cmp    $0x0,%rax
je     11bb <ischroot+0x8b>
movl   $0x2,-0x4(%rbp)
jmp    11f6 <ischroot+0xc6>
movl   $0x0,-0x4(%rbp)
jmp    11f6 <ischroot+0xc6>
mov    -0x18(%rbp),%rax
cmp    -0x28(%rbp),%rax
jne    11ef <ischroot+0xbf>
mov    -0x10(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    11ef <ischroot+0xbf>
movl   $0x1,-0x4(%rbp)
jmp    11f6 <ischroot+0xc6>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ischroot>:
push   %rbx
sub    $0x20,%rsp
lea    0xec4(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1050 <stat@plt>
mov    $0x2,%ebx
test   %rax,%rax
je     1158 <ischroot+0x28>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xea3(%rip),%rdi
mov    %rsp,%rsi
call   1050 <stat@plt>
test   %rax,%rax
je     1193 <ischroot+0x63>
lea    0xe8f(%rip),%rdi
mov    %rsp,%rsi
call   1040 <lstat@plt>
test   %rax,%rax
jne    1150 <ischroot+0x20>
xor    %eax,%eax
call   1030 <geteuid@plt>
xor    %ebx,%ebx
test   %rax,%rax
setne  %bl
add    %ebx,%ebx
jmp    1150 <ischroot+0x20>
mov    0x10(%rsp),%rax
cmp    (%rsp),%rax
jne    11af <ischroot+0x7f>
mov    0x18(%rsp),%rax
mov    $0x1,%ebx
cmp    0x8(%rsp),%rax
je     1150 <ischroot+0x20>
xor    %ebx,%ebx
jmp    1150 <ischroot+0x20>

LLVM IR (-O0):
%struct.stat = type { i64, i64 }
@.str = [2 x i8] c"/\00"
@.str.1 = [13 x i8] c"/proc/1/root\00"
define i32 @ischroot() {
%1 = alloca i32
%2 = alloca %struct.stat
%3 = alloca %struct.stat
%4 = call i64 @stat(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %struct.stat* %2)
%5 = icmp ne i64 %4, 0
br i1 %5, label %6, label %7
6: ; preds = %0
store i32 2, i32* %1
br label %33
7: ; preds = %0
%8 = call i64 @stat(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %3)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %19
10: ; preds = %7
%11 = call i64 @lstat(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %3)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %10
store i32 2, i32* %1
br label %33
14: ; preds = %10
%15 = call i64 (...) @geteuid()
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %14
store i32 2, i32* %1
br label %33
18: ; preds = %14
store i32 0, i32* %1
br label %33
19: ; preds = %7
%20 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 0
%21 = load i64, i64* %20
%22 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%23 = load i64, i64* %22
%24 = icmp eq i64 %21, %23
br i1 %24, label %25, label %32
25: ; preds = %19
%26 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 1
%27 = load i64, i64* %26
%28 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 1
%29 = load i64, i64* %28
%30 = icmp eq i64 %27, %29
br i1 %30, label %31, label %32
31: ; preds = %25
store i32 1, i32* %1
br label %33
32: ; preds = %25, %19
store i32 0, i32* %1
br label %33
33: ; preds = %32, %31, %18, %17, %13, %6
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
%struct.stat = type { i64, i64 }
@.str = [2 x i8] c"/\00"
@.str.1 = [13 x i8] c"/proc/1/root\00"
define i32 @ischroot() {
%1 = alloca %struct.stat
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %1 to i8*
%4 = bitcast %struct.stat* %2 to i8*
%5 = call i64 @stat(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %struct.stat* %1)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %30
7: ; preds = %0
%8 = call i64 @stat(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %2)
%9 = icmp eq i64 %8, 0
br i1 %9, label %17, label %10
10: ; preds = %7
%11 = call i64 @lstat(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %2)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %30
13: ; preds = %10
%14 = call i64 (...) @geteuid()
%15 = icmp eq i64 %14, 0
%16 = select i1 %15, i32 0, i32 2
br label %30
17: ; preds = %7
%18 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%19 = load i64, i64* %18
%20 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%21 = load i64, i64* %20
%22 = icmp eq i64 %19, %21
br i1 %22, label %23, label %29
23: ; preds = %17
%24 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 1
%25 = load i64, i64* %24
%26 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%27 = load i64, i64* %26
%28 = icmp eq i64 %25, %27
br i1 %28, label %30, label %29
29: ; preds = %23, %17
br label %30
30: ; preds = %23, %13, %10, %0, %29
%31 = phi i32 [ 0, %29 ], [ 2, %0 ], [ 2, %10 ], [ %16, %13 ], [ 1, %23 ]
ret i32 %31
}