#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 */
struct string_formatter {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ output_char(struct string_formatter *, char const);
void output_buf_reversed(struct string_formatter *formatter, const char *buf,
                         int nchars) {
  const char hex_val[] = {'0', '1', '2', '3', '4', '5', '6', '7',
                          '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
  for (nchars = nchars - 1; nchars >= 0; --nchars) {
    unsigned digit = buf[nchars];
    output_char(formatter, hex_val[digit]);
  }
}


x86 assembly (clang, -O0):
<output_buf_reversed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    0xed6(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xed3(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     117a <output_buf_reversed+0x6a>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x34(%rbp),%eax
movsbl -0x30(%rbp,%rax,1),%esi
call   1030 <output_char@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1142 <output_buf_reversed+0x32>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<output_buf_reversed>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
jle    1151 <output_buf_reversed+0x41>
mov    %rsi,%r14
mov    %rdi,%r15
mov    %edx,%ebx
add    $0x1,%rbx
lea    0xed1(%rip),%r12
nop
lea    -0x2(%rbx),%eax
movsbq (%r14,%rax,1),%rax
mov    %eax,%eax
movsbl (%rax,%r12,1),%esi
mov    %r15,%rdi
call   1030 <output_char@plt>
add    $0xffffffffffffffff,%rbx
cmp    $0x1,%rbx
ja     1130 <output_buf_reversed+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.string_formatter = type { i32 }
@__const.output_buf_reversed.hex_val = [16 x i8] c"0123456789abcdef"
define void @output_buf_reversed(%struct.string_formatter* %0, i8* %1, i32 %2) {
%4 = alloca %struct.string_formatter*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [16 x i8]
%8 = alloca i32
store %struct.string_formatter* %0, %struct.string_formatter** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = bitcast [16 x i8]* %7 to i8*
%10 = load i32, i32* %6
%11 = sub nsw i32 %10, 1
store i32 %11, i32* %6
br label %12
12: ; preds = %28, %3
%13 = load i32, i32* %6
%14 = icmp sge i32 %13, 0
br i1 %14, label %15, label %31
15: ; preds = %12
%16 = load i8*, i8** %5
%17 = load i32, i32* %6
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
store i32 %21, i32* %8
%22 = load %struct.string_formatter*, %struct.string_formatter** %4
%23 = load i32, i32* %8
%24 = zext i32 %23 to i64
%25 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 %24
%26 = load i8, i8* %25
%27 = call i32 @output_char(%struct.string_formatter* %22, i8 signext %26)
br label %28
28: ; preds = %15
%29 = load i32, i32* %6
%30 = add nsw i32 %29, -1
store i32 %30, i32* %6
br label %12
31: ; preds = %12
ret void
}

LLVM IR (-O2):
%struct.string_formatter = type { i32 }
@__const.output_buf_reversed.hex_val = [16 x i8] c"0123456789abcdef"
define void @output_buf_reversed(%struct.string_formatter* %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %19
5: ; preds = %3
%6 = zext i32 %2 to i64
br label %7
7: ; preds = %5, %7
%8 = phi i64 [ %6, %5 ], [ %9, %7 ]
%9 = add nsw i64 %8, -1
%10 = and i64 %9, 4294967295
%11 = getelementptr inbounds i8, i8* %1, i64 %10
%12 = load i8, i8* %11
%13 = sext i8 %12 to i64
%14 = and i64 %13, 4294967295
%15 = getelementptr inbounds [16 x i8], [16 x i8]* @__const.output_buf_reversed.hex_val, i64 0, i64 %14
%16 = load i8, i8* %15
%17 = tail call i32 @output_char(%struct.string_formatter* %0, i8 signext %16)
%18 = icmp ugt i64 %8, 1
br i1 %18, label %7, label %19
19: ; preds = %7, %3
ret void
}