// File: /workspace/github_c_makefile_star2_O2/stanfordnlp[P]GloVe/src/cooccur.c
// Function: main
int main(int argc, char **argv) {
    int i;
    real rlimit, n = 1e5;
    vocab_file = malloc(sizeof(char) * MAX_STRING_LENGTH);
    file_head = malloc(sizeof(char) * MAX_STRING_LENGTH);
    
    if (argc == 1) {
        printf("Tool to calculate word-word cooccurrence statistics\n");
        printf("Author: Jeffrey Pennington (jpennin@stanford.edu)\n\n");
        printf("Usage options:\n");
        printf("\t-verbose <int>\n");
        printf("\t\tSet verbosity: 0, 1, 2 (default), or 3\n");
        printf("\t-symmetric <int>\n");
        printf("\t\tIf <int> = 0, only use left context; if <int> = 1 (default), use left and right\n");
        printf("\t-window-size <int>\n");
        printf("\t\tNumber of context words to the left (and to the right, if symmetric = 1); default 15\n");
        printf("\t-vocab-file <file>\n");
        printf("\t\tFile containing vocabulary (truncated unigram counts, produced by 'vocab_count'); default vocab.txt\n");
        printf("\t-memory <float>\n");
        printf("\t\tSoft limit for memory consumption, in GB -- based on simple heuristic, so not extremely accurate; default 4.0\n");
        printf("\t-max-product <int>\n");
        printf("\t\tLimit the size of dense cooccurrence array by specifying the max product <int> of the frequency counts of the two cooccurring words.\n\t\tThis value overrides that which is automatically produced by '-memory'. Typically only needs adjustment for use with very large corpora.\n");
        printf("\t-overflow-length <int>\n");
        printf("\t\tLimit to length <int> the sparse overflow array, which buffers cooccurrence data that does not fit in the dense array, before writing to disk. \n\t\tThis value overrides that which is automatically produced by '-memory'. Typically only needs adjustment for use with very large corpora.\n");
        printf("\t-overflow-file <file>\n");
        printf("\t\tFilename, excluding extension, for temporary files; default overflow\n");
        printf("\t-distance-weighting <int>\n");
        printf("\t\tIf <int> = 0, do not weight cooccurrence count by distance between words; if <int> = 1 (default), weight the cooccurrence count by inverse of distance between words\n");

        printf("\nExample usage:\n");
        printf("./cooccur -verbose 2 -symmetric 0 -window-size 10 -vocab-file vocab.txt -memory 8.0 -overflow-file tempoverflow < corpus.txt > cooccurrences.bin\n\n");
        free(vocab_file);
        free(file_head);
        return 0;
    }

    if ((i = find_arg((char *)"-verbose", argc, argv)) > 0) verbose = atoi(argv[i + 1]);
    if ((i = find_arg((char *)"-symmetric", argc, argv)) > 0) symmetric = atoi(argv[i + 1]);
    if ((i = find_arg((char *)"-window-size", argc, argv)) > 0) window_size = atoi(argv[i + 1]);
    if ((i = find_arg((char *)"-vocab-file", argc, argv)) > 0) strcpy(vocab_file, argv[i + 1]);
    else strcpy(vocab_file, (char *)"vocab.txt");
    if ((i = find_arg((char *)"-overflow-file", argc, argv)) > 0) strcpy(file_head, argv[i + 1]);
    else strcpy(file_head, (char *)"overflow");
    if ((i = find_arg((char *)"-memory", argc, argv)) > 0) memory_limit = atof(argv[i + 1]);
    if ((i = find_arg((char *)"-distance-weighting", argc, argv)) > 0)  distance_weighting = atoi(argv[i + 1]);
    
    /* The memory_limit determines a limit on the number of elements in bigram_table and the overflow buffer */
    /* Estimate the maximum value that max_product can take so that this limit is still satisfied */
    rlimit = 0.85 * (real)memory_limit * 1073741824/(sizeof(CREC));
    while (fabs(rlimit - n * (log(n) + 0.1544313298)) > 1e-3) n = rlimit / (log(n) + 0.1544313298);
    max_product = (long long) n;
    overflow_length = (long long) rlimit/6; // 0.85 + 1/6 ~= 1
    
    /* Override estimates by specifying limits explicitly on the command line */
    if ((i = find_arg((char *)"-max-product", argc, argv)) > 0) max_product = atoll(argv[i + 1]);
    if ((i = find_arg((char *)"-overflow-length", argc, argv)) > 0) overflow_length = atoll(argv[i + 1]);
    
    const int returned_value = get_cooccurrence();
    free(vocab_file);
    free(file_head);
    return returned_value;
}

// Assembly:
pushq %rbp
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
movl %edi, %ebp
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x1170
movq %rax, 0x3c33(%rip)      # 0x6140
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x1170
movq %rax, 0x3c0a(%rip)      # 0x6128
cmpl $0x1, %ebp
jne 0x2642
leaq 0xe9d(%rip), %rdi       # 0x33cb
callq 0x1080
leaq 0xec5(%rip), %rdi       # 0x33ff
callq 0x1080
leaq 0xeec(%rip), %rdi       # 0x3432
callq 0x1080
leaq 0xeef(%rip), %rdi       # 0x3441
callq 0x1080
leaq 0xef3(%rip), %rdi       # 0x3451
callq 0x1080
leaq 0xf10(%rip), %rdi       # 0x347a
callq 0x1080
leaq 0xf16(%rip), %rdi       # 0x348c
callq 0x1080
leaq 0xf5c(%rip), %rdi       # 0x34de
callq 0x1080
leaq 0xf64(%rip), %rdi       # 0x34f2
callq 0x1080
leaq 0xfaf(%rip), %rdi       # 0x3549
callq 0x1080
leaq 0xfb7(%rip), %rdi       # 0x355d
callq 0x1080
leaq 0x1011(%rip), %rdi      # 0x35c3
callq 0x1080
leaq 0x1016(%rip), %rdi      # 0x35d4
callq 0x1080
leaq 0x107a(%rip), %rdi      # 0x3644
callq 0x1080
leaq 0x1082(%rip), %rdi      # 0x3658
callq 0x1080
leaq 0x1188(%rip), %rdi      # 0x376a
callq 0x1080
leaq 0x1194(%rip), %rdi      # 0x3782
callq 0x1080
leaq 0x12a5(%rip), %rdi      # 0x389f
callq 0x1080
leaq 0x12b0(%rip), %rdi      # 0x38b6
callq 0x1080
leaq 0x12eb(%rip), %rdi      # 0x38fd
callq 0x1080
leaq 0x12fa(%rip), %rdi      # 0x3918
callq 0x1080
leaq 0x1395(%rip), %rdi      # 0x39bf
callq 0x1080
leaq 0x1399(%rip), %rdi      # 0x39cf
callq 0x1080
xorl %ebx, %ebx
jmp 0x2880
leaq 0xd0c(%rip), %rdi       # 0x3355
movl %ebp, %esi
movq %rbx, %rdx
callq 0x29e8
testl %eax, %eax
jle 0x2669
movl %eax, %eax
movq 0x8(%rbx,%rax,8), %rdi
callq 0x11a0
movl %eax, 0x3a97(%rip)      # 0x6100
leaq 0xcee(%rip), %rdi       # 0x335e
movl %ebp, %esi
movq %rbx, %rdx
callq 0x29e8
testl %eax, %eax
jle 0x2690
movl %eax, %eax
movq 0x8(%rbx,%rax,8), %rdi
callq 0x11a0
movl %eax, 0x3a78(%rip)      # 0x6108
leaq 0xcd2(%rip), %rdi       # 0x3369
movl %ebp, %esi
movq %rbx, %rdx
callq 0x29e8
testl %eax, %eax
jle 0x26b7
movl %eax, %eax
movq 0x8(%rbx,%rax,8), %rdi
callq 0x11a0
movl %eax, 0x3a4d(%rip)      # 0x6104
leaq 0xcb8(%rip), %rdi       # 0x3376
movl %ebp, %esi
movq %rbx, %rdx
callq 0x29e8
movq 0x3a71(%rip), %rdi      # 0x6140
testl %eax, %eax
jle 0x26e1
movl %eax, %eax
movq 0x8(%rbx,%rax,8), %rsi
callq 0x1060
jmp 0x26f4
movabsq $0x78742e6261636f76, %rax # imm = 0x78742E6261636F76
movq %rax, (%rdi)
movw $0x74, 0x8(%rdi)
leaq 0xc87(%rip), %rdi       # 0x3382
movl %ebp, %esi
movq %rbx, %rdx
callq 0x29e8
movq 0x3a1c(%rip), %rdi      # 0x6128
testl %eax, %eax
jle 0x271e
movl %eax, %eax
movq 0x8(%rbx,%rax,8), %rsi
callq 0x1060
jmp 0x272f
movabsq $0x776f6c667265766f, %rax # imm = 0x776F6C667265766F
movq %rax, (%rdi)
movb $0x0, 0x8(%rdi)
leaq 0xc5b(%rip), %rdi       # 0x3391
movl %ebp, %esi
movq %rbx, %rdx
callq 0x29e8
testl %eax, %eax
jle 0x2758
movl %eax, %eax
movq 0x8(%rbx,%rax,8), %rdi
callq 0x10b0
movsd %xmm0, 0x39b8(%rip)     # 0x6110
leaq 0xc3a(%rip), %rdi       # 0x3399
movl %ebp, %esi
movq %rbx, %rdx
callq 0x29e8
testl %eax, %eax
jle 0x277f
movl %eax, %eax
movq 0x8(%rbx,%rax,8), %rdi
callq 0x11a0
movl %eax, 0x3999(%rip)      # 0x6118
movsd 0x3989(%rip), %xmm0     # 0x6110
mulsd 0x891(%rip), %xmm0      # 0x3020
mulsd 0x891(%rip), %xmm0      # 0x3028
mulsd 0x891(%rip), %xmm0      # 0x3030
movsd %xmm0, 0x8(%rsp)
movsd 0x88b(%rip), %xmm1      # 0x3038
movsd %xmm1, 0x10(%rsp)
movapd %xmm1, %xmm0
callq 0x1110
movsd 0x10(%rsp), %xmm2
addsd 0x876(%rip), %xmm0      # 0x3040
mulsd %xmm2, %xmm0
movsd 0x8(%rsp), %xmm1
subsd %xmm0, %xmm1
andpd 0x870(%rip), %xmm1      # 0x3050
ucomisd 0x860(%rip), %xmm1      # 0x3048
jbe 0x2807
movapd %xmm2, %xmm0
callq 0x1110
addsd 0x845(%rip), %xmm0      # 0x3040
movsd 0x8(%rsp), %xmm1
divsd %xmm0, %xmm1
jmp 0x27ad
cvttsd2si %xmm2, %rax
movq %rax, 0x3925(%rip)      # 0x6138
cvttsd2si 0x8(%rsp), %rax
pushq $0x6
popq %rcx
cqto
idivq %rcx
movq %rax, 0x3907(%rip)      # 0x6130
leaq 0xb7d(%rip), %rdi       # 0x33ad
movl %ebp, %esi
movq %rbx, %rdx
callq 0x29e8
testl %eax, %eax
jle 0x2851
movl %eax, %eax
movq 0x8(%rbx,%rax,8), %rdi
callq 0x1140
movq %rax, 0x38e7(%rip)      # 0x6138
leaq 0xb62(%rip), %rdi       # 0x33ba
movl %ebp, %esi
movq %rbx, %rdx
callq 0x29e8
testl %eax, %eax
jle 0x2879
movl %eax, %eax
movq 0x8(%rbx,%rax,8), %rdi
callq 0x1140
movq %rax, 0x38b7(%rip)      # 0x6130
callq 0x1afd
movl %eax, %ebx
movq 0x38b9(%rip), %rdi      # 0x6140
callq 0x1030
movq 0x3895(%rip), %rdi      # 0x6128
callq 0x1030
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %rbp
retq
nopl (%rax)