#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 MAP_ANONYMOUS;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int getpagesize();
int klogctl(int, char *, int);
scalar_t__ mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
void mmap_syslog(char **buffer, int *size) {
  *size = klogctl(10, 0, 0);
  if (*size == -1) {
    perror("[-] klogctl(SYSLOG_ACTION_SIZE_BUFFER)");
    exit(EXIT_FAILURE);
  }
  *size = (*size / getpagesize() + 1) * getpagesize();
  *buffer = (char *)mmap(NULL, *size, PROT_READ | PROT_WRITE,
                         MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  *size = klogctl(3, &((*buffer)[0]), *size);
  if (*size == -1) {
    perror("[-] klogctl(SYSLOG_ACTION_READ_ALL)");
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<mmap_syslog>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0xa,%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1040 <klogctl@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0xffffffff,(%rax)
jne    119f <mmap_syslog+0x4f>
lea    0xe74(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1060 <getpagesize@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
cltd
idiv   %ecx
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1060 <getpagesize@plt>
mov    -0x14(%rbp),%ecx
imul   %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
mov    0x2de2(%rip),%rax
or     (%rax),%edx
mov    0x2dd1(%rip),%rax
mov    (%rax),%ecx
mov    0x2de8(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
mov    $0x3,%edi
call   1040 <klogctl@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0xffffffff,(%rax)
jne    125c <mmap_syslog+0x10c>
lea    0xdde(%rip),%rdi
call   1050 <perror@plt>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mmap_syslog>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
mov    $0xa,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <klogctl@plt>
mov    %eax,(%rbx)
cmp    $0xffffffff,%eax
je     11e2 <mmap_syslog+0x92>
mov    %eax,%ebp
xor    %eax,%eax
call   1060 <getpagesize@plt>
mov    %eax,%ecx
mov    %ebp,%eax
cltd
idiv   %ecx
lea    0x1(%rax),%ebp
xor    %eax,%eax
call   1060 <getpagesize@plt>
imul   %eax,%ebp
mov    %ebp,(%rbx)
mov    0x2e63(%rip),%rax
mov    0x2e2c(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e19(%rip),%rax
mov    0x2e32(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,(%r14)
mov    (%rbx),%edx
mov    $0x3,%edi
mov    %rax,%rsi
call   1040 <klogctl@plt>
mov    %eax,(%rbx)
cmp    $0xffffffff,%eax
je     11eb <mmap_syslog+0x9b>
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe17(%rip),%rdi
jmp    11f2 <mmap_syslog+0xa2>
lea    0xe35(%rip),%rdi
call   1050 <perror@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [39 x i8] c"[-] klogctl(SYSLOG_ACTION_SIZE_BUFFER)\00"
@EXIT_FAILURE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@.str.1 = [36 x i8] c"[-] klogctl(SYSLOG_ACTION_READ_ALL)\00"
define void @mmap_syslog(i8** %0, i32* %1) {
%3 = alloca i8**
%4 = alloca i32*
store i8** %0, i8*** %3
store i32* %1, i32** %4
%5 = call i32 @klogctl(i32 10, i8* null, i32 0)
%6 = load i32*, i32** %4
store i32 %5, i32* %6
%7 = load i32*, i32** %4
%8 = load i32, i32* %7
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %2
%15 = load i32*, i32** %4
%16 = load i32, i32* %15
%17 = call i32 (...) @getpagesize()
%18 = sdiv i32 %16, %17
%19 = add nsw i32 %18, 1
%20 = call i32 (...) @getpagesize()
%21 = mul nsw i32 %19, %20
%22 = load i32*, i32** %4
store i32 %21, i32* %22
%23 = load i32*, i32** %4
%24 = load i32, i32* %23
%25 = load i32, i32* @PROT_READ
%26 = load i32, i32* @PROT_WRITE
%27 = or i32 %25, %26
%28 = load i32, i32* @MAP_PRIVATE
%29 = load i32, i32* @MAP_ANONYMOUS
%30 = or i32 %28, %29
%31 = call i64 @mmap(i32* null, i32 %24, i32 %27, i32 %30, i32 -1, i32 0)
%32 = inttoptr i64 %31 to i8*
%33 = load i8**, i8*** %3
store i8* %32, i8** %33
%34 = load i8**, i8*** %3
%35 = load i8*, i8** %34
%36 = getelementptr inbounds i8, i8* %35, i64 0
%37 = load i32*, i32** %4
%38 = load i32, i32* %37
%39 = call i32 @klogctl(i32 3, i8* %36, i32 %38)
%40 = load i32*, i32** %4
store i32 %39, i32* %40
%41 = load i32*, i32** %4
%42 = load i32, i32* %41
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %48
44: ; preds = %14
%45 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%46 = load i32, i32* @EXIT_FAILURE
%47 = call i32 @exit(i32 %46)
unreachable
48: ; preds = %14
ret void
}

LLVM IR (-O2):
@.str = [39 x i8] c"[-] klogctl(SYSLOG_ACTION_SIZE_BUFFER)\00"
@EXIT_FAILURE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@.str.1 = [36 x i8] c"[-] klogctl(SYSLOG_ACTION_READ_ALL)\00"
define void @mmap_syslog(i8** nocapture writeonly %0, i32* nocapture %1) {
%3 = tail call i32 @klogctl(i32 10, i8* null, i32 0)
store i32 %3, i32* %1
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %2
%10 = tail call i32 (...) @getpagesize()
%11 = sdiv i32 %3, %10
%12 = add nsw i32 %11, 1
%13 = tail call i32 (...) @getpagesize()
%14 = mul nsw i32 %12, %13
store i32 %14, i32* %1
%15 = load i32, i32* @PROT_READ
%16 = load i32, i32* @PROT_WRITE
%17 = or i32 %16, %15
%18 = load i32, i32* @MAP_PRIVATE
%19 = load i32, i32* @MAP_ANONYMOUS
%20 = or i32 %19, %18
%21 = tail call i64 @mmap(i32* null, i32 %14, i32 %17, i32 %20, i32 -1, i32 0)
%22 = inttoptr i64 %21 to i8*
store i8* %22, i8** %0
%23 = load i32, i32* %1
%24 = tail call i32 @klogctl(i32 3, i8* %22, i32 %23)
store i32 %24, i32* %1
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %30
26: ; preds = %9
%27 = tail call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = tail call i32 @exit(i32 %28)
unreachable
30: ; preds = %9
ret void
}