// File: /workspace/github_c_makefile_star2_O1/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 $0x68, %rsp
movq %rdi, %rbx
cmpl $0x5, %edx
jl 0x4c3a
movq 0x5319(%rip), %rax      # 0x9f40
movq %rbx, %rdi
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *(%rax)
movl %edx, %r14d
movq %rsi, %r15
movq $0xa, -0x38(%rbp)
movq $0x0, -0x60(%rbp)
cmpl $0x3, %edx
jl 0x4c7d
movq 0x52c4(%rip), %rax      # 0x9f20
movq 0x10(%r15), %rdi
leaq -0x38(%rbp), %rsi
callq *(%rax)
testl %eax, %eax
je 0x4c7d
movq 0x50ff(%rip), %rax      # 0x9d70
leaq 0x24c5(%rip), %rsi      # 0x713d
jmp 0x4da3
cmpl $0x4, %r14d
jne 0x4cab
movq 0x5296(%rip), %rax      # 0x9f20
movq 0x18(%r15), %rdi
leaq -0x60(%rbp), %rsi
callq *(%rax)
testl %eax, %eax
je 0x4cab
movq 0x50d1(%rip), %rax      # 0x9d70
leaq 0x24ad(%rip), %rsi      # 0x7153
jmp 0x4da3
movq 0x5156(%rip), %rax      # 0x9e08
movq 0x8(%r15), %rsi
movq %rbx, %rdi
movl $0x1, %edx
callq *(%rax)
movq %rax, %r14
movq 0x5166(%rip), %rax      # 0x9e30
movq %r14, %rdi
callq *(%rax)
movl %eax, %r12d
movq 0x530f(%rip), %rax      # 0x9fe8
movq %r14, %rdi
callq *(%rax)
cmpl $0x2, %r12d
jne 0x4d95
movq %rsp, %r12
movq -0x38(%rbp), %rax
movq %rsp, %r13
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
movq -0x38(%rbp), %rdx
testq %rdx, %rdx
jle 0x4d1b
shlq $0x3, %rdx
movq %r13, %rdi
xorl %esi, %esi
callq 0x4090
movq 0x51b6(%rip), %r10      # 0x9ed8
movq 0x8(%r15), %rcx
leaq 0x2458(%rip), %rsi      # 0x7185
leaq 0x2458(%rip), %rdx      # 0x718c
leaq 0x2455(%rip), %r8       # 0x7190
leaq 0x2450(%rip), %r9       # 0x7192
movq %rbx, -0x58(%rbp)
movq %rbx, %rdi
xorl %eax, %eax
callq *(%r10)
movq %rax, %rbx
movq 0x51d0(%rip), %r14      # 0x9f28
movq %rax, %rdi
callq *(%r14)
cmpl $0x4, %eax
je 0x4d6e
movq %rbx, %rdi
callq *(%r14)
cmpl $0x1, %eax
jne 0x4dbb
movq 0x51a3(%rip), %rax      # 0x9f18
movq %rbx, %rdi
callq *(%rax)
movq 0x4fef(%rip), %rax      # 0x9d70
leaq 0x240d(%rip), %rsi      # 0x7195
movq -0x58(%rbp), %rdi
callq *(%rax)
movl %eax, %ecx
movq %r12, %rsp
jmp 0x4daa
movq 0x4fd4(%rip), %rax      # 0x9d70
leaq 0x23c9(%rip), %rsi      # 0x716c
movq %rbx, %rdi
callq *(%rax)
movl %eax, %ecx
movl %ecx, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, -0x50(%rbp)
movq %rbx, %rdi
callq *(%r14)
cmpl $0x3, %eax
movq %rbx, -0x70(%rbp)
movq -0x58(%rbp), %rbx
movq %r13, -0x68(%rbp)
jne 0x4fc2
movq 0x511f(%rip), %rax      # 0x9f00
movq -0x70(%rbp), %rbx
movq %rbx, %rdi
callq *(%rax)
testq %rax, %rax
je 0x5079
xorpd %xmm3, %xmm3
movb $0x1, -0x29(%rbp)
xorl %r12d, %r12d
movq 0x5073(%rip), %rcx      # 0x9e78
movq 0x51bc(%rip), %r14      # 0x9fc8
movq %rdx, -0x88(%rbp)
xorpd %xmm0, %xmm0
movq %rax, -0x90(%rbp)
movsd %xmm0, -0x40(%rbp)
movsd %xmm3, -0x48(%rbp)
movq %rbx, %rdi
movq %r12, %rsi
callq *(%rcx)
movq %rax, %rdi
callq *(%r14)
movq %rax, %r15
movq %rax, %rdi
leaq -0x78(%rbp), %rsi
movq 0x5099(%rip), %rax      # 0x9ee0
callq *(%rax)
movl %eax, %r13d
movq -0x58(%rbp), %r14
movq %r14, %rdi
movq %r15, %rsi
movq 0x4e13(%rip), %rax      # 0x9c70
callq *(%rax)
movl %r13d, -0x7c(%rbp)
cmpl $0x1, %r13d
jne 0x4eb0
movq %rbx, %rdi
movq 0x50a5(%rip), %rax      # 0x9f18
callq *(%rax)
movq %r14, %rdi
leaq 0x2327(%rip), %rsi      # 0x71a6
movq 0x4eea(%rip), %rax      # 0x9d70
callq *(%rax)
movq %rax, -0x88(%rbp)
movq -0x90(%rbp), %r13
movq 0x4fdb(%rip), %rcx      # 0x9e78
movsd -0x48(%rbp), %xmm3
movq 0x511f(%rip), %r14      # 0x9fc8
movsd -0x40(%rbp), %xmm0
jmp 0x4eed
movsd -0x78(%rbp), %xmm0
movapd %xmm0, %xmm1
movapd %xmm0, %xmm2
testq %r12, %r12
je 0x4ecc
movsd -0x40(%rbp), %xmm1
movsd -0x48(%rbp), %xmm2
movapd %xmm0, %xmm3
minsd %xmm2, %xmm3
maxsd %xmm1, %xmm0
movq -0x90(%rbp), %r13
movq 0x4f92(%rip), %rcx      # 0x9e78
movq 0x50db(%rip), %r14      # 0x9fc8
cmpl $0x1, -0x7c(%rbp)
je 0x4f03
incq %r12
cmpq %r12, %r13
seta -0x29(%rbp)
jne 0x4e1e
testb $0x1, -0x29(%rbp)
movq -0x50(%rbp), %r12
movq -0x88(%rbp), %rcx
jne 0x4d90
subsd %xmm3, %xmm0
movsd %xmm0, -0x40(%rbp)
cmpq $0x1, %r13
adcq $0x0, %r13
xorl %r12d, %r12d
movq -0x68(%rbp), %r15
movq -0x58(%rbp), %rbx
movsd %xmm3, -0x48(%rbp)
movq -0x70(%rbp), %rdi
movq %r12, %rsi
movq 0x4f31(%rip), %rax      # 0x9e78
callq *(%rax)
movq %rax, %rdi
callq *(%r14)
movq %rax, %r14
movq %rax, %rdi
leaq -0x78(%rbp), %rsi
movq 0x4f80(%rip), %rax      # 0x9ee0
callq *(%rax)
movq %rbx, %rdi
movq %r14, %rsi
movq 0x4d01(%rip), %rax      # 0x9c70
callq *(%rax)
movsd -0x78(%rbp), %xmm1
subsd -0x48(%rbp), %xmm1
divsd -0x40(%rbp), %xmm1
movq -0x38(%rbp), %r14
xorps %xmm0, %xmm0
cvtsi2sd %r14, %xmm0
mulsd %xmm1, %xmm0
callq 0x4160
cvttsd2si %xmm0, %rax
xorl %ecx, %ecx
cmpq %rax, %r14
movq 0x5022(%rip), %r14      # 0x9fc8
sete %cl
leaq (%r15,%rax,8), %rax
shll $0x3, %ecx
subq %rcx, %rax
incq (%rax)
incq %r12
cmpq %r12, %r13
jne 0x4f39
movq 0x4f4f(%rip), %rax      # 0x9f18
movq -0x70(%rbp), %rdi
callq *(%rax)
movq 0x4eb2(%rip), %rax      # 0x9e88
movq -0x38(%rbp), %rsi
movq %rbx, %rdi
callq *(%rax)
movq -0x60(%rbp), %rax
testq %rax, %rax
je 0x5045
movq %rsp, -0x40(%rbp)
movq %rsp, %r15
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movq -0x60(%rbp), %rdx
testq %rdx, %rdx
movq -0x68(%rbp), %r14
jle 0x5017
movq %r15, %rdi
movl $0x2a, %esi
callq 0x4090
xorps %xmm0, %xmm0
cvtsi2sdq (%r14), %xmm0
movq -0x38(%rbp), %rax
testq %rax, %rax
jle 0x5087
xorl %ecx, %ecx
xorps %xmm2, %xmm2
cvtsi2sdq (%r14,%rcx,8), %xmm2
maxsd %xmm0, %xmm2
incq %rcx
movapd %xmm2, %xmm0
cmpq %rcx, %rax
jne 0x502a
jmp 0x508b
cmpq $0x0, -0x38(%rbp)
movq -0x50(%rbp), %r12
jle 0x5072
movq 0x4f19(%rip), %r13      # 0x9f70
xorl %r15d, %r15d
movq -0x68(%rbp), %r14
movq (%r14,%r15,8), %rsi
movq %rbx, %rdi
callq *(%r13)
incq %r15
cmpq %r15, -0x38(%rbp)
jg 0x505e
xorl %ecx, %ecx
jmp 0x4d90
movq 0x4e98(%rip), %rax      # 0x9f18
movq %rbx, %rdi
callq *(%rax)
jmp 0x50fb
movapd %xmm0, %xmm2
cmpq $0x0, -0x38(%rbp)
jle 0x50f7
xorl %r12d, %r12d
movq 0x4db4(%rip), %r13      # 0x9e50
movsd %xmm2, -0x48(%rbp)
xorps %xmm1, %xmm1
cvtsi2sdq (%r14,%r12,8), %xmm1
divsd %xmm2, %xmm1
xorps %xmm0, %xmm0
cvtsi2sdq -0x60(%rbp), %xmm0
mulsd %xmm1, %xmm0
callq 0x4160
cvttsd2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subsd 0x1f34(%rip), %xmm0     # 0x7008
cvttsd2si %xmm0, %rdx
andq %rcx, %rdx
orq %rax, %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq *(%r13)
movsd -0x48(%rbp), %xmm2
incq %r12
cmpq %r12, -0x38(%rbp)
jg 0x50a1
movq -0x40(%rbp), %rsp
xorl %ecx, %ecx
movq -0x50(%rbp), %r12
jmp 0x4d90