#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 */
char *calloc(int, int);
int /*<<< orphan*/ free(char *);
scalar_t__ klogctl(int, char *, int);
char *klog_read(void) {
  int len;
  char *klog;
  if ((len = klogctl(10, NULL, 0)) < 0)
    return NULL;
  if ((klog = calloc(1, len)) == NULL)
    return NULL;
  if (klogctl(3, klog, len) < 0) {
    free(klog);
    return NULL;
  }
  return klog;
}


x86 assembly (clang, -O0):
<klog_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0xa,%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <klogctl@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1161 <klog_read+0x31>
movq   $0x0,-0x8(%rbp)
jmp    11c2 <klog_read+0x92>
mov    -0xc(%rbp),%esi
mov    $0x1,%edi
call   1040 <calloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1189 <klog_read+0x59>
movq   $0x0,-0x8(%rbp)
jmp    11c2 <klog_read+0x92>
mov    -0x18(%rbp),%rsi
mov    -0xc(%rbp),%edx
mov    $0x3,%edi
call   1050 <klogctl@plt>
cmp    $0x0,%rax
jge    11ba <klog_read+0x8a>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11c2 <klog_read+0x92>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<klog_read>:
push   %r14
push   %rbx
push   %rax
xor    %r14d,%r14d
mov    $0xa,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <klogctl@plt>
mov    %rax,%rbx
test   %ebx,%ebx
js     117f <klog_read+0x4f>
mov    $0x1,%edi
mov    %ebx,%esi
call   1040 <calloc@plt>
test   %rax,%rax
je     117c <klog_read+0x4c>
mov    %rax,%r14
mov    $0x3,%edi
mov    %rax,%rsi
mov    %ebx,%edx
call   1050 <klogctl@plt>
test   %rax,%rax
jns    117f <klog_read+0x4f>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @klog_read() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i8*
%4 = call i64 @klogctl(i32 10, i8* null, i32 0)
%5 = trunc i64 %4 to i32
store i32 %5, i32* %2
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %8
7: ; preds = %0
store i8* null, i8** %1
br label %23
8: ; preds = %0
%9 = load i32, i32* %2
%10 = call i8* @calloc(i32 1, i32 %9)
store i8* %10, i8** %3
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %13
12: ; preds = %8
store i8* null, i8** %1
br label %23
13: ; preds = %8
%14 = load i8*, i8** %3
%15 = load i32, i32* %2
%16 = call i64 @klogctl(i32 3, i8* %14, i32 %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i8*, i8** %3
%20 = call i32 @free(i8* %19)
store i8* null, i8** %1
br label %23
21: ; preds = %13
%22 = load i8*, i8** %3
store i8* %22, i8** %1
br label %23
23: ; preds = %21, %18, %12, %7
%24 = load i8*, i8** %1
ret i8* %24
}

LLVM IR (-O2):
define i8* @klog_read() {
%1 = tail call i64 @klogctl(i32 10, i8* null, i32 0)
%2 = trunc i64 %1 to i32
%3 = icmp slt i32 %2, 0
br i1 %3, label %12, label %4
4: ; preds = %0
%5 = tail call i8* @calloc(i32 1, i32 %2)
%6 = icmp eq i8* %5, null
br i1 %6, label %12, label %7
7: ; preds = %4
%8 = tail call i64 @klogctl(i32 3, i8* %5, i32 %2)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 @free(i8* %5)
br label %12
12: ; preds = %7, %4, %0, %10
%13 = phi i8* [ null, %10 ], [ null, %0 ], [ null, %4 ], [ %5, %7 ]
ret i8* %13
}