#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*/ assert(int);
int /*<<< orphan*/ sprintf(char *, char *, int);
void render_hex(char *str, int strlen, const unsigned char *buf, int n,
                int as_ipv6_address) {
  char *p = str;
  int i;
  for (i = 0; i < n; ++i) {
    if (as_ipv6_address && i > 0 && i % 2 == 0)
      *p++ = ':';
    sprintf(p, "%02x", (int)buf[i]);
    p += 2;
  }
  assert(p - str < strlen);
  *p = 0;
}


x86 assembly (clang, -O0):
<render_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11c5 <render_hex+0xa5>
cmpl   $0x0,-0x20(%rbp)
je     118f <render_hex+0x6f>
cmpl   $0x0,-0x2c(%rbp)
jle    118f <render_hex+0x6f>
mov    -0x2c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    118f <render_hex+0x6f>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movb   $0x3a,(%rax)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movzbl (%rax,%rcx,1),%edx
lea    0xe5a(%rip),%rsi
call   1040 <sprintf@plt>
mov    -0x28(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1149 <render_hex+0x29>
mov    -0x28(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
movslq -0xc(%rbp),%rcx
cmp    %rcx,%rax
setl   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<render_hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,0xc(%rsp)
mov    %rdi,%rbx
mov    %rdi,0x10(%rsp)
test   %ecx,%ecx
jle    119c <render_hex+0x7c>
mov    %r8d,%r12d
mov    %rdx,%rbp
mov    %ecx,%r14d
xor    %r15d,%r15d
lea    0xeaf(%rip),%r13
mov    0x10(%rsp),%rbx
jmp    117e <render_hex+0x5e>
nopl   0x0(%rax,%rax,1)
movzbl 0x0(%rbp,%r15,1),%edx
mov    %rbx,%rdi
mov    %r13,%rsi
call   1040 <sprintf@plt>
add    $0x2,%rbx
add    $0x1,%r15
cmp    %r15,%r14
je     119c <render_hex+0x7c>
test   %r12d,%r12d
je     1160 <render_hex+0x40>
test   %r15,%r15
je     1160 <render_hex+0x40>
mov    %r15d,%eax
and    $0x1,%eax
test   %rax,%rax
jne    1160 <render_hex+0x40>
movb   $0x3a,(%rbx)
add    $0x1,%rbx
jmp    1160 <render_hex+0x40>
mov    %rbx,%rax
sub    0x10(%rsp),%rax
movslq 0xc(%rsp),%rcx
xor    %edi,%edi
cmp    %rcx,%rax
setl   %dil
call   1030 <assert@plt>
movb   $0x0,(%rbx)
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"%02x\00"
define void @render_hex(i8* %0, i32 %1, i8* nocapture %2, i32 %3, i32 %4) {
%6 = icmp sgt i32 %3, 0
br i1 %6, label %7, label %29
7: ; preds = %5
%8 = icmp ne i32 %4, 0
%9 = zext i32 %3 to i64
br label %10
10: ; preds = %7, %20
%11 = phi i64 [ 0, %7 ], [ %27, %20 ]
%12 = phi i8* [ %0, %7 ], [ %26, %20 ]
%13 = icmp ne i64 %11, 0
%14 = select i1 %8, i1 %13, i1 false
%15 = and i64 %11, 1
%16 = icmp eq i64 %15, 0
%17 = select i1 %14, i1 %16, i1 false
br i1 %17, label %18, label %20
18: ; preds = %10
%19 = getelementptr inbounds i8, i8* %12, i64 1
store i8 58, i8* %12
br label %20
20: ; preds = %18, %10
%21 = phi i8* [ %19, %18 ], [ %12, %10 ]
%22 = getelementptr inbounds i8, i8* %2, i64 %11
%23 = load i8, i8* %22
%24 = zext i8 %23 to i32
%25 = tail call i32 @sprintf(i8* dereferenceable(1) %21, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %24)
%26 = getelementptr inbounds i8, i8* %21, i64 2
%27 = add nuw nsw i64 %11, 1
%28 = icmp eq i64 %27, %9
br i1 %28, label %29, label %10
29: ; preds = %20, %5
%30 = phi i8* [ %0, %5 ], [ %26, %20 ]
%31 = ptrtoint i8* %30 to i64
%32 = ptrtoint i8* %0 to i64
%33 = sub i64 %31, %32
%34 = sext i32 %1 to i64
%35 = icmp slt i64 %33, %34
%36 = zext i1 %35 to i32
%37 = tail call i32 @assert(i32 %36)
store i8 0, i8* %30
ret void
}