#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 hex_to_char(char);
int /*<<< orphan*/ printf(char *, ...);
int strlen(char *);
void parse_hex(char *buf, int *size, char *hex) {
  int len = strlen(hex);
  int i;
  if (len % 2 == 1) {
    printf("Invalid hex length\n");
    *size = 0;
    return;
  }
  if (len / 2 > *size) {
    printf("Hex too long\n");
    *size = 0;
    return;
  }
  for (i = 0; i < len / 2; i++, hex += 2) {
    int c1 = hex_to_char(hex[0]);
    int c2 = hex_to_char(hex[1]);
    if (c1 == -1 || c2 == -1) {
      printf("Invalid character in hex: @%d: %c%c\n", i, hex[0], hex[1]);
      *size = 0;
      return;
    }
    buf[i] = (c1 << 4) | c2;
  }
  *size = len / 2;
}


x86 assembly (clang, -O0):
<parse_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x1,%edx
jne    1181 <parse_hex+0x51>
lea    0xe95(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
jmp    127f <parse_hex+0x14f>
mov    -0x1c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%eax
jle    11b5 <parse_hex+0x85>
lea    0xe75(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
jmp    127f <parse_hex+0x14f>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
cmp    %ecx,%eax
jge    126c <parse_hex+0x13c>
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1050 <hex_to_char@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
movsbl 0x1(%rax),%edi
call   1050 <hex_to_char@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
je     120d <parse_hex+0xdd>
cmpl   $0xffffffff,-0x28(%rbp)
jne    123c <parse_hex+0x10c>
mov    -0x20(%rbp),%esi
mov    -0x18(%rbp),%rax
movsbl (%rax),%edx
mov    -0x18(%rbp),%rax
movsbl 0x1(%rax),%ecx
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
jmp    127f <parse_hex+0x14f>
mov    -0x24(%rbp),%eax
shl    $0x4,%eax
or     -0x28(%rbp),%eax
mov    %al,%dl
mov    -0x8(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x18(%rbp)
jmp    11bc <parse_hex+0x8c>
mov    -0x1c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rdx,%rdi
call   1040 <strlen@plt>
mov    %eax,%r13d
shr    $0x1f,%r13d
add    %eax,%r13d
mov    %r13d,%ecx
and    $0xfffffffe,%ecx
mov    %eax,%edx
sub    %ecx,%edx
cmp    $0x1,%edx
jne    117e <parse_hex+0x3e>
lea    0xeb6(%rip),%rdi
jmp    118d <parse_hex+0x4d>
sar    %r13d
cmp    (%r14),%r13d
jle    11a7 <parse_hex+0x67>
lea    0xe98(%rip),%rdi
call   1030 <puts@plt>
xor    %r13d,%r13d
mov    %r13d,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
cmp    $0x1,%eax
jle    1195 <parse_hex+0x55>
xor    %ebx,%ebx
xchg   %ax,%ax
movsbl (%r12,%rbx,2),%edi
call   1060 <hex_to_char@plt>
mov    %eax,%ebp
movsbl 0x1(%r12,%rbx,2),%edi
call   1060 <hex_to_char@plt>
cmp    $0xffffffff,%ebp
je     11e5 <parse_hex+0xa5>
cmp    $0xffffffff,%eax
je     11e5 <parse_hex+0xa5>
shl    $0x4,%ebp
or     %ebp,%eax
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r13
jne    11b0 <parse_hex+0x70>
jmp    1195 <parse_hex+0x55>
movsbl (%r12,%rbx,2),%edx
movsbl 0x1(%r12,%rbx,2),%ecx
lea    0xe09(%rip),%rdi
xor    %r13d,%r13d
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1195 <parse_hex+0x55>

LLVM IR (-O0):
@.str = [20 x i8] c"Invalid hex length\0A\00"
@.str.1 = [14 x i8] c"Hex too long\0A\00"
@.str.2 = [37 x i8] c"Invalid character in hex: @%d: %c%c\0A\00"
define void @parse_hex(i8* %0, i32* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
store i8* %2, i8** %6
%11 = load i8*, i8** %6
%12 = call i32 @strlen(i8* %11)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = srem i32 %13, 2
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %19
16: ; preds = %3
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%18 = load i32*, i32** %5
store i32 0, i32* %18
br label %79
19: ; preds = %3
%20 = load i32, i32* %7
%21 = sdiv i32 %20, 2
%22 = load i32*, i32** %5
%23 = load i32, i32* %22
%24 = icmp sgt i32 %21, %23
br i1 %24, label %25, label %28
25: ; preds = %19
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32*, i32** %5
store i32 0, i32* %27
br label %79
28: ; preds = %19
store i32 0, i32* %8
br label %29
29: ; preds = %70, %28
%30 = load i32, i32* %8
%31 = load i32, i32* %7
%32 = sdiv i32 %31, 2
%33 = icmp slt i32 %30, %32
br i1 %33, label %34, label %75
34: ; preds = %29
%35 = load i8*, i8** %6
%36 = getelementptr inbounds i8, i8* %35, i64 0
%37 = load i8, i8* %36
%38 = call i32 @hex_to_char(i8 signext %37)
store i32 %38, i32* %9
%39 = load i8*, i8** %6
%40 = getelementptr inbounds i8, i8* %39, i64 1
%41 = load i8, i8* %40
%42 = call i32 @hex_to_char(i8 signext %41)
store i32 %42, i32* %10
%43 = load i32, i32* %9
%44 = icmp eq i32 %43, -1
br i1 %44, label %48, label %45
45: ; preds = %34
%46 = load i32, i32* %10
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %60
48: ; preds = %45, %34
%49 = load i32, i32* %8
%50 = load i8*, i8** %6
%51 = getelementptr inbounds i8, i8* %50, i64 0
%52 = load i8, i8* %51
%53 = sext i8 %52 to i32
%54 = load i8*, i8** %6
%55 = getelementptr inbounds i8, i8* %54, i64 1
%56 = load i8, i8* %55
%57 = sext i8 %56 to i32
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %49, i32 %53, i32 %57)
%59 = load i32*, i32** %5
store i32 0, i32* %59
br label %79
60: ; preds = %45
%61 = load i32, i32* %9
%62 = shl i32 %61, 4
%63 = load i32, i32* %10
%64 = or i32 %62, %63
%65 = trunc i32 %64 to i8
%66 = load i8*, i8** %4
%67 = load i32, i32* %8
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i8, i8* %66, i64 %68
store i8 %65, i8* %69
br label %70
70: ; preds = %60
%71 = load i32, i32* %8
%72 = add nsw i32 %71, 1
store i32 %72, i32* %8
%73 = load i8*, i8** %6
%74 = getelementptr inbounds i8, i8* %73, i64 2
store i8* %74, i8** %6
br label %29
75: ; preds = %29
%76 = load i32, i32* %7
%77 = sdiv i32 %76, 2
%78 = load i32*, i32** %5
store i32 %77, i32* %78
br label %79
79: ; preds = %75, %48, %25, %16
ret void
}

LLVM IR (-O2):
@.str.2 = [37 x i8] c"Invalid character in hex: @%d: %c%c\0A\00"
@str = [13 x i8] c"Hex too long\00"
@str.3 = [19 x i8] c"Invalid hex length\00"
define void @parse_hex(i8* nocapture writeonly %0, i32* nocapture %1, i8* %2) {
%4 = tail call i32 @strlen(i8* %2)
%5 = srem i32 %4, 2
%6 = sdiv i32 %4, 2
%7 = icmp eq i32 %5, 1
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.3, i64 0, i64 0))
br label %46
10: ; preds = %3
%11 = load i32, i32* %1
%12 = icmp sgt i32 %6, %11
br i1 %12, label %17, label %13
13: ; preds = %10
%14 = icmp sgt i32 %4, 1
br i1 %14, label %15, label %46
15: ; preds = %13
%16 = zext i32 %6 to i64
br label %19
17: ; preds = %10
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %46
19: ; preds = %15, %38
%20 = phi i64 [ 0, %15 ], [ %43, %38 ]
%21 = phi i8* [ %2, %15 ], [ %44, %38 ]
%22 = load i8, i8* %21
%23 = tail call i32 @hex_to_char(i8 signext %22)
%24 = getelementptr inbounds i8, i8* %21, i64 1
%25 = load i8, i8* %24
%26 = tail call i32 @hex_to_char(i8 signext %25)
%27 = icmp eq i32 %23, -1
%28 = icmp eq i32 %26, -1
%29 = select i1 %27, i1 true, i1 %28
br i1 %29, label %30, label %38
30: ; preds = %19
%31 = getelementptr inbounds i8, i8* %21, i64 1
%32 = trunc i64 %20 to i32
%33 = load i8, i8* %21
%34 = sext i8 %33 to i32
%35 = load i8, i8* %31
%36 = sext i8 %35 to i32
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %32, i32 %34, i32 %36)
br label %46
38: ; preds = %19
%39 = shl i32 %23, 4
%40 = or i32 %26, %39
%41 = trunc i32 %40 to i8
%42 = getelementptr inbounds i8, i8* %0, i64 %20
store i8 %41, i8* %42
%43 = add nuw nsw i64 %20, 1
%44 = getelementptr inbounds i8, i8* %21, i64 2
%45 = icmp eq i64 %43, %16
br i1 %45, label %46, label %19
46: ; preds = %38, %13, %30, %17, %8
%47 = phi i32 [ 0, %30 ], [ 0, %17 ], [ 0, %8 ], [ %6, %13 ], [ %6, %38 ]
store i32 %47, i32* %1
ret void
}