#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 */
struct pevent {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ pevent_register_print_string(struct pevent *, char *,
                                                unsigned long long);
char *strdup(char *);
char *strtok_r(char *, char *, char **);
unsigned long long strtoull(char *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ warning(char *);
void parse_ftrace_printk(struct pevent *pevent, char *file,
                         unsigned int size __attribute__((unused))) {
  unsigned long long addr;
  char *printk;
  char *line;
  char *next = NULL;
  char *addr_str;
  char *fmt;
  line = strtok_r(file, "\n", &next);
  while (line) {
    addr_str = strtok_r(line, ":", &fmt);
    if (!addr_str) {
      warning("printk format with empty entry");
      break;
    }
    addr = strtoull(addr_str, NULL, 16);
    printk = strdup(fmt + 1);
    line = strtok_r(NULL, "\n", &next);
    pevent_register_print_string(pevent, printk, addr);
    free(printk);
  }
}


x86 assembly (clang, -O0):
<parse_ftrace_printk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe7a(%rip),%rsi
lea    -0x38(%rbp),%rdx
call   1050 <strtok_r@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1230 <parse_ftrace_printk+0xd0>
mov    -0x30(%rbp),%rdi
lea    0xe59(%rip),%rsi
lea    -0x48(%rbp),%rdx
call   1050 <strtok_r@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11d2 <parse_ftrace_printk+0x72>
lea    0xe3c(%rip),%rdi
call   1040 <warning@plt>
jmp    1230 <parse_ftrace_printk+0xd0>
mov    -0x40(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1060 <strtoull@plt>
mov    %rax,-0x20(%rbp)
mov    -0x48(%rbp),%rdi
add    $0x1,%rdi
call   1080 <strdup@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xdfc(%rip),%rsi
lea    -0x38(%rbp),%rdx
call   1050 <strtok_r@plt>
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <pevent_register_print_string@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    1193 <parse_ftrace_printk+0x33>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_ftrace_printk>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rax
mov    %rdi,%rbx
movq   $0x0,0x8(%rsp)
lea    0xe7c(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
call   1050 <strtok_r@plt>
test   %rax,%rax
je     1225 <parse_ftrace_printk+0xc5>
mov    %rax,%rbp
lea    0xe5c(%rip),%r12
lea    0x8(%rsp),%r13
nopl   0x0(%rax)
mov    %rbp,%rdi
lea    0xe48(%rip),%rsi
lea    0x10(%rsp),%rdx
call   1050 <strtok_r@plt>
test   %rax,%rax
je     1219 <parse_ftrace_printk+0xb9>
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <strtoull@plt>
mov    %rax,%r14
mov    0x10(%rsp),%rdi
add    $0x1,%rdi
call   1080 <strdup@plt>
mov    %rax,%r15
xor    %edi,%edi
mov    %r12,%rsi
mov    %r13,%rdx
call   1050 <strtok_r@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1070 <pevent_register_print_string@plt>
mov    %r15,%rdi
call   1030 <free@plt>
test   %rbp,%rbp
jne    11b0 <parse_ftrace_printk+0x50>
jmp    1225 <parse_ftrace_printk+0xc5>
lea    0xde4(%rip),%rdi
call   1040 <warning@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pevent = type { i32 }
@.str = [2 x i8] c"\0A\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [31 x i8] c"printk format with empty entry\00"
define void @parse_ftrace_printk(%struct.pevent* %0, i8* %1, i32 %2) {
%4 = alloca %struct.pevent*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
store %struct.pevent* %0, %struct.pevent** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i8* null, i8** %10
%13 = load i8*, i8** %5
%14 = call i8* @strtok_r(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %10)
store i8* %14, i8** %9
br label %15
15: ; preds = %25, %3
%16 = load i8*, i8** %9
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %38
18: ; preds = %15
%19 = load i8*, i8** %9
%20 = call i8* @strtok_r(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %12)
store i8* %20, i8** %11
%21 = load i8*, i8** %11
%22 = icmp ne i8* %21, null
br i1 %22, label %25, label %23
23: ; preds = %18
%24 = call i32 @warning(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %38
25: ; preds = %18
%26 = load i8*, i8** %11
%27 = call i64 @strtoull(i8* %26, i32* null, i32 16)
store i64 %27, i64* %7
%28 = load i8*, i8** %12
%29 = getelementptr inbounds i8, i8* %28, i64 1
%30 = call i8* @strdup(i8* %29)
store i8* %30, i8** %8
%31 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %10)
store i8* %31, i8** %9
%32 = load %struct.pevent*, %struct.pevent** %4
%33 = load i8*, i8** %8
%34 = load i64, i64* %7
%35 = call i32 @pevent_register_print_string(%struct.pevent* %32, i8* %33, i64 %34)
%36 = load i8*, i8** %8
%37 = call i32 @free(i8* %36)
br label %15
38: ; preds = %23, %15
ret void
}

LLVM IR (-O2):
%struct.pevent = type { i32 }
@.str = [2 x i8] c"\0A\00"
@.str.1 = [2 x i8] c":\00"
@.str.2 = [31 x i8] c"printk format with empty entry\00"
define void @parse_ftrace_printk(%struct.pevent* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = bitcast i8** %4 to i8*
store i8* null, i8** %4
%7 = bitcast i8** %5 to i8*
%8 = call i8* @strtok_r(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %4)
%9 = icmp eq i8* %8, null
br i1 %9, label %25, label %10
10: ; preds = %3, %16
%11 = phi i8* [ %21, %16 ], [ %8, %3 ]
%12 = call i8* @strtok_r(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %5)
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = call i32 @warning(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %25
16: ; preds = %10
%17 = call i64 @strtoull(i8* nocapture %12, i32* null, i32 16)
%18 = load i8*, i8** %5
%19 = getelementptr inbounds i8, i8* %18, i64 1
%20 = call i8* @strdup(i8* %19)
%21 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %4)
%22 = call i32 @pevent_register_print_string(%struct.pevent* %0, i8* %20, i64 %17)
%23 = call i32 @free(i8* %20)
%24 = icmp eq i8* %21, null
br i1 %24, label %25, label %10
25: ; preds = %16, %3, %14
ret void
}