#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 hexCharToValue(char const);
int strlen(char const *);
int hexToStr(const char *hex, char *ch) {
  int high, low;
  int tmp = 0;
  if (hex == NULL || ch == NULL) {
    return -1;
  }
  if (strlen(hex) % 2 == 1) {
    return -2;
  }
  while (*hex) {
    high = hexCharToValue(*hex);
    if (high < 0) {
      *ch = '\0';
      return -3;
    }
    hex++;
    low = hexCharToValue(*hex);
    if (low < 0) {
      *ch = '\0';
      return -3;
    }
    tmp = (high << 4) + low;
    *ch++ = (char)tmp;
    hex++;
  }
  *ch = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<hexToStr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     114d <hexToStr+0x2d>
cmpq   $0x0,-0x18(%rbp)
jne    1159 <hexToStr+0x39>
movl   $0xffffffff,-0x4(%rbp)
jmp    1236 <hexToStr+0x116>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x1,%edx
jne    117f <hexToStr+0x5f>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1236 <hexToStr+0x116>
jmp    1184 <hexToStr+0x64>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     1228 <hexToStr+0x108>
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1030 <hexCharToValue@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11bd <hexToStr+0x9d>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
movl   $0xfffffffd,-0x4(%rbp)
jmp    1236 <hexToStr+0x116>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1030 <hexCharToValue@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11f5 <hexToStr+0xd5>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
movl   $0xfffffffd,-0x4(%rbp)
jmp    1236 <hexToStr+0x116>
mov    -0x1c(%rbp),%eax
shl    $0x4,%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1184 <hexToStr+0x64>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hexToStr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0xffffffff,%r14d
test   %rdi,%rdi
je     11af <hexToStr+0x8f>
mov    %rsi,%r15
test   %rsi,%rsi
je     11af <hexToStr+0x8f>
mov    %rdi,%rbp
call   1040 <strlen@plt>
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
and    $0xfffffffe,%ecx
sub    %ecx,%eax
mov    $0xfffffffe,%r14d
cmp    $0x1,%eax
je     11af <hexToStr+0x8f>
mov    0x0(%rbp),%al
xor    %r14d,%r14d
test   %al,%al
je     11ab <hexToStr+0x8b>
add    $0x2,%rbp
nopw   0x0(%rax,%rax,1)
movsbl %al,%edi
call   1030 <hexCharToValue@plt>
test   %eax,%eax
js     11a5 <hexToStr+0x85>
mov    %eax,%ebx
movsbl -0x1(%rbp),%edi
call   1030 <hexCharToValue@plt>
test   %eax,%eax
js     11a5 <hexToStr+0x85>
shl    $0x4,%ebx
add    %ebx,%eax
mov    %al,(%r15)
add    $0x1,%r15
movzbl 0x0(%rbp),%eax
add    $0x2,%rbp
test   %al,%al
jne    1170 <hexToStr+0x50>
jmp    11ab <hexToStr+0x8b>
mov    $0xfffffffd,%r14d
movb   $0x0,(%r15)
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @hexToStr(i8* %0, i8* writeonly %1) {
%3 = icmp eq i8* %0, null
%4 = icmp eq i8* %1, null
%5 = or i1 %3, %4
br i1 %5, label %35, label %6
6: ; preds = %2
%7 = tail call i32 @strlen(i8* %0)
%8 = srem i32 %7, 2
%9 = icmp eq i32 %8, 1
br i1 %9, label %35, label %10
10: ; preds = %6
%11 = load i8, i8* %0
%12 = icmp eq i8 %11, 0
br i1 %12, label %32, label %13
13: ; preds = %10, %24
%14 = phi i8 [ %30, %24 ], [ %11, %10 ]
%15 = phi i8* [ %29, %24 ], [ %0, %10 ]
%16 = phi i8* [ %28, %24 ], [ %1, %10 ]
%17 = tail call i32 @hexCharToValue(i8 signext %14)
%18 = icmp slt i32 %17, 0
br i1 %18, label %32, label %19
19: ; preds = %13
%20 = getelementptr inbounds i8, i8* %15, i64 1
%21 = load i8, i8* %20
%22 = tail call i32 @hexCharToValue(i8 signext %21)
%23 = icmp slt i32 %22, 0
br i1 %23, label %32, label %24
24: ; preds = %19
%25 = shl i32 %17, 4
%26 = add nsw i32 %22, %25
%27 = trunc i32 %26 to i8
%28 = getelementptr inbounds i8, i8* %16, i64 1
store i8 %27, i8* %16
%29 = getelementptr inbounds i8, i8* %15, i64 2
%30 = load i8, i8* %29
%31 = icmp eq i8 %30, 0
br i1 %31, label %32, label %13
32: ; preds = %24, %19, %13, %10
%33 = phi i8* [ %1, %10 ], [ %16, %13 ], [ %16, %19 ], [ %28, %24 ]
%34 = phi i32 [ 0, %10 ], [ -3, %13 ], [ -3, %19 ], [ 0, %24 ]
store i8 0, i8* %33
br label %35
35: ; preds = %32, %6, %2
%36 = phi i32 [ -1, %2 ], [ -2, %6 ], [ %34, %32 ]
ret i32 %36
}