#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*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
scalar_t__ memmem(char *, int, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char const *);
unsigned long strtoul(char *, char **, int);
unsigned long get_kernel_addr_trusty(char *buffer, int size) {
  const char *needle1 = "Freeing unused";
  char *substr = (char *)memmem(&buffer[0], size, needle1, strlen(needle1));
  if (substr == NULL) {
    fprintf(stderr, "[-] substring '%s' not found in syslog\n", needle1);
    exit(EXIT_FAILURE);
  }
  int start = 0;
  int end = 0;
  for (end = start; substr[end] != '-'; end++)
    ;
  const char *needle2 = "ffffff";
  substr =
      (char *)memmem(&substr[start], end - start, needle2, strlen(needle2));
  if (substr == NULL) {
    fprintf(stderr, "[-] substring '%s' not found in syslog\n", needle2);
    exit(EXIT_FAILURE);
  }
  char *endptr = &substr[16];
  unsigned long r = strtoul(&substr[0], &endptr, 16);
  r &= 0xffffffffff000000ul;
  return r;
}


x86 assembly (clang, -O0):
<get_kernel_addr_trusty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    0xe9a(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x58(%rbp),%rdi
mov    -0x4c(%rbp),%esi
mov    -0x48(%rbp),%rdx
mov    %eax,%ecx
call   1050 <memmem@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d1 <get_kernel_addr_trusty+0x81>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe51(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2d,%eax
je     120d <get_kernel_addr_trusty+0xbd>
jmp    11ff <get_kernel_addr_trusty+0xaf>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11e5 <get_kernel_addr_trusty+0x95>
lea    0xe23(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x70(%rbp)
mov    -0x28(%rbp),%eax
sub    -0x24(%rbp),%eax
mov    %eax,-0x64(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x70(%rbp),%rdi
mov    -0x64(%rbp),%esi
mov    -0x60(%rbp),%rdx
mov    %eax,%ecx
call   1050 <memmem@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1289 <get_kernel_addr_trusty+0x139>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xd99(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x38(%rbp),%rsi
mov    $0x10,%edx
call   1060 <strtoul@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
and    $0xffffffffff000000,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_kernel_addr_trusty>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0xe9c(%rip),%r14
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r14,%rdx
mov    %eax,%ecx
call   1050 <memmem@plt>
test   %rax,%rax
je     11ee <get_kernel_addr_trusty+0x9e>
mov    %rax,%r14
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
cmpb   $0x2d,(%r14,%rbx,1)
lea    0x1(%rbx),%rbx
jne    1190 <get_kernel_addr_trusty+0x40>
add    $0xffffffff,%ebx
lea    0xe92(%rip),%rbp
mov    %rbp,%rdi
call   1030 <strlen@plt>
mov    %r14,%rdi
mov    %ebx,%esi
mov    %rbp,%rdx
mov    %eax,%ecx
call   1050 <memmem@plt>
test   %rax,%rax
je     1207 <get_kernel_addr_trusty+0xb7>
mov    %rax,%rcx
add    $0x10,%rcx
mov    %rcx,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %rax,%rdi
mov    $0x10,%edx
call   1060 <strtoul@plt>
and    $0xffffffffff000000,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
lea    0xe11(%rip),%rsi
lea    0xdfb(%rip),%rdx
jmp    121e <get_kernel_addr_trusty+0xce>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
lea    0xdf8(%rip),%rsi
lea    0xe19(%rip),%rdx
call   1040 <fprintf@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"Freeing unused\00"
@stderr = global i32 0
@.str.1 = [40 x i8] c"[-] substring '%s' not found in syslog\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [7 x i8] c"ffffff\00"
define i64 @get_kernel_addr_trusty(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
store i8* %0, i8** %3
store i32 %1, i32* %4
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8** %5
%12 = load i8*, i8** %3
%13 = getelementptr inbounds i8, i8* %12, i64 0
%14 = load i32, i32* %4
%15 = load i8*, i8** %5
%16 = load i8*, i8** %5
%17 = call i32 @strlen(i8* %16)
%18 = call i64 @memmem(i8* %13, i32 %14, i8* %15, i32 %17)
%19 = inttoptr i64 %18 to i8*
store i8* %19, i8** %6
%20 = load i8*, i8** %6
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %28
22: ; preds = %2
%23 = load i32, i32* @stderr
%24 = load i8*, i8** %5
%25 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %2
store i32 0, i32* %7
store i32 0, i32* %8
%29 = load i32, i32* %7
store i32 %29, i32* %8
br label %30
30: ; preds = %39, %28
%31 = load i8*, i8** %6
%32 = load i32, i32* %8
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp ne i32 %36, 45
br i1 %37, label %38, label %42
38: ; preds = %30
br label %39
39: ; preds = %38
%40 = load i32, i32* %8
%41 = add nsw i32 %40, 1
store i32 %41, i32* %8
br label %30
42: ; preds = %30
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8** %9
%43 = load i8*, i8** %6
%44 = load i32, i32* %7
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %43, i64 %45
%47 = load i32, i32* %8
%48 = load i32, i32* %7
%49 = sub nsw i32 %47, %48
%50 = load i8*, i8** %9
%51 = load i8*, i8** %9
%52 = call i32 @strlen(i8* %51)
%53 = call i64 @memmem(i8* %46, i32 %49, i8* %50, i32 %52)
%54 = inttoptr i64 %53 to i8*
store i8* %54, i8** %6
%55 = load i8*, i8** %6
%56 = icmp eq i8* %55, null
br i1 %56, label %57, label %63
57: ; preds = %42
%58 = load i32, i32* @stderr
%59 = load i8*, i8** %9
%60 = call i32 @fprintf(i32 %58, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i8* %59)
%61 = load i32, i32* @EXIT_FAILURE
%62 = call i32 @exit(i32 %61)
unreachable
63: ; preds = %42
%64 = load i8*, i8** %6
%65 = getelementptr inbounds i8, i8* %64, i64 16
store i8* %65, i8** %10
%66 = load i8*, i8** %6
%67 = getelementptr inbounds i8, i8* %66, i64 0
%68 = call i64 @strtoul(i8* %67, i8** %10, i32 16)
store i64 %68, i64* %11
%69 = load i64, i64* %11
%70 = and i64 %69, -16777216
store i64 %70, i64* %11
%71 = load i64, i64* %11
ret i64 %71
}

LLVM IR (-O2):
@.str = [15 x i8] c"Freeing unused\00"
@stderr = global i32 0
@.str.1 = [40 x i8] c"[-] substring '%s' not found in syslog\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [7 x i8] c"ffffff\00"
define i64 @get_kernel_addr_trusty(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = tail call i32 @strlen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%5 = tail call i64 @memmem(i8* %0, i32 %1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = inttoptr i64 %5 to i8*
%7 = icmp eq i64 %5, 0
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %2, %13
%14 = phi i64 [ %18, %13 ], [ 0, %2 ]
%15 = getelementptr inbounds i8, i8* %6, i64 %14
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 45
%18 = add nuw i64 %14, 1
br i1 %17, label %19, label %13
19: ; preds = %13
%20 = trunc i64 %14 to i32
%21 = tail call i32 @strlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%22 = tail call i64 @memmem(i8* %6, i32 %20, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %21)
%23 = inttoptr i64 %22 to i8*
%24 = icmp eq i64 %22, 0
br i1 %24, label %25, label %30
25: ; preds = %19
%26 = load i32, i32* @stderr
%27 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = tail call i32 @exit(i32 %28)
unreachable
30: ; preds = %19
%31 = bitcast i8** %3 to i8*
%32 = getelementptr inbounds i8, i8* %23, i64 16
store i8* %32, i8** %3
%33 = call i64 @strtoul(i8* %23, i8** %3, i32 16)
%34 = and i64 %33, -16777216
ret i64 %34
}