// File: /workspace/github_c_makefile_star2_O1/CBaquero[P]random/Random.cc
// Function: RandomLNorm_RedisCommand(RedisModuleCtx*, RedisModuleString**, int)
int RandomLNorm_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  double mean=0.0, sd=1.0;
  if (argc > 5) return RedisModule_WrongArity(ctx);
  if (argc >= 4) /* Get mean */
  {
    if (RedisModule_StringToDouble(argv[3],&mean) != REDISMODULE_OK)
      return RedisModule_ReplyWithError(ctx,"ERR invalid mean");
  }
  if (argc == 5) /* get stddev */
  {
    if (RedisModule_StringToDouble(argv[4],&sd) != REDISMODULE_OK)
      return RedisModule_ReplyWithError(ctx,"ERR invalid standard deviation");
  }

  /* Open key */
  RedisModuleKey *key = (RedisModuleKey *) RedisModule_OpenKey(ctx, argv[1], REDISMODULE_READ | REDISMODULE_WRITE);
 /* Key must be empty or list */
  if ((RedisModule_KeyType(key) != REDISMODULE_KEYTYPE_LIST &&
       RedisModule_KeyType(key) != REDISMODULE_KEYTYPE_EMPTY)) 
  {
     RedisModule_CloseKey(key);
     return RedisModule_ReplyWithError(ctx,REDISMODULE_ERRORMSG_WRONGTYPE);
  }

  /* Get count */
  long long count;
  if ((RedisModule_StringToLongLong(argv[2],&count) != REDISMODULE_OK) ||
        (count < 0)) 
  {
     RedisModule_CloseKey(key);
     return RedisModule_ReplyWithError(ctx,"ERR invalid count");
  }

  /* Push count randoms */
  std::normal_distribution<double> rnorm(mean,sd);
  while (count-- > 0)
  {
    RedisModuleString *ele;
    /* Make string with the same 19 digita precision of ReplyWithDouble */
    ele=RedisModule_CreateStringPrintf(ctx,"%.19f",rnorm(gen));
    RedisModule_ListPush(key,REDISMODULE_LIST_HEAD,ele);
    RedisModule_FreeString(ctx,ele);
  }
 
  size_t len = RedisModule_ValueLength(key);
  RedisModule_CloseKey(key);
  RedisModule_ReplyWithLongLong(ctx, len);
  return REDISMODULE_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movq $0x0, -0x40(%rbp)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, -0x38(%rbp)
cmpl $0x6, %edx
jl 0x47a2
movq 0x57b1(%rip), %rax      # 0x9f40
movq %rbx, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *(%rax)
movl %edx, %r14d
movq %rsi, %r15
cmpl $0x4, %edx
jl 0x47d5
movq 0x572c(%rip), %rax      # 0x9ee0
movq 0x18(%r15), %rdi
leaq -0x40(%rbp), %rsi
callq *(%rax)
testl %eax, %eax
je 0x47d5
movq 0x55a7(%rip), %rax      # 0x9d70
leaq 0x28ae(%rip), %rsi      # 0x707e
jmp 0x495a
cmpl $0x5, %r14d
jne 0x4803
movq 0x56fe(%rip), %rax      # 0x9ee0
movq 0x20(%r15), %rdi
leaq -0x38(%rbp), %rsi
callq *(%rax)
testl %eax, %eax
je 0x4803
movq 0x5579(%rip), %rax      # 0x9d70
leaq 0x2891(%rip), %rsi      # 0x708f
jmp 0x495a
movq 0x55fe(%rip), %rax      # 0x9e08
movq 0x8(%r15), %rsi
movq %rbx, %rdi
movl $0x3, %edx
callq *(%rax)
movq %rax, %r14
movq 0x560e(%rip), %r12      # 0x9e30
movq %rax, %rdi
callq *(%r12)
cmpl $0x2, %eax
je 0x4858
movq %r14, %rdi
callq *(%r12)
testl %eax, %eax
je 0x4858
movq 0x57a8(%rip), %rax      # 0x9fe8
movq %r14, %rdi
callq *(%rax)
movq 0x5524(%rip), %rax      # 0x9d70
leaq 0x287d(%rip), %rsi      # 0x70d0
jmp 0x495a
movq 0x56c1(%rip), %rax      # 0x9f20
movq 0x10(%r15), %rdi
leaq -0x30(%rbp), %rsi
callq *(%rax)
testl %eax, %eax
jne 0x4940
cmpq $0x0, -0x30(%rbp)
js 0x4940
movsd -0x40(%rbp), %xmm0
movsd -0x38(%rbp), %xmm1
movsd %xmm0, -0x60(%rbp)
movsd %xmm1, -0x58(%rbp)
movq $0x0, -0x50(%rbp)
movb $0x0, -0x48(%rbp)
movq -0x30(%rbp), %rax
leaq -0x1(%rax), %rcx
movq %rcx, -0x30(%rbp)
testq %rax, %rax
jle 0x4912
leaq -0x60(%rbp), %r12
movq 0x53b8(%rip), %r13      # 0x9c70
movq 0x5561(%rip), %rax      # 0x9e20
movq (%rax), %r15
movq %r12, %rdi
movq 0x56cc(%rip), %rsi      # 0x9f98
movq %r12, %rdx
callq 0x41b0
movq %rbx, %rdi
leaq 0x2846(%rip), %rsi      # 0x7124
movb $0x1, %al
callq *%r15
movq %rax, %r15
movq %r14, %rdi
xorl %esi, %esi
movq %rax, %rdx
movq 0x563b(%rip), %rax      # 0x9f30
callq *(%rax)
movq %rbx, %rdi
movq %r15, %rsi
callq *(%r13)
movq -0x30(%rbp), %rax
leaq -0x1(%rax), %rcx
movq %rcx, -0x30(%rbp)
testq %rax, %rax
jg 0x48b8
movq 0x52ff(%rip), %rax      # 0x9c18
movq %r14, %rdi
callq *(%rax)
movq %rax, %r15
movq 0x56c0(%rip), %rax      # 0x9fe8
movq %r14, %rdi
callq *(%rax)
movq 0x563c(%rip), %rax      # 0x9f70
movq %rbx, %rdi
movq %r15, %rsi
callq *(%rax)
xorl %eax, %eax
jmp 0x495f
movq 0x56a1(%rip), %rax      # 0x9fe8
movq %r14, %rdi
callq *(%rax)
movq 0x541d(%rip), %rax      # 0x9d70
leaq 0x27b8(%rip), %rsi      # 0x7112
movq %rbx, %rdi
callq *(%rax)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq