// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
movq %r9, %rbx
movq %r8, 0x30(%rsp)
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbp
movq %rdi, 0x8(%rsp)
movq %rsi, 0x10(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rsp)
movb $0x0, 0x28(%rsp)
movzwl 0x120(%rdx), %eax
imulw 0x122(%rdx), %ax
movw %ax, (%rcx)
andl $0x0, 0x4(%rcx)
cmpw $0x2, %ax
jb 0x2710
testq %r13, %r13
je 0x2872
leaq 0x8(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x24ef
movq %rax, %rcx
movl %ecx, 0x4(%r15)
movzwl (%r15), %eax
testl %ecx, %ecx
sete %cl
cmpw $0x1, %ax
sete %dl
orb %cl, %dl
cmpb $0x1, %dl
jne 0x27ef
andw $0x0, 0x8(%r15)
decl %eax
movw %ax, 0xa(%r15)
xorl %edx, %edx
movzbl 0x28(%rsp), %ecx
andl $-0x8, %ecx
movq 0x18(%rsp), %rsi
shlq $0x3, %rsi
subq %rcx, %rsi
shrq $0x3, %rsi
subq %rsi, %r13
movzwl %ax, %edi
cmpw %di, %dx
ja 0x27a6
jne 0x274b
movq %r13, 0x10(%r15,%rdi,8)
jmp 0x27a2
movzbl 0x128(%r12), %r8d
incq %r8
movzwl %dx, %eax
cmpq %r8, %r13
jb 0x27c3
leaq (%rsi,%rbp), %r10
movzwl %r8w, %ecx
movzbl %cl, %r11d
shll $0x3, %r11d
xorl %ecx, %ecx
xorl %r9d, %r9d
cmpq %rcx, %r11
je 0x278c
movzbl (%r10), %r14d
shlq %cl, %r14
addq %r14, %r9
addq $0x8, %rcx
incq %r10
jmp 0x2774
incq %r9
movq %r9, 0x10(%r15,%rax,8)
cmpq %r9, %r13
jb 0x27d3
addq %r8, %r9
subq %r9, %r13
addq %r9, %rsi
incl %edx
jmp 0x273d
movzwl (%r15), %ecx
decl %ecx
xorl %eax, %eax
cmpl %edi, %ecx
jne 0x288a
movq 0x30(%rsp), %rcx
andl $0x0, (%rcx)
jmp 0x288a
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
leaq 0x5a73(%rip), %rdx      # 0x8244
jmp 0x27e1
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
leaq 0x5a98(%rip), %rdx      # 0x8279
movl %eax, %ecx
xorl %eax, %eax
callq 0x1030
jmp 0x2887
movzwl 0x122(%r12), %esi
pushq $0x1
popq %rdi
callq 0x28b5
movl %eax, %r14d
movzwl 0x120(%r12), %esi
pushq $0x1
popq %rdi
callq 0x28b5
addl %r14d, %eax
movzbl %al, %r14d
movzbl 0x28(%rsp), %eax
negl %eax
addl %r14d, %eax
addl $0x7, %eax
sarl $0x3, %eax
cltq
movq 0x10(%rsp), %rcx
subq 0x18(%rsp), %rcx
cmpq %rax, %rcx
jb 0x2872
leaq 0x8(%rsp), %rdi
movl %r14d, %esi
callq 0x24ef
movw %ax, 0x8(%r15)
leaq 0x8(%rsp), %rdx
movzbl 0x20(%rdx), %eax
movl %r14d, %ecx
subl %eax, %ecx
addl $0x7, %ecx
sarl $0x3, %ecx
movslq %ecx, %rax
movq 0x8(%rdx), %rcx
subq 0x10(%rdx), %rcx
cmpq %rax, %rcx
jae 0x2899
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
leaq 0x58ed(%rip), %rdx      # 0x816d
xorl %eax, %eax
callq 0x1030
pushq $-0x1
popq %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8(%rsp), %rdi
movl %r14d, %esi
callq 0x24ef
movw %ax, 0xa(%r15)
movzwl 0x8(%r15), %edx
jmp 0x271f