// File: /workspace/github_c_makefile_star2_O1/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, 0x10(%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 0x10(%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 0x2ee(%rip), %rcx       # 0x1649
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, 0x18(%rsp)
callq 0x1090
movq 0x2c32(%rip), %rdi      # 0x3ff0
callq 0x1770
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 0x18(%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, 0x10(%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 0x1657
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 0x1657
addl %r13d, %r15d
xorl %eax, %eax
movsd 0x10(%rsp), %xmm3
addsd (%rbx), %xmm3
incq %r14
addq $0x20, %rbx
cmpq $0x18, %r14
movsd 0x18(%rsp), %xmm1
jne 0x14a8
incl %ebp
movl %ebp, 0x10(%rsp)
movq $-0x18, %r15
leaq 0xa8(%rsp), %rbx
xorl %r12d, %r12d
xorl %r14d, %r14d
movl $0x100, %ebp            # imm = 0x100
xorpd %xmm0, %xmm0
ucomisd -0x8(%rbx), %xmm0
jae 0x1622
movl %r14d, %eax
shrl %eax
movl $0x92492493, %ecx       # imm = 0x92492493
imulq %rcx, %rax
shrq $0x22, %rax
movl %eax, %ecx
shll $0x4, %ecx
addl %eax, %eax
subl %ecx, %eax
addl %r14d, %eax
movl %r14d, %ecx
imulq $0x24924925, %rcx, %rcx # imm = 0x24924925
shrq $0x20, %rcx
movl %r14d, %edx
subl %ecx, %edx
shrl %edx
addl %ecx, %edx
shrl $0x2, %edx
imull $0x700, %edx, %ecx      # imm = 0x700
movl %r12d, %edx
subl %ecx, %edx
xorl %esi, %esi
cmpl $0x7, %eax
setae %sil
movq 0x29ff(%rip), %r13      # 0x3fd8
movq (%r13), %rdi
shll $0x15, %esi
addl %ebp, %esi
addl %edx, %esi
callq 0x1040
leal 0x59(%r15), %ecx
leal 0x18(%r15), %edi
movl 0x10(%rsp), %esi
leaq 0xa16(%rip), %rdx       # 0x2012
movq %rbx, %r8
xorl %eax, %eax
callq 0x10d0
movq (%r13), %rdi
callq 0x10a0
addq $0x20, %rbx
incl %r14d
addl %ebp, %r12d
incq %r15
jne 0x1577
movq 0x29af(%rip), %rbx      # 0x3fd8
movq (%rbx), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1100
movq (%rbx), %rdi
callq 0x10a0
movl $0x1, %edi
callq 0x1130
jmp 0x163d