#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 scalar_t__ uint8_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fprint_line(int /*<<< orphan*/ *, size_t, size_t,
                               scalar_t__ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
size_t write_hex(uint8_t *code, FILE *stream) {
  size_t i, nb;
  size_t last = 0;
  for (i = 0; i < 65536; i++) {
    if (code[i] != 0)
      last = i;
  }
  for (nb = 0; nb < last; nb += 16)
    fprint_line(stream, nb, 16, code + nb);
  nb -= 16;
  if (i)
    fprint_line(stream, nb, last - nb + 1, code + nb);
  fprintf(stream, ":00000001FF\n");
  return last;
}


x86 assembly (clang, -O0):
<write_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x10000,-0x18(%rbp)
jae    117f <write_hex+0x5f>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     1169 <write_hex+0x49>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    116e <write_hex+0x4e>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1140 <write_hex+0x20>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    11c7 <write_hex+0xa7>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x8(%rbp),%rcx
mov    -0x20(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rcx
mov    $0x10,%edx
call   1030 <fprint_line@plt>
mov    -0x20(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x20(%rbp)
jmp    1187 <write_hex+0x67>
mov    -0x20(%rbp),%rax
sub    $0x10,%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1206 <write_hex+0xe6>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
sub    -0x20(%rbp),%rdx
add    $0x1,%rdx
mov    -0x8(%rbp),%rcx
mov    -0x20(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rcx
call   1030 <fprint_line@plt>
mov    -0x10(%rbp),%rdi
lea    0xdef(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_hex>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
xor    %ebx,%ebx
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmpq   $0x0,(%r15,%rax,8)
cmovne %rax,%rbx
cmpq   $0x0,0x8(%r15,%rax,8)
lea    0x1(%rax),%rcx
cmove  %rbx,%rcx
lea    0x2(%rax),%rdx
cmpq   $0x0,0x10(%r15,%rax,8)
cmove  %rcx,%rdx
lea    0x3(%rax),%rbx
cmpq   $0x0,0x18(%r15,%rax,8)
cmove  %rdx,%rbx
add    $0x4,%rax
cmp    $0x10000,%rax
jne    1140 <write_hex+0x20>
xor    %r12d,%r12d
test   %rbx,%rbx
je     11b0 <write_hex+0x90>
mov    %r15,%r13
nopw   0x0(%rax,%rax,1)
mov    $0x10,%edx
mov    %r14,%rdi
mov    %r12,%rsi
mov    %r13,%rcx
call   1030 <fprint_line@plt>
add    $0x10,%r12
sub    $0xffffffffffffff80,%r13
cmp    %rbx,%r12
jb     1190 <write_hex+0x70>
lea    -0x10(%r12),%rsi
mov    %rbx,%rdx
sub    %r12,%rdx
add    $0x11,%rdx
lea    (%r15,%r12,8),%rcx
add    $0xffffffffffffff80,%rcx
mov    %r14,%rdi
call   1030 <fprint_line@plt>
lea    0xe2a(%rip),%rdi
mov    $0xc,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1040 <fwrite@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [13 x i8] c":00000001FF\0A\00"
define i64 @write_hex(i64* %0, i32* %1) {
%3 = alloca i64*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
store i64* %0, i64** %3
store i32* %1, i32** %4
store i64 0, i64* %7
store i64 0, i64* %5
br label %8
8: ; preds = %20, %2
%9 = load i64, i64* %5
%10 = icmp ult i64 %9, 65536
br i1 %10, label %11, label %23
11: ; preds = %8
%12 = load i64*, i64** %3
%13 = load i64, i64* %5
%14 = getelementptr inbounds i64, i64* %12, i64 %13
%15 = load i64, i64* %14
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = load i64, i64* %5
store i64 %18, i64* %7
br label %19
19: ; preds = %17, %11
br label %20
20: ; preds = %19
%21 = load i64, i64* %5
%22 = add i64 %21, 1
store i64 %22, i64* %5
br label %8
23: ; preds = %8
store i64 0, i64* %6
br label %24
24: ; preds = %35, %23
%25 = load i64, i64* %6
%26 = load i64, i64* %7
%27 = icmp ult i64 %25, %26
br i1 %27, label %28, label %38
28: ; preds = %24
%29 = load i32*, i32** %4
%30 = load i64, i64* %6
%31 = load i64*, i64** %3
%32 = load i64, i64* %6
%33 = getelementptr inbounds i64, i64* %31, i64 %32
%34 = call i32 @fprint_line(i32* %29, i64 %30, i64 16, i64* %33)
br label %35
35: ; preds = %28
%36 = load i64, i64* %6
%37 = add i64 %36, 16
store i64 %37, i64* %6
br label %24
38: ; preds = %24
%39 = load i64, i64* %6
%40 = sub i64 %39, 16
store i64 %40, i64* %6
%41 = load i64, i64* %5
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %54
43: ; preds = %38
%44 = load i32*, i32** %4
%45 = load i64, i64* %6
%46 = load i64, i64* %7
%47 = load i64, i64* %6
%48 = sub i64 %46, %47
%49 = add i64 %48, 1
%50 = load i64*, i64** %3
%51 = load i64, i64* %6
%52 = getelementptr inbounds i64, i64* %50, i64 %51
%53 = call i32 @fprint_line(i32* %44, i64 %45, i64 %49, i64* %52)
br label %54
54: ; preds = %43, %38
%55 = load i32*, i32** %4
%56 = call i32 @fprintf(i32* %55, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%57 = load i64, i64* %7
ret i64 %57
}

LLVM IR (-O2):
@.str = [13 x i8] c":00000001FF\0A\00"
define i64 @write_hex(i64* %0, i32* %1) {
br label %5
3: ; preds = %5
%4 = icmp eq i64 %26, 0
br i1 %4, label %35, label %29
5: ; preds = %5, %2
%6 = phi i64 [ 0, %2 ], [ %26, %5 ]
%7 = phi i64 [ 0, %2 ], [ %27, %5 ]
%8 = getelementptr inbounds i64, i64* %0, i64 %7
%9 = load i64, i64* %8
%10 = icmp eq i64 %9, 0
%11 = select i1 %10, i64 %6, i64 %7
%12 = or i64 %7, 1
%13 = getelementptr inbounds i64, i64* %0, i64 %12
%14 = load i64, i64* %13
%15 = icmp eq i64 %14, 0
%16 = select i1 %15, i64 %11, i64 %12
%17 = or i64 %7, 2
%18 = getelementptr inbounds i64, i64* %0, i64 %17
%19 = load i64, i64* %18
%20 = icmp eq i64 %19, 0
%21 = select i1 %20, i64 %16, i64 %17
%22 = or i64 %7, 3
%23 = getelementptr inbounds i64, i64* %0, i64 %22
%24 = load i64, i64* %23
%25 = icmp eq i64 %24, 0
%26 = select i1 %25, i64 %21, i64 %22
%27 = add nuw nsw i64 %7, 4
%28 = icmp eq i64 %27, 65536
br i1 %28, label %3, label %5
29: ; preds = %3, %29
%30 = phi i64 [ %33, %29 ], [ 0, %3 ]
%31 = getelementptr inbounds i64, i64* %0, i64 %30
%32 = tail call i32 @fprint_line(i32* %1, i64 %30, i64 16, i64* %31)
%33 = add i64 %30, 16
%34 = icmp ult i64 %33, %26
br i1 %34, label %29, label %35
35: ; preds = %29, %3
%36 = phi i64 [ 0, %3 ], [ %33, %29 ]
%37 = add i64 %36, -16
%38 = sub i64 %26, %36
%39 = add i64 %38, 17
%40 = getelementptr inbounds i64, i64* %0, i64 %37
%41 = tail call i32 @fprint_line(i32* %1, i64 %37, i64 %39, i64* %40)
%42 = tail call i64 @fwrite(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i64 12, i64 1, i32* %1)
ret i64 %26
}