#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__ calloc(int, int);
int /*<<< orphan*/ sprintf(char *, char *, char);
char *hex(char *s, int len) {
  int i;
  char *buf = (char *)calloc((len * 2) + 1, sizeof(char));
  for (i = 0; i < len; i++)
    sprintf(&(buf[i * 2]), "%.2x", s[i] & 0xff);
  buf[len * 2] = 0;
  return buf;
}


x86 assembly (clang, -O0):
<hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
shl    $0x1,%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1030 <calloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1196 <hex+0x76>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%eax
shl    $0x1,%eax
cltq
add    %rax,%rdi
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
and    $0xff,%eax
lea    0xe80(%rip),%rsi
movsbl %al,%edx
call   1040 <sprintf@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    114d <hex+0x2d>
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%r13
movslq %esi,%rbx
lea    (%rbx,%rbx,1),%rcx
mov    %rcx,(%rsp)
lea    0x1(%rcx),%edi
mov    $0x1,%esi
call   1030 <calloc@plt>
mov    %rax,%r14
test   %ebx,%ebx
jle    117e <hex+0x5e>
mov    %ebp,%ebx
xor    %r12d,%r12d
lea    0xea5(%rip),%r15
mov    %r14,%rbp
xchg   %ax,%ax
movsbl 0x0(%r13,%r12,1),%edx
mov    %rbp,%rdi
mov    %r15,%rsi
call   1040 <sprintf@plt>
add    $0x1,%r12
add    $0x2,%rbp
cmp    %r12,%rbx
jne    1160 <hex+0x40>
mov    (%rsp),%rax
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"%.2x\00"
define i8* @hex(i8* nocapture %0, i32 %1) {
%3 = shl nsw i32 %1, 1
%4 = or i32 %3, 1
%5 = tail call i64 @calloc(i32 %4, i32 1)
%6 = inttoptr i64 %5 to i8*
%7 = icmp sgt i32 %1, 0
br i1 %7, label %8, label %19
8: ; preds = %2
%9 = zext i32 %1 to i64
br label %10
10: ; preds = %8, %10
%11 = phi i64 [ 0, %8 ], [ %17, %10 ]
%12 = shl nuw nsw i64 %11, 1
%13 = getelementptr inbounds i8, i8* %6, i64 %12
%14 = getelementptr inbounds i8, i8* %0, i64 %11
%15 = load i8, i8* %14
%16 = tail call i32 @sprintf(i8* dereferenceable(1) %13, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 signext %15)
%17 = add nuw nsw i64 %11, 1
%18 = icmp eq i64 %17, %9
br i1 %18, label %19, label %10
19: ; preds = %10, %2
%20 = sext i32 %3 to i64
%21 = getelementptr inbounds i8, i8* %6, i64 %20
store i8 0, i8* %21
ret i8* %6
}