#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 */
size_t atoi(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int getopt(int, char **, char *);
size_t klogctl(int, char *, size_t);
int /*<<< orphan*/ optarg;
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  size_t logleng = klogctl(10, NULL, 0);
  size_t c = 0;
  int o, clrbuf = 0;
  while ((o = getopt(argc, argv, "hcs:")) != -1)
    switch (o) {
    case 'c':
      clrbuf = 1;
      continue;
    case 's':
      logleng = atoi(optarg);
      continue;
    case 'h':
      printf("Usage:  dmesg [OPTION]\n");
      printf(" -c  clear the ring buffer\n");
      printf(" -s  buffer size (1 = 1 char)\n");
      exit(0);
    default:
      break;
    }
  char buffer[logleng];
  klogctl(3, buffer, logleng);
  while (buffer[c] != '\0') {
    printf("%c", buffer[c]);
    if (buffer[c] == '\n')
      c += 3;
    c++;
  }
  if (clrbuf == 1)
    klogctl(5, NULL, logleng);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0xa,%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1040 <klogctl@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe68(%rip),%rdx
call   1050 <getopt@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
je     123d <main+0xed>
mov    -0x24(%rbp),%eax
mov    %eax,-0x3c(%rbp)
sub    $0x63,%eax
je     11df <main+0x8f>
jmp    11bd <main+0x6d>
mov    -0x3c(%rbp),%eax
sub    $0x68,%eax
je     1202 <main+0xb2>
jmp    11ce <main+0x7e>
mov    -0x3c(%rbp),%eax
sub    $0x73,%eax
je     11eb <main+0x9b>
jmp    1233 <main+0xe3>
movl   $0x1,-0x28(%rbp)
jmp    118a <main+0x3a>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
call   1060 <atoi@plt>
mov    %rax,-0x18(%rbp)
jmp    118a <main+0x3a>
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    1238 <main+0xe8>
jmp    118a <main+0x3a>
mov    -0x18(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x48(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdx
mov    $0x3,%edi
call   1040 <klogctl@plt>
mov    -0x48(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     12d3 <main+0x183>
mov    -0x48(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%esi
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x48(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    12c2 <main+0x172>
mov    -0x20(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1272 <main+0x122>
cmpl   $0x1,-0x28(%rbp)
jne    12ef <main+0x19f>
mov    -0x18(%rbp),%rdx
mov    $0x5,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <klogctl@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%ebx
xor    %r13d,%r13d
mov    $0xa,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <klogctl@plt>
mov    %rax,%r14
lea    0xe72(%rip),%r12
xchg   %ax,%ax
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   1060 <getopt@plt>
cmp    $0x67,%eax
jg     11c0 <main+0x60>
cmp    $0xffffffff,%eax
je     11e2 <main+0x82>
cmp    $0x63,%eax
jne    1190 <main+0x30>
mov    $0x1,%r13b
jmp    1190 <main+0x30>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmp    $0x73,%eax
je     11cf <main+0x6f>
cmp    $0x68,%eax
jne    1190 <main+0x30>
jmp    1271 <main+0x111>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
call   1070 <atoi@plt>
mov    %rax,%r14
jmp    1190 <main+0x30>
mov    %rsp,%r12
lea    0xf(%r14),%rbx
and    $0xfffffffffffffff0,%rbx
mov    %r12,%r15
sub    %rbx,%r15
mov    %r15,%rsp
neg    %rbx
mov    $0x3,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1050 <klogctl@plt>
mov    (%r12,%rbx,1),%al
test   %al,%al
je     124b <main+0xeb>
xor    %ebx,%ebx
mov    %r15,%r12
cs nopw 0x0(%rax,%rax,1)
movsbl %al,%edi
call   1030 <putchar@plt>
lea    0x3(%rbx),%rax
cmpb   $0xa,(%r12)
cmovne %rbx,%rax
lea    0x1(%rax),%rbx
lea    (%r15,%rax,1),%r12
add    $0x1,%r12
movzbl 0x1(%r15,%rax,1),%eax
test   %al,%al
jne    1220 <main+0xc0>
test   $0x1,%r13b
je     1260 <main+0x100>
mov    $0x5,%edi
xor    %esi,%esi
mov    %r14,%rdx
call   1050 <klogctl@plt>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd8d(%rip),%rdi
call   1040 <puts@plt>
lea    0xd98(%rip),%rdi
call   1040 <puts@plt>
lea    0xda7(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"hcs:\00"
@optarg = global i32 0
@.str.1 = [24 x i8] c"Usage: dmesg [OPTION]\0A\00"
@.str.2 = [28 x i8] c" -c clear the ring buffer\0A\00"
@.str.3 = [31 x i8] c" -s buffer size (1 = 1 char)\0A\00"
@.str.4 = [3 x i8] c"%c\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = call i64 @klogctl(i32 10, i8* null, i64 0)
store i64 %12, i64* %6
store i64 0, i64* %7
store i32 0, i32* %9
br label %13
13: ; preds = %30, %21, %20, %2
%14 = load i32, i32* %4
%15 = load i8**, i8*** %5
%16 = call i32 @getopt(i32 %14, i8** %15, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 %16, i32* %8
%17 = icmp ne i32 %16, -1
br i1 %17, label %18, label %31
18: ; preds = %13
%19 = load i32, i32* %8
switch i32 %19, label %29 [
i32 99, label %20
i32 115, label %21
i32 104, label %24
]
20: ; preds = %18
store i32 1, i32* %9
br label %13
21: ; preds = %18
%22 = load i32, i32* @optarg
%23 = call i64 @atoi(i32 %22)
store i64 %23, i64* %6
br label %13
24: ; preds = %18
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @exit(i32 0)
unreachable
29: ; preds = %18
br label %30
30: ; preds = %29
br label %13
31: ; preds = %13
%32 = load i64, i64* %6
%33 = call i8* @llvm.stacksave()
store i8* %33, i8** %10
%34 = alloca i8, i64 %32
store i64 %32, i64* %11
%35 = load i64, i64* %6
%36 = call i64 @klogctl(i32 3, i8* %34, i64 %35)
br label %37
37: ; preds = %57, %31
%38 = load i64, i64* %7
%39 = getelementptr inbounds i8, i8* %34, i64 %38
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %60
43: ; preds = %37
%44 = load i64, i64* %7
%45 = getelementptr inbounds i8, i8* %34, i64 %44
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %47)
%49 = load i64, i64* %7
%50 = getelementptr inbounds i8, i8* %34, i64 %49
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = icmp eq i32 %52, 10
br i1 %53, label %54, label %57
54: ; preds = %43
%55 = load i64, i64* %7
%56 = add i64 %55, 3
store i64 %56, i64* %7
br label %57
57: ; preds = %54, %43
%58 = load i64, i64* %7
%59 = add i64 %58, 1
store i64 %59, i64* %7
br label %37
60: ; preds = %37
%61 = load i32, i32* %9
%62 = icmp eq i32 %61, 1
br i1 %62, label %63, label %66
63: ; preds = %60
%64 = load i64, i64* %6
%65 = call i64 @klogctl(i32 5, i8* null, i64 %64)
br label %66
66: ; preds = %63, %60
store i32 0, i32* %3
%67 = load i8*, i8** %10
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
@.str = [5 x i8] c"hcs:\00"
@optarg = global i32 0
@str = [23 x i8] c"Usage: dmesg [OPTION]\00"
@str.5 = [27 x i8] c" -c clear the ring buffer\00"
@str.6 = [30 x i8] c" -s buffer size (1 = 1 char)\00"
define i32 @main(i32 %0, i8** %1) {
%3 = tail call i64 @klogctl(i32 10, i8* null, i64 0)
br label %4
4: ; preds = %9, %2
%5 = phi i1 [ false, %2 ], [ true, %9 ]
%6 = phi i64 [ %3, %2 ], [ %8, %9 ]
br label %7
7: ; preds = %4, %11
%8 = phi i64 [ %6, %4 ], [ %13, %11 ]
br label %9
9: ; preds = %7, %9
%10 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
switch i32 %10, label %9 [
i32 -1, label %19
i32 99, label %4
i32 115, label %11
i32 104, label %14
]
11: ; preds = %9
%12 = load i32, i32* @optarg
%13 = tail call i64 @atoi(i32 %12)
br label %7
14: ; preds = %9
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.5, i64 0, i64 0))
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.6, i64 0, i64 0))
%18 = tail call i32 @exit(i32 0)
unreachable
19: ; preds = %9
%20 = alloca i8, i64 %8
%21 = call i64 @klogctl(i32 3, i8* %20, i64 %8)
%22 = load i8, i8* %20
%23 = icmp eq i8 %22, 0
br i1 %23, label %38, label %24
24: ; preds = %19, %24
%25 = phi i8 [ %36, %24 ], [ %22, %19 ]
%26 = phi i8* [ %35, %24 ], [ %20, %19 ]
%27 = phi i64 [ %34, %24 ], [ 0, %19 ]
%28 = sext i8 %25 to i32
%29 = call i32 @putchar(i32 %28)
%30 = load i8, i8* %26
%31 = icmp eq i8 %30, 10
%32 = add i64 %27, 3
%33 = select i1 %31, i64 %32, i64 %27
%34 = add i64 %33, 1
%35 = getelementptr inbounds i8, i8* %20, i64 %34
%36 = load i8, i8* %35
%37 = icmp eq i8 %36, 0
br i1 %37, label %38, label %24
38: ; preds = %24, %19
br i1 %5, label %39, label %41
39: ; preds = %38
%40 = call i64 @klogctl(i32 5, i8* null, i64 %8)
br label %41
41: ; preds = %39, %38
ret i32 0
}