#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(int);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ putchar(char);
int sysctlbyname(char *, char *, int *, int /*<<< orphan*/ *,
                 int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int len, i;
  char *buf, *p;
  char *mib = "kern.msgbuf";
  i = sysctlbyname(mib, NULL, &len, NULL, 0);
  if (i) {
    perror("buffer sizing");
    exit(-1);
  }
  buf = (char *)malloc(len * sizeof(char));
  i = sysctlbyname(mib, buf, &len, NULL, 0);
  if (i) {
    perror("retrieving data");
    exit(-1);
  }
  p = buf;
  i = 0;
  while (p < (buf + len)) {
    switch (*p) {
    case '\0':
      break;
    default:
      putchar(*p);
    }
    p++;
  }
  if (*--p != '\n')
    putchar('\n');
  free(buf);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe83(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x14(%rbp),%rdx
xor    %r8d,%r8d
mov    %rcx,%rsi
call   1080 <sysctlbyname@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     11bb <main+0x5b>
lea    0xe60(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    -0x14(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
xor    %r8d,%r8d
call   1080 <sysctlbyname@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1209 <main+0xa9>
lea    0xe20(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movslq -0x14(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jae    1265 <main+0x105>
mov    -0x28(%rbp),%rax
mov    (%rax),%al
test   %al,%al
jne    1248 <main+0xe8>
jmp    1243 <main+0xe3>
jmp    1254 <main+0xf4>
mov    -0x28(%rbp),%rax
movsbl (%rax),%edi
call   1040 <putchar@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1218 <main+0xb8>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x28(%rbp)
movsbl -0x1(%rax),%eax
cmp    $0xa,%eax
je     128b <main+0x12b>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
lea    0xe95(%rip),%rdi
lea    0x4(%rsp),%rdx
xor    %esi,%esi
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <sysctlbyname@plt>
test   %eax,%eax
jne    11e4 <main+0x84>
mov    0x4(%rsp),%edi
call   1050 <malloc@plt>
mov    %rax,%r14
lea    0xe6d(%rip),%rdi
lea    0x4(%rsp),%rdx
mov    %rax,%rsi
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <sysctlbyname@plt>
test   %eax,%eax
jne    11ed <main+0x8d>
mov    0x4(%rsp),%eax
mov    %r14,%rbx
test   %eax,%eax
jle    1203 <main+0xa3>
mov    %r14,%rbx
jmp    11cf <main+0x6f>
nopl   0x0(%rax)
add    $0x1,%rbx
movslq %eax,%rcx
add    %r14,%rcx
cmp    %rcx,%rbx
jae    1203 <main+0xa3>
movzbl (%rbx),%ecx
test   %cl,%cl
je     11c0 <main+0x60>
movsbl %cl,%edi
call   1040 <putchar@plt>
mov    0x4(%rsp),%eax
jmp    11c0 <main+0x60>
lea    0xe21(%rip),%rdi
jmp    11f4 <main+0x94>
lea    0xe26(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
cmpb   $0xa,-0x1(%rbx)
je     1213 <main+0xb3>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"kern.msgbuf\00"
@.str.1 = [14 x i8] c"buffer sizing\00"
@.str.2 = [16 x i8] c"retrieving data\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %10
%11 = load i8*, i8** %10
%12 = call i32 @sysctlbyname(i8* %11, i8* null, i32* %6, i32* null, i32 0)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 -1)
unreachable
18: ; preds = %2
%19 = load i32, i32* %6
%20 = sext i32 %19 to i64
%21 = mul i64 %20, 1
%22 = trunc i64 %21 to i32
%23 = call i64 @malloc(i32 %22)
%24 = inttoptr i64 %23 to i8*
store i8* %24, i8** %8
%25 = load i8*, i8** %10
%26 = load i8*, i8** %8
%27 = call i32 @sysctlbyname(i8* %25, i8* %26, i32* %6, i32* null, i32 0)
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %18
%31 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @exit(i32 -1)
unreachable
33: ; preds = %18
%34 = load i8*, i8** %8
store i8* %34, i8** %9
store i32 0, i32* %7
br label %35
35: ; preds = %51, %33
%36 = load i8*, i8** %9
%37 = load i8*, i8** %8
%38 = load i32, i32* %6
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
%41 = icmp ult i8* %36, %40
br i1 %41, label %42, label %54
42: ; preds = %35
%43 = load i8*, i8** %9
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
switch i32 %45, label %47 [
i32 0, label %46
]
46: ; preds = %42
br label %51
47: ; preds = %42
%48 = load i8*, i8** %9
%49 = load i8, i8* %48
%50 = call i32 @putchar(i8 signext %49)
br label %51
51: ; preds = %47, %46
%52 = load i8*, i8** %9
%53 = getelementptr inbounds i8, i8* %52, i32 1
store i8* %53, i8** %9
br label %35
54: ; preds = %35
%55 = load i8*, i8** %9
%56 = getelementptr inbounds i8, i8* %55, i32 -1
store i8* %56, i8** %9
%57 = load i8, i8* %56
%58 = sext i8 %57 to i32
%59 = icmp ne i32 %58, 10
br i1 %59, label %60, label %62
60: ; preds = %54
%61 = call i32 @putchar(i8 signext 10)
br label %62
62: ; preds = %60, %54
%63 = load i8*, i8** %8
%64 = call i32 @free(i8* %63)
%65 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [12 x i8] c"kern.msgbuf\00"
@.str.1 = [14 x i8] c"buffer sizing\00"
@.str.2 = [16 x i8] c"retrieving data\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = call i32 @sysctlbyname(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* null, i32* %3, i32* null, i32 0)
%6 = icmp eq i32 %5, 0
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @exit(i32 -1)
unreachable
10: ; preds = %2
%11 = load i32, i32* %3
%12 = call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i8*
%14 = call i32 @sysctlbyname(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %13, i32* %3, i32* null, i32 0)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = load i32, i32* %3
%18 = icmp sgt i32 %17, 0
br i1 %18, label %22, label %36
19: ; preds = %10
%20 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @exit(i32 -1)
unreachable
22: ; preds = %16, %30
%23 = phi i32 [ %31, %30 ], [ %17, %16 ]
%24 = phi i8* [ %32, %30 ], [ %13, %16 ]
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %22
%28 = call i32 @putchar(i8 signext %25)
%29 = load i32, i32* %3
br label %30
30: ; preds = %22, %27
%31 = phi i32 [ %23, %22 ], [ %29, %27 ]
%32 = getelementptr inbounds i8, i8* %24, i64 1
%33 = sext i32 %31 to i64
%34 = getelementptr inbounds i8, i8* %13, i64 %33
%35 = icmp ult i8* %32, %34
br i1 %35, label %22, label %36
36: ; preds = %30, %16
%37 = phi i8* [ %13, %16 ], [ %32, %30 ]
%38 = getelementptr inbounds i8, i8* %37, i64 -1
%39 = load i8, i8* %38
%40 = icmp eq i8 %39, 10
br i1 %40, label %43, label %41
41: ; preds = %36
%42 = call i32 @putchar(i8 signext 10)
br label %43
43: ; preds = %41, %36
%44 = call i32 @free(i8* %13)
%45 = call i32 @exit(i32 0)
unreachable
}