#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ getline(char **, size_t *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sscanf(char *, char *, char *, int *);
int main(int argc, char *argv[]) {
  char name[128];
  int value = 0;
  char *buff = NULL;
  size_t len = 0;
  FILE *fp = fopen(argv[1], "r");
  if (!fp)
    return -1;
  printf("/* autogenerated by mkparser - DO NOT EDIT */\n\n");
  printf("#include \"esekey.h\"\n\n");
  printf("char*\n");
  printf("parse(struct input_event ev)\n");
  printf("{\n");
  printf("    static char *ret_val;\n\n");
  printf("    if (ev.type == EV_KEY) {\n\n");
  printf("        switch (ev.code) {\n\n");
  while (!feof(fp)) {
    getline(&buff, &len, fp);
    sscanf(buff, "#define KEY_%s %i", name, &value);
    if (value) {
      printf("            case %i:\n", value);
      printf("                return \"%s\";\n", name);
      value = 0;
    }
  }
  printf("            default:\n");
  printf("                if (!ret_val) ret_val = malloc (12);\n");
  printf("                sprintf(ret_val, \"KEY_%%d\", ev.code);\n");
  printf("                return ret_val;\n");
  printf("\n        }\n\n");
  printf("    }\n\n");
  printf("    return NULL;\n\n");
  printf("}\n");
  printf("\n");
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x94(%rbp)
movq   $0x0,-0xa0(%rbp)
movq   $0x0,-0xa8(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe58(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0xb0(%rbp)
cmpq   $0x0,-0xb0(%rbp)
jne    11ce <main+0x6e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1370 <main+0x210>
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe4f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe50(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xb0(%rbp),%rdi
call   1050 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    125f <main+0xff>
jmp    12df <main+0x17f>
mov    -0xb0(%rbp),%rdx
lea    -0xa0(%rbp),%rdi
lea    -0xa8(%rbp),%rsi
call   1080 <getline@plt>
mov    -0xa0(%rbp),%rdi
lea    -0x90(%rbp),%rdx
lea    0xe3a(%rip),%rsi
lea    -0x94(%rbp),%rcx
call   1060 <sscanf@plt>
cmpl   $0x0,-0x94(%rbp)
je     12da <main+0x17a>
mov    -0x94(%rbp),%esi
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x90(%rbp),%rsi
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x94(%rbp)
jmp    123e <main+0xde>
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe81(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe94(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe8b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe85(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe8a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xcf0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xb0(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xa8,%rsp
movl   $0x0,0xc(%rsp)
movq   $0x0,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x8(%rsi),%rdi
lea    0xe4a(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     12af <main+0x12f>
mov    %rax,%rbx
lea    0xeb1(%rip),%rdi
call   1040 <puts@plt>
lea    0xed4(%rip),%rdi
call   1040 <puts@plt>
lea    0xedd(%rip),%rdi
call   1040 <puts@plt>
lea    0xed7(%rip),%rdi
call   1040 <puts@plt>
lea    0xee8(%rip),%rdi
call   1040 <puts@plt>
lea    0xede(%rip),%rdi
call   1040 <puts@plt>
lea    0xeed(%rip),%rdi
call   1040 <puts@plt>
lea    0xeff(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %eax,%eax
je     12c8 <main+0x148>
lea    0xeff(%rip),%rdi
call   1040 <puts@plt>
lea    0xf08(%rip),%rdi
call   1040 <puts@plt>
lea    0xdf2(%rip),%rdi
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xf21(%rip),%rdi
call   1040 <puts@plt>
lea    0xf35(%rip),%rdi
call   1040 <puts@plt>
lea    0xf2e(%rip),%rdi
call   1040 <puts@plt>
lea    0xf29(%rip),%rdi
call   1040 <puts@plt>
lea    0xf2f(%rip),%rdi
call   1040 <puts@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    12b4 <main+0x134>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0xa8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x10(%rsp),%r14
lea    0x18(%rsp),%r15
lea    0xd29(%rip),%r12
lea    0x20(%rsp),%r13
lea    0xc(%rsp),%rbp
jmp    1300 <main+0x180>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %eax,%eax
jne    1237 <main+0xb7>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   10a0 <getline@plt>
mov    0x10(%rsp),%rdi
mov    %r12,%rsi
mov    %r13,%rdx
mov    %rbp,%rcx
call   1080 <sscanf@plt>
mov    0xc(%rsp),%esi
test   %esi,%esi
je     12f0 <main+0x170>
lea    0xce4(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xcec(%rip),%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
movl   $0x0,0xc(%rsp)
jmp    12f0 <main+0x170>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [48 x i8] c"/* autogenerated by mkparser - DO NOT EDIT */\0A\0A\00"
@.str.2 = [22 x i8] c"#include \22esekey.h\22\0A\0A\00"
@.str.3 = [7 x i8] c"char*\0A\00"
@.str.4 = [30 x i8] c"parse(struct input_event ev)\0A\00"
@.str.5 = [3 x i8] c"{\0A\00"
@.str.6 = [28 x i8] c" static char *ret_val;\0A\0A\00"
@.str.7 = [31 x i8] c" if (ev.type == EV_KEY) {\0A\0A\00"
@.str.8 = [29 x i8] c" switch (ev.code) {\0A\0A\00"
@.str.9 = [18 x i8] c"#define KEY_%s %i\00"
@.str.10 = [22 x i8] c" case %i:\0A\00"
@.str.11 = [30 x i8] c" return \22%s\22;\0A\00"
@.str.12 = [22 x i8] c" default:\0A\00"
@.str.13 = [54 x i8] c" if (!ret_val) ret_val = malloc (12);\0A\00"
@.str.14 = [55 x i8] c" sprintf(ret_val, \22KEY_%%d\22, ev.code);\0A\00"
@.str.15 = [33 x i8] c" return ret_val;\0A\00"
@.str.16 = [13 x i8] c"\0A }\0A\0A\00"
@.str.17 = [8 x i8] c" }\0A\0A\00"
@.str.18 = [19 x i8] c" return NULL;\0A\0A\00"
@.str.19 = [3 x i8] c"}\0A\00"
@.str.20 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [128 x i8]
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
store i8* null, i8** %8
store i64 0, i64* %9
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %10
%15 = load i32*, i32** %10
%16 = icmp ne i32* %15, null
br i1 %16, label %18, label %17
17: ; preds = %2
store i32 -1, i32* %3
br label %58
18: ; preds = %2
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0))
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.8, i64 0, i64 0))
br label %27
27: ; preds = %45, %18
%28 = load i32*, i32** %10
%29 = call i32 @feof(i32* %28)
%30 = icmp ne i32 %29, 0
%31 = xor i1 %30, true
br i1 %31, label %32, label %46
32: ; preds = %27
%33 = load i32*, i32** %10
%34 = call i32 @getline(i8** %8, i64* %9, i32* %33)
%35 = load i8*, i8** %8
%36 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%37 = call i32 @sscanf(i8* %35, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.9, i64 0, i64 0), i8* %36, i32* %7)
%38 = load i32, i32* %7
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %32
%41 = load i32, i32* %7
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.10, i64 0, i64 0), i32 %41)
%43 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.11, i64 0, i64 0), i8* %43)
store i32 0, i32* %7
br label %45
45: ; preds = %40, %32
br label %27
46: ; preds = %27
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.12, i64 0, i64 0))
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.13, i64 0, i64 0))
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.14, i64 0, i64 0))
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.15, i64 0, i64 0))
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.16, i64 0, i64 0))
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.17, i64 0, i64 0))
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.18, i64 0, i64 0))
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.19, i64 0, i64 0))
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.20, i64 0, i64 0))
%56 = load i32*, i32** %10
%57 = call i32 @fclose(i32* %56)
store i32 0, i32* %3
br label %58
58: ; preds = %46, %17
%59 = load i32, i32* %3
ret i32 %59
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.9 = [18 x i8] c"#define KEY_%s %i\00"
@.str.10 = [22 x i8] c" case %i:\0A\00"
@.str.11 = [30 x i8] c" return \22%s\22;\0A\00"
@.str.14 = [55 x i8] c" sprintf(ret_val, \22KEY_%%d\22, ev.code);\0A\00"
@str = [47 x i8] c"/* autogenerated by mkparser - DO NOT EDIT */\0A\00"
@str.21 = [21 x i8] c"#include \22esekey.h\22\0A\00"
@str.22 = [6 x i8] c"char*\00"
@str.23 = [29 x i8] c"parse(struct input_event ev)\00"
@str.24 = [2 x i8] c"{\00"
@str.25 = [27 x i8] c" static char *ret_val;\0A\00"
@str.26 = [30 x i8] c" if (ev.type == EV_KEY) {\0A\00"
@str.27 = [28 x i8] c" switch (ev.code) {\0A\00"
@str.28 = [21 x i8] c" default:\00"
@str.29 = [53 x i8] c" if (!ret_val) ret_val = malloc (12);\00"
@str.30 = [32 x i8] c" return ret_val;\00"
@str.31 = [12 x i8] c"\0A }\0A\00"
@str.32 = [7 x i8] c" }\0A\00"
@str.33 = [18 x i8] c" return NULL;\0A\00"
@str.34 = [2 x i8] c"}\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [128 x i8]
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%8 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%9 = bitcast i8** %5 to i8*
store i8* null, i8** %5
%10 = bitcast i64* %6 to i8*
store i64 0, i64* %6
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %49, label %15
15: ; preds = %2
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str, i64 0, i64 0))
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.21, i64 0, i64 0))
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.22, i64 0, i64 0))
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.23, i64 0, i64 0))
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str.24, i64 0, i64 0))
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.25, i64 0, i64 0))
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.26, i64 0, i64 0))
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.27, i64 0, i64 0))
%24 = call i32 @feof(i32* %13)
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %38
26: ; preds = %15, %35
%27 = call i32 @getline(i8** %5, i64* %6, i32* %13)
%28 = load i8*, i8** %5
%29 = call i32 @sscanf(i8* %28, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.9, i64 0, i64 0), i8* %7, i32* %4)
%30 = load i32, i32* %4
%31 = icmp eq i32 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %26
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.10, i64 0, i64 0), i32 %30)
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.11, i64 0, i64 0), i8* %7)
store i32 0, i32* %4
br label %35
35: ; preds = %32, %26
%36 = call i32 @feof(i32* %13)
%37 = icmp eq i32 %36, 0
br i1 %37, label %26, label %38
38: ; preds = %35, %15
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.28, i64 0, i64 0))
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str.29, i64 0, i64 0))
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @.str.14, i64 0, i64 0))
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.30, i64 0, i64 0))
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.31, i64 0, i64 0))
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.32, i64 0, i64 0))
%45 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.33, i64 0, i64 0))
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str.34, i64 0, i64 0))
%47 = call i32 @putchar(i32 10)
%48 = call i32 @fclose(i32* %13)
br label %49
49: ; preds = %2, %38
%50 = phi i32 [ 0, %38 ], [ -1, %2 ]
ret i32 %50
}