// File: /workspace/github_c_makefile_star2_O3/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, (%rsp)
movq %r8, 0x30(%rsp)
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %r13
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
movq %rcx, %rbp
movw %ax, (%rcx)
movl $0x0, 0x4(%rcx)
cmpw $0x2, %ax
jb 0x27a2
testq %r12, %r12
je 0x292a
leaq 0x8(%rsp), %rdi
movl $0x1, %esi
callq 0x244e
movq %rax, %rcx
movl %ecx, 0x4(%rbp)
movzwl (%rbp), %eax
testl %ecx, %ecx
sete %cl
cmpw $0x1, %ax
sete %dl
orb %cl, %dl
cmpb $0x1, %dl
jne 0x289c
movw $0x0, 0x8(%rbp)
decl %eax
movw %ax, 0xa(%rbp)
xorl %edx, %edx
movb 0x28(%rsp), %cl
andb $-0x8, %cl
movq 0x18(%rsp), %rsi
shlq $0x3, %rsi
movzbl %cl, %ecx
subq %rcx, %rsi
shrq $0x3, %rsi
subq %rsi, %r12
movzwl %ax, %edi
cmpw %ax, %dx
jne 0x27dc
movq %r12, 0x10(%rbp,%rdi,8)
jmp 0x283d
movzbl 0x128(%r15), %r9d
incq %r9
movzwl %dx, %r8d
cmpq %r9, %r12
jb 0x2867
movl %r9d, %ecx
andw $0xff, %cx
je 0x2824
leaq (%rsi,%r13), %r11
movzwl %cx, %ebx
shll $0x3, %ebx
xorl %ecx, %ecx
xorl %r10d, %r10d
movzbl (%r11), %r14d
shlq %cl, %r14
addq %r14, %r10
addq $0x8, %rcx
incq %r11
cmpq %rcx, %rbx
jne 0x2809
incq %r10
jmp 0x282a
movl $0x1, %r10d
movq %r10, 0x10(%rbp,%r8,8)
cmpq %r10, %r12
jb 0x287b
addq %r9, %r10
subq %r10, %r12
addq %r10, %rsi
incl %edx
cmpw %ax, %dx
jbe 0x27d0
movzwl %ax, %ecx
movzwl (%rbp), %edx
decl %edx
xorl %eax, %eax
cmpl %ecx, %edx
jne 0x2948
movq 0x30(%rsp), %rcx
movl $0x0, (%rcx)
jmp 0x2948
movq (%rsp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rsi
leaq 0x5a17(%rip), %rdx      # 0x8290
jmp 0x288d
movq (%rsp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rsi
leaq 0x5a38(%rip), %rdx      # 0x82c5
movl %r8d, %ecx
xorl %eax, %eax
callq 0x1030
jmp 0x2943
movzwl 0x122(%r15), %eax
movl $0xfffffffe, %ebx       # imm = 0xFFFFFFFE
leal 0x2(%rbx), %ecx
movl $0x1, %edx
shll %cl, %edx
incl %ebx
cmpl %eax, %edx
jb 0x28a9
movzwl 0x120(%r15), %eax
xorl %ecx, %ecx
movl $0x1, %edx
shll %cl, %edx
incl %ecx
incl %ebx
cmpl %eax, %edx
jb 0x28c3
movzbl %bl, %r14d
movzbl 0x28(%rsp), %eax
addl $0x7, %r14d
movl %r14d, %ecx
subl %eax, %ecx
sarl $0x3, %ecx
movslq %ecx, %rax
movq 0x10(%rsp), %rcx
subq 0x18(%rsp), %rcx
cmpq %rax, %rcx
jb 0x292a
movzbl %bl, %esi
leaq 0x8(%rsp), %rdi
callq 0x244e
movw %ax, 0x8(%rbp)
leaq 0x8(%rsp), %rdx
movzbl 0x20(%rdx), %eax
subl %eax, %r14d
sarl $0x3, %r14d
movslq %r14d, %rax
movq 0x8(%rdx), %rcx
subq 0x10(%rdx), %rcx
cmpq %rax, %rcx
jae 0x2957
movq (%rsp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rsi
leaq 0x587d(%rip), %rdx      # 0x81b9
xorl %eax, %eax
callq 0x1030
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movzbl %bl, %esi
leaq 0x8(%rsp), %rdi
callq 0x244e
movw %ax, 0xa(%rbp)
movzwl 0x8(%rbp), %edx
cmpw %ax, %dx
jbe 0x27b0
jmp 0x2844