// File: /workspace/github_c_makefile_star2_O0/videolan[P]bitstream/examples/dvb_ecmg_test.c
// Function: main
int main(int i_argc, char **ppsz_argv)
{
    if (i_argc != 3) {
        fprintf(stderr, "usage: %s <# streams> <crypto-period (s)> < < <input stream> > <output stream>\n",
                ppsz_argv[0]);
        return EXIT_FAILURE;
    }

    i_wanted_streams = atoi(ppsz_argv[1]);
    i_period = atoi(ppsz_argv[2]);
    i_last_time = time(NULL);

    srand(time(NULL));
    i_channelid = rand();
    i_supercasid = rand();
    send_channel_setup();

    for ( ; ; ) {
        uint8_t *p_tlv_h = malloc(TLVH_HEADER_SIZE + TLV_HEADER_SIZE);
        uint8_t *p_tlv = tlvh_get_tlv(p_tlv_h);
        uint16_t i_type;
        fd_set rset;
        struct timeval timeout;
        int i_ret;

        FD_ZERO(&rset);
        FD_SET(STDIN_FILENO, &rset);
        timeout.tv_sec = SELECT_TIMEOUT;
        timeout.tv_usec = 0;

        if ((i_ret = select(STDIN_FILENO + 1, &rset, NULL, NULL, &timeout)) < 0)
            return EXIT_FAILURE;
        if (!i_ret) {
            goto no_packet;
        }

        if (read_wrapper(p_tlv_h, TLVH_HEADER_SIZE + TLV_HEADER_SIZE) <= 0)
            return EXIT_FAILURE;

        p_tlv_h = realloc(p_tlv_h, TLVH_HEADER_SIZE + TLV_HEADER_SIZE
                                    + tlv_get_length(p_tlv));
        p_tlv = tlvh_get_tlv(p_tlv_h);
        if (read_wrapper(p_tlv + TLV_HEADER_SIZE, tlv_get_length(p_tlv)) <= 0)
            return EXIT_FAILURE;

        if (tlvh_get_version(p_tlv_h) != i_version) {
            send_channel_error(i_channelid, 0x2);
            free(p_tlv_h);
            exit(EXIT_FAILURE);
        }

        i_type = tlv_get_type(p_tlv);

        if (!tlv_validate(p_tlv)) {
            send_channel_error(i_channelid, 0x1);
            free(p_tlv_h);
            exit(EXIT_FAILURE);
        }

        if (!ecmg_validate(p_tlv)) {
            send_channel_error(i_channelid, 0xf);
            free(p_tlv_h);
            exit(EXIT_FAILURE);
        }

        if (b_init && i_type != ECMG_TYPE_CHANNEL_STATUS) {
            send_channel_error(i_channelid, 0x6);
            free(p_tlv_h);
            exit(EXIT_FAILURE);
        }

        switch (i_type) {
            case ECMG_TYPE_CHANNEL_STATUS:
                handle_channel_status(p_tlv);
                break;

            case ECMG_TYPE_CHANNEL_TEST:
                handle_channel_test(p_tlv);
                break;

            case ECMG_TYPE_CHANNEL_ERROR:
                handle_channel_error(p_tlv);
                break;

            case ECMG_TYPE_STREAM_STATUS:
                handle_stream_status(p_tlv);
                break;

            case ECMG_TYPE_STREAM_TEST:
                handle_stream_test(p_tlv);
                break;

            case ECMG_TYPE_STREAM_ERROR:
                handle_stream_error(p_tlv);
                break;

            case ECMG_TYPE_ECM:
                handle_ecm(p_tlv);
                break;

            default:
                send_channel_error(i_channelid, 0x3);
                break;
        }

no_packet:
        free(p_tlv_h);

        if (!b_init) {
            time_t i_time = time(NULL);

            if (i_nb_streams < i_wanted_streams)
                create_stream();

            while (i_last_time < i_time) {
                i_last_time++;
                check_cw();
            }
        }
    }

    return EXIT_SUCCESS;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x3, -0x8(%rbp)
je 0x13ca
movq 0x5c52(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0x2c98(%rip), %rsi      # 0x404f
movb $0x0, %al
callq 0x1080
movl $0x1, -0x4(%rbp)
jmp 0x17a2
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x10e0
movl %eax, 0x5ccb(%rip)      # 0x70a8
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x10e0
movl %eax, 0x5cbc(%rip)      # 0x70ac
xorl %eax, %eax
movl %eax, %edi
callq 0x10a0
movq %rax, 0x5cb0(%rip)      # 0x70b0
xorl %eax, %eax
movl %eax, %edi
callq 0x10a0
movl %eax, %edi
callq 0x1070
callq 0x1100
movw %ax, 0x5c9c(%rip)       # 0x70b8
callq 0x1100
movl %eax, 0x5c95(%rip)      # 0x70bc
callq 0x17b0
movl $0x5, %edi
callq 0x10c0
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
callq 0x18a0
movq %rax, -0x20(%rbp)
leaq -0xa8(%rbp), %rax
movq %rax, -0xc8(%rbp)
movl $0x0, -0xc0(%rbp)
movl -0xc0(%rbp), %eax
cmpq $0x10, %rax
jae 0x1491
movq -0xc8(%rbp), %rax
movl -0xc0(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0xc0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc0(%rbp)
jmp 0x145f
jmp 0x1493
movq -0xa8(%rbp), %rax
orq $0x1, %rax
movq %rax, -0xa8(%rbp)
movq $0x1, -0xb8(%rbp)
movq $0x0, -0xb0(%rbp)
movl $0x1, %edi
leaq -0xa8(%rbp), %rsi
xorl %eax, %eax
movl %eax, %ecx
leaq -0xb8(%rbp), %r8
movq %rcx, %rdx
callq 0x10b0
movl %eax, -0xbc(%rbp)
cmpl $0x0, %eax
jge 0x14f1
movl $0x1, -0x4(%rbp)
jmp 0x17a2
cmpl $0x0, -0xbc(%rbp)
jne 0x14ff
jmp 0x173b
movq -0x18(%rbp), %rdi
movl $0x5, %esi
callq 0x1210
cmpq $0x0, %rax
jg 0x151f
movl $0x1, -0x4(%rbp)
jmp 0x17a2
movq -0x18(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0x20(%rbp), %rdi
callq 0x18c0
movq -0xe0(%rbp), %rdi
movzwl %ax, %eax
addl $0x5, %eax
movslq %eax, %rsi
callq 0x10d0
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
callq 0x18a0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
addq $0x4, %rax
movq %rax, -0xd8(%rbp)
movq -0x20(%rbp), %rdi
callq 0x18c0
movq -0xd8(%rbp), %rdi
movzwl %ax, %eax
movl %eax, %esi
callq 0x1210
cmpq $0x0, %rax
jg 0x1594
movl $0x1, -0x4(%rbp)
jmp 0x17a2
movq -0x18(%rbp), %rdi
callq 0x18e0
movzbl %al, %eax
movzbl 0x5af1(%rip), %ecx      # 0x7098
cmpl %ecx, %eax
je 0x15cf
movl $0x2, %esi
movzwl 0x5b01(%rip), %edi      # 0x70b8
callq 0x18f0
movq -0x18(%rbp), %rdi
callq 0x1030
movl $0x1, %edi
callq 0x10f0
movq -0x20(%rbp), %rdi
callq 0x1a00
movw %ax, -0x22(%rbp)
movq -0x20(%rbp), %rdi
callq 0x1a20
testb $0x1, %al
jne 0x160d
movl $0x1, %esi
movzwl 0x5ac3(%rip), %edi      # 0x70b8
callq 0x18f0
movq -0x18(%rbp), %rdi
callq 0x1030
movl $0x1, %edi
callq 0x10f0
movq -0x20(%rbp), %rdi
callq 0x1ae0
testb $0x1, %al
jne 0x163e
movl $0xf, %esi
movzwl 0x5a92(%rip), %edi      # 0x70b8
callq 0x18f0
movq -0x18(%rbp), %rdi
callq 0x1030
movl $0x1, %edi
callq 0x10f0
testb $0x1, 0x5a54(%rip)      # 0x7099
je 0x1674
movzwl -0x22(%rbp), %eax
cmpl $0x3, %eax
je 0x1674
movl $0x6, %esi
movzwl 0x5a5c(%rip), %edi      # 0x70b8
callq 0x18f0
movq -0x18(%rbp), %rdi
callq 0x1030
movl $0x1, %edi
callq 0x10f0
movzwl -0x22(%rbp), %eax
movl %eax, -0xe4(%rbp)
subl $0x2, %eax
je 0x16e6
jmp 0x1685
movl -0xe4(%rbp), %eax
subl $0x3, %eax
je 0x16db
jmp 0x1692
movl -0xe4(%rbp), %eax
subl $0x5, %eax
je 0x16f1
jmp 0x169f
movl -0xe4(%rbp), %eax
subl $0x102, %eax            # imm = 0x102
je 0x1707
jmp 0x16ae
movl -0xe4(%rbp), %eax
subl $0x103, %eax            # imm = 0x103
je 0x16fc
jmp 0x16bd
movl -0xe4(%rbp), %eax
subl $0x106, %eax            # imm = 0x106
je 0x1712
jmp 0x16cc
movl -0xe4(%rbp), %eax
subl $0x202, %eax            # imm = 0x202
je 0x171d
jmp 0x1728
movq -0x20(%rbp), %rdi
callq 0x1cf0
jmp 0x1739
movq -0x20(%rbp), %rdi
callq 0x1e50
jmp 0x1739
movq -0x20(%rbp), %rdi
callq 0x1ec0
jmp 0x1739
movq -0x20(%rbp), %rdi
callq 0x1f20
jmp 0x1739
movq -0x20(%rbp), %rdi
callq 0x1ff0
jmp 0x1739
movq -0x20(%rbp), %rdi
callq 0x2090
jmp 0x1739
movq -0x20(%rbp), %rdi
callq 0x2110
jmp 0x1739
movl $0x3, %esi
movzwl 0x5984(%rip), %edi      # 0x70b8
callq 0x18f0
jmp 0x173b
movq -0x18(%rbp), %rdi
callq 0x1030
testb $0x1, 0x594e(%rip)      # 0x7099
jne 0x179d
xorl %eax, %eax
movl %eax, %edi
callq 0x10a0
movq %rax, -0xd0(%rbp)
movl 0x595d(%rip), %eax      # 0x70c0
cmpl 0x593f(%rip), %eax      # 0x70a8
jge 0x1770
callq 0x2270
jmp 0x1772
movq 0x5937(%rip), %rax      # 0x70b0
cmpq -0xd0(%rbp), %rax
jge 0x179b
movq 0x5927(%rip), %rax      # 0x70b0
addq $0x1, %rax
movq %rax, 0x591c(%rip)      # 0x70b0
callq 0x2330
jmp 0x1772
jmp 0x179d
jmp 0x142c
movl -0x4(%rbp), %eax
addq $0xf0, %rsp
popq %rbp
retq
nop