// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x480, %rsp            # imm = 0x480
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
cmpq $0x1, -0x18(%rbp)
jae 0x11c3
movq 0x18(%rbp), %rax
movq (%rax), %rdi
movq 0x18(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0xde50(%rip), %rdx      # 0xf000
movb $0x0, %al
callq 0x1030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1415
movq -0x10(%rbp), %rax
movq -0x40(%rbp), %rcx
movzbl (%rax,%rcx), %ecx
andl $0x78, %ecx
sarl $0x3, %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
movq -0x40(%rbp), %rcx
movzbl (%rax,%rcx), %eax
andl $0x4, %eax
sarl $0x2, %eax
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rax
movq -0x40(%rbp), %rcx
movzbl (%rax,%rcx), %eax
andl $0x2, %eax
sarl %eax
movl %eax, -0x48(%rbp)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
movq -0x20(%rbp), %rax
movl (%rax), %edi
callq 0x1430
cmpl $0x0, %eax
jne 0x124f
movq 0x18(%rbp), %rax
movq (%rax), %rdi
movq 0x18(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x20(%rbp), %rax
movl (%rax), %ecx
leaq 0xddeb(%rip), %rdx      # 0xf027
movb $0x0, %al
callq 0x1030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1415
cmpl $0x0, -0x44(%rbp)
je 0x12c6
cmpq $0x2, -0x18(%rbp)
jae 0x1285
movq 0x18(%rbp), %rax
movq (%rax), %rdi
movq 0x18(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0xddde(%rip), %rdx      # 0xf050
movb $0x0, %al
callq 0x1030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1415
movq -0x10(%rbp), %rax
movq -0x40(%rbp), %rcx
movzbl (%rax,%rcx), %ecx
andl $0xe0, %ecx
sarl $0x5, %ecx
movq -0x38(%rbp), %rax
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
movq -0x40(%rbp), %rcx
movzbl (%rax,%rcx), %ecx
andl $0x18, %ecx
sarl $0x3, %ecx
movq 0x10(%rbp), %rax
movl %ecx, (%rax)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x12da
movq -0x38(%rbp), %rax
movl $0x0, (%rax)
movq 0x10(%rbp), %rax
movl $0x0, (%rax)
cmpl $0x0, -0x48(%rbp)
je 0x13b7
leaq -0x450(%rbp), %rax
movq %rax, -0x470(%rbp)
movq $0x400, -0x468(%rbp) # imm = 0x400
movq -0x10(%rbp), %rdi
addq -0x40(%rbp), %rdi
movq -0x18(%rbp), %rsi
subq -0x40(%rbp), %rsi
leaq -0x458(%rbp), %rdx
leaq -0x460(%rbp), %rcx
leaq -0x470(%rbp), %r8
callq 0x14b0
movl %eax, -0x474(%rbp)
cmpl $0x0, -0x474(%rbp)
jge 0x1366
movq 0x18(%rbp), %rax
movq (%rax), %rdi
movq 0x18(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x470(%rbp), %rcx
leaq 0xdd35(%rip), %rdx      # 0xf088
movb $0x0, %al
callq 0x1030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1415
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, -0x458(%rbp)
jae 0x1376
jmp 0x1395
leaq 0xdd27(%rip), %rdi      # 0xf0a4
leaq 0xdd33(%rip), %rsi      # 0xf0b7
movl $0x221, %edx            # imm = 0x221
leaq 0xdd32(%rip), %rcx      # 0xf0c2
callq 0x1040
movq -0x40(%rbp), %rcx
addq -0x460(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x458(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
jmp 0x13d1
movq -0x40(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x18(%rbp), %rcx
subq -0x40(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rcx
movq -0x28(%rbp), %rdx
subq (%rdx), %rcx
cmpq %rcx, %rax
jbe 0x140e
movq 0x18(%rbp), %rax
movq (%rax), %rdi
movq 0x18(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0xdd2b(%rip), %rdx      # 0xf129
movb $0x0, %al
callq 0x1030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1415
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x480, %rsp            # imm = 0x480
popq %rbp
retq
nopw %cs:(%rax,%rax)