// File: /workspace/github_cpp_makefile_star2_O1/simap[P]ecctools/lib/uECC.c
// Function: uECC_sign_with_k(unsigned char const*, unsigned char const*, unsigned int, unsigned long*, unsigned char*, uECC_Curve_t const*)
static int uECC_sign_with_k(const uint8_t *private_key,
                            const uint8_t *message_hash,
                            unsigned hash_size,
                            uECC_word_t *k,
                            uint8_t *signature,
                            uECC_Curve curve) {

    uECC_word_t tmp[uECC_MAX_WORDS];
    uECC_word_t s[uECC_MAX_WORDS];
    uECC_word_t *k2[2] = {tmp, s};
#if uECC_VLI_NATIVE_LITTLE_ENDIAN
    uECC_word_t *p = (uECC_word_t *)signature;
#else
    uECC_word_t p[uECC_MAX_WORDS * 2];
#endif
    uECC_word_t carry;
    wordcount_t num_words = curve->num_words;
    wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits);
    bitcount_t num_n_bits = curve->num_n_bits;

    /* Make sure 0 < k < curve_n */
    if (uECC_vli_isZero(k, num_words) || uECC_vli_cmp(curve->n, k, num_n_words) != 1) {
        return 0;
    }

    carry = regularize_k(k, tmp, s, curve);
    EccPoint_mult(p, curve->G, k2[!carry], 0, num_n_bits + 1, curve);
    if (uECC_vli_isZero(p, num_words)) {
        return 0;
    }

    /* If an RNG function was specified, get a random number
       to prevent side channel analysis of k. */
    if (!g_rng_function) {
        uECC_vli_clear(tmp, num_n_words);
        tmp[0] = 1;
    } else if (!uECC_generate_random_int(tmp, curve->n, num_n_words)) {
        return 0;
    }

    /* Prevent side channel analysis of uECC_vli_modInv() to determine
       bits of k / the private key by premultiplying by a random number */
    uECC_vli_modMult(k, k, tmp, curve->n, num_n_words); /* k' = rand * k */
    uECC_vli_modInv(k, k, curve->n, num_n_words);       /* k = 1 / k' */
    uECC_vli_modMult(k, k, tmp, curve->n, num_n_words); /* k = 1 / k */

#if uECC_VLI_NATIVE_LITTLE_ENDIAN == 0
    uECC_vli_nativeToBytes(signature, curve->num_bytes, p); /* store r */
#endif

#if uECC_VLI_NATIVE_LITTLE_ENDIAN
    bcopy((uint8_t *) tmp, private_key, BITS_TO_BYTES(curve->num_n_bits));
#else
    uECC_vli_bytesToNative(tmp, private_key, BITS_TO_BYTES(curve->num_n_bits)); /* tmp = d */
#endif

    s[num_n_words - 1] = 0;
    uECC_vli_set(s, p, num_words);
    uECC_vli_modMult(s, tmp, s, curve->n, num_n_words); /* s = r*d */

    bits2int(tmp, message_hash, hash_size, curve);
    uECC_vli_modAdd(s, tmp, s, curve->n, num_n_words); /* s = e + r*d */
    uECC_vli_modMult(s, s, k, curve->n, num_n_words);  /* s = (e + r*d) / k */
    if (uECC_vli_numBits(s, num_n_words) > (bitcount_t)curve->num_bytes * 8) {
        return 0;
    }
#if uECC_VLI_NATIVE_LITTLE_ENDIAN
    bcopy((uint8_t *) signature + curve->num_bytes, (uint8_t *) s, curve->num_bytes);
#else
    uECC_vli_nativeToBytes(signature + curve->num_bytes, curve->num_bytes, s);
#endif    
    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
leaq 0x20(%rsp), %rax
movq %rax, 0x50(%rsp)
leaq 0x60(%rsp), %rax
movq %rax, 0x58(%rsp)
movsbq (%r9), %rbp
movswl 0x2(%r9), %ebx
leal 0x3f(%rbx), %eax
leal 0x7e(%rbx), %r13d
testl %eax, %eax
cmovnsl %eax, %r13d
testq %rbp, %rbp
jle 0x25a9
movq %r9, %r14
movq %rcx, %r15
sarl $0x6, %r13d
xorl %eax, %eax
xorl %ecx, %ecx
orq (%r15,%rax,8), %rcx
incq %rax
cmpq %rax, %rbp
jne 0x24db
xorl %r12d, %r12d
testq %rcx, %rcx
je 0x25ac
movq %rdi, 0x40(%rsp)
movq %rsi, 0x48(%rsp)
movl %edx, 0x14(%rsp)
movq %r8, 0x18(%rsp)
leaq 0x28(%r14), %rdi
movsbl %r13b, %edx
movq %rdi, 0x8(%rsp)
movq %r15, %rsi
movl %edx, 0x10(%rsp)
callq 0x2394
cmpb $0x1, %al
jne 0x25ac
leaq 0x20(%rsp), %rsi
leaq 0x60(%rsp), %rdx
movq %r15, %rdi
movq %r14, %rcx
callq 0x1b7d
leaq 0x48(%r14), %rsi
xorl $0x1, %eax
movq 0x50(%rsp,%rax,8), %rdx
incl %ebx
movswl %bx, %r8d
xorl %r12d, %r12d
leaq 0x80(%rsp), %rdi
xorl %ecx, %ecx
movq %r14, %r9
callq 0x1c3c
testb %bpl, %bpl
jle 0x25ac
xorl %eax, %eax
xorl %ecx, %ecx
orq 0x80(%rsp,%rax,8), %rcx
incq %rax
cmpq %rax, %rbp
jne 0x256c
xorl %r12d, %r12d
testq %rcx, %rcx
movl 0x10(%rsp), %ebx
movq 0x8(%rsp), %rsi
je 0x25ac
cmpq $0x0, 0x6b1b(%rip)      # 0x90b0
je 0x25c1
leaq 0x20(%rsp), %rdi
movl %ebx, %edx
callq 0x186e
testl %eax, %eax
jne 0x25e4
jmp 0x25ac
xorl %r12d, %r12d
movl %r12d, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testb %r13b, %r13b
jle 0x25db
movl %r13d, %edx
andl $0x7f, %edx
shll $0x3, %edx
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
callq 0x1050
movq $0x1, 0x20(%rsp)
leaq 0x20(%rsp), %rdx
movq %r15, %rdi
movq %r15, %rsi
movq 0x8(%rsp), %rcx
movl %ebx, %r8d
callq 0x367a
movq %r15, %rdi
movq %r15, %rsi
movq 0x8(%rsp), %rdx
movl %ebx, %ecx
callq 0x316a
movq %r15, %rdi
movq %r15, %rsi
leaq 0x20(%rsp), %rdx
movq 0x8(%rsp), %rcx
movl %ebx, %r8d
callq 0x367a
movsbl 0x1(%r14), %eax
testl %eax, %eax
movq 0x18(%rsp), %r9
jle 0x2668
movslq %eax, %rdx
leal -0x8(,%rax,8), %esi
decl %eax
xorl %edi, %edi
movl %eax, %ecx
andl $-0x8, %ecx
movq 0x80(%rsp,%rcx), %r8
movl %esi, %ecx
andb $0x38, %cl
shrq %cl, %r8
movb %r8b, (%r9,%rdi)
incq %rdi
addl $-0x8, %esi
decl %eax
cmpq %rdi, %rdx
jne 0x2642
movswl 0x2(%r14), %edx
leal 0x7(%rdx), %eax
addl $0xe, %edx
testl %eax, %eax
cmovnsl %eax, %edx
sarl $0x3, %edx
leaq 0x20(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x1b19
movsbq %r13b, %rax
movq $0x0, 0x58(%rsp,%rax,8)
testb %bpl, %bpl
jle 0x26b5
shlq $0x3, %rbp
leaq 0x60(%rsp), %rdi
leaq 0x80(%rsp), %rsi
movq %rbp, %rdx
callq 0x1070
leaq 0x20(%rsp), %rsi
leaq 0x60(%rsp), %rbp
movq %rbp, %rdi
movq %rbp, %rdx
movq 0x8(%rsp), %rbx
movq %rbx, %rcx
movl 0x10(%rsp), %r13d
movl %r13d, %r8d
callq 0x367a
leaq 0x20(%rsp), %rdi
movq 0x48(%rsp), %rsi
movl 0x14(%rsp), %edx
movq %r14, %rcx
callq 0x3555
movq %rbp, %rdi
leaq 0x20(%rsp), %rsi
movq %rbp, %rdx
movq %rbx, %rcx
movl %r13d, %r8d
callq 0x42f2
movq %rbp, %rdi
movq %rbp, %rsi
movq %r15, %rdx
movq %rbx, %rcx
movl %r13d, %r8d
callq 0x367a
movq %rbp, %rdi
movl %r13d, %esi
callq 0x3c06
movswl %ax, %ecx
movsbl 0x1(%r14), %eax
leal (,%rax,8), %edx
cmpl %ecx, %edx
jl 0x25ac
movslq %eax, %rsi
movl $0x1, %r12d
testb %sil, %sil
jle 0x25ac
movq 0x18(%rsp), %r9
addq %rsi, %r9
addl $-0x8, %edx
decl %eax
xorl %edi, %edi
movl %eax, %ecx
andl $-0x8, %ecx
movq 0x60(%rsp,%rcx), %r8
movl %edx, %ecx
andb $0x38, %cl
shrq %cl, %r8
movb %r8b, (%r9,%rdi)
incq %rdi
addl $-0x8, %edx
decl %eax
cmpq %rdi, %rsi
jne 0x275d
jmp 0x25ac