#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 DebugStrbuf {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ debug_strbuf_buffer_commit(struct DebugStrbuf *, int);
int /*<<< orphan*/ debug_strbuf_buffer_get(struct DebugStrbuf *, char **,
                                           int *);
scalar_t__ snprintf(char *, int, char *, ...);
const char *debug_str_hexbuf(struct DebugStrbuf *buf, const void *data,
                             int len) {
  int p_len;
  char *p;
  const unsigned char *u_data = data;
  int offset = 0;
  int i;
  debug_strbuf_buffer_get(buf, &p, &p_len);
  for (i = 0; i < len && offset + 3 < p_len; i++) {
    offset += snprintf(p + offset, len - offset, "%02x", u_data[i]);
  }
  if (i < len && offset >= 4) {
    offset -= 4;
    offset += snprintf(p + offset, 4, "...");
  }
  debug_strbuf_buffer_commit(buf, offset);
  return p;
}


x86 assembly (clang, -O0):
<debug_str_hexbuf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x20(%rbp),%rsi
lea    -0x18(%rbp),%rdx
call   1040 <debug_strbuf_buffer_get@plt>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%ecx
xor    %eax,%eax
cmp    -0x14(%rbp),%ecx
mov    %al,-0x31(%rbp)
jge    118a <debug_str_hexbuf+0x5a>
mov    -0x2c(%rbp),%eax
add    $0x3,%eax
cmp    -0x18(%rbp),%eax
setl   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    119a <debug_str_hexbuf+0x6a>
jmp    11e0 <debug_str_hexbuf+0xb0>
mov    -0x20(%rbp),%rdi
movslq -0x2c(%rbp),%rax
add    %rax,%rdi
mov    -0x14(%rbp),%esi
sub    -0x2c(%rbp),%esi
mov    -0x28(%rbp),%rax
movslq -0x30(%rbp),%rcx
movzbl (%rax,%rcx,1),%ecx
lea    0xe42(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
mov    %rax,%rcx
movslq -0x2c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    116a <debug_str_hexbuf+0x3a>
mov    -0x30(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    122a <debug_str_hexbuf+0xfa>
cmpl   $0x4,-0x2c(%rbp)
jl     122a <debug_str_hexbuf+0xfa>
mov    -0x2c(%rbp),%eax
sub    $0x4,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
movslq -0x2c(%rbp),%rax
add    %rax,%rdi
mov    $0x4,%esi
lea    0xdef(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
mov    %rax,%rcx
movslq -0x2c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1050 <debug_strbuf_buffer_commit@plt>
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<debug_str_hexbuf>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r13d
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0x10(%rsp),%rsi
lea    0x4(%rsp),%rdx
call   1040 <debug_strbuf_buffer_get@plt>
xor    %r12d,%r12d
test   %r13d,%r13d
jle    11f6 <debug_str_hexbuf+0xc6>
cmpl   $0x4,0x4(%rsp)
jl     11f6 <debug_str_hexbuf+0xc6>
mov    %rbx,0x8(%rsp)
mov    %r13d,%ebp
mov    $0x1,%ecx
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    %rcx,%r14
movslq %ebx,%rax
mov    0x10(%rsp),%rdi
add    %rax,%rdi
mov    %r13d,%esi
sub    %eax,%esi
movzbl -0x1(%r15,%rcx,1),%ecx
lea    0xe60(%rip),%rdx
xor    %eax,%eax
call   1030 <snprintf@plt>
lea    (%rbx,%rax,1),%r12d
cmp    %rbp,%r14
jae    11c2 <debug_str_hexbuf+0x92>
lea    0x1(%r14),%rcx
add    %ebx,%eax
add    $0x3,%eax
mov    %r12d,%ebx
cmp    0x4(%rsp),%eax
jl     1180 <debug_str_hexbuf+0x50>
cmp    %rbp,%r14
jae    11f1 <debug_str_hexbuf+0xc1>
cmp    $0x4,%r12d
jl     11f1 <debug_str_hexbuf+0xc1>
lea    -0x4(%r12),%edi
add    0x10(%rsp),%rdi
lea    0xe27(%rip),%rdx
mov    $0x4,%esi
xor    %eax,%eax
call   1030 <snprintf@plt>
add    %eax,%r12d
add    $0xfffffffc,%r12d
mov    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    %r12d,%esi
call   1050 <debug_strbuf_buffer_commit@plt>
mov    0x10(%rsp),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.DebugStrbuf = type { i32 }
@.str = [5 x i8] c"%02x\00"
@.str.1 = [4 x i8] c"...\00"
define i8* @debug_str_hexbuf(%struct.DebugStrbuf* %0, i8* %1, i32 %2) {
%4 = alloca %struct.DebugStrbuf*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store %struct.DebugStrbuf* %0, %struct.DebugStrbuf** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%12 = load i8*, i8** %5
store i8* %12, i8** %9
store i32 0, i32* %10
%13 = load %struct.DebugStrbuf*, %struct.DebugStrbuf** %4
%14 = call i32 @debug_strbuf_buffer_get(%struct.DebugStrbuf* %13, i8** %8, i32* %7)
store i32 0, i32* %11
br label %15
15: ; preds = %45, %3
%16 = load i32, i32* %11
%17 = load i32, i32* %6
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %24
19: ; preds = %15
%20 = load i32, i32* %10
%21 = add nsw i32 %20, 3
%22 = load i32, i32* %7
%23 = icmp slt i32 %21, %22
br label %24
24: ; preds = %19, %15
%25 = phi i1 [ false, %15 ], [ %23, %19 ]
br i1 %25, label %26, label %48
26: ; preds = %24
%27 = load i8*, i8** %8
%28 = load i32, i32* %10
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = load i32, i32* %6
%32 = load i32, i32* %10
%33 = sub nsw i32 %31, %32
%34 = load i8*, i8** %9
%35 = load i32, i32* %11
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
%38 = load i8, i8* %37
%39 = zext i8 %38 to i32
%40 = call i64 (i8*, i32, i8*, ...) @snprintf(i8* %30, i32 %33, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %39)
%41 = load i32, i32* %10
%42 = sext i32 %41 to i64
%43 = add nsw i64 %42, %40
%44 = trunc i64 %43 to i32
store i32 %44, i32* %10
br label %45
45: ; preds = %26
%46 = load i32, i32* %11
%47 = add nsw i32 %46, 1
store i32 %47, i32* %11
br label %15
48: ; preds = %24
%49 = load i32, i32* %11
%50 = load i32, i32* %6
%51 = icmp slt i32 %49, %50
br i1 %51, label %52, label %67
52: ; preds = %48
%53 = load i32, i32* %10
%54 = icmp sge i32 %53, 4
br i1 %54, label %55, label %67
55: ; preds = %52
%56 = load i32, i32* %10
%57 = sub nsw i32 %56, 4
store i32 %57, i32* %10
%58 = load i8*, i8** %8
%59 = load i32, i32* %10
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %58, i64 %60
%62 = call i64 (i8*, i32, i8*, ...) @snprintf(i8* %61, i32 4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%63 = load i32, i32* %10
%64 = sext i32 %63 to i64
%65 = add nsw i64 %64, %62
%66 = trunc i64 %65 to i32
store i32 %66, i32* %10
br label %67
67: ; preds = %55, %52, %48
%68 = load %struct.DebugStrbuf*, %struct.DebugStrbuf** %4
%69 = load i32, i32* %10
%70 = call i32 @debug_strbuf_buffer_commit(%struct.DebugStrbuf* %68, i32 %69)
%71 = load i8*, i8** %8
ret i8* %71
}

LLVM IR (-O2):
%struct.DebugStrbuf = type { i32 }
@.str = [5 x i8] c"%02x\00"
@.str.1 = [4 x i8] c"...\00"
define i8* @debug_str_hexbuf(%struct.DebugStrbuf* %0, i8* nocapture %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = bitcast i32* %4 to i8*
%7 = bitcast i8** %5 to i8*
%8 = call i32 @debug_strbuf_buffer_get(%struct.DebugStrbuf* %0, i8** %5, i32* %4)
%9 = icmp sgt i32 %2, 0
%10 = load i32, i32* %4
%11 = icmp sgt i32 %10, 3
%12 = select i1 %9, i1 %11, i1 false
br i1 %12, label %13, label %45
13: ; preds = %3
%14 = zext i32 %2 to i64
br label %15
15: ; preds = %13, %15
%16 = phi i64 [ 0, %13 ], [ %28, %15 ]
%17 = phi i32 [ 0, %13 ], [ %27, %15 ]
%18 = load i8*, i8** %5
%19 = sext i32 %17 to i64
%20 = getelementptr inbounds i8, i8* %18, i64 %19
%21 = sub nsw i32 %2, %17
%22 = getelementptr inbounds i8, i8* %1, i64 %16
%23 = load i8, i8* %22
%24 = zext i8 %23 to i32
%25 = call i64 (i8*, i32, i8*, ...) @snprintf(i8* %20, i32 %21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %24)
%26 = trunc i64 %25 to i32
%27 = add i32 %17, %26
%28 = add nuw nsw i64 %16, 1
%29 = icmp ult i64 %28, %14
%30 = add nsw i32 %27, 3
%31 = load i32, i32* %4
%32 = icmp slt i32 %30, %31
%33 = select i1 %29, i1 %32, i1 false
br i1 %33, label %15, label %34
34: ; preds = %15
%35 = icmp sgt i32 %27, 3
%36 = select i1 %29, i1 %35, i1 false
br i1 %36, label %37, label %45
37: ; preds = %34
%38 = add nsw i32 %27, -4
%39 = load i8*, i8** %5
%40 = zext i32 %38 to i64
%41 = getelementptr inbounds i8, i8* %39, i64 %40
%42 = call i64 (i8*, i32, i8*, ...) @snprintf(i8* %41, i32 4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%43 = trunc i64 %42 to i32
%44 = add i32 %38, %43
br label %45
45: ; preds = %3, %37, %34
%46 = phi i32 [ %44, %37 ], [ %27, %34 ], [ 0, %3 ]
%47 = call i32 @debug_strbuf_buffer_commit(%struct.DebugStrbuf* %0, i32 %46)
%48 = load i8*, i8** %5
ret i8* %48
}