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

/* Variables and functions */
int /*<<< orphan*/ tty_puts(int, char *);
void tty_puthex(int chan, unsigned long val, int length) {
  char hex[] = "0123456789ABCDEF";
  char str[16];
  char *s = &str[length + 3];
  int i;
  *--s = '\0';
  for (i = 0; i < length; i++) {
    *--s = hex[(val & 0x0F)];
    val >>= 4;
  }
  *--s = 'x';
  *--s = '0';
  tty_puts(chan, s);
}


x86 assembly (clang, -O0):
<tty_puthex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    0xed7(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xed4(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xed2(%rip),%al
mov    %al,-0x20(%rbp)
mov    -0x14(%rbp),%eax
add    $0x3,%eax
movslq %eax,%rcx
lea    -0x40(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x48(%rbp)
movb   $0x0,-0x1(%rax)
movl   $0x0,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11b3 <tty_puthex+0xa3>
mov    -0x10(%rbp),%rax
and    $0xf,%rax
mov    -0x30(%rbp,%rax,1),%cl
mov    -0x48(%rbp),%rax
mov    %rax,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x48(%rbp)
mov    %cl,-0x1(%rax)
mov    -0x10(%rbp),%rax
shr    $0x4,%rax
mov    %rax,-0x10(%rbp)
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    116f <tty_puthex+0x5f>
mov    -0x48(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x48(%rbp)
movb   $0x78,-0x1(%rax)
mov    -0x48(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x48(%rbp)
movb   $0x30,-0x1(%rax)
mov    -0x4(%rbp),%edi
mov    -0x48(%rbp),%rsi
call   1030 <tty_puts@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tty_puthex>:
sub    $0x18,%rsp
mov    %rsi,%rcx
movslq %edx,%r8
lea    (%rsp,%r8,1),%rsi
add    $0x2,%rsi
movb   $0x0,0x2(%rsp,%r8,1)
test   %r8d,%r8d
jle    11da <tty_puthex+0xca>
lea    -0x1(%rdx),%r9d
mov    %edx,%r10d
and    $0x3,%r10d
cmp    $0x3,%r9d
jb     11aa <tty_puthex+0x9a>
add    $0x3,%r8
and    $0xfffffffc,%edx
mov    %rsp,%rsi
add    %r8,%rsi
add    $0xffffffffffffffff,%rsi
lea    0xea6(%rip),%r8
nopw   0x0(%rax,%rax,1)
mov    %ecx,%eax
and    $0xf,%eax
movzbl (%rax,%r8,1),%eax
mov    %al,-0x1(%rsi)
mov    %ecx,%eax
shr    $0x4,%eax
and    $0xf,%eax
movzbl (%rax,%r8,1),%eax
mov    %al,-0x2(%rsi)
mov    %ecx,%eax
shr    $0x8,%eax
and    $0xf,%eax
movzbl (%rax,%r8,1),%eax
mov    %al,-0x3(%rsi)
mov    %ecx,%eax
shr    $0xc,%eax
and    $0xf,%eax
movzbl (%rax,%r8,1),%eax
mov    %al,-0x4(%rsi)
add    $0xfffffffffffffffc,%rsi
shr    $0x10,%rcx
add    $0xfffffffc,%edx
jne    1160 <tty_puthex+0x50>
test   %r10d,%r10d
je     11da <tty_puthex+0xca>
lea    0xe4a(%rip),%rdx
cs nopw 0x0(%rax,%rax,1)
mov    %ecx,%eax
and    $0xf,%eax
movzbl (%rax,%rdx,1),%eax
mov    %al,-0x1(%rsi)
add    $0xffffffffffffffff,%rsi
shr    $0x4,%rcx
add    $0xffffffff,%r10d
jne    11c0 <tty_puthex+0xb0>
movw   $0x7830,-0x2(%rsi)
add    $0xfffffffffffffffe,%rsi
call   1030 <tty_puts@plt>
add    $0x18,%rsp
ret

LLVM IR (-O0):
@__const.tty_puthex.hex = [17 x i8] c"0123456789ABCDEF\00"
define void @tty_puthex(i32 %0, i64 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32
%7 = alloca [17 x i8]
%8 = alloca [16 x i8]
%9 = alloca i8*
%10 = alloca i32
store i32 %0, i32* %4
store i64 %1, i64* %5
store i32 %2, i32* %6
%11 = bitcast [17 x i8]* %7 to i8*
%12 = load i32, i32* %6
%13 = add nsw i32 %12, 3
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 %14
store i8* %15, i8** %9
%16 = load i8*, i8** %9
%17 = getelementptr inbounds i8, i8* %16, i32 -1
store i8* %17, i8** %9
store i8 0, i8* %17
store i32 0, i32* %10
br label %18
18: ; preds = %31, %3
%19 = load i32, i32* %10
%20 = load i32, i32* %6
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %34
22: ; preds = %18
%23 = load i64, i64* %5
%24 = and i64 %23, 15
%25 = getelementptr inbounds [17 x i8], [17 x i8]* %7, i64 0, i64 %24
%26 = load i8, i8* %25
%27 = load i8*, i8** %9
%28 = getelementptr inbounds i8, i8* %27, i32 -1
store i8* %28, i8** %9
store i8 %26, i8* %28
%29 = load i64, i64* %5
%30 = lshr i64 %29, 4
store i64 %30, i64* %5
br label %31
31: ; preds = %22
%32 = load i32, i32* %10
%33 = add nsw i32 %32, 1
store i32 %33, i32* %10
br label %18
34: ; preds = %18
%35 = load i8*, i8** %9
%36 = getelementptr inbounds i8, i8* %35, i32 -1
store i8* %36, i8** %9
store i8 120, i8* %36
%37 = load i8*, i8** %9
%38 = getelementptr inbounds i8, i8* %37, i32 -1
store i8* %38, i8** %9
store i8 48, i8* %38
%39 = load i32, i32* %4
%40 = load i8*, i8** %9
%41 = call i32 @tty_puts(i32 %39, i8* %40)
ret void
}

LLVM IR (-O2):
@__const.tty_puthex.hex = [17 x i8] c"0123456789ABCDEF\00"
define void @tty_puthex(i32 %0, i64 %1, i32 %2) {
%4 = alloca [16 x i8]
%5 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%6 = add nsw i32 %2, 3
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 %7
%9 = getelementptr inbounds i8, i8* %8, i64 -1
store i8 0, i8* %9
%10 = icmp sgt i32 %2, 0
br i1 %10, label %11, label %61
11: ; preds = %3
%12 = add i32 %2, -1
%13 = and i32 %2, 3
%14 = icmp ult i32 %12, 3
br i1 %14, label %45, label %15
15: ; preds = %11
%16 = and i32 %2, -4
br label %17
17: ; preds = %17, %15
%18 = phi i8* [ %8, %15 ], [ %34, %17 ]
%19 = phi i64 [ %1, %15 ], [ %40, %17 ]
%20 = phi i32 [ 0, %15 ], [ %41, %17 ]
%21 = and i64 %19, 15
%22 = getelementptr inbounds [17 x i8], [17 x i8]* @__const.tty_puthex.hex, i64 0, i64 %21
%23 = load i8, i8* %22
%24 = getelementptr inbounds i8, i8* %18, i64 -2
store i8 %23, i8* %24
%25 = lshr i64 %19, 4
%26 = and i64 %25, 15
%27 = getelementptr inbounds [17 x i8], [17 x i8]* @__const.tty_puthex.hex, i64 0, i64 %26
%28 = load i8, i8* %27
%29 = getelementptr inbounds i8, i8* %18, i64 -3
store i8 %28, i8* %29
%30 = lshr i64 %19, 8
%31 = and i64 %30, 15
%32 = getelementptr inbounds [17 x i8], [17 x i8]* @__const.tty_puthex.hex, i64 0, i64 %31
%33 = load i8, i8* %32
%34 = getelementptr inbounds i8, i8* %18, i64 -4
store i8 %33, i8* %34
%35 = lshr i64 %19, 12
%36 = and i64 %35, 15
%37 = getelementptr inbounds [17 x i8], [17 x i8]* @__const.tty_puthex.hex, i64 0, i64 %36
%38 = load i8, i8* %37
%39 = getelementptr inbounds i8, i8* %18, i64 -5
store i8 %38, i8* %39
%40 = lshr i64 %19, 16
%41 = add i32 %20, 4
%42 = icmp eq i32 %41, %16
br i1 %42, label %43, label %17
43: ; preds = %17
%44 = getelementptr inbounds i8, i8* %18, i64 -5
br label %45
45: ; preds = %43, %11
%46 = phi i8* [ undef, %11 ], [ %44, %43 ]
%47 = phi i8* [ %9, %11 ], [ %44, %43 ]
%48 = phi i64 [ %1, %11 ], [ %40, %43 ]
%49 = icmp eq i32 %13, 0
br i1 %49, label %61, label %50
50: ; preds = %45, %50
%51 = phi i8* [ %57, %50 ], [ %47, %45 ]
%52 = phi i64 [ %58, %50 ], [ %48, %45 ]
%53 = phi i32 [ %59, %50 ], [ 0, %45 ]
%54 = and i64 %52, 15
%55 = getelementptr inbounds [17 x i8], [17 x i8]* @__const.tty_puthex.hex, i64 0, i64 %54
%56 = load i8, i8* %55
%57 = getelementptr inbounds i8, i8* %51, i64 -1
store i8 %56, i8* %57
%58 = lshr i64 %52, 4
%59 = add i32 %53, 1
%60 = icmp eq i32 %59, %13
br i1 %60, label %61, label %50
61: ; preds = %45, %50, %3
%62 = phi i8* [ %9, %3 ], [ %46, %45 ], [ %57, %50 ]
%63 = getelementptr inbounds i8, i8* %62, i64 -1
store i8 120, i8* %63
%64 = getelementptr inbounds i8, i8* %62, i64 -2
store i8 48, i8* %64
%65 = call i32 @tty_puts(i32 %0, i8* %64)
ret void
}