#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 u_int8_t;
typedef int u_int64_t;

/* Variables and functions */
int /*<<< orphan*/ hex(int);
int htobe64(int);
int /*<<< orphan*/ write_char(int /*<<< orphan*/);
int /*<<< orphan*/ write_string(char *);
void write_hex(u_int64_t v) {
  write_string("0x");
  if (!v) {
    write_string("0 ");
    return;
  }
  v = htobe64(v);
  int leading_zero = 1;
  while (v) {
    u_int8_t byte = v & 0xff;
    if (byte)
      leading_zero = 0;
    if (!leading_zero) {
      write_char(hex(byte >> 4));
      write_char(hex(byte & 0xf));
    }
    v = v >> 8;
  }
  write_string(" ");
}


x86 assembly (clang, -O0):
<write_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
lea    0xeae(%rip),%rdi
call   1030 <write_string@plt>
cmpl   $0x0,-0x4(%rbp)
jne    1172 <write_hex+0x32>
lea    0xe9b(%rip),%rdi
call   1030 <write_string@plt>
jmp    11f2 <write_hex+0xb2>
mov    -0x4(%rbp),%edi
call   1050 <htobe64@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x8(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     11e6 <write_hex+0xa6>
mov    -0x4(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11aa <write_hex+0x6a>
movl   $0x0,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    11d8 <write_hex+0x98>
mov    -0xc(%rbp),%edi
sar    $0x4,%edi
call   1060 <hex@plt>
mov    %eax,%edi
call   1040 <write_char@plt>
mov    -0xc(%rbp),%edi
and    $0xf,%edi
call   1060 <hex@plt>
mov    %eax,%edi
call   1040 <write_char@plt>
mov    -0x4(%rbp),%eax
sar    $0x8,%eax
mov    %eax,-0x4(%rbp)
jmp    1184 <write_hex+0x44>
lea    0xe17(%rip),%rdi
call   1030 <write_string@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_hex>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0xeb0(%rip),%rdi
call   1030 <write_string@plt>
test   %ebx,%ebx
je     11c1 <write_hex+0x81>
mov    %ebx,%edi
call   1050 <htobe64@plt>
lea    0xe9d(%rip),%r14
test   %eax,%eax
je     11c8 <write_hex+0x88>
mov    %eax,%ebp
mov    $0x1,%ebx
xor    %r15d,%r15d
jmp    118f <write_hex+0x4f>
nopw   0x0(%rax,%rax,1)
mov    %ebp,%eax
sar    $0x8,%eax
cmp    $0xff,%ebp
mov    %eax,%ebp
jbe    11c8 <write_hex+0x88>
mov    %ebp,%edi
and    $0xff,%edi
cmovne %r15d,%ebx
test   %ebx,%ebx
jne    1180 <write_hex+0x40>
shr    $0x4,%edi
call   1060 <hex@plt>
mov    %eax,%edi
call   1040 <write_char@plt>
mov    %ebp,%edi
and    $0xf,%edi
call   1060 <hex@plt>
mov    %eax,%edi
call   1040 <write_char@plt>
jmp    1180 <write_hex+0x40>
lea    0xe3b(%rip),%r14
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <write_string@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"0x\00"
@.str.1 = [3 x i8] c"0 \00"
@.str.2 = [2 x i8] c" \00"
define void @write_hex(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = call i32 @write_string(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* %2
%7 = icmp ne i32 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %1
%9 = call i32 @write_string(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %39
10: ; preds = %1
%11 = load i32, i32* %2
%12 = call i32 @htobe64(i32 %11)
store i32 %12, i32* %2
store i32 1, i32* %3
br label %13
13: ; preds = %34, %10
%14 = load i32, i32* %2
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %37
16: ; preds = %13
%17 = load i32, i32* %2
%18 = and i32 %17, 255
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %16
store i32 0, i32* %3
br label %22
22: ; preds = %21, %16
%23 = load i32, i32* %3
%24 = icmp ne i32 %23, 0
br i1 %24, label %34, label %25
25: ; preds = %22
%26 = load i32, i32* %4
%27 = ashr i32 %26, 4
%28 = call i32 @hex(i32 %27)
%29 = call i32 @write_char(i32 %28)
%30 = load i32, i32* %4
%31 = and i32 %30, 15
%32 = call i32 @hex(i32 %31)
%33 = call i32 @write_char(i32 %32)
br label %34
34: ; preds = %25, %22
%35 = load i32, i32* %2
%36 = ashr i32 %35, 8
store i32 %36, i32* %2
br label %13
37: ; preds = %13
%38 = call i32 @write_string(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %39
39: ; preds = %37, %8
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"0x\00"
@.str.1 = [3 x i8] c"0 \00"
@.str.2 = [2 x i8] c" \00"
define void @write_hex(i32 %0) {
%2 = tail call i32 @write_string(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32 %0, 0
br i1 %3, label %24, label %4
4: ; preds = %1
%5 = tail call i32 @htobe64(i32 %0)
%6 = icmp eq i32 %5, 0
br i1 %6, label %24, label %7
7: ; preds = %4, %21
%8 = phi i32 [ %22, %21 ], [ %5, %4 ]
%9 = phi i32 [ %12, %21 ], [ 1, %4 ]
%10 = and i32 %8, 255
%11 = icmp eq i32 %10, 0
%12 = select i1 %11, i32 %9, i32 0
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %21
14: ; preds = %7
%15 = lshr i32 %10, 4
%16 = tail call i32 @hex(i32 %15)
%17 = tail call i32 @write_char(i32 %16)
%18 = and i32 %8, 15
%19 = tail call i32 @hex(i32 %18)
%20 = tail call i32 @write_char(i32 %19)
br label %21
21: ; preds = %14, %7
%22 = ashr i32 %8, 8
%23 = icmp ult i32 %8, 256
br i1 %23, label %24, label %7
24: ; preds = %21, %4, %1
%25 = phi i8* [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), %1 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), %4 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), %21 ]
%26 = tail call i32 @write_string(i8* %25)
ret void
}