#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;

/* Variables and functions */
int /*<<< orphan*/ write(int, char *, int);
void write_hex(int fd, unsigned long int hex) {
  uint8_t ii;
  uint8_t jj;
  char towrite;
  write(fd, "0x", 2);
  for (ii = sizeof(unsigned long int) * 2; ii > 0; ii--) {
    jj = ii - 1;
    uint8_t digit = ((hex & (0xF << (jj * 4))) >> (jj * 4));
    towrite = digit < 0xA ? ('0' + digit) : ('A' + (digit - 0xA));
    write(fd, &towrite, 1);
  }
}


x86 assembly (clang, -O0):
<write_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%edi
lea    0xed7(%rip),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
movl   $0x10,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11bd <write_hex+0xad>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
shl    $0x2,%ecx
mov    $0xf,%edx
shl    %cl,%edx
mov    %edx,%ecx
movslq %ecx,%rcx
and    %rcx,%rax
mov    -0x18(%rbp),%ecx
shl    $0x2,%ecx
mov    %ecx,%ecx
shr    %cl,%rax
mov    %eax,-0x20(%rbp)
cmpl   $0xa,-0x20(%rbp)
jge    118c <write_hex+0x7c>
mov    -0x20(%rbp),%eax
add    $0x30,%eax
mov    %eax,-0x24(%rbp)
jmp    1198 <write_hex+0x88>
mov    -0x20(%rbp),%eax
sub    $0xa,%eax
add    $0x41,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %al,-0x19(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    113a <write_hex+0x2a>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_hex>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%r12d
lea    0xed7(%rip),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    $0x11,%ebp
mov    $0x3c,%ebx
lea    0xf(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    $0xf,%eax
mov    %ebx,%ecx
shl    %cl,%eax
cltq
and    %r14,%rax
shr    %cl,%rax
lea    0x37(%rax),%ecx
lea    0x30(%rax),%edx
cmp    $0xa,%eax
cmovge %ecx,%edx
mov    %dl,0xf(%rsp)
mov    %r12d,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
add    $0xffffffffffffffff,%rbp
add    $0xfffffffffffffffc,%rbx
cmp    $0x1,%rbp
ja     1150 <write_hex+0x40>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"0x\00"
define void @write_hex(i32 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
%8 = alloca i32
store i32 %0, i32* %3
store i64 %1, i64* %4
%9 = load i32, i32* %3
%10 = call i32 @write(i32 %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
store i32 16, i32* %5
br label %11
11: ; preds = %42, %2
%12 = load i32, i32* %5
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %45
14: ; preds = %11
%15 = load i32, i32* %5
%16 = sub nsw i32 %15, 1
store i32 %16, i32* %6
%17 = load i64, i64* %4
%18 = load i32, i32* %6
%19 = mul nsw i32 %18, 4
%20 = shl i32 15, %19
%21 = sext i32 %20 to i64
%22 = and i64 %17, %21
%23 = load i32, i32* %6
%24 = mul nsw i32 %23, 4
%25 = zext i32 %24 to i64
%26 = lshr i64 %22, %25
%27 = trunc i64 %26 to i32
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = icmp slt i32 %28, 10
br i1 %29, label %30, label %33
30: ; preds = %14
%31 = load i32, i32* %8
%32 = add nsw i32 48, %31
br label %37
33: ; preds = %14
%34 = load i32, i32* %8
%35 = sub nsw i32 %34, 10
%36 = add nsw i32 65, %35
br label %37
37: ; preds = %33, %30
%38 = phi i32 [ %32, %30 ], [ %36, %33 ]
%39 = trunc i32 %38 to i8
store i8 %39, i8* %7
%40 = load i32, i32* %3
%41 = call i32 @write(i32 %40, i8* %7, i32 1)
br label %42
42: ; preds = %37
%43 = load i32, i32* %5
%44 = add nsw i32 %43, -1
store i32 %44, i32* %5
br label %11
45: ; preds = %11
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"0x\00"
define void @write_hex(i32 %0, i64 %1) {
%3 = alloca i8
%4 = tail call i32 @write(i32 %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
br label %5
5: ; preds = %2, %5
%6 = phi i64 [ 16, %2 ], [ %20, %5 ]
%7 = shl i64 %6, 2
%8 = add nsw i64 %7, -4
%9 = trunc i64 %8 to i32
%10 = shl i32 15, %9
%11 = sext i32 %10 to i64
%12 = and i64 %11, %1
%13 = lshr i64 %12, %8
%14 = trunc i64 %13 to i32
%15 = icmp slt i32 %14, 10
%16 = select i1 %15, i32 48, i32 55
%17 = add nsw i32 %16, %14
%18 = trunc i32 %17 to i8
store i8 %18, i8* %3
%19 = call i32 @write(i32 %0, i8* %3, i32 1)
%20 = add nsw i64 %6, -1
%21 = icmp ugt i64 %6, 1
br i1 %21, label %5, label %22
22: ; preds = %5
ret void
}