#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*/ uint64_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fscanf(int /*<<< orphan*/ *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
uint64_t read_filter_addr_from_dmesg() {
  uint64_t addr;
  FILE *f = popen(
      "dmesg | perl -lne 'print $1 if m/ image=([0-9a-f]+)$/' | tail -n 1",
      "r");
  if (!f || (fscanf(f, "%lx", &addr) < 1))
    do {
      printf("[-] "
             "could not find filter code address"
             "\n");
      exit(57);
    } while (0);
  fclose(f);
  printf("[+] "
         "found filter at %lx"
         "\n",
         addr);
  return addr;
}


x86 assembly (clang, -O0):
<read_filter_addr_from_dmesg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xea1(%rip),%rdi
lea    0xedd(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1197 <read_filter_addr_from_dmesg+0x47>
mov    -0x10(%rbp),%rdi
lea    0xec0(%rip),%rsi
lea    -0x4(%rbp),%rdx
call   1050 <fscanf@plt>
cmp    $0x1,%eax
jge    11b4 <read_filter_addr_from_dmesg+0x64>
jmp    119c <read_filter_addr_from_dmesg+0x4c>
lea    0xea6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x39,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x4(%rbp),%esi
lea    0xeaa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_filter_addr_from_dmesg>:
push   %rbx
sub    $0x10,%rsp
lea    0xe94(%rip),%rdi
lea    0xed0(%rip),%rsi
call   1070 <popen@plt>
test   %rax,%rax
je     11bc <read_filter_addr_from_dmesg+0x5c>
mov    %rax,%rbx
lea    0xebe(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1060 <fscanf@plt>
test   %eax,%eax
jle    11bc <read_filter_addr_from_dmesg+0x5c>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0xc(%rsp),%esi
lea    0xe9e(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0xc(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe9f(%rip),%rdi
call   1030 <puts@plt>
mov    $0x39,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [67 x i8] c"dmesg | perl -lne 'print $1 if m/ image=([0-9a-f]+)$/' | tail -n 1\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [4 x i8] c"%lx\00"
@.str.3 = [40 x i8] c"[-] could not find filter code address\0A\00"
@.str.4 = [25 x i8] c"[+] found filter at %lx\0A\00"
define i32 @read_filter_addr_from_dmesg() {
%1 = alloca i32
%2 = alloca i32*
%3 = call i32* @popen(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %3, i32** %2
%4 = load i32*, i32** %2
%5 = icmp ne i32* %4, null
br i1 %5, label %6, label %10
6: ; preds = %0
%7 = load i32*, i32** %2
%8 = call i32 @fscanf(i32* %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%9 = icmp slt i32 %8, 1
br i1 %9, label %10, label %15
10: ; preds = %6, %0
br label %11
11: ; preds = %10
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0))
%13 = call i32 @exit(i32 57)
unreachable
14: ; No predecessors!
br label %15
15: ; preds = %14, %6
%16 = load i32*, i32** %2
%17 = call i32 @fclose(i32* %16)
%18 = load i32, i32* %1
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %18)
%20 = load i32, i32* %1
ret i32 %20
}

LLVM IR (-O2):
@.str = [67 x i8] c"dmesg | perl -lne 'print $1 if m/ image=([0-9a-f]+)$/' | tail -n 1\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [4 x i8] c"%lx\00"
@.str.4 = [25 x i8] c"[+] found filter at %lx\0A\00"
@str = [39 x i8] c"[-] could not find filter code address\00"
define i32 @read_filter_addr_from_dmesg() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32* @popen(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %8, label %5
5: ; preds = %0
%6 = call i32 @fscanf(i32* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%7 = icmp slt i32 %6, 1
br i1 %7, label %8, label %11
8: ; preds = %0, %5
%9 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0))
%10 = call i32 @exit(i32 57)
unreachable
11: ; preds = %5
%12 = call i32 @fclose(i32* %3)
%13 = load i32, i32* %1
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %13)
%15 = load i32, i32* %1
ret i32 %15
}