#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_xenial(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 (start = 0; substr[start] != '-'; start++)
    ;
  for (end = start; substr[end] != '\n'; 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 &= 0xfffffffffff00000ul;
  r -= 0x1000000ul;
  return r;
}


x86 assembly (clang, -O0):
<get_kernel_addr_xenial>:
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_xenial+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)
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2d,%eax
je     120e <get_kernel_addr_xenial+0xbe>
jmp    1200 <get_kernel_addr_xenial+0xb0>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11e6 <get_kernel_addr_xenial+0x96>
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     123c <get_kernel_addr_xenial+0xec>
jmp    122e <get_kernel_addr_xenial+0xde>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1214 <get_kernel_addr_xenial+0xc4>
lea    0xdf4(%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    12b8 <get_kernel_addr_xenial+0x168>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xd6a(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2d27(%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    $0xfffffffffff00000,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
sub    $0x1000000,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_kernel_addr_xenial>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
mov    %rdi,%rbp
lea    0xe9c(%rip),%r14
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %rbp,%rdi
mov    %ebx,%esi
mov    %r14,%rdx
mov    %eax,%ecx
call   1050 <memmem@plt>
test   %rax,%rax
je     1215 <get_kernel_addr_xenial+0xc5>
mov    %rax,%rbx
mov    $0xffffffffffffffff,%rax
xchg   %ax,%ax
cmpb   $0x2d,0x1(%rbx,%rax,1)
lea    0x1(%rax),%rax
jne    1190 <get_kernel_addr_xenial+0x40>
mov    %eax,%eax
lea    (%rbx,%rax,1),%rcx
mov    $0xffffffff,%ebp
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%ebp
cmpb   $0xa,(%rcx)
lea    0x1(%rcx),%rcx
jne    11b0 <get_kernel_addr_xenial+0x60>
add    %rax,%rbx
lea    0xe71(%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     122e <get_kernel_addr_xenial+0xde>
mov    %rax,%rcx
add    $0x10,%rcx
mov    %rcx,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %rax,%rdi
mov    $0x10,%edx
call   1060 <strtoul@plt>
add    $0xffffffffff000000,%rax
and    $0xfffffffffff00000,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
lea    0xdea(%rip),%rsi
lea    0xdd4(%rip),%rdx
jmp    1245 <get_kernel_addr_xenial+0xf5>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
lea    0xdd1(%rip),%rsi
lea    0xdf2(%rip),%rdx
call   1040 <fprintf@plt>
mov    0x2d87(%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_xenial(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
store i32 0, i32* %7
br label %29
29: ; preds = %38, %28
%30 = load i8*, i8** %6
%31 = load i32, i32* %7
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp ne i32 %35, 45
br i1 %36, label %37, label %41
37: ; preds = %29
br label %38
38: ; preds = %37
%39 = load i32, i32* %7
%40 = add nsw i32 %39, 1
store i32 %40, i32* %7
br label %29
41: ; preds = %29
%42 = load i32, i32* %7
store i32 %42, i32* %8
br label %43
43: ; preds = %52, %41
%44 = load i8*, i8** %6
%45 = load i32, i32* %8
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
%48 = load i8, i8* %47
%49 = sext i8 %48 to i32
%50 = icmp ne i32 %49, 10
br i1 %50, label %51, label %55
51: ; preds = %43
br label %52
52: ; preds = %51
%53 = load i32, i32* %8
%54 = add nsw i32 %53, 1
store i32 %54, i32* %8
br label %43
55: ; preds = %43
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8** %9
%56 = load i8*, i8** %6
%57 = load i32, i32* %7
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %56, i64 %58
%60 = load i32, i32* %8
%61 = load i32, i32* %7
%62 = sub nsw i32 %60, %61
%63 = load i8*, i8** %9
%64 = load i8*, i8** %9
%65 = call i32 @strlen(i8* %64)
%66 = call i64 @memmem(i8* %59, i32 %62, i8* %63, i32 %65)
%67 = inttoptr i64 %66 to i8*
store i8* %67, i8** %6
%68 = load i8*, i8** %6
%69 = icmp eq i8* %68, null
br i1 %69, label %70, label %76
70: ; preds = %55
%71 = load i32, i32* @stderr
%72 = load i8*, i8** %9
%73 = call i32 @fprintf(i32 %71, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i8* %72)
%74 = load i32, i32* @EXIT_FAILURE
%75 = call i32 @exit(i32 %74)
unreachable
76: ; preds = %55
%77 = load i8*, i8** %6
%78 = getelementptr inbounds i8, i8* %77, i64 16
store i8* %78, i8** %10
%79 = load i8*, i8** %6
%80 = getelementptr inbounds i8, i8* %79, i64 0
%81 = call i64 @strtoul(i8* %80, i8** %10, i32 16)
store i64 %81, i64* %11
%82 = load i64, i64* %11
%83 = and i64 %82, -1048576
store i64 %83, i64* %11
%84 = load i64, i64* %11
%85 = sub i64 %84, 16777216
store i64 %85, i64* %11
%86 = load i64, i64* %11
ret i64 %86
}

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_xenial(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 = and i64 %14, 4294967295
br label %22
22: ; preds = %22, %19
%23 = phi i64 [ %27, %22 ], [ %21, %19 ]
%24 = getelementptr inbounds i8, i8* %6, i64 %23
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 10
%27 = add nuw i64 %23, 1
br i1 %26, label %28, label %22
28: ; preds = %22
%29 = getelementptr inbounds i8, i8* %6, i64 %21
%30 = trunc i64 %23 to i32
%31 = sub nsw i32 %30, %20
%32 = tail call i32 @strlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%33 = tail call i64 @memmem(i8* %29, i32 %31, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %32)
%34 = inttoptr i64 %33 to i8*
%35 = icmp eq i64 %33, 0
br i1 %35, label %36, label %41
36: ; preds = %28
%37 = load i32, i32* @stderr
%38 = tail call i32 @fprintf(i32 %37, 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))
%39 = load i32, i32* @EXIT_FAILURE
%40 = tail call i32 @exit(i32 %39)
unreachable
41: ; preds = %28
%42 = bitcast i8** %3 to i8*
%43 = getelementptr inbounds i8, i8* %34, i64 16
store i8* %43, i8** %3
%44 = call i64 @strtoul(i8* %34, i8** %3, i32 16)
%45 = add i64 %44, -16777216
%46 = and i64 %45, -1048576
ret i64 %46
}