#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 */
scalar_t__ strtoul(char *, char **, int);
int crypt_decode_key(char *key, char *hex, unsigned int size) {
  char buffer[3];
  char *endp;
  unsigned int i;
  buffer[2] = '\0';
  for (i = 0; i < size; i++) {
    buffer[0] = *hex++;
    buffer[1] = *hex++;
    key[i] = (unsigned char)strtoul(buffer, &endp, 16);
    if (endp != &buffer[2])
      return -1;
  }
  if (*hex != '\0')
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<crypt_decode_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movb   $0x0,-0x1d(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jae    11b4 <crypt_decode_key+0xa4>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
mov    (%rax),%al
mov    %al,-0x1f(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
mov    (%rax),%al
mov    %al,-0x1e(%rbp)
lea    -0x1f(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0x10,%edx
call   1030 <strtoul@plt>
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
lea    -0x1f(%rbp),%rcx
add    $0x2,%rcx
cmp    %rcx,%rax
je     11a1 <crypt_decode_key+0x91>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d7 <crypt_decode_key+0xc7>
jmp    11a6 <crypt_decode_key+0x96>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    112e <crypt_decode_key+0x1e>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11d0 <crypt_decode_key+0xc0>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d7 <crypt_decode_key+0xc7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<crypt_decode_key>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
movb   $0x0,0xf(%rsp)
test   %edx,%edx
je     1185 <crypt_decode_key+0x75>
mov    %rdi,%r14
mov    %edx,%r13d
xor    %ebp,%ebp
lea    0x10(%rsp),%r12
xor    %r15d,%r15d
nopw   0x0(%rax,%rax,1)
movzbl (%rbx,%r15,2),%eax
mov    %al,0xd(%rsp)
movzbl 0x1(%rbx,%r15,2),%eax
mov    %al,0xe(%rsp)
lea    0xd(%rsp),%rdi
mov    %r12,%rsi
mov    $0x10,%edx
call   1030 <strtoul@plt>
mov    %al,(%r14,%r15,1)
lea    0xf(%rsp),%rax
cmp    %rax,0x10(%rsp)
jne    118d <crypt_decode_key+0x7d>
add    $0x1,%r15
add    $0xfffffffffffffffe,%rbp
cmp    %r15,%r13
jne    1140 <crypt_decode_key+0x30>
sub    %rbp,%rbx
xor    %eax,%eax
cmp    (%rbx),%al
sbb    %eax,%eax
jmp    1192 <crypt_decode_key+0x82>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @crypt_decode_key(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [3 x i8]
%9 = alloca i8*
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 2
store i8 0, i8* %11
store i32 0, i32* %10
br label %12
12: ; preds = %37, %3
%13 = load i32, i32* %10
%14 = load i32, i32* %7
%15 = icmp ult i32 %13, %14
br i1 %15, label %16, label %40
16: ; preds = %12
%17 = load i8*, i8** %6
%18 = getelementptr inbounds i8, i8* %17, i32 1
store i8* %18, i8** %6
%19 = load i8, i8* %17
%20 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 0
store i8 %19, i8* %20
%21 = load i8*, i8** %6
%22 = getelementptr inbounds i8, i8* %21, i32 1
store i8* %22, i8** %6
%23 = load i8, i8* %21
%24 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 1
store i8 %23, i8* %24
%25 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 0
%26 = call i64 @strtoul(i8* %25, i8** %9, i32 16)
%27 = trunc i64 %26 to i8
%28 = load i8*, i8** %5
%29 = load i32, i32* %10
%30 = zext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
store i8 %27, i8* %31
%32 = load i8*, i8** %9
%33 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 2
%34 = icmp ne i8* %32, %33
br i1 %34, label %35, label %36
35: ; preds = %16
store i32 -1, i32* %4
br label %47
36: ; preds = %16
br label %37
37: ; preds = %36
%38 = load i32, i32* %10
%39 = add i32 %38, 1
store i32 %39, i32* %10
br label %12
40: ; preds = %12
%41 = load i8*, i8** %6
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %40
store i32 -1, i32* %4
br label %47
46: ; preds = %40
store i32 0, i32* %4
br label %47
47: ; preds = %46, %45, %35
%48 = load i32, i32* %4
ret i32 %48
}

LLVM IR (-O2):
define i32 @crypt_decode_key(i8* nocapture writeonly %0, i8* nocapture %1, i32 %2) {
%4 = alloca [3 x i8]
%5 = alloca i8*
%6 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
%7 = bitcast i8** %5 to i8*
%8 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 2
store i8 0, i8* %8
%9 = icmp eq i32 %2, 0
br i1 %9, label %28, label %10
10: ; preds = %3
%11 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 1
%12 = zext i32 %2 to i64
br label %13
13: ; preds = %10, %24
%14 = phi i64 [ 0, %10 ], [ %26, %24 ]
%15 = phi i8* [ %1, %10 ], [ %25, %24 ]
%16 = getelementptr inbounds i8, i8* %15, i64 1
%17 = load i8, i8* %15
store i8 %17, i8* %6
%18 = load i8, i8* %16
store i8 %18, i8* %11
%19 = call i64 @strtoul(i8* %6, i8** %5, i32 16)
%20 = trunc i64 %19 to i8
%21 = getelementptr inbounds i8, i8* %0, i64 %14
store i8 %20, i8* %21
%22 = load i8*, i8** %5
%23 = icmp eq i8* %22, %8
br i1 %23, label %24, label %33
24: ; preds = %13
%25 = getelementptr inbounds i8, i8* %15, i64 2
%26 = add nuw nsw i64 %14, 1
%27 = icmp eq i64 %26, %12
br i1 %27, label %28, label %13
28: ; preds = %24, %3
%29 = phi i8* [ %1, %3 ], [ %25, %24 ]
%30 = load i8, i8* %29
%31 = icmp ne i8 %30, 0
%32 = sext i1 %31 to i32
br label %33
33: ; preds = %13, %28
%34 = phi i32 [ %32, %28 ], [ -1, %13 ]
ret i32 %34
}