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

/* Variables and functions */
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, ...);
int print_array(uint8_t *buf, int len) {
  char *bugger = malloc(sizeof(uint8_t) * ((len * 2) + 3));
  char *ptr = bugger;
  uint8_t *ptr2 = buf;
  sprintf(ptr++, "[");
  for (int i = 0; i < len; i++) {
    sprintf(ptr, "%02X", *ptr2++);
    ptr += 2;
  }
  sprintf(ptr++, "]");
  printf("Read sector: %s\n", bugger);
  free(bugger);
  return 0;
}


x86 assembly (clang, -O0):
<print_array>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
add    $0x3,%eax
cltq
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    %rdi,%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
lea    0xe71(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11e6 <print_array+0xa6>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x28(%rbp)
mov    (%rax),%edx
lea    0xe3d(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    119d <print_array+0x5d>
mov    -0x20(%rbp),%rdi
mov    %rdi,%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
lea    0xe0b(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
mov    -0x18(%rbp),%rsi
lea    0xdfb(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_array>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r12d
mov    %rdi,%r15
lea    0xc(,%r12,8),%edi
call   1050 <malloc@plt>
mov    %rax,%r14
movw   $0x5b,(%rax)
mov    %rax,%rbx
add    $0x1,%rbx
test   %r12d,%r12d
jle    119e <print_array+0x5e>
mov    %r12d,%r13d
lea    0xe84(%rip),%r12
xor    %ebp,%ebp
xchg   %ax,%ax
mov    (%r15,%rbp,4),%edx
mov    %rbx,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1060 <sprintf@plt>
add    $0x2,%rbx
add    $0x1,%rbp
cmp    %ebp,%r13d
jne    1180 <print_array+0x40>
movw   $0x5d,(%rbx)
lea    0xe5b(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str.1 = [5 x i8] c"%02X\00"
@.str.3 = [17 x i8] c"Read sector: %s\0A\00"
define i32 @print_array(i32* nocapture %0, i32 %1) {
%3 = shl i32 %1, 3
%4 = add i32 %3, 12
%5 = tail call i8* @malloc(i32 %4)
%6 = getelementptr inbounds i8, i8* %5, i64 1
%7 = bitcast i8* %5 to i16*
store i16 91, i16* %7
%8 = icmp sgt i32 %1, 0
br i1 %8, label %14, label %9
9: ; preds = %14, %2
%10 = phi i8* [ %6, %2 ], [ %21, %14 ]
%11 = bitcast i8* %10 to i16*
store i16 93, i16* %11
%12 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %5)
%13 = tail call i32 @free(i8* %5)
ret i32 0
14: ; preds = %2, %14
%15 = phi i32 [ %22, %14 ], [ 0, %2 ]
%16 = phi i32* [ %18, %14 ], [ %0, %2 ]
%17 = phi i8* [ %21, %14 ], [ %6, %2 ]
%18 = getelementptr inbounds i32, i32* %16, i64 1
%19 = load i32, i32* %16
%20 = tail call i32 (i8*, i8*, ...) @sprintf(i8* %17, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = getelementptr inbounds i8, i8* %17, i64 2
%22 = add nuw nsw i32 %15, 1
%23 = icmp eq i32 %22, %1
br i1 %23, label %9, label %14
}