// File: /workspace/github_c_makefile_star2_O1/dwbuiten[P]obuparse/obuparse.c
// Function: obp_get_next_obu
int obp_get_next_obu(uint8_t *buf, size_t buf_size, OBPOBUType *obu_type, ptrdiff_t *offset,
                     size_t *size, int *temporal_id, int *spatial_id, OBPError *err)
{
    ptrdiff_t pos = 0;
    int obu_extension_flag;
    int obu_has_size_field;

    if (buf_size < 1) {
        snprintf(err->error, err->size, "Buffer is too small to contain an OBU.");
        return -1;
    }

    *obu_type          = (buf[pos] & 0x78) >> 3;
    obu_extension_flag = (buf[pos] & 0x04) >> 2;
    obu_has_size_field = (buf[pos] & 0x02) >> 1;
    pos++;

    if (!_obp_is_valid_obu(*obu_type)) {
        snprintf(err->error, err->size, "OBU header contains invalid OBU type: %d", *obu_type);
        return -1;
    }

    if (obu_extension_flag) {
        if (buf_size < 2) {
            snprintf(err->error, err->size, "Buffer is too small to contain an OBU extension header.");
            return -1;
        }
        *temporal_id = (buf[pos] & 0xE0) >> 5;
        *spatial_id  = (buf[pos] & 0x18) >> 3;
        pos++;
    } else {
        *temporal_id = 0;
        *spatial_id  = 0;
    }

    if (obu_has_size_field) {
        char err_buf[1024];
        uint64_t value;
        ptrdiff_t consumed;
        OBPError error = { &err_buf[0], 1024 };

        int ret      = _obp_leb128(buf + pos, buf_size - (size_t) pos, &value, &consumed, &error);
        if (ret < 0) {
            snprintf(err->error, err->size, "Failed to read OBU size: %s", &error.error[0]);
            return -1;
        }

        assert(value < UINT32_MAX);

        *offset = (ptrdiff_t) pos + consumed;
        *size   = (size_t) value;
    } else {
        *offset = (ptrdiff_t) pos;
        *size   = buf_size - (size_t) pos;
    }

    if (*size > buf_size - (size_t) *offset) {
        snprintf(err->error, err->size, "Invalid OBU size: larger than remaining buffer.");
        return -1;
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x400, %rsp            # imm = 0x400
movq 0x438(%rsp), %r12
testq %rsi, %rsi
je 0x11b2
movq %rcx, %r15
movzbl (%rdi), %ecx
shrl $0x3, %ecx
andl $0xf, %ecx
movl %ecx, (%rdx)
leal -0x9(%rcx), %eax
cmpl $-0x8, %eax
setae %al
cmpl $0xf, %ecx
sete %dl
orb %al, %dl
jne 0x11de
movq (%r12), %rdi
movq 0x8(%r12), %rsi
leaq 0x9ea2(%rip), %rdx      # 0xb04b
xorl %eax, %eax
callq 0x1030
jmp 0x11c9
movq (%r12), %rdi
movq 0x8(%r12), %rsi
leaq 0x9e62(%rip), %rdx      # 0xb024
xorl %eax, %eax
callq 0x1030
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x400, %rsp            # imm = 0x400
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq %r8, %rbx
movq %rsi, %r14
movzbl (%rdi), %ecx
testb $0x4, %cl
jne 0x11fc
movl $0x0, (%r9)
movl $0x1, %eax
xorl %edx, %edx
jmp 0x122d
cmpq $0x1, %r14
jne 0x1214
movq (%r12), %rdi
movq 0x8(%r12), %rsi
leaq 0x9e62(%rip), %rdx      # 0xb074
jmp 0x11c2
movzbl 0x1(%rdi), %eax
shrl $0x5, %eax
movl %eax, (%r9)
movzbl 0x1(%rdi), %edx
shrl $0x3, %edx
andl $0x3, %edx
movl $0x2, %eax
movq 0x430(%rsp), %rsi
movl %edx, (%rsi)
testb $0x2, %cl
jne 0x124d
movq %rax, (%r15)
movq %r14, %rcx
subq %rax, %rcx
movq %rcx, (%rbx)
jmp 0x1310
addq %rax, %rdi
movq %r14, %rsi
subq %rax, %rsi
xorl %ecx, %ecx
movabsq $0x2e65756c617620, %r8  # imm = 0x2E65756C617620
xorl %r9d, %r9d
xorl %edx, %edx
leaq 0x1(%r9), %r11
cmpq %rsi, %r11
jbe 0x1296
movups 0xa20c(%rip), %xmm0     # 0xb483
movaps %xmm0, 0x10(%rsp)
movups 0xa1f0(%rip), %xmm0     # 0xb473
movaps %xmm0, (%rsp)
movq %r8, 0x1f(%rsp)
movl $0x1, %ebp
xorl %r10d, %r10d
jmp 0x12ba
movzbl (%rdi,%r9), %r10d
xorl %ebp, %ebp
testb %r10b, %r10b
setns %bpl
movl %r10d, %r9d
andl $0x7f, %r9d
shlq %cl, %r9
orq %r9, %rdx
shrb $0x7, %r10b
addl %ebp, %ebp
movq %r11, %r9
testb %r10b, %r10b
je 0x12ce
addq $0x7, %rcx
cmpq $0x38, %rcx
jne 0x1267
movl $0x2, %ebp
cmpl $0x2, %ebp
jne 0x12e8
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %rdx
jae 0x1333
addq %rax, %r9
movq %r9, (%r15)
movq %rdx, (%rbx)
jmp 0x1302
movq (%r12), %rdi
movq 0x8(%r12), %rsi
leaq 0x9db4(%rip), %rdx      # 0xb0ac
movq %rsp, %rcx
xorl %eax, %eax
callq 0x1030
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl $0x2, %ebp
jne 0x11ce
subq (%r15), %r14
xorl %eax, %eax
cmpq %r14, (%rbx)
jbe 0x11ce
movq (%r12), %rdi
movq 0x8(%r12), %rsi
leaq 0x9e1f(%rip), %rdx      # 0xb14d
jmp 0x11c2
leaq 0x9d8e(%rip), %rdi      # 0xb0c8
leaq 0x9d9a(%rip), %rsi      # 0xb0db
leaq 0x9d9e(%rip), %rcx      # 0xb0e6
movl $0x221, %edx            # imm = 0x221
callq 0x1040