// File: /workspace/github_c_makefile_star2_O1/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
movl %ecx, %ebp
movq %rdx, 0x50(%rsp)
movq %rsi, 0x48(%rsp)
movq (%rdi), %r13
movq 0x8(%rdi), %rbx
movq 0x10(%rdi), %rax
movq %rax, 0x38(%rsp)
movq 0x18(%rdi), %rax
movq %rax, 0x40(%rsp)
movl $0x88, %edi
callq 0x11a0
movq %rax, %r14
movl $0x60, %edi
callq 0x11a0
movq %rax, %r15
movl $0x88, %edx
movq %r14, 0x10(%rsp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x1190
movl $0xc, %ecx
movq %r15, 0x58(%rsp)
movq %r15, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
movslq (%r13), %r14
leaq (,%r14,8), %rdi
callq 0x11a0
movq %rax, %r12
testq %r14, %r14
jle 0xbcd7
movq 0x50(%r13), %rax
xorpd %xmm2, %xmm2
xorl %ecx, %ecx
addsd (%rax,%rcx,8), %xmm2
incq %rcx
cmpq %rcx, %r14
jne 0xbcc8
jmp 0xbcdb
xorpd %xmm2, %xmm2
movl %ebp, 0x24(%rsp)
movq 0x50(%r13), %rax
movsd (%rax), %xmm0
divsd %xmm2, %xmm0
movsd %xmm0, (%r12)
cmpl $0x2, %r14d
jl 0xbd1d
movsd (%r12), %xmm0
movl $0x1, %ecx
movsd (%rax,%rcx,8), %xmm1
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, (%r12,%rcx,8)
incq %rcx
cmpq %rcx, %r14
jne 0xbd02
movsd %xmm2, 0x18(%rsp)
movslq 0x24(%rbx), %rbx
leaq (,%rbx,4), %rdi
movq %rdi, 0x30(%rsp)
callq 0x11a0
movq %rax, %rbp
leaq (,%rbx,8), %rdi
movq %rdi, 0x28(%rsp)
callq 0x11a0
movq %rbx, %rcx
movq %rax, %r14
movl %ecx, %r15d
testq %rbx, %rbx
movq %rbx, 0x8(%rsp)
jle 0xbd7b
leaq (,%r15,4), %rdx
movq %rbp, %rdi
movl $0xff, %esi
callq 0x1110
movq 0x8(%rsp), %rcx
testl %ecx, %ecx
jle 0xbd94
xorl %ebx, %ebx
callq 0x3239
movsd %xmm0, (%r14,%rbx,8)
incq %rbx
cmpq %rbx, %r15
jne 0xbd81
movslq (%r13), %rax
testq %rax, %rax
movq 0x8(%rsp), %rsi
jle 0xbdd3
xorl %ecx, %ecx
testl %esi, %esi
jle 0xbdcb
xorl %edx, %edx
cmpl $0x0, (%rbp,%rdx,4)
jns 0xbdc3
movsd (%r12,%rcx,8), %xmm0
ucomisd (%r14,%rdx,8), %xmm0
jb 0xbdc3
movl %ecx, (%rbp,%rdx,4)
incq %rdx
cmpq %rdx, %r15
jne 0xbdaa
incq %rcx
cmpq %rax, %rcx
jne 0xbda4
testl %esi, %esi
jle 0xbdef
leal -0x1(%rsi), %eax
xorl %ecx, %ecx
cmpl $0x0, (%rbp,%rcx,4)
jns 0xbde7
movl %eax, (%rbp,%rcx,4)
incq %rcx
cmpq %rcx, %r15
jne 0xbddc
movslq 0x4(%r13), %rax
movq 0x30(%rsp), %rdi
imulq %rax, %rdi
callq 0x11a0
movq %rax, %rbx
movq 0x10(%rsp), %rax
movq %rbx, 0x20(%rax)
movq 0x28(%rsp), %rdi
callq 0x11a0
movq 0x8(%rsp), %r8
movq 0x10(%rsp), %rcx
movq %rax, 0x50(%rcx)
testl %r8d, %r8d
jle 0xbeb3
leal 0x1(%r15), %ecx
leaq -0x1(%r15), %rdx
movq %rdx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
shrl %ecx
shlq $0x4, %rcx
pmovsxbq 0x26a5(%rip), %xmm1     # 0xe4f4
xorl %edx, %edx
movdqa 0x2257(%rip), %xmm2     # 0xe0b0
pxor %xmm2, %xmm0
movabsq $0x3ff0000000000000, %rsi # imm = 0x3FF0000000000000
pcmpeqd %xmm3, %xmm3
pmovsxbq 0x2682(%rip), %xmm4     # 0xe4f6
movdqa %xmm1, %xmm5
pxor %xmm2, %xmm5
pcmpgtq %xmm0, %xmm5
movd %xmm5, %edi
notl %edi
testb $0x1, %dil
je 0xbe91
movq %rsi, (%rax,%rdx)
pxor %xmm3, %xmm5
pextrb $0x8, %xmm5, %edi
testb $0x1, %dil
je 0xbea6
movq %rsi, 0x8(%rax,%rdx)
paddq %xmm4, %xmm1
addq $0x10, %rdx
cmpq %rdx, %rcx
jne 0xbe74
testl %r8d, %r8d
jle 0xbefe
xorl %eax, %eax
movl 0x4(%r13), %edi
testl %edi, %edi
jle 0xbef6
movq 0x20(%r13), %rcx
movslq (%rbp,%rax,4), %rdx
xorl %esi, %esi
movslq %edi, %rdi
movq %rdx, %r8
imulq %rdi, %r8
addq %rsi, %r8
movl (%rcx,%r8,4), %r8d
imull %eax, %edi
movslq %edi, %rdi
addq %rsi, %rdi
movl %r8d, (%rbx,%rdi,4)
movl 0x4(%r13), %edi
incq %rsi
cmpl %edi, %esi
jl 0xbecd
incq %rax
cmpq %r15, %rax
jne 0xbeba
movq %r14, %rdi
callq 0x1050
movq %r12, %rdi
callq 0x1050
movq %rbp, %rdi
callq 0x1050
movq 0x10(%rsp), %rax
movq 0x8(%rsp), %r15
movl %r15d, (%rax)
leaq 0x60(%rsp), %rdi
movq %rax, (%rdi)
movq 0x58(%rsp), %r14
movq %r14, 0x8(%rdi)
movq 0x38(%rsp), %rax
movq %rax, 0x10(%rdi)
xorpd %xmm0, %xmm0
movq 0x48(%rsp), %rsi
movq 0x50(%rsp), %rbx
movq %rbx, %rdx
movl 0x24(%rsp), %ebp
movl %ebp, %ecx
callq *0x40(%rsp)
xorps %xmm1, %xmm1
cvtsi2sd %r15d, %xmm1
movsd 0x18(%rsp), %xmm2
divsd %xmm1, %xmm2
testl %ebp, %ebp
jle 0xbf88
movl %ebp, %eax
xorl %ecx, %ecx
movsd (%rbx,%rcx,8), %xmm1
mulsd %xmm2, %xmm1
movsd %xmm1, (%rbx,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0xbf72
mulsd %xmm0, %xmm2
movsd %xmm2, 0x18(%rsp)
movq 0x10(%rsp), %rbx
movq 0x20(%rbx), %rdi
callq 0x1050
movq 0x50(%rbx), %rdi
callq 0x1050
movq %rbx, %rdi
callq 0x1050
movq %r14, %rdi
callq 0x1050
movsd 0x18(%rsp), %xmm0
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq