// File: /workspace/github_c_makefile_star2_O0/algorithm-archivists[P]GathVL/src/encoders/video_encoder.cpp
// Function: write_frame(AVFormatContext*, output_stream*, AVFrame*)
static void write_frame(AVFormatContext *format_ctx, output_stream *ostream,
                        AVFrame *frame) {
    AVCodecContext *c = ostream->codec_ctx;
    AVPacket *pkt = nullptr;

    pkt = av_packet_alloc();
    if (!pkt) {
        std::cout << "Could not allocate a packet" << std::endl;
        std::exit(1);
    }

    int ret = avcodec_send_frame(c, frame);
    if (ret < 0) {
        std::cout << "Error encoding video frame." << std::endl;
        std::exit(1);
    }

    while (ret >= 0) {
        ret = avcodec_receive_packet(c, pkt);
        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
            break;
        else if (ret < 0) {
            std::cout << "Error during encoding." << std::endl;
            std::exit(1);
        }

        av_packet_rescale_ts(pkt, c->time_base, ostream->stream->time_base);
        pkt->stream_index = ostream->stream->index;

        ret = av_interleaved_write_frame(format_ctx, pkt);
        av_packet_unref(pkt);
        if (ret < 0) {
            std::cout << "Error while writing video frame." << std::endl;
            std::exit(1);
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
callq 0x5450
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x39064
movq 0x15f69(%rip), %rdi     # 0x4efa8
leaq 0x47d4(%rip), %rsi      # 0x3d81a
callq 0x5780
movq %rax, %rdi
movq 0x15f6b(%rip), %rsi     # 0x4efc0
callq 0x5960
movl $0x1, %edi
callq 0x57d0
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x5460
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jge 0x390a6
movq 0x15f27(%rip), %rdi     # 0x4efa8
leaq 0x47ae(%rip), %rsi      # 0x3d836
callq 0x5780
movq %rax, %rdi
movq 0x15f29(%rip), %rsi     # 0x4efc0
callq 0x5960
movl $0x1, %edi
callq 0x57d0
jmp 0x390a8
cmpl $0x0, -0x2c(%rbp)
jl 0x39196
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x57c0
movl %eax, -0x2c(%rbp)
cmpl $-0xb, -0x2c(%rbp)
je 0x390d1
cmpl $0xdfb9b0bb, -0x2c(%rbp) # imm = 0xDFB9B0BB
jne 0x390d6
jmp 0x39196
cmpl $0x0, -0x2c(%rbp)
jge 0x39108
movq 0x15ec5(%rip), %rdi     # 0x4efa8
leaq 0x4768(%rip), %rsi      # 0x3d852
callq 0x5780
movq %rax, %rdi
movq 0x15ec7(%rip), %rsi     # 0x4efc0
callq 0x5960
movl $0x1, %edi
callq 0x57d0
jmp 0x3910a
movq -0x28(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x64(%rax), %rax
movq %rax, -0x34(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movq %rax, -0x3c(%rbp)
movq -0x34(%rbp), %rsi
movq -0x3c(%rbp), %rdx
callq 0x55a0
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl (%rax), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x24(%rax)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x54c0
movl %eax, -0x2c(%rbp)
movq -0x28(%rbp), %rdi
callq 0x5930
cmpl $0x0, -0x2c(%rbp)
jge 0x39191
movq 0x15e3c(%rip), %rdi     # 0x4efa8
leaq 0x46f6(%rip), %rsi      # 0x3d869
callq 0x5780
movq %rax, %rdi
movq 0x15e3e(%rip), %rsi     # 0x4efc0
callq 0x5960
movl $0x1, %edi
callq 0x57d0
jmp 0x390a8
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)