#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__ malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
char *md2str(unsigned char *digest, char *s) {
  int i;
  char buf[16], *r;
  if (!digest)
    return NULL;
  if (!s) {
    s = (char *)malloc(33);
    if (!s)
      return NULL;
  }
  r = s;
  for (i = 0; i < 16; i++) {
    sprintf(buf, "%02x", digest[i]);
    *(s++) = buf[0];
    *(s++) = buf[1];
  }
  *s = 0;
  return r;
}


x86 assembly (clang, -O0):
<md2str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1148 <md2str+0x28>
movq   $0x0,-0x8(%rbp)
jmp    11f8 <md2str+0xd8>
cmpq   $0x0,-0x18(%rbp)
jne    117e <md2str+0x5e>
mov    $0x21,%edi
call   1030 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1179 <md2str+0x59>
movq   $0x0,-0x8(%rbp)
jmp    11f8 <md2str+0xd8>
jmp    117e <md2str+0x5e>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x10,-0x1c(%rbp)
jge    11e9 <md2str+0xc9>
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
lea    0xe56(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1040 <sprintf@plt>
mov    -0x30(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x2f(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    118d <md2str+0x6d>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<md2str>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %rdi,%rdi
je     1195 <md2str+0x75>
mov    %rsi,%r13
mov    %rdi,%r14
test   %rsi,%rsi
jne    114f <md2str+0x2f>
mov    $0x21,%edi
call   1030 <malloc@plt>
mov    %rax,%r13
test   %rax,%rax
je     1195 <md2str+0x75>
xor    %ebx,%ebx
lea    0xea8(%rip),%r15
mov    %rsp,%r12
nopl   0x0(%rax,%rax,1)
movzbl (%r14,%rbx,1),%edx
mov    %r12,%rdi
mov    %r15,%rsi
call   1040 <sprintf@plt>
movzbl (%rsp),%eax
mov    %al,0x0(%r13,%rbx,2)
movzbl 0x1(%rsp),%eax
mov    %al,0x1(%r13,%rbx,2)
add    $0x1,%rbx
cmp    $0x10,%rbx
jne    1160 <md2str+0x40>
movb   $0x0,0x0(%r13,%rbx,2)
jmp    1198 <md2str+0x78>
xor    %r13d,%r13d
mov    %r13,%rax
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 i8* @md2str(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [16 x i8]
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = icmp ne i8* %9, null
br i1 %10, label %12, label %11
11: ; preds = %2
store i8* null, i8** %3
br label %49
12: ; preds = %2
%13 = load i8*, i8** %5
%14 = icmp ne i8* %13, null
br i1 %14, label %22, label %15
15: ; preds = %12
%16 = call i64 @malloc(i32 33)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %5
%18 = load i8*, i8** %5
%19 = icmp ne i8* %18, null
br i1 %19, label %21, label %20
20: ; preds = %15
store i8* null, i8** %3
br label %49
21: ; preds = %15
br label %22
22: ; preds = %21, %12
%23 = load i8*, i8** %5
store i8* %23, i8** %8
store i32 0, i32* %6
br label %24
24: ; preds = %43, %22
%25 = load i32, i32* %6
%26 = icmp slt i32 %25, 16
br i1 %26, label %27, label %46
27: ; preds = %24
%28 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%29 = load i8*, i8** %4
%30 = load i32, i32* %6
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
%33 = load i8, i8* %32
%34 = call i32 @sprintf(i8* %28, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %33)
%35 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%36 = load i8, i8* %35
%37 = load i8*, i8** %5
%38 = getelementptr inbounds i8, i8* %37, i32 1
store i8* %38, i8** %5
store i8 %36, i8* %37
%39 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 1
%40 = load i8, i8* %39
%41 = load i8*, i8** %5
%42 = getelementptr inbounds i8, i8* %41, i32 1
store i8* %42, i8** %5
store i8 %40, i8* %41
br label %43
43: ; preds = %27
%44 = load i32, i32* %6
%45 = add nsw i32 %44, 1
store i32 %45, i32* %6
br label %24
46: ; preds = %24
%47 = load i8*, i8** %5
store i8 0, i8* %47
%48 = load i8*, i8** %8
store i8* %48, i8** %3
br label %49
49: ; preds = %46, %20, %11
%50 = load i8*, i8** %3
ret i8* %50
}

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