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

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, unsigned char);
int /*<<< orphan*/ printf(char *, unsigned char);
void write_hex(long long length, unsigned char *bytes, FILE *f) {
  long long i;
  for (i = 0; i < length; i++) {
    printf("%02h", bytes[i]);
    fprintf(f, "%02h", bytes[i]);
  }
}


x86 assembly (clang, -O0):
<write_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x8(%rbp),%rax
jge    118f <write_hex+0x6f>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
lea    0xea7(%rip),%rdi
movzbl (%rax,%rcx,1),%esi
call   1030 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
lea    0xe8b(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1040 <fprintf@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    113c <write_hex+0x1c>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_hex>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
test   %rdi,%rdi
jle    1168 <write_hex+0x48>
mov    %rdx,%r14
mov    %rsi,%r13
mov    %rdi,%r15
xor    %ebx,%ebx
lea    0xec0(%rip),%r12
movzbl 0x0(%r13,%rbx,1),%esi
mov    %r12,%rdi
call   1030 <printf@plt>
movzbl 0x0(%r13,%rbx,1),%edx
mov    %r14,%rdi
mov    %r12,%rsi
call   1040 <fprintf@plt>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1140 <write_hex+0x20>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%02h\00"
define void @write_hex(i64 %0, i8* %1, i32* %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i64
store i64 %0, i64* %4
store i8* %1, i8** %5
store i32* %2, i32** %6
store i64 0, i64* %7
br label %8
8: ; preds = %24, %3
%9 = load i64, i64* %7
%10 = load i64, i64* %4
%11 = icmp slt i64 %9, %10
br i1 %11, label %12, label %27
12: ; preds = %8
%13 = load i8*, i8** %5
%14 = load i64, i64* %7
%15 = getelementptr inbounds i8, i8* %13, i64 %14
%16 = load i8, i8* %15
%17 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %16)
%18 = load i32*, i32** %6
%19 = load i8*, i8** %5
%20 = load i64, i64* %7
%21 = getelementptr inbounds i8, i8* %19, i64 %20
%22 = load i8, i8* %21
%23 = call i32 @fprintf(i32* %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %22)
br label %24
24: ; preds = %12
%25 = load i64, i64* %7
%26 = add nsw i64 %25, 1
store i64 %26, i64* %7
br label %8
27: ; preds = %8
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"%02h\00"
define void @write_hex(i64 %0, i8* nocapture %1, i32* nocapture %2) {
%4 = icmp sgt i64 %0, 0
br i1 %4, label %5, label %14
5: ; preds = %3, %5
%6 = phi i64 [ %12, %5 ], [ 0, %3 ]
%7 = getelementptr inbounds i8, i8* %1, i64 %6
%8 = load i8, i8* %7
%9 = tail call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %8)
%10 = load i8, i8* %7
%11 = tail call i32 @fprintf(i32* %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %10)
%12 = add nuw nsw i64 %6, 1
%13 = icmp eq i64 %12, %0
br i1 %13, label %14, label %5
14: ; preds = %5, %3
ret void
}