#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 *malloc(scalar_t__);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strlen(char const *);
int /*<<< orphan*/ strncat(char *, char const *, int);
scalar_t__ strncmp(char const *, char *, int);
char *gui_keyboard_get_internal_code(const char *key) {
  char *result;
  if ((result = malloc(strlen(key) + 1))) {
    result[0] = '\0';
    while (key[0]) {
      if (strncmp(key, "meta2-", 6) == 0) {
        strcat(result, "^[[");
        key += 6;
      }
      if (strncmp(key, "meta-", 5) == 0) {
        strcat(result, "^[");
        key += 5;
      } else if (strncmp(key, "ctrl-", 5) == 0) {
        strcat(result, "^");
        key += 5;
      } else {
        strncat(result, key, 1);
        key++;
      }
    }
  } else
    return NULL;
  return result;
}


x86 assembly (clang, -O0):
<gui_keyboard_get_internal_code>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     127b <gui_keyboard_get_internal_code+0x12b>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     1276 <gui_keyboard_get_internal_code+0x126>
mov    -0x10(%rbp),%rdi
lea    0xe62(%rip),%rsi
mov    $0x6,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11ce <gui_keyboard_get_internal_code+0x7e>
mov    -0x18(%rbp),%rdi
lea    0xe4a(%rip),%rsi
call   1070 <strcat@plt>
mov    -0x10(%rbp),%rax
add    $0x6,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe32(%rip),%rsi
mov    $0x5,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    120e <gui_keyboard_get_internal_code+0xbe>
mov    -0x18(%rbp),%rdi
lea    0xe19(%rip),%rsi
call   1070 <strcat@plt>
mov    -0x10(%rbp),%rax
add    $0x5,%rax
mov    %rax,-0x10(%rbp)
jmp    1271 <gui_keyboard_get_internal_code+0x121>
mov    -0x10(%rbp),%rdi
lea    0xdfb(%rip),%rsi
mov    $0x5,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    124e <gui_keyboard_get_internal_code+0xfe>
mov    -0x18(%rbp),%rdi
lea    0xde2(%rip),%rsi
call   1070 <strcat@plt>
mov    -0x10(%rbp),%rax
add    $0x5,%rax
mov    %rax,-0x10(%rbp)
jmp    126c <gui_keyboard_get_internal_code+0x11c>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1050 <strncat@plt>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1271 <gui_keyboard_get_internal_code+0x121>
jmp    1186 <gui_keyboard_get_internal_code+0x36>
jmp    1288 <gui_keyboard_get_internal_code+0x138>
movq   $0x0,-0x8(%rbp)
jmp    1290 <gui_keyboard_get_internal_code+0x140>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gui_keyboard_get_internal_code>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1040 <strlen@plt>
lea    0x1(%rax),%rdi
call   1060 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
je     124c <gui_keyboard_get_internal_code+0xfc>
movb   $0x0,(%r14)
cmpb   $0x0,(%rbx)
je     124c <gui_keyboard_get_internal_code+0xfc>
lea    0xe74(%rip),%r15
lea    0xe78(%rip),%r13
lea    0xe77(%rip),%rbp
lea    0xe73(%rip),%r12
jmp    11ce <gui_keyboard_get_internal_code+0x7e>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14,%rdi
mov    %rbp,%rsi
call   1070 <strcat@plt>
mov    $0x5,%eax
cmpb   $0x0,(%rbx,%rax,1)
lea    (%rbx,%rax,1),%rbx
je     124c <gui_keyboard_get_internal_code+0xfc>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x6,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11f6 <gui_keyboard_get_internal_code+0xa6>
mov    %r14,%rdi
lea    0xe1a(%rip),%rsi
call   1070 <strcat@plt>
add    $0x6,%rbx
mov    %rbx,%rdi
mov    %r13,%rsi
mov    $0x5,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     11b0 <gui_keyboard_get_internal_code+0x60>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x5,%edx
call   1030 <strncmp@plt>
mov    %r14,%rdi
test   %rax,%rax
je     1240 <gui_keyboard_get_internal_code+0xf0>
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <strncat@plt>
mov    $0x1,%eax
jmp    11c0 <gui_keyboard_get_internal_code+0x70>
nopw   0x0(%rax,%rax,1)
lea    0xdd3(%rip),%rsi
jmp    11b6 <gui_keyboard_get_internal_code+0x66>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"meta2-\00"
@.str.1 = [4 x i8] c"^[[\00"
@.str.2 = [6 x i8] c"meta-\00"
@.str.3 = [3 x i8] c"^[\00"
@.str.4 = [6 x i8] c"ctrl-\00"
@.str.5 = [2 x i8] c"^\00"
define i8* @gui_keyboard_get_internal_code(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i64 @strlen(i8* %5)
%7 = add nsw i64 %6, 1
%8 = call i8* @malloc(i64 %7)
store i8* %8, i8** %4
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %54
10: ; preds = %1
%11 = load i8*, i8** %4
%12 = getelementptr inbounds i8, i8* %11, i64 0
store i8 0, i8* %12
br label %13
13: ; preds = %52, %10
%14 = load i8*, i8** %3
%15 = getelementptr inbounds i8, i8* %14, i64 0
%16 = load i8, i8* %15
%17 = icmp ne i8 %16, 0
br i1 %17, label %18, label %53
18: ; preds = %13
%19 = load i8*, i8** %3
%20 = call i64 @strncmp(i8* %19, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 6)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %18
%23 = load i8*, i8** %4
%24 = call i32 @strcat(i8* %23, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%25 = load i8*, i8** %3
%26 = getelementptr inbounds i8, i8* %25, i64 6
store i8* %26, i8** %3
br label %27
27: ; preds = %22, %18
%28 = load i8*, i8** %3
%29 = call i64 @strncmp(i8* %28, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 5)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %27
%32 = load i8*, i8** %4
%33 = call i32 @strcat(i8* %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%34 = load i8*, i8** %3
%35 = getelementptr inbounds i8, i8* %34, i64 5
store i8* %35, i8** %3
br label %52
36: ; preds = %27
%37 = load i8*, i8** %3
%38 = call i64 @strncmp(i8* %37, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 5)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %36
%41 = load i8*, i8** %4
%42 = call i32 @strcat(i8* %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%43 = load i8*, i8** %3
%44 = getelementptr inbounds i8, i8* %43, i64 5
store i8* %44, i8** %3
br label %51
45: ; preds = %36
%46 = load i8*, i8** %4
%47 = load i8*, i8** %3
%48 = call i32 @strncat(i8* %46, i8* %47, i32 1)
%49 = load i8*, i8** %3
%50 = getelementptr inbounds i8, i8* %49, i32 1
store i8* %50, i8** %3
br label %51
51: ; preds = %45, %40
br label %52
52: ; preds = %51, %31
br label %13
53: ; preds = %13
br label %55
54: ; preds = %1
store i8* null, i8** %2
br label %57
55: ; preds = %53
%56 = load i8*, i8** %4
store i8* %56, i8** %2
br label %57
57: ; preds = %55, %54
%58 = load i8*, i8** %2
ret i8* %58
}

LLVM IR (-O2):
@.str = [7 x i8] c"meta2-\00"
@.str.1 = [4 x i8] c"^[[\00"
@.str.2 = [6 x i8] c"meta-\00"
@.str.3 = [3 x i8] c"^[\00"
@.str.4 = [6 x i8] c"ctrl-\00"
@.str.5 = [2 x i8] c"^\00"
define i8* @gui_keyboard_get_internal_code(i8* %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = add nsw i64 %2, 1
%4 = tail call i8* @malloc(i64 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %34, label %6
6: ; preds = %1
store i8 0, i8* %4
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 0
br i1 %8, label %34, label %9
9: ; preds = %6, %29
%10 = phi i8* [ %31, %29 ], [ %0, %6 ]
%11 = tail call i64 @strncmp(i8* %10, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 6)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = tail call i32 @strcat(i8* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%15 = getelementptr inbounds i8, i8* %10, i64 6
br label %16
16: ; preds = %13, %9
%17 = phi i8* [ %15, %13 ], [ %10, %9 ]
%18 = tail call i64 @strncmp(i8* %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 5)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = tail call i32 @strcat(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
br label %29
22: ; preds = %16
%23 = tail call i64 @strncmp(i8* %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 5)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = tail call i32 @strcat(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br label %29
27: ; preds = %22
%28 = tail call i32 @strncat(i8* %4, i8* %17, i32 1)
br label %29
29: ; preds = %25, %27, %20
%30 = phi i64 [ 5, %25 ], [ 1, %27 ], [ 5, %20 ]
%31 = getelementptr inbounds i8, i8* %17, i64 %30
%32 = load i8, i8* %31
%33 = icmp eq i8 %32, 0
br i1 %33, label %34, label %9
34: ; preds = %29, %6, %1
ret i8* %4
}