#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 /*<<< orphan*/ uint8_t;
typedef int /*<<< orphan*/ uint32_t;
typedef int /*<<< orphan*/ uint16_t;
struct cbor_buf {
  int dummy;
};

/* Variables and functions */
size_t UINT16_MAX;
size_t UINT32_MAX;
size_t UINT8_MAX;
int cbor_buf_append_2byte_plus(struct cbor_buf *, int, int /*<<< orphan*/,
                               void *, size_t);
int cbor_buf_append_3byte_plus(struct cbor_buf *, int, int /*<<< orphan*/,
                               void *, size_t);
int cbor_buf_append_5byte_plus(struct cbor_buf *, int, int /*<<< orphan*/,
                               void *, size_t);
int cbor_buf_append_9byte_plus(struct cbor_buf *, int, size_t, void *, size_t);
int cbor_buf_append_byte_plus(struct cbor_buf *, int /*<<< orphan*/, void *,
                              size_t);
bool cbor_add_byte_str(struct cbor_buf *buf, void *data, size_t len) {
  if (len <= 23) {
    return cbor_buf_append_byte_plus(buf, (uint8_t)(0x40 | len), data, len);
  }
  if (len <= UINT8_MAX) {
    return cbor_buf_append_2byte_plus(buf, 0x58, (uint8_t)len, data, len);
  }
  if (len <= UINT16_MAX) {
    return cbor_buf_append_3byte_plus(buf, 0x59, (uint16_t)len, data, len);
  }
  if (len <= UINT32_MAX) {
    return cbor_buf_append_5byte_plus(buf, 0x5a, (uint32_t)len, data, len);
  }
  return cbor_buf_append_9byte_plus(buf, 0x5b, len, data, len);
}


x86 assembly (clang, -O0):
<cbor_add_byte_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x17,-0x20(%rbp)
ja     1192 <cbor_add_byte_str+0x42>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
or     $0x40,%rax
mov    %eax,%esi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1070 <cbor_buf_append_byte_plus@plt>
mov    %eax,-0x4(%rbp)
jmp    1257 <cbor_add_byte_str+0x107>
mov    -0x20(%rbp),%rax
mov    0x2e3b(%rip),%rcx
cmp    (%rcx),%rax
ja     11ca <cbor_add_byte_str+0x7a>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    %eax,%edx
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
mov    $0x58,%esi
call   1050 <cbor_buf_append_2byte_plus@plt>
mov    %eax,-0x4(%rbp)
jmp    1257 <cbor_add_byte_str+0x107>
mov    -0x20(%rbp),%rax
mov    0x2dfb(%rip),%rcx
cmp    (%rcx),%rax
ja     1202 <cbor_add_byte_str+0xb2>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    %eax,%edx
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
mov    $0x59,%esi
call   1030 <cbor_buf_append_3byte_plus@plt>
mov    %eax,-0x4(%rbp)
jmp    1257 <cbor_add_byte_str+0x107>
mov    -0x20(%rbp),%rax
mov    0x2deb(%rip),%rcx
cmp    (%rcx),%rax
ja     123a <cbor_add_byte_str+0xea>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    %eax,%edx
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
mov    $0x5a,%esi
call   1040 <cbor_buf_append_5byte_plus@plt>
mov    %eax,-0x4(%rbp)
jmp    1257 <cbor_add_byte_str+0x107>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
mov    $0x5b,%esi
call   1060 <cbor_buf_append_9byte_plus@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cbor_add_byte_str>:
mov    %rdx,%r8
mov    %rsi,%rcx
cmp    $0x17,%rdx
ja     116d <cbor_add_byte_str+0x1d>
mov    %r8d,%esi
or     $0x40,%esi
mov    %rcx,%rdx
mov    %r8,%rcx
jmp    1070 <cbor_buf_append_byte_plus@plt>
mov    0x2e64(%rip),%rax
cmp    %r8,(%rax)
jae    119e <cbor_add_byte_str+0x4e>
mov    0x2e50(%rip),%rax
cmp    %r8,(%rax)
jae    11ab <cbor_add_byte_str+0x5b>
mov    0x2e6c(%rip),%rax
cmp    %r8,(%rax)
jae    11b8 <cbor_add_byte_str+0x68>
mov    $0x5b,%esi
mov    %r8,%rdx
jmp    1060 <cbor_buf_append_9byte_plus@plt>
mov    $0x58,%esi
mov    %r8d,%edx
jmp    1050 <cbor_buf_append_2byte_plus@plt>
mov    $0x59,%esi
mov    %r8d,%edx
jmp    1030 <cbor_buf_append_3byte_plus@plt>
mov    $0x5a,%esi
mov    %r8d,%edx
jmp    1040 <cbor_buf_append_5byte_plus@plt>

LLVM IR (-O0):
%struct.cbor_buf = type { i32 }
@UINT8_MAX = global i64 0
@UINT16_MAX = global i64 0
@UINT32_MAX = global i64 0
define i32 @cbor_add_byte_str(%struct.cbor_buf* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca %struct.cbor_buf*
%6 = alloca i8*
%7 = alloca i64
store %struct.cbor_buf* %0, %struct.cbor_buf** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%8 = load i64, i64* %7
%9 = icmp ule i64 %8, 23
br i1 %9, label %10, label %18
10: ; preds = %3
%11 = load %struct.cbor_buf*, %struct.cbor_buf** %5
%12 = load i64, i64* %7
%13 = or i64 64, %12
%14 = trunc i64 %13 to i32
%15 = load i8*, i8** %6
%16 = load i64, i64* %7
%17 = call i32 @cbor_buf_append_byte_plus(%struct.cbor_buf* %11, i32 %14, i8* %15, i64 %16)
store i32 %17, i32* %4
br label %57
18: ; preds = %3
%19 = load i64, i64* %7
%20 = load i64, i64* @UINT8_MAX
%21 = icmp ule i64 %19, %20
br i1 %21, label %22, label %29
22: ; preds = %18
%23 = load %struct.cbor_buf*, %struct.cbor_buf** %5
%24 = load i64, i64* %7
%25 = trunc i64 %24 to i32
%26 = load i8*, i8** %6
%27 = load i64, i64* %7
%28 = call i32 @cbor_buf_append_2byte_plus(%struct.cbor_buf* %23, i32 88, i32 %25, i8* %26, i64 %27)
store i32 %28, i32* %4
br label %57
29: ; preds = %18
%30 = load i64, i64* %7
%31 = load i64, i64* @UINT16_MAX
%32 = icmp ule i64 %30, %31
br i1 %32, label %33, label %40
33: ; preds = %29
%34 = load %struct.cbor_buf*, %struct.cbor_buf** %5
%35 = load i64, i64* %7
%36 = trunc i64 %35 to i32
%37 = load i8*, i8** %6
%38 = load i64, i64* %7
%39 = call i32 @cbor_buf_append_3byte_plus(%struct.cbor_buf* %34, i32 89, i32 %36, i8* %37, i64 %38)
store i32 %39, i32* %4
br label %57
40: ; preds = %29
%41 = load i64, i64* %7
%42 = load i64, i64* @UINT32_MAX
%43 = icmp ule i64 %41, %42
br i1 %43, label %44, label %51
44: ; preds = %40
%45 = load %struct.cbor_buf*, %struct.cbor_buf** %5
%46 = load i64, i64* %7
%47 = trunc i64 %46 to i32
%48 = load i8*, i8** %6
%49 = load i64, i64* %7
%50 = call i32 @cbor_buf_append_5byte_plus(%struct.cbor_buf* %45, i32 90, i32 %47, i8* %48, i64 %49)
store i32 %50, i32* %4
br label %57
51: ; preds = %40
%52 = load %struct.cbor_buf*, %struct.cbor_buf** %5
%53 = load i64, i64* %7
%54 = load i8*, i8** %6
%55 = load i64, i64* %7
%56 = call i32 @cbor_buf_append_9byte_plus(%struct.cbor_buf* %52, i32 91, i64 %53, i8* %54, i64 %55)
store i32 %56, i32* %4
br label %57
57: ; preds = %51, %44, %33, %22, %10
%58 = load i32, i32* %4
ret i32 %58
}

LLVM IR (-O2):
%struct.cbor_buf = type { i32 }
@UINT8_MAX = global i64 0
@UINT16_MAX = global i64 0
@UINT32_MAX = global i64 0
define i32 @cbor_add_byte_str(%struct.cbor_buf* %0, i8* %1, i64 %2) {
%4 = icmp ult i64 %2, 24
br i1 %4, label %5, label %9
5: ; preds = %3
%6 = trunc i64 %2 to i32
%7 = or i32 %6, 64
%8 = tail call i32 @cbor_buf_append_byte_plus(%struct.cbor_buf* %0, i32 %7, i8* %1, i64 %2)
br label %29
9: ; preds = %3
%10 = load i64, i64* @UINT8_MAX
%11 = icmp ult i64 %10, %2
br i1 %11, label %15, label %12
12: ; preds = %9
%13 = trunc i64 %2 to i32
%14 = tail call i32 @cbor_buf_append_2byte_plus(%struct.cbor_buf* %0, i32 88, i32 %13, i8* %1, i64 %2)
br label %29
15: ; preds = %9
%16 = load i64, i64* @UINT16_MAX
%17 = icmp ult i64 %16, %2
br i1 %17, label %21, label %18
18: ; preds = %15
%19 = trunc i64 %2 to i32
%20 = tail call i32 @cbor_buf_append_3byte_plus(%struct.cbor_buf* %0, i32 89, i32 %19, i8* %1, i64 %2)
br label %29
21: ; preds = %15
%22 = load i64, i64* @UINT32_MAX
%23 = icmp ult i64 %22, %2
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = trunc i64 %2 to i32
%26 = tail call i32 @cbor_buf_append_5byte_plus(%struct.cbor_buf* %0, i32 90, i32 %25, i8* %1, i64 %2)
br label %29
27: ; preds = %21
%28 = tail call i32 @cbor_buf_append_9byte_plus(%struct.cbor_buf* %0, i32 91, i64 %2, i8* %1, i64 %2)
br label %29
29: ; preds = %27, %24, %18, %12, %5
%30 = phi i32 [ %8, %5 ], [ %14, %12 ], [ %20, %18 ], [ %26, %24 ], [ %28, %27 ]
ret i32 %30
}