#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 */
typedef int /*<<< orphan*/ str;

/* Variables and functions */
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
char *hash_string(unsigned char *buf, int len) {
  static char str[32 * 2 + 1];
  int i;
  if (len * 2 + 1 > sizeof(str))
    return NULL;
  for (i = 0; i < len; i++)
    sprintf(&str[i * 2], "%02x", buf[i]);
  return str;
}


x86 assembly (clang, -O0):
<hash_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
add    $0x1,%eax
cltq
cmp    $0x41,%rax
jbe    1141 <hash_string+0x31>
movq   $0x0,-0x8(%rbp)
jmp    1197 <hash_string+0x87>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    118c <hash_string+0x7c>
mov    -0x18(%rbp),%eax
shl    $0x1,%eax
cltq
lea    0x2edd(%rip),%rdi
add    %rax,%rdi
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
lea    0xe8b(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1030 <sprintf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1148 <hash_string+0x38>
lea    0x2ead(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hash_string>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    (%rsi,%rsi,1),%eax
cmp    $0x41,%eax
jbe    1128 <hash_string+0x18>
xor    %r14d,%r14d
jmp    1176 <hash_string+0x66>
test   %esi,%esi
jle    116f <hash_string+0x5f>
mov    %rdi,%r12
mov    %esi,%r13d
lea    0x2f07(%rip),%r14
xor    %ebp,%ebp
lea    0xebe(%rip),%r15
mov    %r14,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
movzbl (%r12,%rbp,1),%edx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1030 <sprintf@plt>
add    $0x1,%rbp
add    $0x2,%rbx
cmp    %rbp,%r13
jne    1150 <hash_string+0x40>
jmp    1176 <hash_string+0x66>
lea    0x2eca(%rip),%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@hash_string.str = internal global [65 x i8] zeroinitializer
@.str = [5 x i8] c"%02x\00"
define i8* @hash_string(i8* nocapture %0, i32 %1) {
%3 = shl nsw i32 %1, 1
%4 = icmp ugt i32 %3, 65
br i1 %4, label %18, label %5
5: ; preds = %2
%6 = icmp sgt i32 %1, 0
br i1 %6, label %7, label %18
7: ; preds = %5
%8 = zext i32 %1 to i64
br label %9
9: ; preds = %7, %9
%10 = phi i64 [ 0, %7 ], [ %16, %9 ]
%11 = shl nuw nsw i64 %10, 1
%12 = getelementptr inbounds [65 x i8], [65 x i8]* @hash_string.str, i64 0, i64 %11
%13 = getelementptr inbounds i8, i8* %0, i64 %10
%14 = load i8, i8* %13
%15 = tail call i32 @sprintf(i8* %12, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %14)
%16 = add nuw nsw i64 %10, 1
%17 = icmp eq i64 %16, %8
br i1 %17, label %18, label %9
18: ; preds = %9, %5, %2
%19 = phi i8* [ null, %2 ], [ getelementptr inbounds ([65 x i8], [65 x i8]* @hash_string.str, i64 0, i64 0), %5 ], [ getelementptr inbounds ([65 x i8], [65 x i8]* @hash_string.str, i64 0, i64 0), %9 ]
ret i8* %19
}