// File: /workspace/github_c_makefile_star2_O2/debbiemarkslab[P]plmc/src/inference.c
// Function: SGDWrapperPLM
numeric_t SGDWrapperPLM(void *data, const numeric_t *x, numeric_t *g, 
    const int n) {
    /* Wrap objective function for L-BFGS to support
        minibatched Stochastic Gradient Descent (SGD)
    */
    void **d = (void **)data;
    alignment_t *ali = (alignment_t *) d[0];
    options_t *options = (options_t *) d[1];
    numeric_t *lambdas = (numeric_t *) d[2];
    lbfgs_evaluate_t lbfgsfun = (lbfgs_evaluate_t) d[3];

    /* Shallow copy alignment and options */
    alignment_t *aliBatch = (alignment_t *) malloc(sizeof(alignment_t));
    options_t *optionsBatch = (options_t *) malloc(sizeof(options_t));
    *aliBatch = *ali;
    *optionsBatch = *options;

    /* Build CDF */
    numeric_t *CDF = (numeric_t *) malloc(sizeof(numeric_t) * ali->nSeqs);
    numeric_t weightSum = 0;
    for (int i = 0; i < ali->nSeqs; i++) weightSum += ali->weights[i];
    CDF[0] = ali->weights[0] / weightSum;
    for (int i = 1; i < ali->nSeqs; i++) CDF[i] = CDF[i-1] + ali->weights[i] / weightSum;

    /* Sample a batch of sequences */
    int batchSize = options->sgdBatchSize;
    int *indices = (int *) malloc(sizeof(int) * batchSize);
    numeric_t *u = (numeric_t *) malloc(sizeof(numeric_t) * batchSize);
    for (int i = 0; i < batchSize; i++) indices[i] = -1;
    for (int i = 0; i < batchSize; i++) u[i] = (numeric_t) genrand_real3();
    for (int s = 0; s < ali->nSeqs; s++)
        for (int i = 0; i < batchSize; i++)
            if (indices[i] < 0 && u[i] <= CDF[s]) indices[i] = s;
    for (int i = 0; i < batchSize; i++)
        if (indices[i] < 0) indices[i] = batchSize - 1;

    /* Clone mini-alignment and weights */
    aliBatch->sequences =
        (letter_t *) malloc(sizeof(letter_t) * batchSize * ali->nSites);
    aliBatch->weights =
        (numeric_t *) malloc(sizeof(numeric_t) * batchSize);
    for (int i = 0; i < batchSize; i++)
        aliBatch->weights[i] = 1.0;
    for (int i = 0; i < batchSize; i++)
        for (int j = 0; j < ali->nSites; j++)
            aliBatch->sequences[j + i * ali->nSites] = seq(indices[i], j);
    free(u);
    free(CDF);
    free(indices);
    aliBatch->nSeqs = batchSize;

    /* Run the wrapped objective */
    void *instance[3] = {(void *)aliBatch, (void *)optionsBatch, (void *)lambdas};
    numeric_t f = lbfgsfun(instance, x, g, n, 0);

    /* Rescale */
    numeric_t scale = weightSum / (numeric_t) batchSize;
    f *= scale;
    for (int i = 0; i < n; i++) g[i] *= scale;

    /* Clean up */
    free(aliBatch->sequences);
    free(aliBatch->weights);
    free(aliBatch);
    free(optionsBatch);
    return f;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rcx, 0x58(%rsp)
movq %rdx, 0x50(%rsp)
movq %rsi, 0x40(%rsp)
movq (%rdi), %rbp
movq 0x8(%rdi), %rbx
movq 0x10(%rdi), %rax
movq %rax, 0x30(%rsp)
movq 0x18(%rdi), %rax
movq %rax, 0x38(%rsp)
movl $0x88, %edi
callq 0x11a0
movq %rax, %r14
pushq $0x60
popq %rdi
callq 0x11a0
movq %rax, %r15
movl $0x88, %edx
movq %r14, (%rsp)
movq %r14, %rdi
movq %rbp, %rsi
callq 0x1190
pushq $0xc
popq %rcx
movq %r15, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
movslq (%rbp), %r14
leaq (,%r14,8), %rdi
callq 0x11a0
movq %rax, %r13
xorl %ecx, %ecx
testl %r14d, %r14d
movl $0x0, %edx
cmovgl %r14d, %edx
movq 0x50(%rbp), %rax
xorpd %xmm1, %xmm1
cmpq %rcx, %rdx
je 0x90e8
addsd (%rax,%rcx,8), %xmm1
incq %rcx
jmp 0x90d9
movsd (%rax), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%r13)
pushq $0x1
popq %rcx
cmpq %r14, %rcx
jge 0x911a
movsd (%rax,%rcx,8), %xmm0
divsd %xmm1, %xmm0
addsd -0x8(%r13,%rcx,8), %xmm0
movsd %xmm0, (%r13,%rcx,8)
incq %rcx
jmp 0x90f9
movsd %xmm1, 0x8(%rsp)
movq %r15, 0x48(%rsp)
movslq 0x24(%rbx), %r14
leaq (,%r14,4), %rdi
movq %rdi, 0x20(%rsp)
callq 0x11a0
movq %rax, %r12
movq %r14, %rdi
shlq $0x3, %rdi
movq %rdi, 0x18(%rsp)
callq 0x11a0
movq %rax, %rbx
xorl %eax, %eax
testl %r14d, %r14d
movl $0x0, %r15d
movq %r14, 0x10(%rsp)
cmovgl %r14d, %r15d
cmpq %rax, %r15
je 0x9175
orl $-0x1, (%r12,%rax,4)
incq %rax
jmp 0x9166
xorl %r14d, %r14d
cmpq %r14, %r15
je 0x918d
callq 0x1634
movsd %xmm0, (%rbx,%r14,8)
incq %r14
jmp 0x9178
movl (%rbp), %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
cmpq %rax, %rcx
je 0x91c6
xorl %edx, %edx
cmpq %rdx, %r15
je 0x91c1
cmpl $0x0, (%r12,%rdx,4)
jns 0x91bc
movsd (%r13,%rcx,8), %xmm0
ucomisd (%rbx,%rdx,8), %xmm0
jb 0x91bc
movl %ecx, (%r12,%rdx,4)
incq %rdx
jmp 0x919e
incq %rcx
jmp 0x9197
movq 0x10(%rsp), %rax
decl %eax
xorl %ecx, %ecx
cmpq %rcx, %r15
je 0x91e4
cmpl $0x0, (%r12,%rcx,4)
jns 0x91df
movl %eax, (%r12,%rcx,4)
incq %rcx
jmp 0x91cf
movq %r13, 0x28(%rsp)
movslq 0x4(%rbp), %rax
movl %eax, %r13d
movq 0x20(%rsp), %rdi
imulq %rax, %rdi
callq 0x11a0
movq %rax, %r14
movq (%rsp), %rax
movq %r14, 0x20(%rax)
movq 0x18(%rsp), %rdi
callq 0x11a0
movq (%rsp), %rcx
movq %rax, 0x50(%rcx)
xorl %ecx, %ecx
movabsq $0x3ff0000000000000, %rdx # imm = 0x3FF0000000000000
cmpq %rcx, %r15
je 0x9235
movq %rdx, (%rax,%rcx,8)
incq %rcx
jmp 0x9227
xorl %eax, %eax
xorl %ecx, %ecx
cmpq %r15, %rcx
je 0x9272
xorl %edx, %edx
cmpl %r13d, %edx
jge 0x926a
movq 0x20(%rbp), %rsi
movslq (%r12,%rcx,4), %rdi
movslq %r13d, %r8
imulq %rdi, %r8
addq %rdx, %r8
movl (%rsi,%r8,4), %esi
leal (%rax,%rdx), %edi
movslq %edi, %rdi
movl %esi, (%r14,%rdi,4)
incq %rdx
jmp 0x9240
incq %rcx
addq %r13, %rax
jmp 0x9239
movq %rbx, %rdi
callq 0x1050
movq 0x28(%rsp), %rdi
callq 0x1050
movq %r12, %rdi
callq 0x1050
movq (%rsp), %rax
movq 0x10(%rsp), %r12
movl %r12d, (%rax)
leaq 0x60(%rsp), %rdi
movq %rax, (%rdi)
movq 0x48(%rsp), %r15
movq %r15, 0x8(%rdi)
movq 0x30(%rsp), %rax
movq %rax, 0x10(%rdi)
xorpd %xmm0, %xmm0
movq 0x40(%rsp), %rsi
movq 0x50(%rsp), %r14
movq %r14, %rdx
movq 0x58(%rsp), %rbx
movl %ebx, %ecx
callq *0x38(%rsp)
cvtsi2sd %r12d, %xmm1
movsd 0x8(%rsp), %xmm2
divsd %xmm1, %xmm2
xorl %eax, %eax
testl %ebx, %ebx
cmovlel %eax, %ebx
cmpq %rax, %rbx
je 0x92fe
movsd (%r14,%rax,8), %xmm1
mulsd %xmm2, %xmm1
movsd %xmm1, (%r14,%rax,8)
incq %rax
jmp 0x92e4
mulsd %xmm0, %xmm2
movsd %xmm2, 0x8(%rsp)
movq (%rsp), %rbx
movq 0x20(%rbx), %rdi
callq 0x1050
movq 0x50(%rbx), %rdi
callq 0x1050
movq %rbx, %rdi
callq 0x1050
movq %r15, %rdi
callq 0x1050
movsd 0x8(%rsp), %xmm0
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq