#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*/ SYS_syslog;
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ syscall(int /*<<< orphan*/, int, char *, int);
int main(int argc, char **argv) {
  int size = (int)syscall(SYS_syslog, 10, NULL, -1);
  char *buf = malloc(size);
  if (!buf) {
    perror("dmesg");
    return 1;
  }
  syscall(SYS_syslog, 2, buf, size);
  printf("%s\n", buf);
  return 0;
}


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    0x2e9b(%rip),%rax
mov    (%rax),%edi
mov    $0xa,%esi
xor    %eax,%eax
mov    %eax,%edx
mov    $0xffffffff,%ecx
call   1040 <syscall@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a4 <main+0x64>
lea    0xe6d(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11d5 <main+0x95>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
mov    -0x14(%rbp),%ecx
mov    $0x2,%esi
call   1040 <syscall@plt>
mov    -0x20(%rbp),%rsi
lea    0xe3d(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x2ea9(%rip),%r12
mov    (%r12),%edi
xor    %r14d,%r14d
mov    $0xa,%esi
xor    %edx,%edx
mov    $0xffffffff,%ecx
call   1040 <syscall@plt>
mov    %rax,%r15
mov    %r15d,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11ac <main+0x6c>
mov    %rax,%rbx
mov    (%r12),%edi
mov    $0x2,%esi
mov    %rax,%rdx
mov    %r15d,%ecx
call   1040 <syscall@plt>
lea    0xe71(%rip),%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe4d(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%r14d
jmp    119d <main+0x5d>

LLVM IR (-O0):
@SYS_syslog = global i32 0
@.str = [6 x i8] c"dmesg\00"
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* @SYS_syslog
%9 = call i64 @syscall(i32 %8, i32 10, i8* null, i32 -1)
%10 = trunc i64 %9 to i32
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = call i8* @malloc(i32 %11)
store i8* %12, i8** %7
%13 = load i8*, i8** %7
%14 = icmp ne i8* %13, null
br i1 %14, label %17, label %15
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %24
17: ; preds = %2
%18 = load i32, i32* @SYS_syslog
%19 = load i8*, i8** %7
%20 = load i32, i32* %6
%21 = call i64 @syscall(i32 %18, i32 2, i8* %19, i32 %20)
%22 = load i8*, i8** %7
%23 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %22)
store i32 0, i32* %3
br label %24
24: ; preds = %17, %15
%25 = load i32, i32* %3
ret i32 %25
}

LLVM IR (-O2):
@SYS_syslog = global i32 0
@.str = [6 x i8] c"dmesg\00"
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @SYS_syslog
%4 = tail call i64 @syscall(i32 %3, i32 10, i8* null, i32 -1)
%5 = trunc i64 %4 to i32
%6 = tail call i8* @malloc(i32 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %14
10: ; preds = %2
%11 = load i32, i32* @SYS_syslog
%12 = tail call i64 @syscall(i32 %11, i32 2, i8* %6, i32 %5)
%13 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %6)
br label %14
14: ; preds = %10, %8
%15 = phi i32 [ 0, %10 ], [ 1, %8 ]
ret i32 %15
}