// File: /workspace/github_c_makefile_star2_O2/Enes1313[P]DataStructures/Simulation/main.c
// Function: main
int main()
{
	size_t i, j;
	Box box, * pBox;
	eaDSDynamicArray dynamicArray1;
	eaDSDynamicArray dynamicArray2[COUNT_LOCATIONS];
	int counter = 0, product_uniqe_id = 0, interval = SPEED_OF_BOX_ON_CONVEYOR * 1000;

	if (EXIT_FAILURE == eaDSDynamicArrayInit(&dynamicArray1, boxCreateAndCopy, boxCompare, free))
		return EXIT_FAILURE;

	for (i = 0; i < COUNT_LOCATIONS; i++)
		if (EXIT_FAILURE == eaDSDynamicArrayInit(&dynamicArray2[i], boxCreateAndCopy, boxCompare, free))
			return EXIT_FAILURE;

	srand((unsigned int)time(NULL));

	for (i = 2;;)
	{
		if ((NEW_BOX % interval == 0) && (LOG_TIME % interval == 0) && (WAITING_BOX_TO_FINISH % interval == 0))
			break;

		while((SPEED_OF_BOX_ON_CONVEYOR * 1000) % i) i++;
		interval = (SPEED_OF_BOX_ON_CONVEYOR * 1000) / (int)i++;
	}

	printf("Hourly log\t\t\t\t\t: %6.1f hour!\n", (float) HOURLY_LOG);
	printf("Simulation days\t\t\t\t\t: %6d days!\n", SIMULATION_DAYS);
	printf("Count locations\t\t\t\t\t: %6d targets!\n", COUNT_LOCATIONS);
	printf("Count locations on same line\t\t\t: %6d targets!\n", COUNT_LOCATIONS_SAME_LINE);
	printf("Workers per same locations\t\t\t: %6d workers!\n", WORKERS_PER_SAME_LOCATIONS);
	printf("Length of conveyor\t\t\t\t: %6d meters!\n", LENGTH_OF_CONVEYOR);
	printf("Hourly performence\t\t\t\t: %6d boxes/hour!\n", HOURLY_PERFORMENCE);
	printf("Last waiting box to finish on location\t\t: %6d ms!\n", WAITING_BOX_TO_FINISH);
	printf("Speed of box on conveyor\t\t\t: %6d meter!\n\n", SPEED_OF_BOX_ON_CONVEYOR);

	printf("New box\t\t\t\t\t\t: %6d per ms!\n", NEW_BOX);
	printf("Distance between two locations\t\t\t: %6d m!\n", DISTANCE_BETWEEN_TWO_LOCATIONS_IN_DIFFERENT_LINE);
	printf("Interval value\t\t\t\t\t: %6d ms!\n\n", interval);

	printf("Press enter to start!\n\n\n");
	getchar();

	for (counter = 0; counter <= TOTAL_SIMULATION_TIME; counter += interval)
	{
		/* logging */
		if (counter % LOG_TIME == 0)
		{
			printf("Passed time\t\t: %15d hour!\n", counter / 1000 / 3600);
			printf("All boxes\t\t: %15d boxes!\n", product_uniqe_id);
			printf("Total boxes in conveyor\t: %15d boxes!\n", (int) eaDSDynamicArrayGetCount(&dynamicArray1));
			printf("Total boxes on locations\t:\n");

			for (i = 0; i < COUNT_LOCATIONS; ++i)
				printf("%4d ", (int) eaDSDynamicArrayGetCount(&dynamicArray2[i]));

			printf("boxes!\n\n");
		}

		/* new box */
		if (counter % NEW_BOX == 0)
		{
			box.id = product_uniqe_id++;
			box.location = rand() % COUNT_LOCATIONS + 1;
			box.waitingOnLocation = WAITING_BOX_TO_FINISH;
			box.way = ((box.location + COUNT_LOCATIONS_SAME_LINE - 1) / COUNT_LOCATIONS_SAME_LINE) * DISTANCE_BETWEEN_TWO_LOCATIONS_IN_DIFFERENT_LINE * 1000; /* mm */

			if (EXIT_FAILURE == eaDSDynamicArrayAdd(&dynamicArray1, &box))
			{
				debugFreeExit("Dinamik dizi'ye eleman eklenemedi!", &dynamicArray1, dynamicArray2);
			}
		}

		/* moving */
		for(i = 0; NULL != (pBox = (Box *) eaDSDynamicArrayGetAddressFrom(&dynamicArray1, i)); i++)
		{
			if ((pBox->way -= SPEED_OF_BOX_ON_CONVEYOR * interval) <= 0) /* ... / 1000 * 1000  = ... / 1 */
			{
				if(EXIT_SUCCESS == eaDSDynamicArrayAdd(&dynamicArray2[pBox->location - 1], pBox))
				{
					eaDSDynamicArrayRemoveAtCopyLastItem(&dynamicArray1, i--);
				}
				else
				{
					debugFreeExit("Dinamik dizi'ye eleman eklenemedi!", &dynamicArray1, dynamicArray2);
				}
			}
		}

		/* controlling */
		for (i = 0; i < COUNT_LOCATIONS; i++)
		{
			for(j = 0; (NULL != (pBox = (Box *) eaDSDynamicArrayGetAddressFrom(&dynamicArray2[i], j))) && (j < WORKERS_PER_SAME_LOCATIONS); j++)
			{
				if ((pBox->waitingOnLocation -= interval) <= 0)
				{
					eaDSDynamicArrayRemoveAtCopyLastItem(&dynamicArray2[i], j--);
				}
			}
		}
	}

	/* free memory */
	freeMemory(&dynamicArray1, dynamicArray2);

	/* wait for enter to be pressed */
	getchar();

	return EXIT_SUCCESS;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x838, %rsp            # imm = 0x838
leaq -0x3f(%rip), %rsi       # 0x2330
leaq -0x32(%rip), %rdx       # 0x2344
movq 0x3c53(%rip), %rcx      # 0x5fd0
leaq 0x18(%rsp), %rdi
callq 0x1540
pushq $0x1
popq %rbx
cmpl $0x1, %eax
je 0x270c
xorl %ecx, %ecx
leaq -0x6c(%rip), %r14       # 0x2330
leaq -0x5f(%rip), %r15       # 0x2344
movq 0x3c26(%rip), %r12      # 0x5fd0
leaq 0x38(%rcx), %r13
cmpq $0x818, %r13            # imm = 0x818
je 0x23da
leaq (%rsp,%rcx), %rdi
addq $0x50, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x1540
movq %r13, %rcx
cmpl $0x1, %eax
jne 0x23aa
jmp 0x270c
xorl %edi, %edi
callq 0x1080
movl %eax, %edi
callq 0x1050
movl $0x3e8, %ebx            # imm = 0x3E8
pushq $0x2
popq %rcx
movw $0x960, %ax             # imm = 0x960
xorl %edx, %edx
idivw %bx
testw %dx, %dx
jne 0x2419
movl $0x36ee80, %eax         # imm = 0x36EE80
xorl %edx, %edx
idivl %ebx
testl %edx, %edx
jne 0x2419
movw $0x61a8, %ax            # imm = 0x61A8
xorl %edx, %edx
idivw %bx
testw %dx, %dx
je 0x243d
leal -0x1(%rcx), %esi
movl $0x3e8, %eax            # imm = 0x3E8
xorl %edx, %edx
divq %rcx
incq %rcx
incl %esi
testq %rdx, %rdx
jne 0x241c
movl $0x3e8, %eax            # imm = 0x3E8
xorl %edx, %edx
idivl %esi
movl %eax, %ebx
jmp 0x23f0
leaq 0xbdc(%rip), %rdi       # 0x3020
movsd 0xbbc(%rip), %xmm0      # 0x3008
movb $0x1, %al
callq 0x1040
leaq 0xbe4(%rip), %rdi       # 0x303e
pushq $0x7
popq %rsi
xorl %eax, %eax
callq 0x1040
leaq 0xbf4(%rip), %rdi       # 0x305f
pushq $0x24
popq %rsi
xorl %eax, %eax
callq 0x1040
leaq 0xc07(%rip), %rdi       # 0x3083
pushq $0x2
popq %rsi
xorl %eax, %eax
callq 0x1040
leaq 0xc25(%rip), %rdi       # 0x30b2
pushq $0x3
popq %rsi
xorl %eax, %eax
callq 0x1040
leaq 0xc41(%rip), %rdi       # 0x30df
movl $0x96, %esi
xorl %eax, %eax
callq 0x1040
leaq 0xc53(%rip), %rdi       # 0x3104
movl $0x5dc, %esi            # imm = 0x5DC
xorl %eax, %eax
callq 0x1040
leaq 0xc69(%rip), %rdi       # 0x312d
movl $0x61a8, %esi           # imm = 0x61A8
xorl %eax, %eax
callq 0x1040
leaq 0xc89(%rip), %rdi       # 0x3160
pushq $0x1
popq %rsi
xorl %eax, %eax
callq 0x1040
leaq 0xca2(%rip), %rdi       # 0x318a
movl $0x960, %esi            # imm = 0x960
xorl %eax, %eax
callq 0x1040
leaq 0xcab(%rip), %rdi       # 0x31a6
pushq $0x8
popq %rsi
xorl %eax, %eax
callq 0x1040
leaq 0xcc5(%rip), %rdi       # 0x31d1
movl %ebx, %esi
xorl %eax, %eax
callq 0x1040
leaq 0xd58(%rip), %rdi       # 0x3274
callq 0x1030
callq 0x1070
leaq 0x18(%rsp), %r12
leaq 0xd1a(%rip), %r15       # 0x324c
xorl %ebp, %ebp
xorl %r13d, %r13d
cmpl $0x240c8400, %ebp       # imm = 0x240C8400
jg 0x26f6
movl %ebp, %eax
cltd
movl $0x36ee80, %ecx         # imm = 0x36EE80
idivl %ecx
testl %edx, %edx
jne 0x25cd
leaq 0xc98(%rip), %rdi       # 0x31f0
movl %eax, %esi
xorl %eax, %eax
callq 0x1040
leaq 0xca3(%rip), %rdi       # 0x320b
movl %r13d, %esi
xorl %eax, %eax
callq 0x1040
movq %r12, %rdi
callq 0x1751
leaq 0xca4(%rip), %rdi       # 0x3225
movl %eax, %esi
xorl %eax, %eax
callq 0x1040
leaq 0xcfb(%rip), %rdi       # 0x328c
callq 0x1030
xorl %r14d, %r14d
cmpq $0x7e0, %r14            # imm = 0x7E0
je 0x25c1
leaq (%rsp,%r14), %rdi
addq $0x50, %rdi
callq 0x1751
movq %r15, %rdi
movl %eax, %esi
xorl %eax, %eax
callq 0x1040
addq $0x38, %r14
jmp 0x2599
leaq 0xc55(%rip), %rdi       # 0x321d
callq 0x1030
movl %ebp, %eax
cltd
movl $0x960, %ecx            # imm = 0x960
idivl %ecx
testl %edx, %edx
movl %ebp, 0x4(%rsp)
je 0x25ea
movl %r13d, (%rsp)
leaq 0x50(%rsp), %r15
jmp 0x2642
movl %r13d, 0x8(%rsp)
callq 0x10d0
cltd
pushq $0x24
popq %rcx
idivl %ecx
leal 0x1(%rdx), %eax
movl %eax, 0x10(%rsp)
movl $0x61a8, 0x14(%rsp)     # imm = 0x61A8
addb $0x2, %dl
movsbl %dl, %eax
movb $0x2, %cl
idivb %cl
movsbl %al, %eax
imull $0x1f40, %eax, %eax     # imm = 0x1F40
movl %eax, 0xc(%rsp)
movq %r12, %rdi
leaq 0x8(%rsp), %rsi
callq 0x175b
cmpl $0x1, %eax
leaq 0x50(%rsp), %r15
je 0x2720
incl %r13d
movl %r13d, (%rsp)
xorl %r13d, %r13d
movq %r12, %rdi
movq %r13, %rsi
callq 0x1ac4
testq %rax, %rax
je 0x2691
subl %ebx, 0x4(%rax)
jg 0x268c
movslq 0x8(%rax), %rcx
imulq $0x38, %rcx, %rcx
leaq (%rcx,%r15), %rdi
addq $-0x38, %rdi
movq %rax, %rsi
callq 0x175b
testl %eax, %eax
jne 0x2720
leaq -0x1(%r13), %r14
movq %r12, %rdi
movq %r13, %rsi
callq 0x1a32
movq %r14, %r13
incq %r13
jmp 0x2645
xorl %r15d, %r15d
cmpq $0x24, %r15
je 0x26e0
imulq $0x38, %r15, %rax
leaq (%rsp,%rax), %r13
addq $0x50, %r13
xorl %r14d, %r14d
movq %r13, %rdi
movq %r14, %rsi
callq 0x1ac4
cmpq $0x2, %r14
ja 0x26db
testq %rax, %rax
je 0x26db
subl %ebx, 0xc(%rax)
jg 0x26d6
leaq -0x1(%r14), %rbp
movq %r13, %rdi
movq %r14, %rsi
callq 0x1a32
movq %rbp, %r14
incq %r14
jmp 0x26a9
incq %r15
jmp 0x2694
movl 0x4(%rsp), %ebp
addl %ebx, %ebp
leaq 0xb5f(%rip), %r15       # 0x324c
movl (%rsp), %r13d
jmp 0x2537
leaq 0x18(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x2758
callq 0x1070
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x838, %rsp            # imm = 0x838
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xb2a(%rip), %rdi       # 0x3251
leaq 0x18(%rsp), %rsi
leaq 0x50(%rsp), %rdx
callq 0x2736