#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*/ memcpy(char **, char *, int);
unsigned int strlen(char *);
char strtoul(char const *, int /*<<< orphan*/ *, int);
int StringToHex(char *string, char *hexstr, int len) {
  char *ptr;
  char digits[3];
  int i;
  if (!string)
    return -1;
  if (strlen(string) < (unsigned int)len * 2)
    return -1;
  ptr = string;
  for (i = 0; i < len; i++) {
    memcpy(&digits, ptr, 2);
    digits[2] = '\0';
    hexstr[i] = strtoul((const char *)&digits, NULL, 16);
    ptr += 2;
    if ((*ptr == ' ') || (*ptr == ':'))
      ptr++;
  }
  return 1;
}


x86 assembly (clang, -O0):
<StringToHex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    115a <StringToHex+0x2a>
movl   $0xffffffff,-0x4(%rbp)
jmp    121f <StringToHex+0xef>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x1c(%rbp),%ecx
shl    $0x1,%ecx
cmp    %ecx,%eax
jae    117d <StringToHex+0x4d>
movl   $0xffffffff,-0x4(%rbp)
jmp    121f <StringToHex+0xef>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1218 <StringToHex+0xe8>
lea    -0x2b(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    $0x2,%edx
call   1040 <memcpy@plt>
movb   $0x0,-0x29(%rbp)
lea    -0x2b(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1050 <strtoul@plt>
mov    %al,%dl
mov    -0x18(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x20,%eax
je     11f9 <StringToHex+0xc9>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x3a,%eax
jne    1205 <StringToHex+0xd5>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    120a <StringToHex+0xda>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    118c <StringToHex+0x5c>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<StringToHex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xffffffff,%r14d
test   %rdi,%rdi
je     11b7 <StringToHex+0x87>
mov    %edx,%ebx
mov    %rsi,%r15
mov    %rdi,%rbp
call   1030 <strlen@plt>
lea    (%rbx,%rbx,1),%ecx
cmp    %ecx,%eax
jb     11b7 <StringToHex+0x87>
mov    $0x1,%r14d
test   %ebx,%ebx
jle    11b7 <StringToHex+0x87>
mov    %ebx,%r12d
xor    %ebx,%ebx
lea    0x5(%rsp),%r13
jmp    117d <StringToHex+0x4d>
add    $0x3,%rbp
add    $0x1,%rbx
cmp    %rbx,%r12
je     11b7 <StringToHex+0x87>
mov    %r13,%rdi
mov    %rbp,%rsi
mov    $0x2,%edx
call   1040 <memcpy@plt>
movb   $0x0,0x7(%rsp)
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <strtoul@plt>
mov    %al,(%r15,%rbx,1)
movzbl 0x2(%rbp),%eax
cmp    $0x3a,%al
je     1170 <StringToHex+0x40>
cmp    $0x20,%al
je     1170 <StringToHex+0x40>
add    $0x2,%rbp
jmp    1174 <StringToHex+0x44>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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