#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 */
char *calloc(size_t, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
scalar_t__ sprintf(char *, char *, unsigned int);
int /*<<< orphan*/ strcat(char *, char *);
size_t strlen(char *);
size_t str_to_hex(char *s, char **out) {
  char *buffer = calloc(strlen(s) + 1, sizeof(char));
  memset(buffer, 0, strlen(buffer) + 1);
  char output[9];
  while (*s) {
    if (sprintf(output, "%02X", (unsigned)*s)) {
      strcat(buffer, output);
    }
    ++s;
  }
  *out = buffer;
  return strlen(buffer);
}


x86 assembly (clang, -O0):
<str_to_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    %rax,%rdx
add    $0x1,%rdx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
je     11ed <str_to_hex+0x9d>
lea    -0x21(%rbp),%rdi
mov    -0x8(%rbp),%rax
movsbl (%rax),%edx
lea    0xe40(%rip),%rsi
call   1070 <sprintf@plt>
cmp    $0x0,%rax
je     11dc <str_to_hex+0x8c>
mov    -0x18(%rbp),%rdi
lea    -0x21(%rbp),%rsi
call   1060 <strcat@plt>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    11a1 <str_to_hex+0x51>
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_to_hex>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <strlen@plt>
lea    0x1(%rax),%rdi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %r15,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
mov    (%rbx),%al
test   %al,%al
je     11db <str_to_hex+0x8b>
add    $0x1,%rbx
lea    0xe60(%rip),%r12
lea    0x7(%rsp),%r13
jmp    11bb <str_to_hex+0x6b>
nopw   0x0(%rax,%rax,1)
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
je     11db <str_to_hex+0x8b>
movsbl %al,%edx
mov    %r13,%rdi
mov    %r12,%rsi
call   1070 <sprintf@plt>
test   %rax,%rax
je     11b0 <str_to_hex+0x60>
mov    %r15,%rdi
mov    %r13,%rsi
call   1060 <strcat@plt>
jmp    11b0 <str_to_hex+0x60>
mov    %r15,(%r14)
mov    %r15,%rdi
call   1030 <strlen@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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