// File: /workspace/github_c_makefile_star2_O0/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:
subq $0xe8, %rsp
movq %rdi, 0xe0(%rsp)
movq %rsi, 0xd8(%rsp)
movq %rdx, 0xd0(%rsp)
movl %ecx, 0xcc(%rsp)
movq 0xe0(%rsp), %rax
movq %rax, 0xc0(%rsp)
movq 0xc0(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xb8(%rsp)
movq 0xc0(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0xb0(%rsp)
movq 0xc0(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0xa8(%rsp)
movq 0xc0(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0xa0(%rsp)
movl $0x88, %edi
callq 0x1170
movq %rax, 0x98(%rsp)
movl $0x60, %edi
callq 0x1170
movq %rax, 0x90(%rsp)
movq 0x98(%rsp), %rdi
movq 0xb8(%rsp), %rsi
movl $0x88, %edx
callq 0x1160
movq 0x90(%rsp), %rdi
movq 0xb0(%rsp), %rsi
movl $0x60, %edx
callq 0x1160
movq 0xb8(%rsp), %rax
movslq (%rax), %rdi
shlq $0x3, %rdi
callq 0x1170
movq %rax, 0x88(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x80(%rsp)
movl $0x0, 0x7c(%rsp)
movl 0x7c(%rsp), %eax
movq 0xb8(%rsp), %rcx
cmpl (%rcx), %eax
jge 0x13b32
movq 0xb8(%rsp), %rax
movq 0x50(%rax), %rax
movslq 0x7c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x80(%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x13aed
movq 0xb8(%rsp), %rax
movq 0x50(%rax), %rax
movsd (%rax), %xmm0
divsd 0x80(%rsp), %xmm0
movq 0x88(%rsp), %rax
movsd %xmm0, (%rax)
movl $0x1, 0x78(%rsp)
movl 0x78(%rsp), %eax
movq 0xb8(%rsp), %rcx
cmpl (%rcx), %eax
jge 0x13bc8
movq 0x88(%rsp), %rax
movl 0x78(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0xb8(%rsp), %rax
movq 0x50(%rax), %rax
movslq 0x78(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm1
divsd 0x80(%rsp), %xmm1
addsd %xmm1, %xmm0
movq 0x88(%rsp), %rax
movslq 0x78(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0x13b5f
movq 0xb0(%rsp), %rax
movl 0x24(%rax), %eax
movl %eax, 0x74(%rsp)
movslq 0x74(%rsp), %rdi
shlq $0x2, %rdi
callq 0x1170
movq %rax, 0x68(%rsp)
movslq 0x74(%rsp), %rdi
shlq $0x3, %rdi
callq 0x1170
movq %rax, 0x60(%rsp)
movl $0x0, 0x5c(%rsp)
movl 0x5c(%rsp), %eax
cmpl 0x74(%rsp), %eax
jge 0x13c2d
movq 0x68(%rsp), %rax
movslq 0x5c(%rsp), %rcx
movl $0xffffffff, (%rax,%rcx,4) # imm = 0xFFFFFFFF
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x13c05
movl $0x0, 0x58(%rsp)
movl 0x58(%rsp), %eax
cmpl 0x74(%rsp), %eax
jge 0x13c60
callq 0x1940
movq 0x60(%rsp), %rax
movslq 0x58(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x58(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x58(%rsp)
jmp 0x13c35
movl $0x0, 0x54(%rsp)
movl 0x54(%rsp), %eax
movq 0xb8(%rsp), %rcx
cmpl (%rcx), %eax
jge 0x13cf3
movl $0x0, 0x50(%rsp)
movl 0x50(%rsp), %eax
cmpl 0x74(%rsp), %eax
jge 0x13ce1
movq 0x68(%rsp), %rax
movslq 0x50(%rsp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jge 0x13cd2
movq 0x60(%rsp), %rax
movslq 0x50(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm1
movq 0x88(%rsp), %rax
movslq 0x54(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd %xmm1, %xmm0
jb 0x13cd2
movl 0x54(%rsp), %edx
movq 0x68(%rsp), %rax
movslq 0x50(%rsp), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x13cd4
movl 0x50(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x50(%rsp)
jmp 0x13c80
jmp 0x13ce3
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x13c68
movl $0x0, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
cmpl 0x74(%rsp), %eax
jge 0x13d38
movq 0x68(%rsp), %rax
movslq 0x4c(%rsp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jge 0x13d29
movl 0x74(%rsp), %edx
subl $0x1, %edx
movq 0x68(%rsp), %rax
movslq 0x4c(%rsp), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x13d2b
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x13cfb
movslq 0x74(%rsp), %rdi
shlq $0x2, %rdi
movq 0xb8(%rsp), %rax
movslq 0x4(%rax), %rax
imulq %rax, %rdi
callq 0x1170
movq %rax, %rcx
movq 0x98(%rsp), %rax
movq %rcx, 0x20(%rax)
movslq 0x74(%rsp), %rdi
shlq $0x3, %rdi
callq 0x1170
movq %rax, %rcx
movq 0x98(%rsp), %rax
movq %rcx, 0x50(%rax)
movl $0x0, 0x48(%rsp)
movl 0x48(%rsp), %eax
cmpl 0x74(%rsp), %eax
jge 0x13dbf
movq 0x98(%rsp), %rax
movq 0x50(%rax), %rax
movslq 0x48(%rsp), %rcx
movsd 0x52a3(%rip), %xmm0     # 0x19050
movsd %xmm0, (%rax,%rcx,8)
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0x13d8a
movl $0x0, 0x44(%rsp)
movl 0x44(%rsp), %eax
cmpl 0x74(%rsp), %eax
jge 0x13e66
movl $0x0, 0x40(%rsp)
movl 0x40(%rsp), %eax
movq 0xb8(%rsp), %rcx
cmpl 0x4(%rcx), %eax
jge 0x13e54
movq 0xb8(%rsp), %rax
movq 0x20(%rax), %rax
movl 0x40(%rsp), %ecx
movq 0x68(%rsp), %rdx
movslq 0x44(%rsp), %rsi
movl (%rdx,%rsi,4), %edx
movq 0xb8(%rsp), %rsi
imull 0x4(%rsi), %edx
addl %edx, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
movq 0x98(%rsp), %rax
movq 0x20(%rax), %rax
movl 0x40(%rsp), %ecx
movl 0x44(%rsp), %esi
movq 0xb8(%rsp), %rdi
imull 0x4(%rdi), %esi
addl %esi, %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movl 0x40(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
jmp 0x13ddd
jmp 0x13e56
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0x13dc7
movq 0x60(%rsp), %rdi
callq 0x1030
movq 0x88(%rsp), %rdi
callq 0x1030
movq 0x68(%rsp), %rdi
callq 0x1030
movl 0x74(%rsp), %ecx
movq 0x98(%rsp), %rax
movl %ecx, (%rax)
movq 0x98(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0xa8(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0xa0(%rsp), %rax
leaq 0x20(%rsp), %rdi
movq 0xd8(%rsp), %rsi
movq 0xd0(%rsp), %rdx
movl 0xcc(%rsp), %ecx
xorps %xmm0, %xmm0
callq *%rax
movsd %xmm0, 0x18(%rsp)
movsd 0x80(%rsp), %xmm0
cvtsi2sdl 0x74(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x10(%rsp), %xmm0
mulsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movl $0x0, 0xc(%rsp)
movl 0xc(%rsp), %eax
cmpl 0xcc(%rsp), %eax
jge 0x13f55
movsd 0x10(%rsp), %xmm0
movq 0xd0(%rsp), %rax
movslq 0xc(%rsp), %rcx
mulsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0x13f1e
movq 0x98(%rsp), %rax
movq 0x20(%rax), %rdi
callq 0x1030
movq 0x98(%rsp), %rax
movq 0x50(%rax), %rdi
callq 0x1030
movq 0x98(%rsp), %rdi
callq 0x1030
movq 0x90(%rsp), %rdi
callq 0x1030
movsd 0x18(%rsp), %xmm0
addq $0xe8, %rsp
retq
nop