// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x8
popq %rax
cmpq $0x308, %rax            # imm = 0x308
je 0x1269
andq $0x0, 0x18(%rsp,%rax)
movb $0x0, 0x20(%rsp,%rax)
addq $0x20, %rax
jmp 0x1250
movupd 0xda7(%rip), %xmm0      # 0x2018
movupd %xmm0, 0x308(%rsp)
movb $0x0, 0x318(%rsp)
xorl %r13d, %r13d
movq 0x2d44(%rip), %rbp      # 0x3fd0
leaq 0x320(%rsp), %rbx
leaq 0xd75(%rip), %r14       # 0x2010
pushq $0x11
popq %r15
movq (%rbp), %rdx
movq %rbx, %rdi
movl $0x80, %esi
callq 0x10c0
testq %rax, %rax
je 0x1334
movq %rbx, %rdi
movq %r14, %rsi
callq 0x10f0
testq %rax, %rax
je 0x129f
movq %rax, %rdi
callq 0x1070
movsd %xmm0, 0x18(%rsp)
xorl %edi, %edi
movq %r14, %rsi
callq 0x10f0
testq %rax, %rax
je 0x129f
movsd 0x18(%rsp), %xmm0
cmpl $0x17, %r13d
jb 0x1302
addsd 0x300(%rsp), %xmm0
movsd %xmm0, 0x300(%rsp)
jmp 0x129f
movl %r13d, %ecx
shlq $0x5, %rcx
leaq (%rsp,%rcx), %r12
addq $0x28, %r12
movsd %xmm0, -0x8(%r12)
movq %r12, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x1050
movb $0x0, 0x11(%r12)
incl %r13d
jmp 0x129f
leaq 0x295(%rip), %rcx       # 0x15d0
leaq 0x20(%rsp), %rbx
pushq $0x18
popq %rsi
pushq $0x20
popq %rdx
movq %rbx, %rdi
callq 0x1060
xorpd %xmm0, %xmm0
xorl %eax, %eax
xorpd %xmm2, %xmm2
cmpq $0x300, %rax            # imm = 0x300
je 0x1376
movsd 0x20(%rsp,%rax), %xmm1
ucomisd %xmm1, %xmm0
jae 0x1376
addsd %xmm1, %xmm2
addq $0x20, %rax
jmp 0x1358
xorpd %xmm0, %xmm0
ucomisd %xmm2, %xmm0
jae 0x142b
movsd %xmm2, 0x10(%rsp)
callq 0x1090
movq 0x2c5a(%rip), %rdi      # 0x3ff0
callq 0x16c0
callq 0x1030
testb %al, %al
je 0x1404
callq 0x10b0
pushq $0x1
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x10e0
pushq $0x2
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x10e0
pushq $0x3
popq %rbp
pushq $0x4
popq %r14
movl %ebp, %edi
movl %r14d, %esi
xorl %edx, %edx
callq 0x10e0
movl %r14d, %edi
movl %ebp, %esi
xorl %edx, %edx
callq 0x10e0
pushq $0x5
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x10e0
pushq $0x6
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x10e0
pushq $0x7
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x10e0
callq 0x1080
movq 0x2bc8(%rip), %rax      # 0x3fd8
movq (%rax), %rax
testq %rax, %rax
je 0x1440
movswl 0x4(%rax), %ecx
incl %ecx
movl %ecx, 0xc(%rsp)
movswl 0x6(%rax), %ebp
addl $-0x13, %ebp
jmp 0x144a
pushq $0x1
popq %rax
addq $0x3a8, %rsp            # imm = 0x3A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $-0x15
popq %rbp
pushq $-0x1
popq %rax
movl %eax, 0xc(%rsp)
movsd 0x10(%rsp), %xmm1
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %eax, %eax
xorl %r12d, %r12d
xorpd %xmm3, %xmm3
cmpq $0x18, %r14
je 0x150b
movsd (%rbx), %xmm0
xorpd %xmm2, %xmm2
ucomisd %xmm0, %xmm2
jae 0x150b
movsd %xmm3, 0x18(%rsp)
subsd %xmm3, %xmm1
divsd %xmm1, %xmm0
testl %eax, %eax
jne 0x14bf
movl 0xc(%rsp), %ecx
movl %ebp, %eax
subl %r15d, %eax
cvtsi2sd %eax, %xmm1
subl %r12d, %ecx
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %r13d
movl %r14d, %edi
movl %r12d, %esi
movl %r15d, %edx
movl %r13d, %r8d
callq 0x15de
addl %r13d, %r15d
pushq $0x1
popq %rax
jmp 0x14ef
movl %ebp, %r8d
movl 0xc(%rsp), %eax
subl %r12d, %eax
cvtsi2sd %eax, %xmm1
subl %r15d, %r8d
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %r13d
movl %r14d, %edi
movl %r12d, %esi
movl %r15d, %edx
movl %r13d, %ecx
callq 0x15de
addl %r13d, %r12d
xorl %eax, %eax
movsd 0x18(%rsp), %xmm3
addsd (%rbx), %xmm3
incq %r14
addq $0x20, %rbx
movsd 0x10(%rsp), %xmm1
jmp 0x145f
incl %ebp
movl $0x200100, %r12d        # imm = 0x200100
leaq 0x28(%rsp), %rbx
movl $0x100, %r14d           # imm = 0x100
xorl %r15d, %r15d
movl %r15d, %eax
xorl %edx, %edx
pushq $0x7
popq %rcx
divl %ecx
cmpq $0x18, %r15
je 0x15a7
xorpd %xmm0, %xmm0
ucomisd -0x8(%rbx), %xmm0
movq 0x2a97(%rip), %r13      # 0x3fd8
movq (%r13), %rdi
jae 0x15b2
imull $0x700, %eax, %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 0xa95(%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
jmp 0x1521
movq 0x2a2a(%rip), %r13      # 0x3fd8
movq (%r13), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1100
movq (%r13), %rdi
callq 0x10a0
pushq $0x1
popq %rbx
movl %ebx, %edi
callq 0x1130
jmp 0x15c7