#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 */
int /*<<< orphan*/ isxdigit(char);
scalar_t__ strtol(char *, char **, int);
int string_to_hex(char *string, unsigned char *key, int len) {
  char tmpBuf[4];
  int idx, ii = 0;
  for (idx = 0; idx < len; idx += 2) {
    tmpBuf[0] = string[idx];
    tmpBuf[1] = string[idx + 1];
    tmpBuf[2] = 0;
    if (!isxdigit(tmpBuf[0]) || !isxdigit(tmpBuf[1]))
      return 0;
    key[ii++] = (unsigned char)strtol(tmpBuf, (char **)NULL, 16);
  }
  return 1;
}


x86 assembly (clang, -O0):
<string_to_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11d9 <string_to_hex+0xb9>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x1f(%rbp)
movb   $0x0,-0x1e(%rbp)
movsbl -0x20(%rbp),%edi
call   1040 <isxdigit@plt>
cmp    $0x0,%eax
je     1196 <string_to_hex+0x76>
movsbl -0x1f(%rbp),%edi
call   1040 <isxdigit@plt>
cmp    $0x0,%eax
jne    11a2 <string_to_hex+0x82>
movl   $0x0,-0x4(%rbp)
jmp    11e0 <string_to_hex+0xc0>
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1030 <strtol@plt>
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x28(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x24(%rbp)
jmp    1141 <string_to_hex+0x21>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_to_hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x1,%eax
test   %edx,%edx
jle    11a8 <string_to_hex+0x88>
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %rdi,%r13
xor    %ebp,%ebp
lea    0x4(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsbl 0x0(%r13,%rbp,1),%edi
mov    %dil,0x4(%rsp)
movzbl 0x1(%r13,%rbp,1),%r14d
mov    %r14b,0x5(%rsp)
movb   $0x0,0x6(%rsp)
call   1040 <isxdigit@plt>
test   %eax,%eax
je     11a6 <string_to_hex+0x86>
movsbl %r14b,%edi
call   1040 <isxdigit@plt>
test   %eax,%eax
je     11a6 <string_to_hex+0x86>
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <strtol@plt>
mov    %al,(%rbx)
add    $0x2,%rbp
add    $0x1,%rbx
cmp    %r15d,%ebp
jl     1150 <string_to_hex+0x30>
mov    $0x1,%eax
jmp    11a8 <string_to_hex+0x88>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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