// File: /workspace/github_c_makefile_star2_O2/CBaquero[P]random/Random.cc
// Function: RandomLExp_RedisCommand(RedisModuleCtx*, RedisModuleString**, int)
int RandomLExp_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  double lambda;
  if (argc < 3 || argc > 4) return RedisModule_WrongArity(ctx);
  if (argc == 4)
  {
    if (RedisModule_StringToDouble(argv[3],&lambda) != REDISMODULE_OK)
      return RedisModule_ReplyWithError(ctx,"ERR invalid lambda");
  }
  else // If no parameter, use default lambda of 1
    lambda=1.0;

  /* 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::exponential_distribution<double> rexp(lambda);
  while (count-- > 0)
  {
    RedisModuleString *ele;
    /* Make string with the same 19 digita precision of ReplyWithDouble */
    ele=RedisModule_CreateStringPrintf(ctx,"%.19f",rexp(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 $0x18, %rsp
movq %rdi, %rbx
leal -0x5(%rdx), %eax
cmpl $-0x3, %eax
ja 0x5a55
movq 0x54fe(%rip), %rax      # 0xaf40
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *(%rax)
movq %rsi, %r15
cmpl $0x4, %edx
jne 0x5a85
movq 0x547c(%rip), %rax      # 0xaee0
movq 0x18(%r15), %rdi
leaq -0x38(%rbp), %rsi
callq *(%rax)
testl %eax, %eax
je 0x5a93
movq 0x52f7(%rip), %rax      # 0xad70
leaq 0x26ae(%rip), %rsi      # 0x812e
jmp 0x5b26
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, -0x38(%rbp)
movq 0x536e(%rip), %rax      # 0xae08
movq 0x8(%r15), %rsi
pushq $0x3
popq %rdx
movq %rbx, %rdi
callq *(%rax)
movq %rax, %r14
movq 0x5380(%rip), %r12      # 0xae30
movq %rax, %rdi
callq *(%r12)
cmpl $0x2, %eax
je 0x5ae3
movq %r14, %rdi
callq *(%r12)
testl %eax, %eax
je 0x5ae3
movq 0x551a(%rip), %rax      # 0xafe8
movq %r14, %rdi
callq *(%rax)
movq 0x5296(%rip), %rax      # 0xad70
leaq 0x25f3(%rip), %rsi      # 0x80d4
jmp 0x5b26
movq 0x5436(%rip), %rax      # 0xaf20
movq 0x10(%r15), %rdi
leaq -0x30(%rbp), %r15
movq %r15, %rsi
callq *(%rax)
testl %eax, %eax
setne %cl
movq (%r15), %rax
testq %rax, %rax
sets %dl
orb %cl, %dl
cmpb $0x1, %dl
jne 0x5b30
movq 0x54d5(%rip), %rax      # 0xafe8
movq %r14, %rdi
callq *(%rax)
movq 0x5251(%rip), %rax      # 0xad70
leaq 0x25f0(%rip), %rsi      # 0x8116
movq %rbx, %rdi
callq *(%rax)
jmp 0x5bcc
movsd -0x38(%rbp), %xmm0
leaq -0x40(%rbp), %rcx
movsd %xmm0, (%rcx)
movq 0x53ec(%rip), %r12      # 0xaf30
movq 0x5125(%rip), %r13      # 0xac70
leaq -0x1(%rax), %rcx
movq %rcx, -0x30(%rbp)
testq %rax, %rax
jle 0x5ba0
movq 0x52c1(%rip), %rax      # 0xae20
movq (%rax), %r15
leaq -0x40(%rbp), %rdi
movq 0x542b(%rip), %rsi      # 0xaf98
callq 0x5230
movq %rbx, %rdi
leaq 0x25ac(%rip), %rsi      # 0x8128
movb $0x1, %al
callq *%r15
movq %rax, %r15
movq %r14, %rdi
xorl %esi, %esi
movq %rax, %rdx
callq *(%r12)
movq %rbx, %rdi
movq %r15, %rsi
callq *(%r13)
movq -0x30(%rbp), %rax
jmp 0x5b4b
movq 0x5071(%rip), %rax      # 0xac18
movq %r14, %rdi
callq *(%rax)
movq %rax, %r15
movq 0x5432(%rip), %rax      # 0xafe8
movq %r14, %rdi
callq *(%rax)
movq 0x53ae(%rip), %rax      # 0xaf70
movq %rbx, %rdi
movq %r15, %rsi
callq *(%rax)
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq