// File: /workspace/github_c_makefile_star2_O0/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
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
cmpl $0x3, -0x1c(%rbp)
jl 0x6f2f
cmpl $0x4, -0x1c(%rbp)
jle 0x6f47
movq 0x700a(%rip), %rax      # 0xdf40
movq (%rax), %rax
movq -0x10(%rbp), %rdi
callq *%rax
movl %eax, -0x4(%rbp)
jmp 0x7130
cmpl $0x4, -0x1c(%rbp)
jne 0x6f8b
movq 0x6f8c(%rip), %rax      # 0xdee0
movq (%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x18(%rcx), %rdi
leaq -0x28(%rbp), %rsi
callq *%rax
cmpl $0x0, %eax
je 0x6f89
movq 0x6dff(%rip), %rax      # 0xdd70
movq (%rax), %rax
movq -0x10(%rbp), %rdi
leaq 0x318f(%rip), %rsi      # 0xa10e
callq *%rax
movl %eax, -0x4(%rbp)
jmp 0x7130
jmp 0x6f98
movsd 0x306d(%rip), %xmm0     # 0xa000
movsd %xmm0, -0x28(%rbp)
movq 0x6e69(%rip), %rax      # 0xde08
movq (%rax), %rax
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rcx
movq 0x8(%rcx), %rsi
movl $0x3, %edx
callq *%rax
movq %rax, -0x30(%rbp)
movq 0x6e70(%rip), %rax      # 0xde30
movq (%rax), %rax
movq -0x30(%rbp), %rdi
callq *%rax
cmpl $0x2, %eax
je 0x7012
movq 0x6e5b(%rip), %rax      # 0xde30
movq (%rax), %rax
movq -0x30(%rbp), %rdi
callq *%rax
cmpl $0x0, %eax
je 0x7012
movq 0x6ffe(%rip), %rax      # 0xdfe8
movq (%rax), %rax
movq -0x30(%rbp), %rdi
callq *%rax
movq 0x6d76(%rip), %rax      # 0xdd70
movq (%rax), %rax
movq -0x10(%rbp), %rdi
leaq 0x30ac(%rip), %rsi      # 0xa0b4
callq *%rax
movl %eax, -0x4(%rbp)
jmp 0x7130
movq 0x6f07(%rip), %rax      # 0xdf20
movq (%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x10(%rcx), %rdi
leaq -0x38(%rbp), %rsi
callq *%rax
cmpl $0x0, %eax
jne 0x7036
cmpq $0x0, -0x38(%rbp)
jge 0x7065
movq 0x6fab(%rip), %rax      # 0xdfe8
movq (%rax), %rax
movq -0x30(%rbp), %rdi
callq *%rax
movq 0x6d23(%rip), %rax      # 0xdd70
movq (%rax), %rax
movq -0x10(%rbp), %rdi
leaq 0x309b(%rip), %rsi      # 0xa0f6
callq *%rax
movl %eax, -0x4(%rbp)
jmp 0x7130
movsd -0x28(%rbp), %xmm0
leaq -0x40(%rbp), %rdi
callq 0x6330
movq -0x38(%rbp), %rax
movq %rax, %rcx
addq $-0x1, %rcx
movq %rcx, -0x38(%rbp)
cmpq $0x0, %rax
jle 0x70f1
movq 0x6d91(%rip), %rax      # 0xde20
movq (%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x60(%rbp)
leaq -0x40(%rbp), %rdi
movq 0x6eef(%rip), %rsi      # 0xdf98
callq 0x6520
movq -0x60(%rbp), %rdi
movq -0x58(%rbp), %rcx
leaq 0x304b(%rip), %rsi      # 0xa108
movb $0x1, %al
callq *%rcx
movq %rax, -0x48(%rbp)
movq 0x6e64(%rip), %rax      # 0xdf30
movq (%rax), %rax
movq -0x30(%rbp), %rdi
movq -0x48(%rbp), %rdx
xorl %esi, %esi
callq *%rax
movq 0x6b8e(%rip), %rax      # 0xdc70
movq (%rax), %rax
movq -0x10(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq *%rax
jmp 0x7073
movq 0x6b20(%rip), %rax      # 0xdc18
movq (%rax), %rax
movq -0x30(%rbp), %rdi
callq *%rax
movq %rax, -0x50(%rbp)
movq 0x6edc(%rip), %rax      # 0xdfe8
movq (%rax), %rax
movq -0x30(%rbp), %rdi
callq *%rax
movq 0x6e54(%rip), %rax      # 0xdf70
movq (%rax), %rax
movq -0x10(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq *%rax
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)