#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 */
unsigned char CHAR2INT(char);
int /*<<< orphan*/ isxdigit(char);
int strlen(char *);
char toupper(char);
int string2raw(char *str, unsigned char *raw) {
  int len = (strlen(str) + 1) / 2;
  do {
    if (!isxdigit(*str)) {
      return -1;
    }
    *str = toupper(*str);
    *raw = CHAR2INT(*str) << 4;
    ++str;
    *str = toupper(*str);
    if (*str == '\0')
      break;
    *raw |= CHAR2INT(*str);
    ++raw;
  } while (*++str != '\0');
  return len;
}


x86 assembly (clang, -O0):
<string2raw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1060 <isxdigit@plt>
cmp    $0x0,%eax
jne    1188 <string2raw+0x48>
movl   $0xffffffff,-0x4(%rbp)
jmp    1233 <string2raw+0xf3>
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1040 <toupper@plt>
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1030 <CHAR2INT@plt>
movzbl %al,%eax
shl    $0x4,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1040 <toupper@plt>
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11eb <string2raw+0xab>
jmp    122d <string2raw+0xed>
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1030 <CHAR2INT@plt>
movzbl %al,%edx
mov    -0x18(%rbp),%rax
movzbl (%rax),%ecx
or     %edx,%ecx
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
jne    1167 <string2raw+0x27>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string2raw>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
call   1050 <strlen@plt>
mov    %eax,%ecx
add    $0x1,%ecx
shr    $0x1f,%ecx
lea    (%rax,%rcx,1),%r15d
add    $0x1,%r15d
sar    %r15d
mov    (%rbx),%al
xor    %ebp,%ebp
nopl   0x0(%rax)
movsbl %al,%edi
call   1060 <isxdigit@plt>
test   %eax,%eax
je     11c4 <string2raw+0x84>
movsbl (%rbx,%rbp,2),%edi
call   1040 <toupper@plt>
mov    %al,(%rbx,%rbp,2)
movsbl %al,%edi
call   1030 <CHAR2INT@plt>
shl    $0x4,%al
mov    %al,(%r14,%rbp,1)
movsbl 0x1(%rbx,%rbp,2),%edi
call   1040 <toupper@plt>
mov    %al,0x1(%rbx,%rbp,2)
test   %al,%al
je     11ca <string2raw+0x8a>
movsbl %al,%edi
call   1030 <CHAR2INT@plt>
or     %al,(%r14,%rbp,1)
movzbl 0x2(%rbx,%rbp,2),%eax
add    $0x1,%rbp
test   %al,%al
jne    1170 <string2raw+0x30>
jmp    11ca <string2raw+0x8a>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @string2raw(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%7 = load i8*, i8** %4
%8 = call i32 @strlen(i8* %7)
%9 = add nsw i32 %8, 1
%10 = sdiv i32 %9, 2
store i32 %10, i32* %6
br label %11
11: ; preds = %52, %2
%12 = load i8*, i8** %4
%13 = load i8, i8* %12
%14 = call i32 @isxdigit(i8 signext %13)
%15 = icmp ne i32 %14, 0
br i1 %15, label %17, label %16
16: ; preds = %11
store i32 -1, i32* %3
br label %60
17: ; preds = %11
%18 = load i8*, i8** %4
%19 = load i8, i8* %18
%20 = call signext i8 @toupper(i8 signext %19)
%21 = load i8*, i8** %4
store i8 %20, i8* %21
%22 = load i8*, i8** %4
%23 = load i8, i8* %22
%24 = call zeroext i8 @CHAR2INT(i8 signext %23)
%25 = zext i8 %24 to i32
%26 = shl i32 %25, 4
%27 = trunc i32 %26 to i8
%28 = load i8*, i8** %5
store i8 %27, i8* %28
%29 = load i8*, i8** %4
%30 = getelementptr inbounds i8, i8* %29, i32 1
store i8* %30, i8** %4
%31 = load i8*, i8** %4
%32 = load i8, i8* %31
%33 = call signext i8 @toupper(i8 signext %32)
%34 = load i8*, i8** %4
store i8 %33, i8* %34
%35 = load i8*, i8** %4
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %17
br label %58
40: ; preds = %17
%41 = load i8*, i8** %4
%42 = load i8, i8* %41
%43 = call zeroext i8 @CHAR2INT(i8 signext %42)
%44 = zext i8 %43 to i32
%45 = load i8*, i8** %5
%46 = load i8, i8* %45
%47 = zext i8 %46 to i32
%48 = or i32 %47, %44
%49 = trunc i32 %48 to i8
store i8 %49, i8* %45
%50 = load i8*, i8** %5
%51 = getelementptr inbounds i8, i8* %50, i32 1
store i8* %51, i8** %5
br label %52
52: ; preds = %40
%53 = load i8*, i8** %4
%54 = getelementptr inbounds i8, i8* %53, i32 1
store i8* %54, i8** %4
%55 = load i8, i8* %54
%56 = sext i8 %55 to i32
%57 = icmp ne i32 %56, 0
br i1 %57, label %11, label %58
58: ; preds = %52, %39
%59 = load i32, i32* %6
store i32 %59, i32* %3
br label %60
60: ; preds = %58, %16
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
define i32 @string2raw(i8* %0, i8* nocapture %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = add nsw i32 %3, 1
%5 = sdiv i32 %4, 2
%6 = load i8, i8* %0
br label %7
7: ; preds = %22, %2
%8 = phi i8 [ %6, %2 ], [ %28, %22 ]
%9 = phi i8* [ %0, %2 ], [ %27, %22 ]
%10 = phi i8* [ %1, %2 ], [ %26, %22 ]
%11 = tail call i32 @isxdigit(i8 signext %8)
%12 = icmp eq i32 %11, 0
br i1 %12, label %30, label %13
13: ; preds = %7
%14 = load i8, i8* %9
%15 = tail call signext i8 @toupper(i8 signext %14)
store i8 %15, i8* %9
%16 = tail call zeroext i8 @CHAR2INT(i8 signext %15)
%17 = shl i8 %16, 4
store i8 %17, i8* %10
%18 = getelementptr inbounds i8, i8* %9, i64 1
%19 = load i8, i8* %18
%20 = tail call signext i8 @toupper(i8 signext %19)
store i8 %20, i8* %18
%21 = icmp eq i8 %20, 0
br i1 %21, label %30, label %22
22: ; preds = %13
%23 = tail call zeroext i8 @CHAR2INT(i8 signext %20)
%24 = load i8, i8* %10
%25 = or i8 %24, %23
store i8 %25, i8* %10
%26 = getelementptr inbounds i8, i8* %10, i64 1
%27 = getelementptr inbounds i8, i8* %9, i64 2
%28 = load i8, i8* %27
%29 = icmp eq i8 %28, 0
br i1 %29, label %30, label %7
30: ; preds = %22, %13, %7
%31 = phi i32 [ -1, %7 ], [ %5, %13 ], [ %5, %22 ]
ret i32 %31
}