// File: /workspace/github_c_makefile_star2_O0/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
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
cmpl $0x4, -0x1c(%rbp)
jle 0x7174
movq 0x6ddd(%rip), %rax      # 0xdf40
movq (%rax), %rax
movq -0x10(%rbp), %rdi
callq *%rax
movl %eax, -0x4(%rbp)
jmp 0x7859
movq $0xa, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
cmpl $0x3, -0x1c(%rbp)
jl 0x71c8
movq 0x6d8f(%rip), %rax      # 0xdf20
movq (%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x10(%rcx), %rdi
leaq -0x28(%rbp), %rsi
callq *%rax
cmpl $0x0, %eax
je 0x71c6
movq 0x6bc2(%rip), %rax      # 0xdd70
movq (%rax), %rax
movq -0x10(%rbp), %rdi
leaq 0x2f65(%rip), %rsi      # 0xa121
callq *%rax
movl %eax, -0x4(%rbp)
jmp 0x7859
jmp 0x71c8
cmpl $0x4, -0x1c(%rbp)
jne 0x720c
movq 0x6d4b(%rip), %rax      # 0xdf20
movq (%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x18(%rcx), %rdi
leaq -0x30(%rbp), %rsi
callq *%rax
cmpl $0x0, %eax
je 0x720a
movq 0x6b7e(%rip), %rax      # 0xdd70
movq (%rax), %rax
movq -0x10(%rbp), %rdi
leaq 0x2f37(%rip), %rsi      # 0xa137
callq *%rax
movl %eax, -0x4(%rbp)
jmp 0x7859
jmp 0x720c
movq 0x6bf5(%rip), %rax      # 0xde08
movq (%rax), %rax
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rcx
movq 0x8(%rcx), %rsi
movl $0x1, %edx
callq *%rax
movq %rax, -0x38(%rbp)
movq 0x6bfc(%rip), %rax      # 0xde30
movq (%rax), %rax
movq -0x38(%rbp), %rdi
callq *%rax
cmpl $0x2, %eax
je 0x7271
movq 0x6d9f(%rip), %rax      # 0xdfe8
movq (%rax), %rax
movq -0x38(%rbp), %rdi
callq *%rax
movq 0x6b17(%rip), %rax      # 0xdd70
movq (%rax), %rax
movq -0x10(%rbp), %rdi
leaq 0x2ee9(%rip), %rsi      # 0xa150
callq *%rax
movl %eax, -0x4(%rbp)
jmp 0x7859
movq 0x6d70(%rip), %rax      # 0xdfe8
movq (%rax), %rax
movq -0x38(%rbp), %rdi
callq *%rax
movq -0x28(%rbp), %rax
movq %rsp, %rcx
movq %rcx, -0x40(%rbp)
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x100(%rbp)
movq %rcx, %rsp
movq %rax, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movslq -0x4c(%rbp), %rax
cmpq -0x28(%rbp), %rax
jge 0x72db
movq -0x100(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x72b3
movq 0x6bf6(%rip), %rax      # 0xded8
movq (%rax), %r10
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
leaq 0x2e71(%rip), %rsi      # 0xa169
leaq 0x2e71(%rip), %rdx      # 0xa170
leaq 0x2e6e(%rip), %r8       # 0xa174
leaq 0x2e69(%rip), %r9       # 0xa176
movb $0x0, %al
callq *%r10
movq %rax, -0x58(%rbp)
movq 0x6c0b(%rip), %rax      # 0xdf28
movq (%rax), %rax
movq -0x58(%rbp), %rdi
callq *%rax
cmpl $0x4, %eax
je 0x7340
movq 0x6bf6(%rip), %rax      # 0xdf28
movq (%rax), %rax
movq -0x58(%rbp), %rdi
callq *%rax
cmpl $0x1, %eax
jne 0x7376
movq 0x6bd1(%rip), %rax      # 0xdf18
movq (%rax), %rax
movq -0x58(%rbp), %rdi
callq *%rax
movq 0x6a19(%rip), %rax      # 0xdd70
movq (%rax), %rax
movq -0x10(%rbp), %rdi
leaq 0x2e14(%rip), %rsi      # 0xa179
callq *%rax
movl %eax, -0x4(%rbp)
movl $0x1, -0x5c(%rbp)
jmp 0x7852
xorps %xmm0, %xmm0
movsd %xmm0, -0x70(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x78(%rbp)
movq 0x6b9b(%rip), %rax      # 0xdf28
movq (%rax), %rax
movq -0x58(%rbp), %rdi
callq *%rax
cmpl $0x3, %eax
jne 0x7623
movq 0x6b5a(%rip), %rax      # 0xdf00
movq (%rax), %rax
movq -0x58(%rbp), %rdi
callq *%rax
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
jne 0x73dd
movq 0x6b57(%rip), %rax      # 0xdf18
movq (%rax), %rax
movq -0x58(%rbp), %rdi
callq *%rax
movl $0x0, -0x4(%rbp)
movl $0x1, -0x5c(%rbp)
jmp 0x7852
movl $0x0, -0x7c(%rbp)
movslq -0x7c(%rbp), %rax
cmpq -0x68(%rbp), %rax
jae 0x750e
movq 0x6a7f(%rip), %rax      # 0xde78
movq (%rax), %rax
movq -0x58(%rbp), %rdi
movslq -0x7c(%rbp), %rsi
callq *%rax
movq %rax, -0x88(%rbp)
movq 0x6bb4(%rip), %rax      # 0xdfc8
movq (%rax), %rax
movq -0x88(%rbp), %rdi
callq *%rax
movq %rax, -0x98(%rbp)
movq 0x6ab2(%rip), %rax      # 0xdee0
movq (%rax), %rax
movq -0x98(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq *%rax
cmpl $0x1, %eax
jne 0x7493
movq 0x6823(%rip), %rax      # 0xdc70
movq (%rax), %rax
movq -0x10(%rbp), %rdi
movq -0x98(%rbp), %rsi
callq *%rax
movq 0x6ab4(%rip), %rax      # 0xdf18
movq (%rax), %rax
movq -0x58(%rbp), %rdi
callq *%rax
movq 0x68fc(%rip), %rax      # 0xdd70
movq (%rax), %rax
movq -0x10(%rbp), %rdi
leaq 0x2d08(%rip), %rsi      # 0xa18a
callq *%rax
movl %eax, -0x4(%rbp)
movl $0x1, -0x5c(%rbp)
jmp 0x7852
movq 0x67d6(%rip), %rax      # 0xdc70
movq (%rax), %rax
movq -0x10(%rbp), %rdi
movq -0x98(%rbp), %rsi
callq *%rax
cmpl $0x0, -0x7c(%rbp)
jne 0x74c2
movsd -0x90(%rbp), %xmm0
movsd %xmm0, -0x78(%rbp)
movsd %xmm0, -0x70(%rbp)
movsd -0x90(%rbp), %xmm1
movsd -0x70(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x74e2
movsd -0x90(%rbp), %xmm0
movsd %xmm0, -0x70(%rbp)
movsd -0x90(%rbp), %xmm0
ucomisd -0x78(%rbp), %xmm0
jbe 0x74fe
movsd -0x90(%rbp), %xmm0
movsd %xmm0, -0x78(%rbp)
jmp 0x7500
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x73e4
movsd -0x78(%rbp), %xmm0
subsd -0x70(%rbp), %xmm0
movsd %xmm0, -0xa0(%rbp)
movl $0x0, -0xa4(%rbp)
movslq -0xa4(%rbp), %rax
cmpq -0x68(%rbp), %rax
jae 0x7621
movq 0x6936(%rip), %rax      # 0xde78
movq (%rax), %rax
movq -0x58(%rbp), %rdi
movslq -0xa4(%rbp), %rsi
callq *%rax
movq %rax, -0xb0(%rbp)
movq 0x6a68(%rip), %rax      # 0xdfc8
movq (%rax), %rax
movq -0xb0(%rbp), %rdi
callq *%rax
movq %rax, -0xc0(%rbp)
movq 0x6966(%rip), %rax      # 0xdee0
movq (%rax), %rax
movq -0xc0(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq *%rax
movq 0x66dc(%rip), %rax      # 0xdc70
movq (%rax), %rax
movq -0x10(%rbp), %rdi
movq -0xc0(%rbp), %rsi
callq *%rax
movsd -0xb8(%rbp), %xmm0
subsd -0x70(%rbp), %xmm0
divsd -0xa0(%rbp), %xmm0
cvtsi2sdq -0x28(%rbp), %xmm1
mulsd %xmm1, %xmm0
callq 0x6400
cvttsd2si %xmm0, %rax
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rax
cmpq -0x28(%rbp), %rax
jne 0x75f3
movq -0xc8(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0xc8(%rbp)
movq -0x100(%rbp), %rax
movq -0xc8(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
addq $0x1, %rdx
movq %rdx, (%rax,%rcx,8)
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
jmp 0x752a
jmp 0x7623
movq 0x68ee(%rip), %rax      # 0xdf18
movq (%rax), %rax
movq -0x58(%rbp), %rdi
callq *%rax
movq 0x684e(%rip), %rax      # 0xde88
movq (%rax), %rax
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq *%rax
cmpq $0x0, -0x30(%rbp)
jne 0x769d
movl $0x0, -0xcc(%rbp)
movslq -0xcc(%rbp), %rax
cmpq -0x28(%rbp), %rax
jge 0x7698
movq -0x100(%rbp), %rcx
movq 0x68fd(%rip), %rax      # 0xdf70
movq (%rax), %rax
movq -0x10(%rbp), %rdi
movslq -0xcc(%rbp), %rdx
movq (%rcx,%rdx,8), %rsi
callq *%rax
movl -0xcc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xcc(%rbp)
jmp 0x7658
jmp 0x7844
movq -0x30(%rbp), %rax
movq %rsp, %rcx
movq %rcx, -0xd8(%rbp)
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x108(%rbp)
movq %rcx, %rsp
movq %rax, -0xe0(%rbp)
movl $0x0, -0xe4(%rbp)
movslq -0xe4(%rbp), %rax
cmpq -0x30(%rbp), %rax
jge 0x7707
movq -0x108(%rbp), %rax
movslq -0xe4(%rbp), %rcx
movb $0x2a, (%rax,%rcx)
movl -0xe4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xe4(%rbp)
jmp 0x76d7
movq -0x100(%rbp), %rax
cvtsi2sdq (%rax), %xmm0
movsd %xmm0, -0xf0(%rbp)
movl $0x0, -0xf4(%rbp)
movslq -0xf4(%rbp), %rax
cmpq -0x28(%rbp), %rax
jge 0x7783
movq -0x100(%rbp), %rax
movsd -0xf0(%rbp), %xmm1
movslq -0xf4(%rbp), %rcx
cvtsi2sdq (%rax,%rcx,8), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x7770
movq -0x100(%rbp), %rax
movslq -0xf4(%rbp), %rcx
cvtsi2sdq (%rax,%rcx,8), %xmm0
movsd %xmm0, -0xf0(%rbp)
jmp 0x7772
movl -0xf4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xf4(%rbp)
jmp 0x7725
movl $0x0, -0xf8(%rbp)
movslq -0xf8(%rbp), %rax
cmpq -0x28(%rbp), %rax
jge 0x783a
movq -0x100(%rbp), %rax
movq 0x66a4(%rip), %rcx      # 0xde50
movq (%rcx), %rcx
movq %rcx, -0x110(%rbp)
movq -0x10(%rbp), %rcx
movq %rcx, -0x118(%rbp)
movslq -0xf8(%rbp), %rcx
cvtsi2sdq (%rax,%rcx,8), %xmm0
movsd -0xf0(%rbp), %xmm1
divsd %xmm1, %xmm0
cvtsi2sdq -0x30(%rbp), %xmm1
mulsd %xmm1, %xmm0
callq 0x6400
movq -0x118(%rbp), %rdi
movq -0x108(%rbp), %rsi
movq -0x110(%rbp), %rax
movaps %xmm0, %xmm1
cvttsd2si %xmm1, %rdx
movq %rdx, %r8
sarq $0x3f, %r8
movsd 0x27f3(%rip), %xmm1     # 0xa008
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %rcx
andq %r8, %rcx
orq %rcx, %rdx
callq *%rax
movl -0xf8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xf8(%rbp)
jmp 0x778d
movq -0xd8(%rbp), %rax
movq %rax, %rsp
movl $0x0, -0x4(%rbp)
movl $0x1, -0x5c(%rbp)
movq -0x40(%rbp), %rax
movq %rax, %rsp
movl -0x4(%rbp), %eax
movq %rbp, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)