// File: /workspace/github_c_makefile_star2_O1/dwbuiten[P]obuparse/obuparse.c
// Function: obp_parse_tile_group
int obp_parse_tile_group(uint8_t *buf, size_t buf_size, OBPFrameHeader *frame_header, OBPTileGroup *tile_group,
                         int *SeenFrameHeader, OBPError *err)
{
    _OBPBitReader b   = _obp_new_br(buf, buf_size);
    _OBPBitReader *br = &b;

    tile_group->NumTiles                        = frame_header->tile_info.TileCols * frame_header->tile_info.TileRows;
    size_t startBitPos                          = 0;
    tile_group->tile_start_and_end_present_flag = 0;

    if (tile_group->NumTiles > 1) {
        _obp_br(tile_group->tile_start_and_end_present_flag, br, 1);
    }
    if (tile_group->NumTiles == 1 || !tile_group->tile_start_and_end_present_flag) {
        tile_group->tg_start = 0;
        tile_group->tg_end   = tile_group->NumTiles - 1;
    } else {
        uint8_t tileBits = _obp_tile_log2(1, frame_header->tile_info.TileCols) + _obp_tile_log2(1, frame_header->tile_info.TileRows);
        _obp_br(tile_group->tg_start, br, tileBits);
        _obp_br(tile_group->tg_end, br, tileBits);
    }
    _obp_br_byte_alignment(br);
    size_t endBitPos   = _obp_br_get_pos(br);
    size_t headerBytes = (endBitPos - startBitPos) / 8;
    size_t sz          = buf_size - headerBytes;
    size_t pos         = headerBytes;

    for (uint16_t TileNum = tile_group->tg_start; TileNum <= tile_group->tg_end; TileNum++) {
        /* tileRow = TileNum / TileCols */
        /* tileCol = TileNum % TileCols */
        int lastTile     = (TileNum == tile_group->tg_end);
        if (lastTile) {
            tile_group->TileSize[TileNum] = sz;
        } else {
            uint16_t TileSizeBytes = frame_header->tile_info.tile_size_bytes_minus_1 + 1;
            uint64_t tile_size_minus_1;
            if (sz < TileSizeBytes) {
                snprintf(err->error, err->size, "Not enough bytes left to read tile size for tile %"PRIu16".", TileNum);
                return -1;
            }
            tile_size_minus_1             = _obp_le(buf + pos, TileSizeBytes);
            tile_group->TileSize[TileNum] = tile_size_minus_1 + 1;
            if (sz < tile_group->TileSize[TileNum]) {
                snprintf(err->error, err->size, "Not enough bytes to contain TileSize for tile %"PRIu16".", TileNum);
                return -1;
            }
            sz  -= tile_group->TileSize[TileNum] + TileSizeBytes;
            pos += tile_group->TileSize[TileNum] + TileSizeBytes;
        }
        /* MiRowStart = MiRowStarts[ tileRow ] */
        /* MiRowEnd = MiRowStarts[ tileRow + 1 ] */
        /* MiColStart = MiColStarts[ tileCol ] */
        /* MiColEnd = MiColStarts[ tileCol + 1 ] */
        /* CurrentQIndex = base_q_idx */
        /* init_symbol( tileSize ) */
        /* decode_tile( ) */
        /* exit_symbol( ) */
    }
    if (tile_group->tg_end == tile_group->NumTiles - 1) {
        /* if ( !disable_frame_end_update_cdf ) {
               frame_end_update_cdf( )
           }
         */
        /* decode_frame_wrapup() is handled in obp_parse_frame_header. */
        *SeenFrameHeader = 0;
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r9, 0x8(%rsp)
movq %r8, %r13
movq %rsi, %r12
movq %rdi, 0x50(%rsp)
movq %rdi, 0x18(%rsp)
movq %rsi, 0x20(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%rsp)
movb $0x0, 0x38(%rsp)
movzwl 0x120(%rdx), %eax
movq %rdx, 0x10(%rsp)
imulw 0x122(%rdx), %ax
movq %rcx, %rbp
movw %ax, (%rcx)
movl $0x0, 0x4(%rcx)
cmpw $0x2, %ax
jb 0x30d1
movzbl 0x38(%rsp), %eax
movl $0x8, %ecx
subl %eax, %ecx
sarl $0x3, %ecx
movslq %ecx, %r14
movq %r12, %r15
subq 0x28(%rsp), %r15
cmpq %r14, %r15
jae 0x30b1
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rsi
leaq 0x80d5(%rip), %rdx      # 0xb17d
xorl %eax, %eax
callq 0x1030
jmp 0x30c3
leaq 0x18(%rsp), %rdi
movl $0x1, %esi
callq 0x2cf9
movl %eax, 0x4(%rbp)
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpq %r14, %r15
jb 0x332a
movzwl (%rbp), %eax
cmpw $0x1, %ax
je 0x317d
cmpl $0x0, 0x4(%rbp)
je 0x317d
movq %r13, 0x40(%rsp)
movq 0x10(%rsp), %rax
movzwl 0x122(%rax), %eax
movl $0xfffffffe, %r13d      # imm = 0xFFFFFFFE
leal 0x2(%r13), %ecx
movl $0x1, %edx
shll %cl, %edx
incl %r13d
cmpl %eax, %edx
jb 0x3100
movq 0x10(%rsp), %rax
movzwl 0x120(%rax), %eax
xorl %ecx, %ecx
movl $0x1, %edx
shll %cl, %edx
incl %ecx
incl %r13d
cmpl %eax, %edx
jb 0x3120
movzbl %r13b, %r14d
movzbl 0x38(%rsp), %eax
negl %eax
addl %r14d, %eax
addl $0x7, %eax
sarl $0x3, %eax
cltq
movq 0x20(%rsp), %r15
subq 0x28(%rsp), %r15
cmpq %rax, %r15
movq %rax, 0x48(%rsp)
jae 0x32ae
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rsi
leaq 0x800c(%rip), %rdx      # 0xb17d
xorl %eax, %eax
callq 0x1030
jmp 0x32c0
movw $0x0, 0x8(%rbp)
decl %eax
movw %ax, 0xa(%rbp)
movq %r13, %r15
movzbl 0x38(%rsp), %ecx
andl $-0x8, %ecx
movb %cl, 0x38(%rsp)
movzwl 0x8(%rbp), %ebx
movzwl 0xa(%rbp), %eax
cmpw %ax, %bx
jbe 0x31c5
movzwl 0xa(%rbp), %eax
movzwl (%rbp), %ecx
decl %ecx
xorl %ebx, %ebx
cmpl %eax, %ecx
jne 0x332a
movl $0x0, (%r15)
jmp 0x332a
movq 0x28(%rsp), %r13
shlq $0x3, %r13
subq %rcx, %r13
shrq $0x3, %r13
subq %r13, %r12
movzwl %bx, %ebx
cmpw %ax, %bx
jne 0x31ef
movl %ebx, %eax
movq %r12, 0x10(%rbp,%rax,8)
movb $0x1, %r14b
jmp 0x3280
movq 0x10(%rsp), %rax
movzbl 0x128(%rax), %eax
incq %rax
cmpq %rax, %r12
jae 0x321b
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rsi
xorl %r14d, %r14d
leaq 0x803b(%rip), %rdx      # 0xb254
jmp 0x3277
testb %al, %al
je 0x3250
movq 0x50(%rsp), %rcx
leaq (%rcx,%r13), %rsi
movzwl %ax, %ecx
movzbl %cl, %edi
shll $0x3, %edi
xorl %ecx, %ecx
xorl %edx, %edx
movzbl (%rsi), %r8d
shlq %cl, %r8
addq %r8, %rdx
addq $0x8, %rcx
incq %rsi
cmpq %rcx, %rdi
jne 0x3235
incq %rdx
jmp 0x3255
movl $0x1, %edx
movl %ebx, %ecx
movq %rdx, 0x10(%rbp,%rcx,8)
cmpq %rdx, %r12
jae 0x3299
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rsi
xorl %r14d, %r14d
leaq 0x8012(%rip), %rdx      # 0xb289
movl %ebx, %ecx
xorl %eax, %eax
callq 0x1030
testb %r14b, %r14b
je 0x32a7
incl %ebx
movzwl 0xa(%rbp), %eax
cmpw %ax, %bx
jbe 0x31d8
jmp 0x31a5
addq %rax, %rdx
subq %rdx, %r12
addq %rdx, %r13
jmp 0x31e7
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x332a
movzbl %r13b, %esi
leaq 0x18(%rsp), %rdi
callq 0x2cf9
movw %ax, 0x8(%rbp)
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpq 0x48(%rsp), %r15
jb 0x332a
movzbl 0x38(%rsp), %eax
subl %eax, %r14d
addl $0x7, %r14d
sarl $0x3, %r14d
movslq %r14d, %r14
movq 0x20(%rsp), %r15
subq 0x28(%rsp), %r15
cmpq %r14, %r15
jae 0x330a
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rsi
leaq 0x7e7c(%rip), %rdx      # 0xb17d
xorl %eax, %eax
callq 0x1030
jmp 0x331c
movzbl %r13b, %esi
leaq 0x18(%rsp), %rdi
callq 0x2cf9
movw %ax, 0xa(%rbp)
movq 0x40(%rsp), %r13
cmpq %r14, %r15
jae 0x3189
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq