// File: /workspace/github_c_makefile_star2_O2/CBaquero[P]random/Random.cc
// Function: RandomHist_RedisCommand(RedisModuleCtx*, RedisModuleString**, int)
int RandomHist_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  if (argc > 4) return RedisModule_WrongArity(ctx);

  long long slots=10;
  long long col=0;

  if (argc >= 3) 
  {
    if (RedisModule_StringToLongLong(argv[2],&slots) != REDISMODULE_OK)
      return RedisModule_ReplyWithError(ctx,"ERR invalid hist size");
  }
  if (argc == 4) 
  {
    if (RedisModule_StringToLongLong(argv[3],&col) != REDISMODULE_OK)
      return RedisModule_ReplyWithError(ctx,"ERR invalid columns size");
  }

  /* Check key type */
  RedisModuleKey * key = ( RedisModuleKey *) 
    RedisModule_OpenKey(ctx, argv[1], REDISMODULE_READ);
  if (RedisModule_KeyType(key) != REDISMODULE_KEYTYPE_LIST)
  {
    RedisModule_CloseKey(key);
    return RedisModule_ReplyWithError(ctx,"ERR key not of List type");
  }
  RedisModule_CloseKey(key);

  long long hist[slots];
  for (auto i=0; i < slots; i++) hist[i]=0;
  RedisModuleCallReply *reply;
  reply = RedisModule_Call(ctx,"LRANGE","scc",argv[1],"0","-1");

  if (RedisModule_CallReplyType(reply) == REDISMODULE_REPLY_NULL ||
      RedisModule_CallReplyType(reply) == REDISMODULE_REPLY_ERROR )
  {
    RedisModule_FreeCallReply(reply);
    return RedisModule_ReplyWithError(ctx,"ERR error in key");
  }

  size_t len;
  double min=0, max=0;
  if (RedisModule_CallReplyType(reply) == REDISMODULE_REPLY_ARRAY)
  {
    len = RedisModule_CallReplyLength(reply);
    if (len == 0)
    {
      RedisModule_FreeCallReply(reply);
      return REDISMODULE_OK;
    }
    for (auto i=0; i < len; i++)
    {
      RedisModuleCallReply *subreply;
      subreply = RedisModule_CallReplyArrayElement(reply,i);
      double e;
      RedisModuleString *ele = RedisModule_CreateStringFromCallReply(subreply);
      if (RedisModule_StringToDouble(ele,&e)==REDISMODULE_ERR)
      {
        RedisModule_FreeString(ctx,ele);
        RedisModule_FreeCallReply(reply);
        return RedisModule_ReplyWithError(ctx,"ERR bad list value");
      }
      RedisModule_FreeString(ctx,ele);
      if (i==0) min=max=e;
      if (e < min) min=e;
      if (e > max) max=e;
    }
    double range=max-min;
    for (auto i=0; i < len; i++)
    {
      RedisModuleCallReply *subreply;
      subreply = RedisModule_CallReplyArrayElement(reply,i);
      double e;
      RedisModuleString *ele = RedisModule_CreateStringFromCallReply(subreply);
      RedisModule_StringToDouble(ele,&e);
      RedisModule_FreeString(ctx,ele);
      long long slot=(long long) std::floor(((e-min)/range)*slots);
      if (slot==slots) slot--; /* max value goes to last slot */
      hist[slot]++;
    }

  }
  RedisModule_FreeCallReply(reply);

  RedisModule_ReplyWithArray(ctx,slots);
  if (col==0)
  {
    for (auto i=0; i < slots; i++)
      RedisModule_ReplyWithLongLong(ctx,hist[i]);
  }
  else
  {
    char s[col];
    for (auto i=0; i < col; i++)
      s[i] = {'*'};
    double hmax=hist[0];
    for (auto i=0; i < slots; i++)
      if (hmax < hist[i]) 
        hmax = hist[i];
    for (auto i=0; i < slots; i++)
      RedisModule_ReplyWithStringBuffer(ctx,s,std::floor(((double) hist[i])/hmax*col));
  }
  return REDISMODULE_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %r13
cmpl $0x5, %edx
jl 0x5c0e
movq 0x5345(%rip), %rax      # 0xaf40
movq %r13, %rdi
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *(%rax)
movl %edx, %r15d
movq %rsi, %r14
movq $0xa, -0x30(%rbp)
andq $0x0, -0x58(%rbp)
cmpl $0x3, %edx
jl 0x5c72
movq 0x52f3(%rip), %rbx      # 0xaf20
movq 0x10(%r14), %rdi
leaq -0x30(%rbp), %rsi
callq *(%rbx)
testl %eax, %eax
je 0x5c4e
movq 0x512e(%rip), %rax      # 0xad70
leaq 0x24f8(%rip), %rsi      # 0x8141
jmp 0x5cef
cmpl $0x4, %r15d
jne 0x5c72
movq 0x18(%r14), %rdi
leaq -0x58(%rbp), %rsi
callq *(%rbx)
testl %eax, %eax
je 0x5c72
movq 0x5107(%rip), %rax      # 0xad70
leaq 0x24e7(%rip), %rsi      # 0x8157
jmp 0x5cef
movq 0x518f(%rip), %rax      # 0xae08
movq 0x8(%r14), %rsi
pushq $0x1
popq %rdx
movq %r13, %rdi
callq *(%rax)
movq %rax, %r15
movq 0x51a1(%rip), %rax      # 0xae30
movq %r15, %rdi
callq *(%rax)
movl %eax, %r12d
movq 0x534a(%rip), %rax      # 0xafe8
movq %r15, %rdi
callq *(%rax)
cmpl $0x2, %r12d
jne 0x5ce1
movq %rsp, %r12
movq -0x30(%rbp), %rax
movq %rsp, %rbx
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
movq -0x30(%rbp), %rax
xorl %ecx, %ecx
testq %rax, %rax
cmovleq %rcx, %rax
cmpq %rcx, %rax
je 0x5cfc
andq $0x0, (%rbx,%rcx,8)
incq %rcx
jmp 0x5cd2
movq 0x5088(%rip), %rax      # 0xad70
leaq 0x2481(%rip), %rsi      # 0x8170
movq %r13, %rdi
callq *(%rax)
movl %eax, %r14d
jmp 0x6038
movq 0x51d5(%rip), %r10      # 0xaed8
movq 0x8(%r14), %rcx
leaq 0x247b(%rip), %rsi      # 0x8189
leaq 0x247b(%rip), %rdx      # 0x8190
leaq 0x2478(%rip), %r8       # 0x8194
leaq 0x2473(%rip), %r9       # 0x8196
movq %r13, %rdi
xorl %eax, %eax
callq *(%r10)
movq %rax, %r14
movq 0x51f3(%rip), %r15      # 0xaf28
movq %rax, %rdi
callq *(%r15)
cmpl $0x4, %eax
je 0x5d4b
movq %r14, %rdi
callq *(%r15)
cmpl $0x1, %eax
jne 0x5d72
movq 0x51c6(%rip), %rax      # 0xaf18
movq %r14, %rdi
callq *(%rax)
movq 0x5012(%rip), %rax      # 0xad70
leaq 0x2434(%rip), %rsi      # 0x8199
movq %r13, %rdi
callq *(%rax)
movl %eax, %r14d
jmp 0x6035
movq %r12, -0x48(%rbp)
movq %r13, -0x40(%rbp)
movq %r14, %rdi
callq *(%r15)
cmpl $0x3, %eax
jne 0x5ef3
movq 0x5170(%rip), %rax      # 0xaf00
movq %r14, %rdi
callq *(%rax)
movq %rax, -0x68(%rbp)
testq %rax, %rax
je 0x5e42
xorpd %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
xorl %r12d, %r12d
movsd %xmm0, -0x50(%rbp)
cmpq %r12, -0x68(%rbp)
je 0x5e53
movq %r14, %rdi
movq %r12, %rsi
movq 0x50ae(%rip), %rax      # 0xae78
callq *(%rax)
movq %rax, %rdi
movq 0x51f2(%rip), %rax      # 0xafc8
callq *(%rax)
movq %rax, %r13
movq %rax, %rdi
leaq -0x60(%rbp), %rsi
movq 0x50f7(%rip), %rax      # 0xaee0
callq *(%rax)
movl %eax, %r15d
movq -0x40(%rbp), %rdi
movq %r13, %rsi
movq 0x4e74(%rip), %rax      # 0xac70
callq *(%rax)
cmpl $0x1, %r15d
je 0x600e
movsd -0x60(%rbp), %xmm0
movapd %xmm0, %xmm1
movapd %xmm0, %xmm2
testq %r12, %r12
je 0x5e24
movsd -0x38(%rbp), %xmm1
movsd -0x50(%rbp), %xmm2
movapd %xmm0, %xmm3
minsd %xmm2, %xmm3
movsd %xmm3, -0x50(%rbp)
maxsd %xmm1, %xmm0
incq %r12
movsd %xmm0, -0x38(%rbp)
jmp 0x5db3
movq 0x50cf(%rip), %rax      # 0xaf18
movq %r14, %rdi
callq *(%rax)
jmp 0x5fe7
movsd -0x38(%rbp), %xmm0
subsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
xorl %r12d, %r12d
leaq -0x60(%rbp), %r13
cmpq %r12, -0x68(%rbp)
je 0x5ef3
movq %r14, %rdi
movq %r12, %rsi
movq 0x4ff8(%rip), %rax      # 0xae78
callq *(%rax)
movq %rax, %rdi
movq 0x513c(%rip), %rax      # 0xafc8
callq *(%rax)
movq %rax, %r15
movq %rax, %rdi
movq %r13, %rsi
movq 0x5042(%rip), %rax      # 0xaee0
callq *(%rax)
movq -0x40(%rbp), %rdi
movq %r15, %rsi
movq 0x4dc2(%rip), %rax      # 0xac70
callq *(%rax)
movsd -0x60(%rbp), %xmm1
subsd -0x50(%rbp), %xmm1
divsd -0x38(%rbp), %xmm1
movq -0x30(%rbp), %r15
cvtsi2sd %r15, %xmm0
mulsd %xmm1, %xmm0
callq 0x51a0
cvttsd2si %xmm0, %rax
xorl %ecx, %ecx
cmpq %rax, %r15
sete %cl
leaq (%rbx,%rax,8), %rax
shll $0x3, %ecx
subq %rcx, %rax
incq (%rax)
incq %r12
jmp 0x5e69
movq 0x501e(%rip), %rax      # 0xaf18
movq %r14, %rdi
callq *(%rax)
movq 0x4f82(%rip), %rax      # 0xae88
movq -0x30(%rbp), %rsi
movq -0x40(%rbp), %r13
movq %r13, %rdi
callq *(%rax)
movq -0x58(%rbp), %rax
testq %rax, %rax
je 0x5fec
movq %rsp, %r15
movq %rsp, %r14
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
movq -0x58(%rbp), %rax
xorl %ecx, %ecx
testq %rax, %rax
cmovleq %rcx, %rax
cmpq %rcx, %rax
je 0x5f50
movb $0x2a, (%r14,%rcx)
incq %rcx
jmp 0x5f41
cvtsi2sdq (%rbx), %xmm2
movq -0x30(%rbp), %rax
xorl %ecx, %ecx
testq %rax, %rax
movl $0x0, %edx
cmovgq %rax, %rdx
cmpq %rcx, %rdx
je 0x5f7f
cvtsi2sdq (%rbx,%rcx,8), %xmm0
maxsd %xmm2, %xmm0
incq %rcx
movapd %xmm0, %xmm2
jmp 0x5f67
xorl %r12d, %r12d
movq 0x4ec7(%rip), %r13      # 0xae50
movsd %xmm2, -0x38(%rbp)
cmpq %r12, %rax
jle 0x5fe4
cvtsi2sdq (%rbx,%r12,8), %xmm1
divsd %xmm2, %xmm1
cvtsi2sdq -0x58(%rbp), %xmm0
mulsd %xmm1, %xmm0
callq 0x51a0
cvttsd2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subsd 0x2048(%rip), %xmm0     # 0x8008
cvttsd2si %xmm0, %rdx
andq %rcx, %rdx
orq %rax, %rdx
movq -0x40(%rbp), %rdi
movq %r14, %rsi
callq *(%r13)
movsd -0x38(%rbp), %xmm2
incq %r12
movq -0x30(%rbp), %rax
jmp 0x5f8e
movq %r15, %rsp
xorl %r14d, %r14d
jmp 0x6031
xorl %r14d, %r14d
movq 0x4f7a(%rip), %r15      # 0xaf70
xorl %r12d, %r12d
cmpq %r12, -0x30(%rbp)
jle 0x6031
movq (%rbx,%r12,8), %rsi
movq %r13, %rdi
callq *(%r15)
incq %r12
jmp 0x5ff9
movq 0x4f03(%rip), %rax      # 0xaf18
movq %r14, %rdi
callq *(%rax)
movq 0x4d4f(%rip), %rax      # 0xad70
leaq 0x2182(%rip), %rsi      # 0x81aa
movq -0x40(%rbp), %rdi
callq *(%rax)
movl %eax, %r14d
movq -0x48(%rbp), %r12
movq %r12, %rsp
movl %r14d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq