#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 unsigned int uint8_t;

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ errno;
unsigned long strtoul(char *, char **, int);
int str2hex(char *string, uint8_t *hexstr, int len) {
  char *ptr, *next;
  unsigned long val;
  int i;
  ptr = next = string;
  for (i = 0; i < len; i++) {
    if ((val = strtoul(next, &ptr, 16)) > 255) {
      errno = EINVAL;
      return (-1);
    }
    hexstr[i] = (unsigned int)val;
    if ((next == ptr) && (i != len - 1)) {
      errno = EINVAL;
      return (-1);
    }
    next = ptr + 1;
  }
  return (1);
}


x86 assembly (clang, -O0):
<str2hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11ea <str2hex+0xda>
mov    -0x30(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0x10,%edx
call   1030 <strtoul@plt>
mov    %rax,-0x38(%rbp)
cmp    $0xff,%rax
jbe    1182 <str2hex+0x72>
mov    0x2e6d(%rip),%rax
mov    (%rax),%ecx
mov    0x2e6c(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11f1 <str2hex+0xe1>
mov    -0x38(%rbp),%rax
mov    %eax,%edx
mov    -0x18(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
jne    11d0 <str2hex+0xc0>
mov    -0x3c(%rbp),%eax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
je     11d0 <str2hex+0xc0>
mov    0x2e1f(%rip),%rax
mov    (%rax),%ecx
mov    0x2e1e(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11f1 <str2hex+0xe1>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1136 <str2hex+0x26>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str2hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,(%rsp)
mov    $0x1,%eax
test   %edx,%edx
jle    1199 <str2hex+0x89>
mov    %rsi,%r13
mov    %rdi,%rbp
lea    -0x1(%rdx),%r12d
mov    %edx,%ebx
xor    %r14d,%r14d
mov    %rsp,%r15
jmp    1150 <str2hex+0x40>
nopl   0x0(%rax)
add    $0x1,%rax
add    $0x1,%r14
mov    %rax,%rbp
cmp    %r14,%rbx
je     1194 <str2hex+0x84>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    $0x10,%edx
call   1030 <strtoul@plt>
cmp    $0xff,%rax
ja     117b <str2hex+0x6b>
mov    %eax,0x0(%r13,%r14,4)
mov    (%rsp),%rax
cmp    %rax,%rbp
jne    1140 <str2hex+0x30>
cmp    %r14,%r12
je     1140 <str2hex+0x30>
mov    0x2e56(%rip),%rax
mov    (%rax),%eax
mov    0x2e55(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
jmp    1199 <str2hex+0x89>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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