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

/* Variables and functions */
int UINT16_MAX;
int UINT32_MAX;
int UINT8_MAX;
int cbor_add_uint64(struct cbor_buf *, int);
int cbor_buf_append_2byte(struct cbor_buf *, int, int /*<<< orphan*/);
int cbor_buf_append_3byte(struct cbor_buf *, int, int /*<<< orphan*/);
int cbor_buf_append_5byte(struct cbor_buf *, int, int /*<<< orphan*/);
int cbor_buf_append_9byte(struct cbor_buf *, int, int);
int cbor_buf_append_byte(struct cbor_buf *, int /*<<< orphan*/);
bool cbor_add_int64(struct cbor_buf *buf, int64_t i) {
  if (i >= 0) {
    return cbor_add_uint64(buf, (uint64_t)i);
  }
  uint64_t n = ((uint64_t)(-i)) - 1;
  if (n <= 23) {
    return cbor_buf_append_byte(buf, (uint8_t)(0x20 | n));
  }
  if (n <= UINT8_MAX) {
    return cbor_buf_append_2byte(buf, 0x38, (uint8_t)n);
  }
  if (n <= UINT16_MAX) {
    return cbor_buf_append_3byte(buf, 0x39, (uint16_t)n);
  }
  if (n <= UINT32_MAX) {
    return cbor_buf_append_5byte(buf, 0x3a, (uint32_t)n);
  }
  return cbor_buf_append_9byte(buf, 0x3b, n);
}


x86 assembly (clang, -O0):
<cbor_add_int64>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jl     1192 <cbor_add_int64+0x32>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    %eax,%esi
call   1080 <cbor_add_uint64@plt>
mov    %eax,-0x4(%rbp)
jmp    1254 <cbor_add_int64+0xf4>
xor    %eax,%eax
sub    -0x18(%rbp),%rax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x17,-0x1c(%rbp)
jg     11bf <cbor_add_int64+0x5f>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
or     $0x20,%esi
call   1040 <cbor_buf_append_byte@plt>
mov    %eax,-0x4(%rbp)
jmp    1254 <cbor_add_int64+0xf4>
mov    -0x1c(%rbp),%eax
mov    0x2e0f(%rip),%rcx
cmp    (%rcx),%eax
jg     11ea <cbor_add_int64+0x8a>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    $0x38,%esi
call   1070 <cbor_buf_append_2byte@plt>
mov    %eax,-0x4(%rbp)
jmp    1254 <cbor_add_int64+0xf4>
mov    -0x1c(%rbp),%eax
mov    0x2ddc(%rip),%rcx
cmp    (%rcx),%eax
jg     1215 <cbor_add_int64+0xb5>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    $0x39,%esi
call   1050 <cbor_buf_append_3byte@plt>
mov    %eax,-0x4(%rbp)
jmp    1254 <cbor_add_int64+0xf4>
mov    -0x1c(%rbp),%eax
mov    0x2dd9(%rip),%rcx
cmp    (%rcx),%eax
jg     1240 <cbor_add_int64+0xe0>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    $0x3a,%esi
call   1030 <cbor_buf_append_5byte@plt>
mov    %eax,-0x4(%rbp)
jmp    1254 <cbor_add_int64+0xf4>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    $0x3b,%esi
call   1060 <cbor_buf_append_9byte@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cbor_add_int64>:
mov    %rsi,%rdx
test   %rsi,%rsi
js     116f <cbor_add_int64+0xf>
mov    %edx,%esi
jmp    1080 <cbor_add_uint64@plt>
cmp    $0xffffffe8,%edx
not    %edx
jl     1180 <cbor_add_int64+0x20>
or     $0x20,%edx
mov    %edx,%esi
jmp    1040 <cbor_buf_append_byte@plt>
mov    0x2e51(%rip),%rax
cmp    %edx,(%rax)
jge    11ab <cbor_add_int64+0x4b>
mov    0x2e3e(%rip),%rax
cmp    %edx,(%rax)
jge    11b5 <cbor_add_int64+0x55>
mov    0x2e5b(%rip),%rax
cmp    %edx,(%rax)
jge    11bf <cbor_add_int64+0x5f>
mov    $0x3b,%esi
jmp    1060 <cbor_buf_append_9byte@plt>
mov    $0x38,%esi
jmp    1070 <cbor_buf_append_2byte@plt>
mov    $0x39,%esi
jmp    1050 <cbor_buf_append_3byte@plt>
mov    $0x3a,%esi
jmp    1030 <cbor_buf_append_5byte@plt>

LLVM IR (-O0):
%struct.cbor_buf = type { i32 }
@UINT8_MAX = global i32 0
@UINT16_MAX = global i32 0
@UINT32_MAX = global i32 0
define i32 @cbor_add_int64(%struct.cbor_buf* %0, i64 %1) {
%3 = alloca i32
%4 = alloca %struct.cbor_buf*
%5 = alloca i64
%6 = alloca i32
store %struct.cbor_buf* %0, %struct.cbor_buf** %4
store i64 %1, i64* %5
%7 = load i64, i64* %5
%8 = icmp sge i64 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %2
%10 = load %struct.cbor_buf*, %struct.cbor_buf** %4
%11 = load i64, i64* %5
%12 = trunc i64 %11 to i32
%13 = call i32 @cbor_add_uint64(%struct.cbor_buf* %10, i32 %12)
store i32 %13, i32* %3
br label %54
14: ; preds = %2
%15 = load i64, i64* %5
%16 = sub nsw i64 0, %15
%17 = trunc i64 %16 to i32
%18 = sub nsw i32 %17, 1
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp sle i32 %19, 23
br i1 %20, label %21, label %26
21: ; preds = %14
%22 = load %struct.cbor_buf*, %struct.cbor_buf** %4
%23 = load i32, i32* %6
%24 = or i32 32, %23
%25 = call i32 @cbor_buf_append_byte(%struct.cbor_buf* %22, i32 %24)
store i32 %25, i32* %3
br label %54
26: ; preds = %14
%27 = load i32, i32* %6
%28 = load i32, i32* @UINT8_MAX
%29 = icmp sle i32 %27, %28
br i1 %29, label %30, label %34
30: ; preds = %26
%31 = load %struct.cbor_buf*, %struct.cbor_buf** %4
%32 = load i32, i32* %6
%33 = call i32 @cbor_buf_append_2byte(%struct.cbor_buf* %31, i32 56, i32 %32)
store i32 %33, i32* %3
br label %54
34: ; preds = %26
%35 = load i32, i32* %6
%36 = load i32, i32* @UINT16_MAX
%37 = icmp sle i32 %35, %36
br i1 %37, label %38, label %42
38: ; preds = %34
%39 = load %struct.cbor_buf*, %struct.cbor_buf** %4
%40 = load i32, i32* %6
%41 = call i32 @cbor_buf_append_3byte(%struct.cbor_buf* %39, i32 57, i32 %40)
store i32 %41, i32* %3
br label %54
42: ; preds = %34
%43 = load i32, i32* %6
%44 = load i32, i32* @UINT32_MAX
%45 = icmp sle i32 %43, %44
br i1 %45, label %46, label %50
46: ; preds = %42
%47 = load %struct.cbor_buf*, %struct.cbor_buf** %4
%48 = load i32, i32* %6
%49 = call i32 @cbor_buf_append_5byte(%struct.cbor_buf* %47, i32 58, i32 %48)
store i32 %49, i32* %3
br label %54
50: ; preds = %42
%51 = load %struct.cbor_buf*, %struct.cbor_buf** %4
%52 = load i32, i32* %6
%53 = call i32 @cbor_buf_append_9byte(%struct.cbor_buf* %51, i32 59, i32 %52)
store i32 %53, i32* %3
br label %54
54: ; preds = %50, %46, %38, %30, %21, %9
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
%struct.cbor_buf = type { i32 }
@UINT8_MAX = global i32 0
@UINT16_MAX = global i32 0
@UINT32_MAX = global i32 0
define i32 @cbor_add_int64(%struct.cbor_buf* %0, i64 %1) {
%3 = icmp sgt i64 %1, -1
%4 = trunc i64 %1 to i32
br i1 %3, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @cbor_add_uint64(%struct.cbor_buf* %0, i32 %4)
br label %30
7: ; preds = %2
%8 = xor i32 %4, -1
%9 = icmp sgt i32 %4, -25
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = or i32 %8, 32
%12 = tail call i32 @cbor_buf_append_byte(%struct.cbor_buf* %0, i32 %11)
br label %30
13: ; preds = %7
%14 = load i32, i32* @UINT8_MAX
%15 = icmp slt i32 %14, %8
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = tail call i32 @cbor_buf_append_2byte(%struct.cbor_buf* %0, i32 56, i32 %8)
br label %30
18: ; preds = %13
%19 = load i32, i32* @UINT16_MAX
%20 = icmp slt i32 %19, %8
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = tail call i32 @cbor_buf_append_3byte(%struct.cbor_buf* %0, i32 57, i32 %8)
br label %30
23: ; preds = %18
%24 = load i32, i32* @UINT32_MAX
%25 = icmp slt i32 %24, %8
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = tail call i32 @cbor_buf_append_5byte(%struct.cbor_buf* %0, i32 58, i32 %8)
br label %30
28: ; preds = %23
%29 = tail call i32 @cbor_buf_append_9byte(%struct.cbor_buf* %0, i32 59, i32 %8)
br label %30
30: ; preds = %10, %16, %21, %26, %28, %5
%31 = phi i32 [ %6, %5 ], [ %12, %10 ], [ %17, %16 ], [ %22, %21 ], [ %27, %26 ], [ %29, %28 ]
ret i32 %31
}