// File: /workspace/github_c_makefile_star2_O0/dwbuiten[P]obuparse/obuparse.c
// Function: obp_set_frame_refs
static inline int _obp_set_frame_refs(OBPFrameHeader *fh, OBPSequenceHeader *seq, OBPState *state, OBPError *err)
{
    int usedFrame[8];
    uint32_t curFrameHint, lastOrderHint, goldOrderHint, latestOrderHint, earliestOrderHint;
    int32_t ref;
    uint8_t shiftedOrderHints[8];
    const int Ref_Frame_List[7 - 2] = { 2, 3, 5, 6, 7 }; /*LAST2_FRAME, LAST3_FRAME, BWDREF_FRAME, ALTREF2_FRAME, ALTREF_FRAME */
    int32_t ref_frame_idx[8];

    for (int i = 0; i < 7; i++) {
        ref_frame_idx[i] = -1;
    }
    ref_frame_idx[1 - 1] = fh->last_frame_idx;
    ref_frame_idx[4 - 1] = fh->gold_frame_idx;
    for (int i = 0; i < 8; i++) {
        usedFrame[i] = 0;
    }
    usedFrame[fh->last_frame_idx] = 1;
    usedFrame[fh->gold_frame_idx] = 2;
    curFrameHint                  = 1 << (seq->OrderHintBits - 1);
    for (int i = 0; i < 8; i++) {
        shiftedOrderHints[i] = curFrameHint + _obp_get_relative_dist(state->RefOrderHint[i], fh->order_hint, seq);
    }
    lastOrderHint = shiftedOrderHints[fh->last_frame_idx];
    goldOrderHint = shiftedOrderHints[fh->gold_frame_idx];
    if (lastOrderHint >= curFrameHint || goldOrderHint >= curFrameHint) {
        snprintf(err->error, err->size, "(lastOrderHint >= curFrameHint || goldOrderHint >= curFrameHint) not allowed.");
        return -1;
    }
    /* find_latest_backward() */
    ref             = -1;
    latestOrderHint = 0;
    for (int i = 0; i < 8; i++) {
        uint32_t hint = shiftedOrderHints[i];
        if (!usedFrame[i] && (hint >= curFrameHint) && (ref < 0 || hint >= latestOrderHint)) {
            ref             = i;
            latestOrderHint = hint;
        }
    }
    if (ref >= 0) {
        ref_frame_idx[7 - 1] = ref;
        usedFrame[ref]       = 1;
    }
    /* find_earliest_backward() */
    ref               = -1;
    earliestOrderHint = 0;
    for (int i = 0; i < 8; i++) {
        uint32_t hint = shiftedOrderHints[i];
        if (!usedFrame[i] && (hint >= curFrameHint) && (ref < 0 || hint < earliestOrderHint)) {
            ref               = i;
            earliestOrderHint = hint;
        }
    }
    if (ref >= 0) {
        ref_frame_idx[5 - 1] = ref;
        usedFrame[ref]       = 1;
    }
    /* find_earliest_backward() */
    ref               = -1;
    earliestOrderHint = 0;
    for (int i = 0; i < 8; i++) {
        uint32_t hint = shiftedOrderHints[i];
        if (!usedFrame[i] && (hint >= curFrameHint) && (ref < 0 || hint < earliestOrderHint)) {
            ref               = i;
            earliestOrderHint = hint;
        }
    }
    if (ref >= 0) {
        ref_frame_idx[6 - 1] = ref;
        usedFrame[ref]       = 1;
    }
    for (int i = 0; i < 7 - 2; i++) {
        uint8_t refFrame = Ref_Frame_List[i];
        if (ref_frame_idx[refFrame - 1] < 0) {
            int32_t subref              = -1;
            uint32_t subLatestOrderHint = 0;
            for (int i = 0; i < 8; i++) {
                uint32_t hint = shiftedOrderHints[i];
                if (!usedFrame[i] && (hint < curFrameHint) && (ref < 0 || hint >= subLatestOrderHint)) {
                    ref                = i;
                    subLatestOrderHint = 0;
                }
            }
            ref = subref;
            if (ref >= 0) {
                ref_frame_idx[refFrame - 1] = ref;
                usedFrame[ref]              = 1;
            }
        }
    }
    ref = -1;
    for (int i = 0; i < 8; i++) {
        uint32_t hint = shiftedOrderHints[i];
        if (ref < 0 || hint < earliestOrderHint) {
            ref = i;
            earliestOrderHint = hint;
        }
    }
    for (int i = 0; i < 7; i++) {
        if (ref_frame_idx[i] < 0) {
            ref_frame_idx[i] = ref;
        }
    }
    for (int i = 0; i < 7; i++) {
        fh->ref_frame_idx[i] = ref_frame_idx[i];
    }

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq 0x164e(%rip), %rax      # 0xf5e0
movq %rax, -0x90(%rbp)
movq 0x1648(%rip), %rax      # 0xf5e8
movq %rax, -0x88(%rbp)
movl 0x1643(%rip), %eax      # 0xf5f0
movl %eax, -0x80(%rbp)
movl $0x0, -0xb4(%rbp)
cmpl $0x7, -0xb4(%rbp)
jge 0xdfe6
movslq -0xb4(%rbp), %rax
movl $0xffffffff, -0xb0(%rbp,%rax,4) # imm = 0xFFFFFFFF
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0xdfba
movq -0x10(%rbp), %rax
movzbl 0xf0(%rax), %eax
movl %eax, -0xb0(%rbp)
movq -0x10(%rbp), %rax
movzbl 0xf1(%rax), %eax
movl %eax, -0xa4(%rbp)
movl $0x0, -0xb8(%rbp)
cmpl $0x8, -0xb8(%rbp)
jge 0xe03b
movslq -0xb8(%rbp), %rax
movl $0x0, -0x50(%rbp,%rax,4)
movl -0xb8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb8(%rbp)
jmp 0xe012
movq -0x10(%rbp), %rax
movzbl 0xf0(%rax), %eax
movl $0x1, -0x50(%rbp,%rax,4)
movq -0x10(%rbp), %rax
movzbl 0xf1(%rax), %eax
movl $0x2, -0x50(%rbp,%rax,4)
movq -0x18(%rbp), %rax
movzbl 0x505(%rax), %ecx
subl $0x1, %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, -0x54(%rbp)
movl $0x0, -0xbc(%rbp)
cmpl $0x8, -0xbc(%rbp)
jge 0xe0e1
movl -0x54(%rbp), %eax
movl %eax, -0x100(%rbp)
movq -0x20(%rbp), %rax
movslq -0xbc(%rbp), %rcx
movzbl 0x490(%rax,%rcx), %edi
movq -0x10(%rbp), %rax
movzbl 0x34(%rax), %esi
movq -0x18(%rbp), %rdx
callq 0xe540
movl %eax, %ecx
movl -0x100(%rbp), %eax
addl %ecx, %eax
movb %al, %cl
movslq -0xbc(%rbp), %rax
movb %cl, -0x70(%rbp,%rax)
movl -0xbc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xbc(%rbp)
jmp 0xe083
movq -0x10(%rbp), %rax
movzbl 0xf0(%rax), %eax
movzbl -0x70(%rbp,%rax), %eax
movl %eax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movzbl 0xf1(%rax), %eax
movzbl -0x70(%rbp,%rax), %eax
movl %eax, -0x5c(%rbp)
movl -0x58(%rbp), %eax
cmpl -0x54(%rbp), %eax
jae 0xe117
movl -0x5c(%rbp), %eax
cmpl -0x54(%rbp), %eax
jb 0xe140
movq -0x28(%rbp), %rax
movq (%rax), %rdi
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0x1393(%rip), %rdx      # 0xf4c0
movb $0x0, %al
callq 0x1030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xe528
movl $0xffffffff, -0x68(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x60(%rbp)
movl $0x0, -0xc0(%rbp)
cmpl $0x8, -0xc0(%rbp)
jge 0xe1c2
movslq -0xc0(%rbp), %rax
movzbl -0x70(%rbp,%rax), %eax
movl %eax, -0xc4(%rbp)
movslq -0xc0(%rbp), %rax
cmpl $0x0, -0x50(%rbp,%rax,4)
jne 0xe1af
movl -0xc4(%rbp), %eax
cmpl -0x54(%rbp), %eax
jb 0xe1af
cmpl $0x0, -0x68(%rbp)
jl 0xe19d
movl -0xc4(%rbp), %eax
cmpl -0x60(%rbp), %eax
jb 0xe1af
movl -0xc0(%rbp), %eax
movl %eax, -0x68(%rbp)
movl -0xc4(%rbp), %eax
movl %eax, -0x60(%rbp)
jmp 0xe1b1
movl -0xc0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc0(%rbp)
jmp 0xe158
cmpl $0x0, -0x68(%rbp)
jl 0xe1dd
movl -0x68(%rbp), %eax
movl %eax, -0x98(%rbp)
movslq -0x68(%rbp), %rax
movl $0x1, -0x50(%rbp,%rax,4)
movl $0xffffffff, -0x68(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x64(%rbp)
movl $0x0, -0xc8(%rbp)
cmpl $0x8, -0xc8(%rbp)
jge 0xe25f
movslq -0xc8(%rbp), %rax
movzbl -0x70(%rbp,%rax), %eax
movl %eax, -0xcc(%rbp)
movslq -0xc8(%rbp), %rax
cmpl $0x0, -0x50(%rbp,%rax,4)
jne 0xe24c
movl -0xcc(%rbp), %eax
cmpl -0x54(%rbp), %eax
jb 0xe24c
cmpl $0x0, -0x68(%rbp)
jl 0xe23a
movl -0xcc(%rbp), %eax
cmpl -0x64(%rbp), %eax
jae 0xe24c
movl -0xc8(%rbp), %eax
movl %eax, -0x68(%rbp)
movl -0xcc(%rbp), %eax
movl %eax, -0x64(%rbp)
jmp 0xe24e
movl -0xc8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc8(%rbp)
jmp 0xe1f5
cmpl $0x0, -0x68(%rbp)
jl 0xe27a
movl -0x68(%rbp), %eax
movl %eax, -0xa0(%rbp)
movslq -0x68(%rbp), %rax
movl $0x1, -0x50(%rbp,%rax,4)
movl $0xffffffff, -0x68(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x64(%rbp)
movl $0x0, -0xd0(%rbp)
cmpl $0x8, -0xd0(%rbp)
jge 0xe2fc
movslq -0xd0(%rbp), %rax
movzbl -0x70(%rbp,%rax), %eax
movl %eax, -0xd4(%rbp)
movslq -0xd0(%rbp), %rax
cmpl $0x0, -0x50(%rbp,%rax,4)
jne 0xe2e9
movl -0xd4(%rbp), %eax
cmpl -0x54(%rbp), %eax
jb 0xe2e9
cmpl $0x0, -0x68(%rbp)
jl 0xe2d7
movl -0xd4(%rbp), %eax
cmpl -0x64(%rbp), %eax
jae 0xe2e9
movl -0xd0(%rbp), %eax
movl %eax, -0x68(%rbp)
movl -0xd4(%rbp), %eax
movl %eax, -0x64(%rbp)
jmp 0xe2eb
movl -0xd0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd0(%rbp)
jmp 0xe292
cmpl $0x0, -0x68(%rbp)
jl 0xe317
movl -0x68(%rbp), %eax
movl %eax, -0x9c(%rbp)
movslq -0x68(%rbp), %rax
movl $0x1, -0x50(%rbp,%rax,4)
movl $0x0, -0xd8(%rbp)
cmpl $0x5, -0xd8(%rbp)
jge 0xe431
movslq -0xd8(%rbp), %rax
movl -0x90(%rbp,%rax,4), %eax
movb %al, -0xd9(%rbp)
movzbl -0xd9(%rbp), %eax
subl $0x1, %eax
cltq
cmpl $0x0, -0xb0(%rbp,%rax,4)
jge 0xe41b
movl $0xffffffff, -0xe0(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0xe4(%rbp)
movl $0x0, -0xe8(%rbp)
cmpl $0x8, -0xe8(%rbp)
jge 0xe3e8
movslq -0xe8(%rbp), %rax
movzbl -0x70(%rbp,%rax), %eax
movl %eax, -0xec(%rbp)
movslq -0xe8(%rbp), %rax
cmpl $0x0, -0x50(%rbp,%rax,4)
jne 0xe3d5
movl -0xec(%rbp), %eax
cmpl -0x54(%rbp), %eax
jae 0xe3d5
cmpl $0x0, -0x68(%rbp)
jl 0xe3c2
movl -0xec(%rbp), %eax
cmpl -0xe4(%rbp), %eax
jb 0xe3d5
movl -0xe8(%rbp), %eax
movl %eax, -0x68(%rbp)
movl $0x0, -0xe4(%rbp)
jmp 0xe3d7
movl -0xe8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xe8(%rbp)
jmp 0xe37a
movl -0xe0(%rbp), %eax
movl %eax, -0x68(%rbp)
cmpl $0x0, -0x68(%rbp)
jl 0xe419
movl -0x68(%rbp), %ecx
movzbl -0xd9(%rbp), %eax
subl $0x1, %eax
cltq
movl %ecx, -0xb0(%rbp,%rax,4)
movslq -0x68(%rbp), %rax
movl $0x1, -0x50(%rbp,%rax,4)
jmp 0xe41b
jmp 0xe41d
movl -0xd8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd8(%rbp)
jmp 0xe321
movl $0xffffffff, -0x68(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0xf0(%rbp)
cmpl $0x8, -0xf0(%rbp)
jge 0xe493
movslq -0xf0(%rbp), %rax
movzbl -0x70(%rbp,%rax), %eax
movl %eax, -0xf4(%rbp)
cmpl $0x0, -0x68(%rbp)
jl 0xe46e
movl -0xf4(%rbp), %eax
cmpl -0x64(%rbp), %eax
jae 0xe480
movl -0xf0(%rbp), %eax
movl %eax, -0x68(%rbp)
movl -0xf4(%rbp), %eax
movl %eax, -0x64(%rbp)
jmp 0xe482
movl -0xf0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xf0(%rbp)
jmp 0xe442
movl $0x0, -0xf8(%rbp)
cmpl $0x7, -0xf8(%rbp)
jge 0xe4db
movslq -0xf8(%rbp), %rax
cmpl $0x0, -0xb0(%rbp,%rax,4)
jge 0xe4c8
movl -0x68(%rbp), %ecx
movslq -0xf8(%rbp), %rax
movl %ecx, -0xb0(%rbp,%rax,4)
jmp 0xe4ca
movl -0xf8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xf8(%rbp)
jmp 0xe49d
movl $0x0, -0xfc(%rbp)
cmpl $0x7, -0xfc(%rbp)
jge 0xe521
movslq -0xfc(%rbp), %rax
movl -0xb0(%rbp,%rax,4), %eax
movb %al, %dl
movq -0x10(%rbp), %rax
movslq -0xfc(%rbp), %rcx
movb %dl, 0xf2(%rax,%rcx)
movl -0xfc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xfc(%rbp)
jmp 0xe4e5
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopw %cs:(%rax,%rax)