#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 uint8_t;
typedef int uint32_t;

/* Variables and functions */
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, int);
uint8_t *toHex(uint32_t num) {
  printf("converting int: %i", num);
  uint8_t *hex = (uint8_t *)malloc(sizeof(uint8_t) * 4);
  hex[0] = num >> 24;
  hex[1] = (num & 0x00ff0000) >> 16;
  hex[2] = (num & 0x0000ff00) >> 8;
  hex[3] = (num & 0x000000ff);
  printf("Hex should be: %02x", num);
  return hex;
}


x86 assembly (clang, -O0):
<toHex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%esi
lea    0xecb(%rip),%rdi
call   1030 <printf@plt>
mov    $0x10,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%ecx
sar    $0x18,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x4(%rbp),%ecx
and    $0xff0000,%ecx
sar    $0x10,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x4(%rbp),%ecx
and    $0xff00,%ecx
sar    $0x8,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x4(%rbp),%ecx
and    $0xff,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0xc(%rax)
mov    -0x4(%rbp),%esi
lea    0xe7f(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<toHex>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
lea    0xed4(%rip),%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
mov    $0x10,%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
mov    %ebp,%ecx
mov    %ebp,%eax
sar    $0x18,%eax
mov    %eax,(%rbx)
mov    %ebp,%eax
shr    $0x10,%eax
movzbl %al,%eax
mov    %eax,0x4(%rbx)
movzbl %ch,%eax
mov    %eax,0x8(%rbx)
movzbl %cl,%eax
mov    %ebp,%esi
mov    %eax,0xc(%rbx)
lea    0xeaa(%rip),%rdi
call   1030 <printf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c"converting int: %i\00"
@.str.1 = [20 x i8] c"Hex should be: %02x\00"
define i32* @toHex(i32 %0) {
%2 = alloca i32
%3 = alloca i32*
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = call i64 @malloc(i32 16)
%7 = inttoptr i64 %6 to i32*
store i32* %7, i32** %3
%8 = load i32, i32* %2
%9 = ashr i32 %8, 24
%10 = load i32*, i32** %3
%11 = getelementptr inbounds i32, i32* %10, i64 0
store i32 %9, i32* %11
%12 = load i32, i32* %2
%13 = and i32 %12, 16711680
%14 = ashr i32 %13, 16
%15 = load i32*, i32** %3
%16 = getelementptr inbounds i32, i32* %15, i64 1
store i32 %14, i32* %16
%17 = load i32, i32* %2
%18 = and i32 %17, 65280
%19 = ashr i32 %18, 8
%20 = load i32*, i32** %3
%21 = getelementptr inbounds i32, i32* %20, i64 2
store i32 %19, i32* %21
%22 = load i32, i32* %2
%23 = and i32 %22, 255
%24 = load i32*, i32** %3
%25 = getelementptr inbounds i32, i32* %24, i64 3
store i32 %23, i32* %25
%26 = load i32, i32* %2
%27 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%28 = load i32*, i32** %3
ret i32* %28
}

LLVM IR (-O2):
@.str = [19 x i8] c"converting int: %i\00"
@.str.1 = [20 x i8] c"Hex should be: %02x\00"
define i32* @toHex(i32 %0) {
%2 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %0)
%3 = tail call i64 @malloc(i32 16)
%4 = inttoptr i64 %3 to i32*
%5 = ashr i32 %0, 24
store i32 %5, i32* %4
%6 = lshr i32 %0, 16
%7 = and i32 %6, 255
%8 = getelementptr inbounds i32, i32* %4, i64 1
store i32 %7, i32* %8
%9 = lshr i32 %0, 8
%10 = and i32 %9, 255
%11 = getelementptr inbounds i32, i32* %4, i64 2
store i32 %10, i32* %11
%12 = and i32 %0, 255
%13 = getelementptr inbounds i32, i32* %4, i64 3
store i32 %12, i32* %13
%14 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %0)
ret i32* %4
}