#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 */
scalar_t__ strlen(char const *);
void *valueToHexCh(int);
int strToHex(const char *ch, char *hex) {
  int high, low;
  int tmp = 0;
  if (ch == NULL || hex == NULL) {
    return -1;
  }
  if (strlen(ch) == 0) {
    return -2;
  }
  while (*ch) {
    tmp = (int)*ch;
    high = tmp >> 4;
    low = tmp & 15;
    *hex++ = valueToHexCh(high);
    *hex++ = valueToHexCh(low);
    ch++;
  }
  *hex = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<strToHex>:
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 <strToHex+0x2d>
cmpq   $0x0,-0x18(%rbp)
jne    1159 <strToHex+0x39>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fb <strToHex+0xdb>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x0,%rax
jne    1178 <strToHex+0x58>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11fb <strToHex+0xdb>
jmp    117d <strToHex+0x5d>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     11ed <strToHex+0xcd>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
sar    $0x4,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x24(%rbp),%eax
and    $0xf,%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <valueToHexCh@plt>
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x20(%rbp),%edi
call   1040 <valueToHexCh@plt>
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    117d <strToHex+0x5d>
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):
<strToHex>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0xffffffff,%eax
test   %rdi,%rdi
je     117c <strToHex+0x6c>
mov    %rsi,%r15
test   %rsi,%rsi
je     117c <strToHex+0x6c>
mov    %rdi,%r14
mov    (%rdi),%al
test   %al,%al
je     1177 <strToHex+0x67>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsbl %al,%ebp
mov    %ebp,%edi
sar    $0x4,%edi
and    $0xf,%ebp
call   1030 <valueToHexCh@plt>
mov    %al,(%r15,%rbx,2)
mov    %ebp,%edi
call   1030 <valueToHexCh@plt>
mov    %al,0x1(%r15,%rbx,2)
movzbl 0x1(%r14,%rbx,1),%eax
add    $0x1,%rbx
test   %al,%al
jne    1140 <strToHex+0x30>
movb   $0x0,(%r15,%rbx,2)
xor    %eax,%eax
jmp    117c <strToHex+0x6c>
mov    $0xfffffffe,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @strToHex(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 %47
15: ; preds = %11
%16 = load i8*, i8** %4
%17 = call i64 @strlen(i8* %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %15
store i32 -2, i32* %3
br label %47
20: ; preds = %15
br label %21
21: ; preds = %25, %20
%22 = load i8*, i8** %4
%23 = load i8, i8* %22
%24 = icmp ne i8 %23, 0
br i1 %24, label %25, label %45
25: ; preds = %21
%26 = load i8*, i8** %4
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
store i32 %28, i32* %8
%29 = load i32, i32* %8
%30 = ashr i32 %29, 4
store i32 %30, i32* %6
%31 = load i32, i32* %8
%32 = and i32 %31, 15
store i32 %32, i32* %7
%33 = load i32, i32* %6
%34 = call i8* @valueToHexCh(i32 %33)
%35 = ptrtoint i8* %34 to i8
%36 = load i8*, i8** %5
%37 = getelementptr inbounds i8, i8* %36, i32 1
store i8* %37, i8** %5
store i8 %35, i8* %36
%38 = load i32, i32* %7
%39 = call i8* @valueToHexCh(i32 %38)
%40 = ptrtoint i8* %39 to i8
%41 = load i8*, i8** %5
%42 = getelementptr inbounds i8, i8* %41, i32 1
store i8* %42, i8** %5
store i8 %40, i8* %41
%43 = load i8*, i8** %4
%44 = getelementptr inbounds i8, i8* %43, i32 1
store i8* %44, i8** %4
br label %21
45: ; preds = %21
%46 = load i8*, i8** %5
store i8 0, i8* %46
store i32 0, i32* %3
br label %47
47: ; preds = %45, %19, %14
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
define i32 @strToHex(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 %28, label %6
6: ; preds = %2
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 0
br i1 %8, label %28, label %9
9: ; preds = %6, %9
%10 = phi i8* [ %24, %9 ], [ %0, %6 ]
%11 = phi i8* [ %23, %9 ], [ %1, %6 ]
%12 = phi i8 [ %25, %9 ], [ %7, %6 ]
%13 = sext i8 %12 to i32
%14 = ashr i32 %13, 4
%15 = and i32 %13, 15
%16 = tail call i8* @valueToHexCh(i32 %14)
%17 = ptrtoint i8* %16 to i64
%18 = trunc i64 %17 to i8
%19 = getelementptr inbounds i8, i8* %11, i64 1
store i8 %18, i8* %11
%20 = tail call i8* @valueToHexCh(i32 %15)
%21 = ptrtoint i8* %20 to i64
%22 = trunc i64 %21 to i8
%23 = getelementptr inbounds i8, i8* %11, i64 2
store i8 %22, i8* %19
%24 = getelementptr inbounds i8, i8* %10, i64 1
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 0
br i1 %26, label %27, label %9
27: ; preds = %9
store i8 0, i8* %23
br label %28
28: ; preds = %6, %2, %27
%29 = phi i32 [ 0, %27 ], [ -1, %2 ], [ -2, %6 ]
ret i32 %29
}