// File: /workspace/github_c_makefile_star2_O3/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 $0x58, %rsp
movq %rdi, %rbx
cmpl $0x5, %edx
jl 0x4c4e
movq 0x5305(%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, -0x30(%rbp)
movq $0x0, -0x58(%rbp)
cmpl $0x3, %edx
jl 0x4cbc
movq 0x52b0(%rip), %r12      # 0x9f20
movq 0x10(%r15), %rdi
leaq -0x30(%rbp), %rsi
callq *(%r12)
testl %eax, %eax
je 0x4c93
movq 0x50e9(%rip), %rax      # 0x9d70
leaq 0x24af(%rip), %rsi      # 0x713d
jmp 0x4db0
cmpl $0x4, %r14d
jne 0x4cbc
movq 0x18(%r15), %rdi
leaq -0x58(%rbp), %rsi
callq *(%r12)
testl %eax, %eax
je 0x4cbc
movq 0x50c0(%rip), %rax      # 0x9d70
leaq 0x249c(%rip), %rsi      # 0x7153
jmp 0x4db0
movq 0x5145(%rip), %rax      # 0x9e08
movq 0x8(%r15), %rsi
movq %rbx, %rdi
movl $0x1, %edx
callq *(%rax)
movq %rax, %r14
movq 0x5155(%rip), %rax      # 0x9e30
movq %r14, %rdi
callq *(%rax)
movl %eax, %r12d
movq 0x52fe(%rip), %rax      # 0x9fe8
movq %r14, %rdi
callq *(%rax)
cmpl $0x2, %r12d
jne 0x4da2
movq %rsp, %r12
movq -0x30(%rbp), %rax
movq %rsp, %r13
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
movq -0x30(%rbp), %rdx
testq %rdx, %rdx
jle 0x4d2c
shlq $0x3, %rdx
movq %r13, %rdi
xorl %esi, %esi
callq 0x4090
movq 0x51a5(%rip), %r10      # 0x9ed8
movq 0x8(%r15), %rcx
leaq 0x2447(%rip), %rsi      # 0x7185
leaq 0x2447(%rip), %rdx      # 0x718c
leaq 0x2444(%rip), %r8       # 0x7190
leaq 0x243f(%rip), %r9       # 0x7192
movq %rbx, %rdi
xorl %eax, %eax
callq *(%r10)
movq %rax, %r15
movq 0x51c3(%rip), %r14      # 0x9f28
movq %rax, %rdi
callq *(%r14)
cmpl $0x4, %eax
je 0x4d7b
movq %r15, %rdi
callq *(%r14)
cmpl $0x1, %eax
jne 0x4dbd
movq 0x5196(%rip), %rax      # 0x9f18
movq %r15, %rdi
callq *(%rax)
movq 0x4fe2(%rip), %rax      # 0x9d70
leaq 0x2400(%rip), %rsi      # 0x7195
movq %rbx, %rdi
callq *(%rax)
movl %eax, %r15d
jmp 0x509b
movq 0x4fc7(%rip), %rax      # 0x9d70
leaq 0x23bc(%rip), %rsi      # 0x716c
movq %rbx, %rdi
callq *(%rax)
movl %eax, %r15d
jmp 0x509e
movq %r12, -0x40(%rbp)
movq %r15, %rdi
callq *(%r14)
cmpl $0x3, %eax
movq %r13, %r12
movq %r15, -0x38(%rbp)
movq %rbx, -0x50(%rbp)
jne 0x4f37
movq %r12, -0x78(%rbp)
movq 0x511a(%rip), %rax      # 0x9f00
movq -0x38(%rbp), %rbx
movq %rbx, %rdi
callq *(%rax)
testq %rax, %rax
je 0x5062
xorpd %xmm2, %xmm2
xorl %r13d, %r13d
movq 0x5072(%rip), %rcx      # 0x9e78
movq 0x51bb(%rip), %r14      # 0x9fc8
xorpd %xmm0, %xmm0
movq %rax, -0x70(%rbp)
movsd %xmm0, -0x60(%rbp)
movsd %xmm2, -0x48(%rbp)
movq -0x38(%rbp), %rdi
movq %r13, %rsi
movq %rcx, %rbx
callq *(%rcx)
movq %rax, %rdi
movq %r14, %r15
callq *(%r14)
movq %rax, %r12
movq %rax, %rdi
leaq -0x68(%rbp), %rsi
movq 0x509b(%rip), %rax      # 0x9ee0
callq *(%rax)
movl %eax, %r14d
movq -0x50(%rbp), %rdi
movq %r12, %rsi
movq 0x4e18(%rip), %rax      # 0x9c70
callq *(%rax)
cmpl $0x1, %r14d
je 0x5073
movsd -0x68(%rbp), %xmm3
movapd %xmm3, %xmm0
movapd %xmm3, %xmm1
testq %r13, %r13
je 0x4e80
movsd -0x60(%rbp), %xmm0
movsd -0x48(%rbp), %xmm1
movapd %xmm3, %xmm2
minsd %xmm1, %xmm2
maxsd %xmm0, %xmm3
incq %r13
movapd %xmm3, %xmm0
movq -0x70(%rbp), %rax
cmpq %r13, %rax
movq %rbx, %rcx
movq %r15, %r14
jne 0x4e15
subsd %xmm2, %xmm3
movsd %xmm3, -0x60(%rbp)
xorl %r13d, %r13d
movq -0x78(%rbp), %r12
movsd %xmm2, -0x48(%rbp)
movq -0x38(%rbp), %rdi
movq %r13, %rsi
callq *(%rbx)
movq %rax, %rdi
callq *(%r14)
movq %rax, %r14
movq %rax, %rdi
leaq -0x68(%rbp), %rsi
movq 0x5005(%rip), %rax      # 0x9ee0
callq *(%rax)
movq -0x50(%rbp), %rdi
movq %r14, %rsi
movq 0x4d85(%rip), %rax      # 0x9c70
callq *(%rax)
movsd -0x68(%rbp), %xmm1
subsd -0x48(%rbp), %xmm1
divsd -0x60(%rbp), %xmm1
movq -0x30(%rbp), %r14
xorps %xmm0, %xmm0
cvtsi2sd %r14, %xmm0
mulsd %xmm1, %xmm0
callq 0x4160
cvttsd2si %xmm0, %rax
xorl %ecx, %ecx
cmpq %rax, %r14
movq %r15, %r14
sete %cl
leaq (%r12,%rax,8), %rax
shll $0x3, %ecx
subq %rcx, %rax
incq (%rax)
incq %r13
cmpq %r13, -0x70(%rbp)
jne 0x4ebb
movq %r12, %r13
movq 0x4fd7(%rip), %rax      # 0x9f18
movq -0x38(%rbp), %rdi
callq *(%rax)
movq 0x4f3a(%rip), %rax      # 0x9e88
movq -0x30(%rbp), %rsi
movq -0x50(%rbp), %rbx
movq %rbx, %rdi
callq *(%rax)
movq -0x58(%rbp), %rax
testq %rax, %rax
je 0x5034
movq %rsp, -0x38(%rbp)
movq %rsp, %r15
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movq -0x58(%rbp), %rdx
testq %rdx, %rdx
jle 0x4f93
movq %r15, %rdi
movl $0x2a, %esi
callq 0x4090
movq -0x30(%rbp), %rax
testq %rax, %rax
jle 0x502e
cvtsi2sdq (%r13), %xmm2
xorl %ecx, %ecx
movq %r13, %r14
movapd %xmm2, %xmm0
xorps %xmm2, %xmm2
cvtsi2sdq (%r14,%rcx,8), %xmm2
maxsd %xmm0, %xmm2
incq %rcx
cmpq %rcx, %rax
jne 0x4fab
testq %rax, %rax
jle 0x502e
xorl %r12d, %r12d
movq 0x4e7d(%rip), %r13      # 0x9e50
movsd %xmm2, -0x48(%rbp)
xorps %xmm1, %xmm1
cvtsi2sdq (%r14,%r12,8), %xmm1
divsd %xmm2, %xmm1
xorps %xmm0, %xmm0
cvtsi2sdq -0x58(%rbp), %xmm0
mulsd %xmm1, %xmm0
callq 0x4160
cvttsd2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subsd 0x1ffd(%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, -0x30(%rbp)
jg 0x4fd3
movq -0x38(%rbp), %rsp
jmp 0x506e
cmpq $0x0, -0x30(%rbp)
movq -0x40(%rbp), %r12
jle 0x50b0
xorl %r15d, %r15d
movq 0x4f27(%rip), %r14      # 0x9f70
xorl %r12d, %r12d
movq (%r13,%r12,8), %rsi
movq %rbx, %rdi
callq *(%r14)
incq %r12
cmpq %r12, -0x30(%rbp)
jg 0x504c
jmp 0x5097
movq 0x4eaf(%rip), %rax      # 0x9f18
movq %rbx, %rdi
callq *(%rax)
xorl %r15d, %r15d
jmp 0x5097
movq 0x4e9e(%rip), %rax      # 0x9f18
movq -0x38(%rbp), %rdi
callq *(%rax)
movq 0x4ce9(%rip), %rax      # 0x9d70
leaq 0x2118(%rip), %rsi      # 0x71a6
movq -0x50(%rbp), %rdi
callq *(%rax)
movl %eax, %r15d
movq -0x40(%rbp), %r12
movq %r12, %rsp
movl %r15d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r15d, %r15d
jmp 0x509b