// File: /workspace/github_c_makefile_star2_O3/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, 0x1c(%rsp)
movq %rdx, %rbx
movq %rsi, 0x48(%rsp)
movq (%rdi), %rbp
movq 0x8(%rdi), %r15
movq 0x10(%rdi), %rax
movq %rax, 0x38(%rsp)
movq 0x18(%rdi), %rax
movq %rax, 0x40(%rsp)
movl $0x88, %edi
callq 0x11b0
movq %rax, %r14
movl $0x60, %edi
callq 0x11b0
movq %rax, %r13
movl $0x88, %edx
movq %r14, %rdi
movq %rbp, %rsi
callq 0x11a0
movl $0xc, %ecx
movq %r13, %rdi
movq %r15, 0x8(%rsp)
movq %r15, %rsi
rep  movsq (%rsi), %es:(%rdi)
movslq (%rbp), %r15
leaq (,%r15,8), %rdi
callq 0x11b0
movq %rax, %r12
movq %rbp, 0x10(%rsp)
movq 0x50(%rbp), %rax
testq %r15, %r15
movq %r14, 0x20(%rsp)
movq %r13, 0x58(%rsp)
movq %rbx, 0x50(%rsp)
jle 0xacff
xorpd %xmm2, %xmm2
xorl %ecx, %ecx
addsd (%rax,%rcx,8), %xmm2
incq %rcx
cmpq %rcx, %r15
jne 0xacb1
movsd (%rax), %xmm0
divsd %xmm2, %xmm0
movsd %xmm0, (%r12)
cmpl $0x1, %r15d
je 0xacf8
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, %r15
jne 0xacdd
movsd %xmm2, (%rsp)
jmp 0xad1a
movsd (%rax), %xmm0
xorpd %xmm2, %xmm2
xorpd %xmm1, %xmm1
movsd %xmm1, (%rsp)
divsd %xmm2, %xmm0
movsd %xmm0, (%r12)
movq 0x8(%rsp), %rax
movslq 0x24(%rax), %r13
leaq (,%r13,4), %rdi
movq %rdi, 0x8(%rsp)
callq 0x11b0
movq %rax, %rbp
leaq (,%r13,8), %rdi
movq %rdi, 0x28(%rsp)
callq 0x11b0
movq %rax, %r14
movl %r13d, %ebx
testq %r13, %r13
jle 0xad88
leaq (,%rbx,4), %rdx
movq %rbp, %rdi
movl $0xff, %esi
callq 0x1110
xorl %r15d, %r15d
callq 0x2806
movsd %xmm0, (%r14,%r15,8)
incq %r15
cmpq %r15, %rbx
jne 0xad6d
movq 0x10(%rsp), %rax
movl (%rax), %r15d
testl %r15d, %r15d
jle 0xadc2
movl %r15d, %eax
xorl %ecx, %ecx
testl %r13d, %r13d
jle 0xadba
xorl %edx, %edx
cmpl $0x0, (%rbp,%rdx,4)
jns 0xadb2
movsd (%r12,%rcx,8), %xmm0
ucomisd (%r14,%rdx,8), %xmm0
jb 0xadb2
movl %ecx, (%rbp,%rdx,4)
incq %rdx
cmpq %rdx, %rbx
jne 0xad99
incq %rcx
cmpq %rax, %rcx
jne 0xad92
testl %r13d, %r13d
jle 0xade0
leal -0x1(%r13), %eax
xorl %ecx, %ecx
cmpl $0x0, (%rbp,%rcx,4)
jns 0xadd8
movl %eax, (%rbp,%rcx,4)
incq %rcx
cmpq %rcx, %rbx
jne 0xadcd
movq 0x10(%rsp), %rax
movslq 0x4(%rax), %r15
movq 0x8(%rsp), %rdi
imulq %r15, %rdi
callq 0x11b0
movq %r13, 0x30(%rsp)
movq 0x20(%rsp), %r13
movq %rax, 0x8(%rsp)
movq %rax, 0x20(%r13)
movq 0x28(%rsp), %rdi
callq 0x11b0
movq %rax, 0x50(%r13)
movq 0x30(%rsp), %r13
testl %r13d, %r13d
jle 0xaef7
leal 0x1(%rbx), %ecx
leaq -0x1(%rbx), %rdx
movq %rdx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
shrl %ecx
shlq $0x4, %rcx
pmovsxbq 0x269e(%rip), %xmm1     # 0xd4e4
xorl %edx, %edx
movdqa 0x2260(%rip), %xmm2     # 0xd0b0
pxor %xmm2, %xmm0
movabsq $0x3ff0000000000000, %rsi # imm = 0x3FF0000000000000
pcmpeqd %xmm3, %xmm3
pmovsxbq 0x267b(%rip), %xmm4     # 0xd4e6
movdqa %xmm1, %xmm5
pxor %xmm2, %xmm5
pcmpgtq %xmm0, %xmm5
movd %xmm5, %edi
notl %edi
testb $0x1, %dil
je 0xae88
movq %rsi, (%rax,%rdx)
pxor %xmm3, %xmm5
pextrb $0x8, %xmm5, %edi
testb $0x1, %dil
je 0xae9d
movq %rsi, 0x8(%rax,%rdx)
paddq %xmm4, %xmm1
addq $0x10, %rdx
cmpq %rdx, %rcx
jne 0xae6b
testl %r13d, %r13d
jle 0xaef7
xorl %eax, %eax
xorl %ecx, %ecx
testl %r15d, %r15d
jle 0xaeec
movslq %eax, %rdx
movq 0x8(%rsp), %rsi
leaq (%rsi,%rdx,4), %rdx
movslq (%rbp,%rcx,4), %rsi
imulq %r15, %rsi
shlq $0x2, %rsi
movq 0x10(%rsp), %rdi
addq 0x20(%rdi), %rsi
xorl %edi, %edi
movl (%rsi,%rdi,4), %r8d
movl %r8d, (%rdx,%rdi,4)
incq %rdi
cmpl %r15d, %edi
jl 0xaedc
incq %rcx
addl %r15d, %eax
cmpq %rbx, %rcx
jne 0xaeb3
movq %r14, %rdi
callq 0x1050
movq %r12, %rdi
callq 0x1050
movq %rbp, %rdi
callq 0x1050
movq 0x20(%rsp), %rbx
movl %r13d, (%rbx)
leaq 0x60(%rsp), %rdi
movq %rbx, (%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), %r15
movq %r15, %rdx
movl 0x1c(%rsp), %ebp
movl %ebp, %ecx
callq *0x40(%rsp)
xorps %xmm1, %xmm1
cvtsi2sd %r13d, %xmm1
movsd (%rsp), %xmm2
divsd %xmm1, %xmm2
testl %ebp, %ebp
jle 0xaf7d
movl %ebp, %eax
xorl %ecx, %ecx
movsd (%r15,%rcx,8), %xmm1
mulsd %xmm2, %xmm1
movsd %xmm1, (%r15,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0xaf65
mulsd %xmm0, %xmm2
movsd %xmm2, (%rsp)
movq 0x20(%rbx), %rdi
callq 0x1050
movq 0x50(%rbx), %rdi
callq 0x1050
movq %rbx, %rdi
callq 0x1050
movq %r14, %rdi
callq 0x1050
movsd (%rsp), %xmm0
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq