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

/* Variables and functions */
int /*<<< orphan*/ EVP_PKEY_CTRL_CIPHER;
int /*<<< orphan*/ EVP_PKEY_CTRL_SET_MAC_KEY;
int /*<<< orphan*/ *EVP_get_cipherbyname(char const *);
int /*<<< orphan*/ free(unsigned char *);
int pkey_cmac_ctrl(int /*<<< orphan*/ *, int /*<<< orphan*/, long,
                   unsigned char *);
int /*<<< orphan*/ strcmp(char const *, char *);
unsigned char *string_to_hex(char const *, long *);
int /*<<< orphan*/ strlen(void *);
int pkey_cmac_ctrl_str(EVP_PKEY_CTX *ctx, const char *type, const char *value) {
  if (!value) {
    return 0;
  }
  if (!strcmp(type, "key")) {
    void *p = (void *)value;
    return pkey_cmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, strlen(p), p);
  }
  if (!strcmp(type, "cipher")) {
    const EVP_CIPHER *c;
    c = EVP_get_cipherbyname(value);
    if (!c)
      return 0;
    return pkey_cmac_ctrl(ctx, EVP_PKEY_CTRL_CIPHER, -1, (void *)c);
  }
  if (!strcmp(type, "hexkey")) {
    unsigned char *key;
    int r;
    long keylen;
    key = string_to_hex(value, &keylen);
    if (!key)
      return 0;
    r = pkey_cmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, keylen, key);
    free(key);
    return r;
  }
  return -2;
}


x86 assembly (clang, -O0):
<pkey_cmac_ctrl_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    118b <pkey_cmac_ctrl_str+0x2b>
movl   $0x0,-0x4(%rbp)
jmp    12bf <pkey_cmac_ctrl_str+0x15f>
mov    -0x18(%rbp),%rdi
lea    0xe74(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    11e4 <pkey_cmac_ctrl_str+0x84>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x58(%rbp),%rdi
mov    -0x4c(%rbp),%esi
movslq %eax,%rdx
mov    -0x28(%rbp),%rcx
call   1060 <pkey_cmac_ctrl@plt>
mov    %eax,-0x4(%rbp)
jmp    12bf <pkey_cmac_ctrl_str+0x15f>
mov    -0x18(%rbp),%rdi
lea    0xe11(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    1246 <pkey_cmac_ctrl_str+0xe6>
mov    -0x20(%rbp),%rdi
call   1070 <EVP_get_cipherbyname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1221 <pkey_cmac_ctrl_str+0xc1>
movl   $0x0,-0x4(%rbp)
jmp    12bf <pkey_cmac_ctrl_str+0x15f>
mov    -0x10(%rbp),%rdi
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%rcx
mov    $0xffffffffffffffff,%rdx
call   1060 <pkey_cmac_ctrl@plt>
mov    %eax,-0x4(%rbp)
jmp    12bf <pkey_cmac_ctrl_str+0x15f>
mov    -0x18(%rbp),%rdi
lea    0xdb6(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    12b8 <pkey_cmac_ctrl_str+0x158>
mov    -0x20(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   1080 <string_to_hex@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1287 <pkey_cmac_ctrl_str+0x127>
movl   $0x0,-0x4(%rbp)
jmp    12bf <pkey_cmac_ctrl_str+0x15f>
mov    -0x10(%rbp),%rdi
mov    0x2d46(%rip),%rax
mov    (%rax),%esi
mov    -0x48(%rbp),%rdx
mov    -0x38(%rbp),%rcx
call   1060 <pkey_cmac_ctrl@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12bf <pkey_cmac_ctrl_str+0x15f>
movl   $0xfffffffe,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pkey_cmac_ctrl_str>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %r15d,%r15d
test   %rdx,%rdx
je     124a <pkey_cmac_ctrl_str+0xea>
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %rdi,%r14
lea    0xe87(%rip),%rsi
mov    %rbp,%rdi
call   1050 <strcmp@plt>
test   %eax,%eax
je     11f7 <pkey_cmac_ctrl_str+0x97>
lea    0xe6a(%rip),%rsi
mov    %rbp,%rdi
call   1050 <strcmp@plt>
test   %eax,%eax
je     1215 <pkey_cmac_ctrl_str+0xb5>
lea    0xe5e(%rip),%rsi
mov    %rbp,%rdi
call   1050 <strcmp@plt>
mov    $0xfffffffe,%r15d
test   %eax,%eax
jne    124a <pkey_cmac_ctrl_str+0xea>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1080 <string_to_hex@plt>
test   %rax,%rax
je     1247 <pkey_cmac_ctrl_str+0xe7>
mov    %rax,%rbx
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
mov    (%rsp),%rdx
mov    %r14,%rdi
mov    %rbx,%rcx
call   1060 <pkey_cmac_ctrl@plt>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    124a <pkey_cmac_ctrl_str+0xea>
mov    0x2dda(%rip),%rax
mov    (%rax),%ebp
mov    %rbx,%rdi
call   1040 <strlen@plt>
movslq %eax,%rdx
mov    %r14,%rdi
mov    %ebp,%esi
mov    %rbx,%rcx
jmp    1238 <pkey_cmac_ctrl_str+0xd8>
mov    %rbx,%rdi
call   1070 <EVP_get_cipherbyname@plt>
test   %rax,%rax
je     124a <pkey_cmac_ctrl_str+0xea>
mov    0x2dbf(%rip),%rcx
mov    (%rcx),%esi
mov    %r14,%rdi
mov    $0xffffffffffffffff,%rdx
mov    %rax,%rcx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <pkey_cmac_ctrl@plt>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"key\00"
@EVP_PKEY_CTRL_SET_MAC_KEY = global i32 0
@.str.1 = [7 x i8] c"cipher\00"
@EVP_PKEY_CTRL_CIPHER = global i32 0
@.str.2 = [7 x i8] c"hexkey\00"
define i32 @pkey_cmac_ctrl_str(i32* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i64
store i32* %0, i32** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%13 = load i8*, i8** %7
%14 = icmp ne i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %3
store i32 0, i32* %4
br label %65
16: ; preds = %3
%17 = load i8*, i8** %6
%18 = call i32 @strcmp(i8* %17, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%19 = icmp ne i32 %18, 0
br i1 %19, label %29, label %20
20: ; preds = %16
%21 = load i8*, i8** %7
store i8* %21, i8** %8
%22 = load i32*, i32** %5
%23 = load i32, i32* @EVP_PKEY_CTRL_SET_MAC_KEY
%24 = load i8*, i8** %8
%25 = call i32 @strlen(i8* %24)
%26 = sext i32 %25 to i64
%27 = load i8*, i8** %8
%28 = call i32 @pkey_cmac_ctrl(i32* %22, i32 %23, i64 %26, i8* %27)
store i32 %28, i32* %4
br label %65
29: ; preds = %16
%30 = load i8*, i8** %6
%31 = call i32 @strcmp(i8* %30, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%32 = icmp ne i32 %31, 0
br i1 %32, label %45, label %33
33: ; preds = %29
%34 = load i8*, i8** %7
%35 = call i32* @EVP_get_cipherbyname(i8* %34)
store i32* %35, i32** %9
%36 = load i32*, i32** %9
%37 = icmp ne i32* %36, null
br i1 %37, label %39, label %38
38: ; preds = %33
store i32 0, i32* %4
br label %65
39: ; preds = %33
%40 = load i32*, i32** %5
%41 = load i32, i32* @EVP_PKEY_CTRL_CIPHER
%42 = load i32*, i32** %9
%43 = bitcast i32* %42 to i8*
%44 = call i32 @pkey_cmac_ctrl(i32* %40, i32 %41, i64 -1, i8* %43)
store i32 %44, i32* %4
br label %65
45: ; preds = %29
%46 = load i8*, i8** %6
%47 = call i32 @strcmp(i8* %46, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%48 = icmp ne i32 %47, 0
br i1 %48, label %64, label %49
49: ; preds = %45
%50 = load i8*, i8** %7
%51 = call i8* @string_to_hex(i8* %50, i64* %12)
store i8* %51, i8** %10
%52 = load i8*, i8** %10
%53 = icmp ne i8* %52, null
br i1 %53, label %55, label %54
54: ; preds = %49
store i32 0, i32* %4
br label %65
55: ; preds = %49
%56 = load i32*, i32** %5
%57 = load i32, i32* @EVP_PKEY_CTRL_SET_MAC_KEY
%58 = load i64, i64* %12
%59 = load i8*, i8** %10
%60 = call i32 @pkey_cmac_ctrl(i32* %56, i32 %57, i64 %58, i8* %59)
store i32 %60, i32* %11
%61 = load i8*, i8** %10
%62 = call i32 @free(i8* %61)
%63 = load i32, i32* %11
store i32 %63, i32* %4
br label %65
64: ; preds = %45
store i32 -2, i32* %4
br label %65
65: ; preds = %64, %55, %54, %39, %38, %20, %15
%66 = load i32, i32* %4
ret i32 %66
}

LLVM IR (-O2):
@.str = [4 x i8] c"key\00"
@EVP_PKEY_CTRL_SET_MAC_KEY = global i32 0
@.str.1 = [7 x i8] c"cipher\00"
@EVP_PKEY_CTRL_CIPHER = global i32 0
@.str.2 = [7 x i8] c"hexkey\00"
define i32 @pkey_cmac_ctrl_str(i32* %0, i8* nocapture %1, i8* %2) {
%4 = alloca i64
%5 = icmp eq i8* %2, null
br i1 %5, label %38, label %6
6: ; preds = %3
%7 = tail call i32 @strcmp(i8* dereferenceable(1) %1, i8* dereferenceable(4) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %6
%10 = load i32, i32* @EVP_PKEY_CTRL_SET_MAC_KEY
%11 = tail call i32 @strlen(i8* %2)
%12 = sext i32 %11 to i64
%13 = tail call i32 @pkey_cmac_ctrl(i32* %0, i32 %10, i64 %12, i8* %2)
br label %38
14: ; preds = %6
%15 = tail call i32 @strcmp(i8* dereferenceable(1) %1, i8* dereferenceable(7) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %14
%18 = tail call i32* @EVP_get_cipherbyname(i8* %2)
%19 = icmp eq i32* %18, null
br i1 %19, label %38, label %20
20: ; preds = %17
%21 = load i32, i32* @EVP_PKEY_CTRL_CIPHER
%22 = bitcast i32* %18 to i8*
%23 = tail call i32 @pkey_cmac_ctrl(i32* %0, i32 %21, i64 -1, i8* %22)
br label %38
24: ; preds = %14
%25 = tail call i32 @strcmp(i8* dereferenceable(1) %1, i8* dereferenceable(7) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %38
27: ; preds = %24
%28 = bitcast i64* %4 to i8*
%29 = call i8* @string_to_hex(i8* %2, i64* %4)
%30 = icmp eq i8* %29, null
br i1 %30, label %36, label %31
31: ; preds = %27
%32 = load i32, i32* @EVP_PKEY_CTRL_SET_MAC_KEY
%33 = load i64, i64* %4
%34 = call i32 @pkey_cmac_ctrl(i32* %0, i32 %32, i64 %33, i8* %29)
%35 = call i32 @free(i8* %29)
br label %36
36: ; preds = %27, %31
%37 = phi i32 [ %34, %31 ], [ 0, %27 ]
br label %38
38: ; preds = %24, %20, %17, %3, %36, %9
%39 = phi i32 [ %37, %36 ], [ %13, %9 ], [ 0, %3 ], [ %23, %20 ], [ 0, %17 ], [ -2, %24 ]
ret i32 %39
}