#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 sscanf(char *, char *, int *);
int parse_hex(char *str, unsigned char *addr) {
  int len = 0;
  while (*str) {
    int tmp;
    if (str[1] == 0)
      return -1;
    if (sscanf(str, "%02x", &tmp) != 1)
      return -1;
    addr[len] = tmp;
    len++;
    str += 2;
  }
  return len;
}


x86 assembly (clang, -O0):
<parse_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     11a4 <parse_hex+0x94>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
jne    1151 <parse_hex+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    11aa <parse_hex+0x9a>
mov    -0x10(%rbp),%rdi
lea    0xea4(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1030 <sscanf@plt>
cmp    $0x1,%eax
je     117a <parse_hex+0x6a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11aa <parse_hex+0x9a>
mov    -0x20(%rbp),%eax
mov    %al,%dl
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x10(%rbp)
jmp    1127 <parse_hex+0x17>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmpb   $0x0,(%rdi)
je     1180 <parse_hex+0x70>
mov    %rsi,%r14
mov    %rdi,%r13
xor    %ebp,%ebp
lea    0xed1(%rip),%r15
lea    0x4(%rsp),%r12
mov    %rdi,%rbx
nopw   0x0(%rax,%rax,1)
cmpb   $0x0,0x1(%r13,%rbp,2)
je     1179 <parse_hex+0x69>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1030 <sscanf@plt>
cmp    $0x1,%eax
jne    1179 <parse_hex+0x69>
movzbl 0x4(%rsp),%eax
mov    %al,(%r14,%rbp,1)
lea    0x1(%rbp),%rax
add    $0x2,%rbx
cmpb   $0x0,0x2(%r13,%rbp,2)
mov    %rax,%rbp
jne    1140 <parse_hex+0x30>
jmp    1182 <parse_hex+0x72>
mov    $0xffffffff,%eax
jmp    1182 <parse_hex+0x72>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%02x\00"
define i32 @parse_hex(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %6
br label %8
8: ; preds = %24, %2
%9 = load i8*, i8** %4
%10 = load i8, i8* %9
%11 = icmp ne i8 %10, 0
br i1 %11, label %12, label %35
12: ; preds = %8
%13 = load i8*, i8** %4
%14 = getelementptr inbounds i8, i8* %13, i64 1
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %12
store i32 -1, i32* %3
br label %37
19: ; preds = %12
%20 = load i8*, i8** %4
%21 = call i32 @sscanf(i8* %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32* %7)
%22 = icmp ne i32 %21, 1
br i1 %22, label %23, label %24
23: ; preds = %19
store i32 -1, i32* %3
br label %37
24: ; preds = %19
%25 = load i32, i32* %7
%26 = trunc i32 %25 to i8
%27 = load i8*, i8** %5
%28 = load i32, i32* %6
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
store i8 %26, i8* %30
%31 = load i32, i32* %6
%32 = add nsw i32 %31, 1
store i32 %32, i32* %6
%33 = load i8*, i8** %4
%34 = getelementptr inbounds i8, i8* %33, i64 2
store i8* %34, i8** %4
br label %8
35: ; preds = %8
%36 = load i32, i32* %6
store i32 %36, i32* %3
br label %37
37: ; preds = %35, %23, %18
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
@.str = [5 x i8] c"%02x\00"
define i32 @parse_hex(i8* nocapture %0, i8* nocapture writeonly %1) {
%3 = alloca i32
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 0
br i1 %5, label %28, label %6
6: ; preds = %2
%7 = bitcast i32* %3 to i8*
br label %8
8: ; preds = %6, %18
%9 = phi i64 [ 0, %6 ], [ %22, %18 ]
%10 = phi i8* [ %0, %6 ], [ %23, %18 ]
%11 = getelementptr inbounds i8, i8* %10, i64 1
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %8
%15 = call i32 @sscanf(i8* %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32* %3)
%16 = icmp eq i32 %15, 1
br i1 %16, label %18, label %17
17: ; preds = %8, %14
br label %28
18: ; preds = %14
%19 = load i32, i32* %3
%20 = trunc i32 %19 to i8
%21 = getelementptr inbounds i8, i8* %1, i64 %9
store i8 %20, i8* %21
%22 = add nuw i64 %9, 1
%23 = getelementptr inbounds i8, i8* %10, i64 2
%24 = load i8, i8* %23
%25 = icmp eq i8 %24, 0
br i1 %25, label %26, label %8
26: ; preds = %18
%27 = trunc i64 %22 to i32
br label %28
28: ; preds = %26, %2, %17
%29 = phi i32 [ -1, %17 ], [ 0, %2 ], [ %27, %26 ]
ret i32 %29
}