#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_uid;
};
typedef size_t ssize_t;

/* Variables and functions */
int /*<<< orphan*/ PR_GET_DUMPABLE;
int /*<<< orphan*/ R_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ getenv(char *);
scalar_t__ getuid();
char *gnu_basename(char *);
int prctl(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/,
          int /*<<< orphan*/, int /*<<< orphan*/);
size_t readlink(char *, char *, int);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
void warn_dumpable(void) {
  if (getuid() != 0 && prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 1 &&
      getenv("FIREJAIL_PLUGIN")) {
    fprintf(stderr, "Error: dumpable process\n");
    char path[4096];
    ssize_t len = readlink("/proc/self/exe", path, 4096 - 1);
    if (len < 0)
      return;
    path[len] = '\0';
    const char *base = gnu_basename(path);
    struct stat s;
    if (stat("/proc/self/exe", &s) == 0 && s.st_uid != 0)
      fprintf(stderr, "Change owner of %s executable to root\n", base);
    else if (access("/proc/self/exe", R_OK) == 0)
      fprintf(stderr, "Remove read permission on %s executable\n", base);
  }
}


x86 assembly (clang, -O0):
<warn_dumpable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    $0x0,%al
call   1050 <getuid@plt>
cmp    $0x0,%rax
je     12d5 <warn_dumpable+0x155>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
xor    %r8d,%r8d
mov    %r8d,%esi
mov    %r8d,%edx
mov    %r8d,%ecx
call   1090 <prctl@plt>
cmp    $0x1,%eax
jne    12d5 <warn_dumpable+0x155>
lea    0xe3a(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     12d5 <warn_dumpable+0x155>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
lea    0xe2b(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
lea    -0x1000(%rbp),%rsi
lea    0xe2f(%rip),%rdi
mov    $0xfff,%edx
call   1040 <readlink@plt>
mov    %rax,-0x1008(%rbp)
cmpq   $0x0,-0x1008(%rbp)
jae    121e <warn_dumpable+0x9e>
jmp    12d5 <warn_dumpable+0x155>
mov    -0x1008(%rbp),%rax
movb   $0x0,-0x1000(%rbp,%rax,1)
lea    -0x1000(%rbp),%rdi
call   1060 <gnu_basename@plt>
mov    %rax,-0x1010(%rbp)
lea    0xde2(%rip),%rdi
lea    -0x1018(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jne    128e <warn_dumpable+0x10e>
cmpq   $0x0,-0x1018(%rbp)
je     128e <warn_dumpable+0x10e>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
mov    -0x1010(%rbp),%rdx
lea    0xdb6(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    12d0 <warn_dumpable+0x150>
mov    0x2d3b(%rip),%rax
mov    (%rax),%esi
lea    0xd8b(%rip),%rdi
call   10a0 <access@plt>
cmp    $0x0,%rax
jne    12cb <warn_dumpable+0x14b>
mov    0x2d24(%rip),%rax
mov    (%rax),%edi
mov    -0x1010(%rbp),%rdx
lea    0xd9b(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    12d0 <warn_dumpable+0x150>
jmp    12d5 <warn_dumpable+0x155>
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<warn_dumpable>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1018,%rsp
xor    %eax,%eax
call   1050 <getuid@plt>
test   %rax,%rax
je     126a <warn_dumpable+0xea>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1090 <prctl@plt>
cmp    $0x1,%eax
jne    126a <warn_dumpable+0xea>
lea    0xe3b(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     126a <warn_dumpable+0xea>
mov    0x2dfe(%rip),%r12
mov    (%r12),%edi
lea    0xe2b(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
lea    0xe36(%rip),%r15
lea    0x10(%rsp),%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0xfff,%edx
call   1040 <readlink@plt>
movb   $0x0,0x10(%rsp,%rax,1)
mov    %rbx,%rdi
call   1060 <gnu_basename@plt>
mov    %rax,%r14
lea    0x8(%rsp),%rsi
mov    %r15,%rdi
call   1080 <stat@plt>
test   %rax,%rax
jne    123b <warn_dumpable+0xbb>
cmpq   $0x0,0x8(%rsp)
je     123b <warn_dumpable+0xbb>
lea    0xdff(%rip),%rsi
jmp    125c <warn_dumpable+0xdc>
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
lea    0xdde(%rip),%rdi
call   10a0 <access@plt>
test   %rax,%rax
jne    126a <warn_dumpable+0xea>
lea    0xe03(%rip),%rsi
mov    (%r12),%edi
mov    %r14,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@PR_GET_DUMPABLE = global i32 0
@.str = [16 x i8] c"FIREJAIL_PLUGIN\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"Error: dumpable process\0A\00"
@.str.2 = [15 x i8] c"/proc/self/exe\00"
@.str.3 = [39 x i8] c"Change owner of %s executable to root\0A\00"
@R_OK = global i32 0
@.str.4 = [41 x i8] c"Remove read permission on %s executable\0A\00"
define void @warn_dumpable() {
%1 = alloca [4096 x i8]
%2 = alloca i64
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = call i64 (...) @getuid()
%6 = icmp ne i64 %5, 0
br i1 %6, label %7, label %47
7: ; preds = %0
%8 = load i32, i32* @PR_GET_DUMPABLE
%9 = call i32 @prctl(i32 %8, i32 0, i32 0, i32 0, i32 0)
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %47
11: ; preds = %7
%12 = call i64 @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %47
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%17 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%18 = call i64 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %17, i32 4095)
store i64 %18, i64* %2
%19 = load i64, i64* %2
%20 = icmp ult i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %14
br label %47
22: ; preds = %14
%23 = load i64, i64* %2
%24 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 %23
store i8 0, i8* %24
%25 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%26 = call i8* @gnu_basename(i8* %25)
store i8* %26, i8** %3
%27 = call i64 @stat(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), %struct.stat* %4)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %37
29: ; preds = %22
%30 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%31 = load i64, i64* %30
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %29
%34 = load i32, i32* @stderr
%35 = load i8*, i8** %3
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i8* %35)
br label %46
37: ; preds = %29, %22
%38 = load i32, i32* @R_OK
%39 = call i64 @access(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %38)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %37
%42 = load i32, i32* @stderr
%43 = load i8*, i8** %3
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i8* %43)
br label %45
45: ; preds = %41, %37
br label %46
46: ; preds = %45, %33
br label %47
47: ; preds = %21, %46, %11, %7, %0
ret void
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@PR_GET_DUMPABLE = global i32 0
@.str = [16 x i8] c"FIREJAIL_PLUGIN\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"Error: dumpable process\0A\00"
@.str.2 = [15 x i8] c"/proc/self/exe\00"
@.str.3 = [39 x i8] c"Change owner of %s executable to root\0A\00"
@R_OK = global i32 0
@.str.4 = [41 x i8] c"Remove read permission on %s executable\0A\00"
define void @warn_dumpable() {
%1 = alloca [4096 x i8]
%2 = alloca %struct.stat
%3 = tail call i64 (...) @getuid()
%4 = icmp eq i64 %3, 0
br i1 %4, label %35, label %5
5: ; preds = %0
%6 = load i32, i32* @PR_GET_DUMPABLE
%7 = tail call i32 @prctl(i32 %6, i32 0, i32 0, i32 0, i32 0)
%8 = icmp eq i32 %7, 1
br i1 %8, label %9, label %35
9: ; preds = %5
%10 = tail call i64 @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i64 %10, 0
br i1 %11, label %35, label %12
12: ; preds = %9
%13 = load i32, i32* @stderr
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%15 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%16 = call i64 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %15, i32 4095)
%17 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 %16
store i8 0, i8* %17
%18 = call i8* @gnu_basename(i8* %15)
%19 = bitcast %struct.stat* %2 to i8*
%20 = call i64 @stat(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), %struct.stat* %2)
%21 = icmp eq i64 %20, 0
%22 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%23 = load i64, i64* %22
%24 = icmp ne i64 %23, 0
%25 = select i1 %21, i1 %24, i1 false
br i1 %25, label %30, label %26
26: ; preds = %12
%27 = load i32, i32* @R_OK
%28 = call i64 @access(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %26, %12
%31 = phi i8* [ getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), %12 ], [ getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), %26 ]
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* %31, i8* %18)
br label %34
34: ; preds = %30, %26
br label %35
35: ; preds = %34, %9, %5, %0
ret void
}