#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 /*<<< orphan*/ ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ getuid();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char **, int);
int unlink(char *);
int main(void) {
  if (getuid() != 0) {
    printf("[-] Please run me as root.\n");
    return -1;
  }
  char buf[2048];
  ssize_t rbytes;
  int fd = open("/etc/ld.so.preload", O_RDONLY);
  if (fd < 0) {
    printf("[-] ld.so.preload either truly does not exist, or a deeper kernel "
           "space hook is intercepting open()\n");
    printf("Probably safe to continue with installation.\n");
    return fd;
  }
  while ((rbytes = read(fd, &buf, sizeof(buf))) > 0) {
    printf("ld.so.preload presence detected.\n");
    printf("Location of potentially malicious preloaded library:\n%s\n", buf);
    printf("REMEMBER THIS SO YOU CAN SNOOP AROUND THE LIBRARY AFTER "
           "INSTALLATION.\n");
    printf("Now removing ld.so.preload (LOL BYE)\n");
    int u = unlink("/etc/ld.so.preload");
    if (u < 0) {
      printf("Removing ld.so.preload failed.\nIt is recommended to ^C if you "
             "see this.\n");
      return u;
    }
    printf("ld.so.preload should now be gone.\n");
    printf("It's now safe to continue with installation. :)\n");
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x820,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1040 <getuid@plt>
cmp    $0x0,%rax
je     119d <main+0x3d>
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c7 <main+0x167>
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
lea    0xe6f(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x818(%rbp)
cmpl   $0x0,-0x818(%rbp)
jge    11ef <main+0x8f>
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xeba(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x818(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12c7 <main+0x167>
jmp    11f4 <main+0x94>
mov    -0x818(%rbp),%edi
lea    -0x810(%rbp),%rsi
mov    $0x800,%edx
call   1070 <read@plt>
mov    %eax,-0x814(%rbp)
cmp    $0x0,%eax
jle    12b5 <main+0x155>
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x810(%rbp),%rsi
lea    0xeae(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xed9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xf12(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xdbc(%rip),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x81c(%rbp)
cmpl   $0x0,-0x81c(%rbp)
jge    1294 <main+0x134>
lea    0xf0b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x81c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12c7 <main+0x167>
lea    0xf38(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xf4d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11f4 <main+0x94>
mov    -0x818(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x808,%rsp
xor    %eax,%eax
call   1050 <getuid@plt>
test   %rax,%rax
je     11a4 <main+0x34>
lea    0x106f(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%r15d
jmp    129d <main+0x12d>
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
lea    0xe4c(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
js     1272 <main+0x102>
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0x800,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    1265 <main+0xf5>
mov    %rsp,%r12
lea    0xecd(%rip),%rbp
lea    0xe13(%rip),%r14
lea    0xf06(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    0xe45(%rip),%rdi
call   1040 <puts@plt>
lea    0xe00(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe49(%rip),%rdi
call   1040 <puts@plt>
mov    %rbp,%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
call   1030 <unlink@plt>
test   %eax,%eax
js     128c <main+0x11c>
lea    0xe94(%rip),%rdi
call   1040 <puts@plt>
mov    %r13,%rdi
call   1040 <puts@plt>
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x800,%edx
call   1080 <read@plt>
test   %eax,%eax
jg     1200 <main+0x90>
mov    %r15d,%edi
call   1070 <close@plt>
xor    %r15d,%r15d
jmp    129d <main+0x12d>
lea    0xef9(%rip),%rdi
call   1040 <puts@plt>
lea    0xf51(%rip),%rdi
call   1040 <puts@plt>
jmp    129d <main+0x12d>
mov    %eax,%ebx
lea    0xe95(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%r15d
mov    %r15d,%eax
add    $0x808,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [28 x i8] c"[-] Please run me as root.\0A\00"
@.str.1 = [19 x i8] c"/etc/ld.so.preload\00"
@O_RDONLY = global i32 0
@.str.2 = [101 x i8] c"[-] ld.so.preload either truly does not exist, or a deeper kernel space hook is intercepting open()\0A\00"
@.str.3 = [46 x i8] c"Probably safe to continue with installation.\0A\00"
@.str.4 = [34 x i8] c"ld.so.preload presence detected.\0A\00"
@.str.5 = [57 x i8] c"Location of potentially malicious preloaded library:\0A%s\0A\00"
@.str.6 = [71 x i8] c"REMEMBER THIS SO YOU CAN SNOOP AROUND THE LIBRARY AFTER INSTALLATION.\0A\00"
@.str.7 = [38 x i8] c"Now removing ld.so.preload (LOL BYE)\0A\00"
@.str.8 = [73 x i8] c"Removing ld.so.preload failed.\0AIt is recommended to ^C if you see this.\0A\00"
@.str.9 = [35 x i8] c"ld.so.preload should now be gone.\0A\00"
@.str.10 = [49 x i8] c"It's now safe to continue with installation. :)\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2048 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = call i64 (...) @getuid()
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %43
10: ; preds = %0
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %11)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %10
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([101 x i8], [101 x i8]* @.str.2, i64 0, i64 0))
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0))
%18 = load i32, i32* %4
store i32 %18, i32* %1
br label %43
19: ; preds = %10
br label %20
20: ; preds = %37, %19
%21 = load i32, i32* %4
%22 = bitcast [2048 x i8]* %2 to i8**
%23 = call i32 @read(i32 %21, i8** %22, i32 2048)
store i32 %23, i32* %3
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %40
25: ; preds = %20
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
%27 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.5, i64 0, i64 0), i8* %27)
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str.6, i64 0, i64 0))
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.7, i64 0, i64 0))
%31 = call i32 @unlink(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
store i32 %31, i32* %5
%32 = load i32, i32* %5
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %25
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str.8, i64 0, i64 0))
%36 = load i32, i32* %5
store i32 %36, i32* %1
br label %43
37: ; preds = %25
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.9, i64 0, i64 0))
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.10, i64 0, i64 0))
br label %20
40: ; preds = %20
%41 = load i32, i32* %4
%42 = call i32 @close(i32 %41)
store i32 0, i32* %1
br label %43
43: ; preds = %40, %34, %15, %8
%44 = load i32, i32* %1
ret i32 %44
}

LLVM IR (-O2):
@.str.1 = [19 x i8] c"/etc/ld.so.preload\00"
@O_RDONLY = global i32 0
@.str.5 = [57 x i8] c"Location of potentially malicious preloaded library:\0A%s\0A\00"
@str = [33 x i8] c"ld.so.preload presence detected.\00"
@str.11 = [70 x i8] c"REMEMBER THIS SO YOU CAN SNOOP AROUND THE LIBRARY AFTER INSTALLATION.\00"
@str.12 = [37 x i8] c"Now removing ld.so.preload (LOL BYE)\00"
@str.13 = [34 x i8] c"ld.so.preload should now be gone.\00"
@str.14 = [48 x i8] c"It's now safe to continue with installation. :)\00"
@str.15 = [72 x i8] c"Removing ld.so.preload failed.\0AIt is recommended to ^C if you see this.\00"
@str.16 = [100 x i8] c"[-] ld.so.preload either truly does not exist, or a deeper kernel space hook is intercepting open()\00"
@str.17 = [45 x i8] c"Probably safe to continue with installation.\00"
@str.18 = [27 x i8] c"[-] Please run me as root.\00"
define i32 @main() {
%1 = alloca [2048 x i8]
%2 = tail call i64 (...) @getuid()
%3 = icmp eq i64 %2, 0
br i1 %3, label %6, label %4
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.18, i64 0, i64 0))
br label %36
6: ; preds = %0
%7 = getelementptr inbounds [2048 x i8], [2048 x i8]* %1, i64 0, i64 0
%8 = load i32, i32* @O_RDONLY
%9 = tail call i32 @open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %6
%12 = bitcast [2048 x i8]* %1 to i8**
%13 = call i32 @read(i32 %9, i8** %12, i32 2048)
%14 = icmp sgt i32 %13, 0
br i1 %14, label %18, label %32
15: ; preds = %6
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([100 x i8], [100 x i8]* @str.16, i64 0, i64 0))
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @str.17, i64 0, i64 0))
br label %34
18: ; preds = %11, %27
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @.str.5, i64 0, i64 0), i8* %7)
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([70 x i8], [70 x i8]* @str.11, i64 0, i64 0))
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.12, i64 0, i64 0))
%23 = call i32 @unlink(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %18
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([72 x i8], [72 x i8]* @str.15, i64 0, i64 0))
br label %34
27: ; preds = %18
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.13, i64 0, i64 0))
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str.14, i64 0, i64 0))
%30 = call i32 @read(i32 %9, i8** %12, i32 2048)
%31 = icmp sgt i32 %30, 0
br i1 %31, label %18, label %32
32: ; preds = %27, %11
%33 = call i32 @close(i32 %9)
br label %34
34: ; preds = %25, %32, %15
%35 = phi i32 [ %9, %15 ], [ 0, %32 ], [ %23, %25 ]
br label %36
36: ; preds = %34, %4
%37 = phi i32 [ -1, %4 ], [ %35, %34 ]
ret i32 %37
}