#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 /*<<< orphan*/ sprintf(char *, char *, unsigned char);
int strlen(unsigned char *);
int string_to_hex(unsigned char *str, char *hex) {
  for (int i = 0; i < strlen(str); i++) {
    sprintf(&hex[i * 2], "%02x", str[i]);
  }
}


x86 assembly (clang, -O0):
<string_to_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
cmp    %ecx,%eax
jge    1188 <string_to_hex+0x68>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
cltq
add    %rax,%rdi
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
lea    0xe8f(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1040 <sprintf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1137 <string_to_hex+0x17>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_to_hex>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %rdi,%r14
call   1030 <strlen@plt>
test   %eax,%eax
jle    1167 <string_to_hex+0x47>
xor    %ebx,%ebx
lea    0xec0(%rip),%r15
movzbl (%r14,%rbx,1),%edx
mov    %r12,%rdi
mov    %r15,%rsi
call   1040 <sprintf@plt>
add    $0x1,%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
cltq
add    $0x2,%r12
cmp    %rax,%rbx
jl     1140 <string_to_hex+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%02x\00"
define i32 @string_to_hex(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %6
br label %7
7: ; preds = %24, %2
%8 = load i32, i32* %6
%9 = load i8*, i8** %4
%10 = call i32 @strlen(i8* %9)
%11 = icmp slt i32 %8, %10
br i1 %11, label %12, label %27
12: ; preds = %7
%13 = load i8*, i8** %5
%14 = load i32, i32* %6
%15 = mul nsw i32 %14, 2
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %13, i64 %16
%18 = load i8*, i8** %4
%19 = load i32, i32* %6
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = load i8, i8* %21
%23 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %22)
br label %24
24: ; preds = %12
%25 = load i32, i32* %6
%26 = add nsw i32 %25, 1
store i32 %26, i32* %6
br label %7
27: ; preds = %7
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
@.str = [5 x i8] c"%02x\00"
define i32 @string_to_hex(i8* %0, i8* nocapture writeonly %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = icmp sgt i32 %3, 0
br i1 %4, label %6, label %5
5: ; preds = %6, %2
ret i32 undef
6: ; preds = %2, %6
%7 = phi i64 [ %13, %6 ], [ 0, %2 ]
%8 = shl nuw nsw i64 %7, 1
%9 = getelementptr inbounds i8, i8* %1, i64 %8
%10 = getelementptr inbounds i8, i8* %0, i64 %7
%11 = load i8, i8* %10
%12 = tail call i32 @sprintf(i8* dereferenceable(1) %9, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %11)
%13 = add nuw nsw i64 %7, 1
%14 = tail call i32 @strlen(i8* %0)
%15 = sext i32 %14 to i64
%16 = icmp slt i64 %13, %15
br i1 %16, label %6, label %5
}