#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*/ num;

/* Variables and functions */
scalar_t__ malloc(int);
char *toHex(int num) {
  static const size_t BUFFER_SIZE = 1 + 2 * sizeof(num);
  static const char *HEX_LOOKUP = "0123456789abcdef";
  static const int BASE = 16;
  unsigned bits = (unsigned)num;
  char *hex = (char *)malloc(sizeof(*hex) * BUFFER_SIZE);
  if (hex) {
    char *pchar = hex;
    do {
      *pchar++ = HEX_LOOKUP[bits % BASE];
      bits /= BASE;
    } while (bits > 0);
    *pchar = '\0';
    char *begin = hex;
    char *end = pchar - 1;
    while (begin < end) {
      const char temp = *begin;
      *begin = *end;
      *end = temp;
      ++begin;
      --end;
    }
  }
  return hex;
}


x86 assembly (clang, -O0):
<toHex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    $0x9,%edi
call   1030 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11e1 <toHex+0xd1>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2edf(%rip),%rax
mov    -0x8(%rbp),%ecx
and    $0xf,%ecx
mov    %ecx,%ecx
mov    (%rax,%rcx,1),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x8(%rbp),%eax
shr    $0x4,%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
ja     1142 <toHex+0x32>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    11dc <toHex+0xcc>
mov    -0x20(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x29(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x29(%rbp),%cl
mov    -0x28(%rbp),%rax
mov    %cl,(%rax)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
jmp    1193 <toHex+0x83>
jmp    11e1 <toHex+0xd1>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<toHex>:
push   %rbx
mov    %edi,%ebx
mov    $0x9,%edi
call   1030 <malloc@plt>
test   %rax,%rax
je     119d <toHex+0x8d>
mov    $0xffffffffffffffff,%rcx
lea    0xed0(%rip),%rsi
mov    %ebx,%edx
and    $0xf,%edx
movzbl (%rdx,%rsi,1),%edx
mov    %dl,0x1(%rax,%rcx,1)
mov    %ebx,%edi
shr    $0x4,%edi
add    $0x1,%rcx
cmp    $0xf,%ebx
mov    %edi,%ebx
ja     1130 <toHex+0x20>
lea    (%rax,%rcx,1),%rsi
movb   $0x0,0x1(%rax,%rcx,1)
cmp    %rax,%rsi
jbe    119d <toHex+0x8d>
mov    (%rax),%bl
mov    %dl,(%rax)
mov    %bl,(%rax,%rcx,1)
cmp    $0x3,%rcx
jb     119d <toHex+0x8d>
add    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rax,%rdx
add    $0x2,%rdx
cs nopw 0x0(%rax,%rax,1)
movzbl (%rcx),%ebx
movzbl -0x1(%rdx),%esi
mov    %bl,-0x1(%rdx)
mov    %sil,(%rcx)
add    $0xffffffffffffffff,%rcx
lea    0x1(%rdx),%rsi
cmp    %rcx,%rdx
mov    %rsi,%rdx
jb     1180 <toHex+0x70>
pop    %rbx
ret

LLVM IR (-O0):
@toHex.BUFFER_SIZE = internal constant i64 9
@toHex.HEX_LOOKUP = internal global i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i32 0, i32 0)
@.str = [17 x i8] c"0123456789abcdef\00"
@toHex.BASE = internal constant i32 16
define i8* @toHex(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8
store i32 %0, i32* %2
%9 = load i32, i32* %2
store i32 %9, i32* %3
%10 = call i64 @malloc(i32 9)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %4
%12 = load i8*, i8** %4
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %52
14: ; preds = %1
%15 = load i8*, i8** %4
store i8* %15, i8** %5
br label %16
16: ; preds = %27, %14
%17 = load i8*, i8** @toHex.HEX_LOOKUP
%18 = load i32, i32* %3
%19 = urem i32 %18, 16
%20 = zext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %17, i64 %20
%22 = load i8, i8* %21
%23 = load i8*, i8** %5
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %5
store i8 %22, i8* %23
%25 = load i32, i32* %3
%26 = udiv i32 %25, 16
store i32 %26, i32* %3
br label %27
27: ; preds = %16
%28 = load i32, i32* %3
%29 = icmp ugt i32 %28, 0
br i1 %29, label %16, label %30
30: ; preds = %27
%31 = load i8*, i8** %5
store i8 0, i8* %31
%32 = load i8*, i8** %4
store i8* %32, i8** %6
%33 = load i8*, i8** %5
%34 = getelementptr inbounds i8, i8* %33, i64 -1
store i8* %34, i8** %7
br label %35
35: ; preds = %39, %30
%36 = load i8*, i8** %6
%37 = load i8*, i8** %7
%38 = icmp ult i8* %36, %37
br i1 %38, label %39, label %51
39: ; preds = %35
%40 = load i8*, i8** %6
%41 = load i8, i8* %40
store i8 %41, i8* %8
%42 = load i8*, i8** %7
%43 = load i8, i8* %42
%44 = load i8*, i8** %6
store i8 %43, i8* %44
%45 = load i8, i8* %8
%46 = load i8*, i8** %7
store i8 %45, i8* %46
%47 = load i8*, i8** %6
%48 = getelementptr inbounds i8, i8* %47, i32 1
store i8* %48, i8** %6
%49 = load i8*, i8** %7
%50 = getelementptr inbounds i8, i8* %49, i32 -1
store i8* %50, i8** %7
br label %35
51: ; preds = %35
br label %52
52: ; preds = %51, %1
%53 = load i8*, i8** %4
ret i8* %53
}

LLVM IR (-O2):
@.str = [17 x i8] c"0123456789abcdef\00"
define i8* @toHex(i32 %0) {
%2 = tail call i64 @malloc(i32 9)
%3 = inttoptr i64 %2 to i8*
%4 = icmp eq i64 %2, 0
br i1 %4, label %40, label %5
5: ; preds = %1
%6 = inttoptr i64 %2 to i8*
br label %7
7: ; preds = %5, %7
%8 = phi i64 [ %15, %7 ], [ 0, %5 ]
%9 = phi i32 [ %16, %7 ], [ %0, %5 ]
%10 = getelementptr inbounds i8, i8* %6, i64 %8
%11 = and i32 %9, 15
%12 = zext i32 %11 to i64
%13 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %12
%14 = load i8, i8* %13
%15 = add nuw nsw i64 %8, 1
store i8 %14, i8* %10
%16 = lshr i32 %9, 4
%17 = icmp ult i32 %9, 16
br i1 %17, label %18, label %7
18: ; preds = %7
%19 = inttoptr i64 %2 to i8*
%20 = getelementptr inbounds i8, i8* %19, i64 %8
%21 = inttoptr i64 %2 to i8*
%22 = getelementptr inbounds i8, i8* %21, i64 %15
store i8 0, i8* %22
%23 = icmp ugt i8* %20, %3
br i1 %23, label %24, label %40
24: ; preds = %18
%25 = load i8, i8* %3
store i8 %14, i8* %3
store i8 %25, i8* %20
%26 = icmp ugt i64 %8, 2
br i1 %26, label %27, label %40
27: ; preds = %24
%28 = add nsw i64 %8, -1
%29 = inttoptr i64 %2 to i8*
%30 = getelementptr inbounds i8, i8* %29, i64 %28
%31 = getelementptr inbounds i8, i8* %3, i64 1
br label %32
32: ; preds = %27, %32
%33 = phi i8* [ %30, %27 ], [ %38, %32 ]
%34 = phi i8* [ %31, %27 ], [ %37, %32 ]
%35 = load i8, i8* %33
%36 = load i8, i8* %34
store i8 %35, i8* %34
store i8 %36, i8* %33
%37 = getelementptr inbounds i8, i8* %34, i64 1
%38 = getelementptr inbounds i8, i8* %33, i64 -1
%39 = icmp ult i8* %37, %38
br i1 %39, label %32, label %40
40: ; preds = %32, %24, %18, %1
ret i8* %3
}