#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*/ u_int8_t;
typedef int u_int32_t;

/* Variables and functions */
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ hex2raw(char *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ log_error(char *, ...);
int /*<<< orphan*/ *malloc(int);
scalar_t__ strdup(char *);
int strlen(char *);
void key_from_printable(int type, int private, char *key, u_int8_t **data,
                        u_int32_t *datalenp) {
  u_int32_t datalen;
  switch (type) {
  case 1:
    *datalenp = strlen(key);
    *data = (u_int8_t *)strdup(key);
    break;
  case 2:
    datalen = (strlen(key) + 1) / 2;
    *data = malloc(datalen);
    if (!*data) {
      log_error("key_from_printable: malloc (%d) failed", datalen);
      *datalenp = 0;
      return;
    }
    if (hex2raw(key, *data, datalen)) {
      log_error("key_from_printable: invalid hex key");
      free(*data);
      *data = NULL;
      *datalenp = 0;
      return;
    }
    *datalenp = datalen;
    break;
  default:
    log_error("key_from_printable: "
              "unknown/unsupported key type %d",
              type);
    *data = NULL;
    *datalenp = 0;
    break;
  }
}


x86 assembly (clang, -O0):
<key_from_printable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x28(%rbp)
sub    $0x1,%eax
je     119f <key_from_printable+0x3f>
jmp    118e <key_from_printable+0x2e>
mov    -0x28(%rbp),%eax
sub    $0x2,%eax
je     11c8 <key_from_printable+0x68>
jmp    127d <key_from_printable+0x11d>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
jmp    12a3 <key_from_printable+0x143>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    121f <key_from_printable+0xbf>
mov    -0x24(%rbp),%esi
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1040 <log_error@plt>
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
jmp    12a3 <key_from_printable+0x143>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    -0x24(%rbp),%edx
call   1060 <hex2raw@plt>
cmp    $0x0,%eax
je     126f <key_from_printable+0x10f>
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1040 <log_error@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
jmp    12a3 <key_from_printable+0x143>
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    12a3 <key_from_printable+0x143>
mov    -0x4(%rbp),%esi
lea    0xdc4(%rip),%rdi
mov    $0x0,%al
call   1040 <log_error@plt>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<key_from_printable>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %r8,%r14
mov    %rcx,%r15
mov    %rdx,%rbx
cmp    $0x2,%edi
je     1194 <key_from_printable+0x34>
mov    %edi,%esi
cmp    $0x1,%edi
jne    11e4 <key_from_printable+0x84>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %eax,(%r14)
mov    %rbx,%rdi
call   1080 <strdup@plt>
mov    %rax,(%r15)
jmp    1212 <key_from_printable+0xb2>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
add    $0x1,%ecx
shr    $0x1f,%ecx
lea    (%rax,%rcx,1),%ebp
add    $0x1,%ebp
sar    %ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
mov    %rax,(%r15)
test   %rax,%rax
je     11fb <key_from_printable+0x9b>
mov    %rbx,%rdi
mov    %rax,%rsi
mov    %ebp,%edx
call   1060 <hex2raw@plt>
test   %eax,%eax
je     121d <key_from_printable+0xbd>
lea    0xe54(%rip),%rdi
xor    %eax,%eax
call   1040 <log_error@plt>
mov    (%r15),%rdi
call   1030 <free@plt>
jmp    11f2 <key_from_printable+0x92>
lea    0xe60(%rip),%rdi
xor    %eax,%eax
call   1040 <log_error@plt>
movq   $0x0,(%r15)
jmp    120b <key_from_printable+0xab>
lea    0xdfe(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <log_error@plt>
movl   $0x0,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,(%r14)
jmp    1212 <key_from_printable+0xb2>

LLVM IR (-O0):
@.str = [39 x i8] c"key_from_printable: malloc (%d) failed\00"
@.str.1 = [36 x i8] c"key_from_printable: invalid hex key\00"
@.str.2 = [52 x i8] c"key_from_printable: unknown/unsupported key type %d\00"
define void @key_from_printable(i32 %0, i32 %1, i8* %2, i32** %3, i32* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32**
%10 = alloca i32*
%11 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32** %3, i32*** %9
store i32* %4, i32** %10
%12 = load i32, i32* %6
switch i32 %12, label %53 [
i32 1, label %13
i32 2, label %21
]
13: ; preds = %5
%14 = load i8*, i8** %8
%15 = call i32 @strlen(i8* %14)
%16 = load i32*, i32** %10
store i32 %15, i32* %16
%17 = load i8*, i8** %8
%18 = call i64 @strdup(i8* %17)
%19 = inttoptr i64 %18 to i32*
%20 = load i32**, i32*** %9
store i32* %19, i32** %20
br label %58
21: ; preds = %5
%22 = load i8*, i8** %8
%23 = call i32 @strlen(i8* %22)
%24 = add nsw i32 %23, 1
%25 = sdiv i32 %24, 2
store i32 %25, i32* %11
%26 = load i32, i32* %11
%27 = call i32* @malloc(i32 %26)
%28 = load i32**, i32*** %9
store i32* %27, i32** %28
%29 = load i32**, i32*** %9
%30 = load i32*, i32** %29
%31 = icmp ne i32* %30, null
br i1 %31, label %36, label %32
32: ; preds = %21
%33 = load i32, i32* %11
%34 = call i32 (i8*, ...) @log_error(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %33)
%35 = load i32*, i32** %10
store i32 0, i32* %35
br label %58
36: ; preds = %21
%37 = load i8*, i8** %8
%38 = load i32**, i32*** %9
%39 = load i32*, i32** %38
%40 = load i32, i32* %11
%41 = call i32 @hex2raw(i8* %37, i32* %39, i32 %40)
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %50
43: ; preds = %36
%44 = call i32 (i8*, ...) @log_error(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%45 = load i32**, i32*** %9
%46 = load i32*, i32** %45
%47 = call i32 @free(i32* %46)
%48 = load i32**, i32*** %9
store i32* null, i32** %48
%49 = load i32*, i32** %10
store i32 0, i32* %49
br label %58
50: ; preds = %36
%51 = load i32, i32* %11
%52 = load i32*, i32** %10
store i32 %51, i32* %52
br label %58
53: ; preds = %5
%54 = load i32, i32* %6
%55 = call i32 (i8*, ...) @log_error(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0), i32 %54)
%56 = load i32**, i32*** %9
store i32* null, i32** %56
%57 = load i32*, i32** %10
store i32 0, i32* %57
br label %58
58: ; preds = %32, %43, %53, %50, %13
ret void
}

LLVM IR (-O2):
@.str = [39 x i8] c"key_from_printable: malloc (%d) failed\00"
@.str.1 = [36 x i8] c"key_from_printable: invalid hex key\00"
@.str.2 = [52 x i8] c"key_from_printable: unknown/unsupported key type %d\00"
define void @key_from_printable(i32 %0, i32 %1, i8* %2, i32** nocapture %3, i32* nocapture writeonly %4) {
switch i32 %0, label %26 [
i32 1, label %6
i32 2, label %10
]
6: ; preds = %5
%7 = tail call i32 @strlen(i8* %2)
store i32 %7, i32* %4
%8 = tail call i64 @strdup(i8* %2)
%9 = inttoptr i64 %8 to i32*
store i32* %9, i32** %3
br label %28
10: ; preds = %5
%11 = tail call i32 @strlen(i8* %2)
%12 = add nsw i32 %11, 1
%13 = sdiv i32 %12, 2
%14 = tail call i32* @malloc(i32 %13)
store i32* %14, i32** %3
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %10
%17 = tail call i32 (i8*, ...) @log_error(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 0, i32* %4
br label %28
18: ; preds = %10
%19 = tail call i32 @hex2raw(i8* %2, i32* %14, i32 %13)
%20 = icmp eq i32 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %18
%22 = tail call i32 (i8*, ...) @log_error(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32*, i32** %3
%24 = tail call i32 @free(i32* %23)
store i32* null, i32** %3
store i32 0, i32* %4
br label %28
25: ; preds = %18
store i32 %13, i32* %4
br label %28
26: ; preds = %5
%27 = tail call i32 (i8*, ...) @log_error(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0), i32 %0)
store i32* null, i32** %3
store i32 0, i32* %4
br label %28
28: ; preds = %6, %25, %26, %21, %16
ret void
}