// File: /workspace/github_c_makefile_star2_O3/kobolt[P]curses-collection/storage/storage.c
// Function: main
int main(int argc, char *argv[])
{
  int i, max_y, max_x, start_y, start_x, size_y, size_x, horizontal;
  double sum, share, used, value;
  char line[128], *p;
  data_t data[DATA_MAX];

  for (i = 0; i < DATA_MAX; i++) {
    data[i].value = 0.0;
    data[i].text[0] = '\0';
  }
  strncpy(data[DATA_MAX - 1].text, "*OTHER*", TEXT_MAX);

  i = 0;
  while (fgets(line, sizeof(line), stdin) != NULL) {
    p = strtok(line, DATA_DELIMITER);
    if (p == NULL)
      continue;

    value = atof(p);

    p = strtok(NULL, DATA_DELIMITER);
    if (p == NULL)
      continue;

    if (i >= (DATA_MAX - 1)) {
      data[DATA_MAX - 1].value += value;
    } else {
      data[i].value = value;
      strncpy(data[i].text, p, TEXT_MAX);
      data[i].text[TEXT_MAX] = '\0';
      i++;
    }
  }

  /* Sort to get largest value first. */
  qsort(data, DATA_MAX, sizeof(data_t), data_compare);

  sum = 0.0;
  for (i = 0; i < DATA_MAX; i++) {
    if (data[i].value <= 0.0)
      break;
    sum += data[i].value;
  }

  if (sum <= 0.0) {
    return 1; /* Will divide by zero, abort. */
  }

  screen_init(&max_y, &max_x);
  max_x -= TEXT_X_AREA;

  used = 0.0;
  horizontal = start_y = start_x = 0;
  size_y = max_y;
  size_x = max_x;
  for (i = 0; i < DATA_MAX; i++) {
    if (data[i].value <= 0.0)
      break;

    share = data[i].value / (sum - used);

    if (horizontal) {
      size_x = max_x - start_x;
      size_y = share * (double)(max_y - start_y);
      box_draw(i, start_y, start_x, size_y, size_x);
      start_y += size_y;
      horizontal = 0;
    } else {
      size_y = max_y - start_y;
      size_x = share * (double)(max_x - start_x);
      box_draw(i, start_y, start_x, size_y, size_x);
      start_x += size_x;
      horizontal = 1;
    }

    used += data[i].value;
  }

  for (i = 0; i < DATA_MAX; i++) {
    if (data[i].value <= 0.0)
      break;
    text_draw(i, data[i].text, i, max_x + 1);
  }

  move(0,0);
  refresh();
  while (1) {
    sleep(1); /* Ctrl-C to quit! */
  }

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3a8, %rsp            # imm = 0x3A8
movl $0x8, %eax
movq $0x0, 0x98(%rsp,%rax)
movb $0x0, 0xa0(%rsp,%rax)
addq $0x20, %rax
cmpq $0x308, %rax            # imm = 0x308
jne 0x1252
movupd 0xd9e(%rip), %xmm0      # 0x2018
movupd %xmm0, 0x388(%rsp)
movb $0x0, 0x398(%rsp)
movq 0x2d3e(%rip), %r12      # 0x3fd0
movq (%r12), %rdx
leaq 0x20(%rsp), %rdi
movl $0x80, %esi
callq 0x10c0
testq %rax, %rax
je 0x1354
xorl %r13d, %r13d
leaq 0xd58(%rip), %rbx       # 0x2010
leaq 0x20(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x10f0
testq %rax, %rax
je 0x12ea
movq %rax, %rdi
callq 0x1070
movsd %xmm0, 0x18(%rsp)
xorl %edi, %edi
movq %rbx, %rsi
callq 0x10f0
testq %rax, %rax
jne 0x1302
movq (%r12), %rdx
movq %r14, %rdi
movl $0x80, %esi
callq 0x10c0
testq %rax, %rax
jne 0x12bd
jmp 0x1354
movsd 0x18(%rsp), %xmm0
cmpl $0x17, %r13d
jl 0x1322
addsd 0x380(%rsp), %xmm0
movsd %xmm0, 0x380(%rsp)
jmp 0x12ea
movslq %r13d, %rcx
shlq $0x5, %rcx
leaq (%rsp,%rcx), %r15
addq $0xa8, %r15
movsd %xmm0, -0x8(%r15)
movl $0x11, %edx
movq %r15, %rdi
movq %rax, %rsi
callq 0x1050
movb $0x0, 0x11(%r15)
incl %r13d
jmp 0x12ea
leaq 0x2d0(%rip), %rcx       # 0x162b
leaq 0xa0(%rsp), %rbx
movl $0x18, %esi
movl $0x20, %edx
movq %rbx, %rdi
callq 0x1060
xorpd %xmm0, %xmm0
xorl %eax, %eax
xorpd %xmm2, %xmm2
movsd 0xa0(%rsp,%rax), %xmm1
ucomisd %xmm1, %xmm0
jae 0x139e
addsd %xmm1, %xmm2
addq $0x20, %rax
cmpq $0x300, %rax            # imm = 0x300
jne 0x137f
xorpd %xmm0, %xmm0
ucomisd %xmm2, %xmm0
jae 0x146f
movsd %xmm2, 0x10(%rsp)
callq 0x1090
movq 0x2c32(%rip), %rdi      # 0x3ff0
callq 0x1750
callq 0x1030
testb %al, %al
je 0x1448
callq 0x10b0
movl $0x1, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x10e0
movl $0x2, %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x10e0
movl $0x3, %edi
movl $0x4, %esi
xorl %edx, %edx
callq 0x10e0
movl $0x4, %edi
movl $0x3, %esi
xorl %edx, %edx
callq 0x10e0
movl $0x5, %edi
movl $0x5, %esi
xorl %edx, %edx
callq 0x10e0
movl $0x6, %edi
movl $0x6, %esi
xorl %edx, %edx
callq 0x10e0
movl $0x7, %edi
movl $0x7, %esi
xorl %edx, %edx
callq 0x10e0
callq 0x1080
movq 0x2b84(%rip), %rax      # 0x3fd8
movq (%rax), %rax
testq %rax, %rax
je 0x1486
movswl 0x4(%rax), %ecx
incl %ecx
movl %ecx, 0xc(%rsp)
movswl 0x6(%rax), %ebp
addl $-0x13, %ebp
jmp 0x1493
movl $0x1, %eax
addq $0x3a8, %rsp            # imm = 0x3A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xffffffeb, %ebp       # imm = 0xFFFFFFEB
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
movsd 0x10(%rsp), %xmm1
xorl %r14d, %r14d
xorpd %xmm3, %xmm3
xorl %r15d, %r15d
xorl %eax, %eax
xorl %r12d, %r12d
movsd (%rbx), %xmm0
xorpd %xmm2, %xmm2
ucomisd %xmm0, %xmm2
jae 0x1557
movsd %xmm3, 0x18(%rsp)
subsd %xmm3, %xmm1
divsd %xmm1, %xmm0
testl %eax, %eax
jne 0x1503
movl 0xc(%rsp), %ecx
movl %ebp, %eax
subl %r12d, %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
subl %r15d, %ecx
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %r13d
movl %r14d, %edi
movl %r15d, %esi
movl %r12d, %edx
movl %r13d, %r8d
callq 0x1639
addl %r13d, %r12d
movl $0x1, %eax
jmp 0x1536
movl %ebp, %r8d
movl 0xc(%rsp), %eax
subl %r15d, %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
subl %r12d, %r8d
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %r13d
movl %r14d, %edi
movl %r15d, %esi
movl %r12d, %edx
movl %r13d, %ecx
callq 0x1639
addl %r13d, %r15d
xorl %eax, %eax
movsd 0x18(%rsp), %xmm3
addsd (%rbx), %xmm3
incq %r14
addq $0x20, %rbx
cmpq $0x18, %r14
movsd 0x10(%rsp), %xmm1
jne 0x14a8
incl %ebp
movl $0x200100, %r12d        # imm = 0x200100
leaq 0xa8(%rsp), %rbx
movl $0x100, %r14d           # imm = 0x100
xorl %r15d, %r15d
movq 0x2a61(%rip), %r13      # 0x3fd8
xorpd %xmm0, %xmm0
ucomisd -0x8(%rbx), %xmm0
movq (%r13), %rdi
jae 0x160d
movl %r15d, %eax
imulq $0x24924925, %rax, %rax # imm = 0x24924925
shrq $0x20, %rax
movl %r15d, %ecx
subl %eax, %ecx
shrl %ecx
addl %eax, %ecx
shrl $0x2, %ecx
imull $0x700, %ecx, %eax      # imm = 0x700
movl %r12d, %ecx
subl %eax, %ecx
movl %r14d, %esi
subl %eax, %esi
movq %r15, %rax
subq $0xe, %rax
cmovbl %r15d, %eax
cmpl $0x7, %eax
cmovgel %ecx, %esi
callq 0x1040
leal 0x41(%r15), %ecx
movl %r15d, %edi
movl %ebp, %esi
leaq 0xa38(%rip), %rdx       # 0x2012
movq %rbx, %r8
xorl %eax, %eax
callq 0x10d0
movq (%r13), %rdi
callq 0x10a0
incq %r15
movl $0x100, %eax            # imm = 0x100
addl %eax, %r12d
addl %eax, %r14d
addq $0x20, %rbx
cmpq $0x18, %r15
jne 0x1577
movq (%r13), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1100
movq (%r13), %rdi
callq 0x10a0
movl $0x1, %edi
callq 0x1130
jmp 0x161f