// File: /workspace/github_c_makefile_star2_O1/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 -0x41(%rip), %rsi       # 0x23bc
leaq -0x32(%rip), %rdx       # 0x23d2
movq 0x3bc5(%rip), %rcx      # 0x5fd0
leaq 0x18(%rsp), %rdi
callq 0x15d8
movl $0x1, %ebx
cmpl $0x1, %eax
je 0x2802
xorl %r13d, %r13d
leaq -0x71(%rip), %r14       # 0x23bc
leaq -0x62(%rip), %r15       # 0x23d2
movq 0x3b95(%rip), %r12      # 0x5fd0
leaq (%rsp,%r13), %rdi
addq $0x50, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x15d8
cmpl $0x1, %eax
je 0x2802
addq $0x38, %r13
cmpq $0x7e0, %r13            # imm = 0x7E0
jne 0x243b
xorl %edi, %edi
callq 0x1080
movl %eax, %edi
callq 0x1050
movl $0x3e8, %ebx            # imm = 0x3E8
movl $0x2, %ecx
movl $0x960, %eax            # imm = 0x960
xorl %edx, %edx
idivl %ebx
testl %edx, %edx
jne 0x24a6
movl $0x36ee80, %eax         # imm = 0x36EE80
xorl %edx, %edx
idivl %ebx
testl %edx, %edx
jne 0x24a6
movl $0x61a8, %eax           # imm = 0x61A8
xorl %edx, %edx
idivl %ebx
testl %edx, %edx
je 0x24ca
leal -0x1(%rcx), %esi
movl $0x3e8, %eax            # imm = 0x3E8
xorl %edx, %edx
divq %rcx
incq %rcx
incl %esi
testq %rdx, %rdx
jne 0x24a9
movl $0x3e8, %eax            # imm = 0x3E8
xorl %edx, %edx
idivl %esi
movl %eax, %ebx
jmp 0x247f
leaq 0xb4f(%rip), %rdi       # 0x3020
movsd 0xb2f(%rip), %xmm0      # 0x3008
movb $0x1, %al
callq 0x1040
leaq 0xb57(%rip), %rdi       # 0x303e
movl $0x7, %esi
xorl %eax, %eax
callq 0x1040
leaq 0xb65(%rip), %rdi       # 0x305f
movl $0x24, %esi
xorl %eax, %eax
callq 0x1040
leaq 0xb76(%rip), %rdi       # 0x3083
movl $0x2, %esi
xorl %eax, %eax
callq 0x1040
leaq 0xb92(%rip), %rdi       # 0x30b2
movl $0x3, %esi
xorl %eax, %eax
callq 0x1040
leaq 0xbac(%rip), %rdi       # 0x30df
movl $0x96, %esi
xorl %eax, %eax
callq 0x1040
leaq 0xbbe(%rip), %rdi       # 0x3104
movl $0x5dc, %esi            # imm = 0x5DC
xorl %eax, %eax
callq 0x1040
leaq 0xbd4(%rip), %rdi       # 0x312d
movl $0x61a8, %esi           # imm = 0x61A8
xorl %eax, %eax
callq 0x1040
leaq 0xbf4(%rip), %rdi       # 0x3160
movl $0x1, %esi
xorl %eax, %eax
callq 0x1040
leaq 0xc0b(%rip), %rdi       # 0x318a
movl $0x960, %esi            # imm = 0x960
xorl %eax, %eax
callq 0x1040
leaq 0xc14(%rip), %rdi       # 0x31a6
movl $0x8, %esi
xorl %eax, %eax
callq 0x1040
leaq 0xc2c(%rip), %rdi       # 0x31d1
movl %ebx, %esi
xorl %eax, %eax
callq 0x1040
leaq 0xcbf(%rip), %rdi       # 0x3274
callq 0x1030
callq 0x1070
leaq 0x18(%rsp), %r12
leaq 0xc81(%rip), %r15       # 0x324c
leaq 0x50(%rsp), %r13
movl $0x0, (%rsp)
xorl %r14d, %r14d
imull $0x1dc15275, %r14d, %eax # imm = 0x1DC15275
addl $0x12a00, %eax          # imm = 0x12A00
rorl $0x7, %eax
cmpl $0x4a8, %eax            # imm = 0x4A8
ja 0x2689
movl %r14d, %ebp
movslq %r14d, %rax
imulq $0x4a90be59, %rax, %rsi # imm = 0x4A90BE59
movq %rsi, %rax
shrq $0x3f, %rax
sarq $0x34, %rsi
addl %eax, %esi
leaq 0xbdb(%rip), %rdi       # 0x31f0
xorl %eax, %eax
callq 0x1040
leaq 0xbe8(%rip), %rdi       # 0x320b
movl (%rsp), %esi
xorl %eax, %eax
callq 0x1040
movq %r12, %rdi
callq 0x17cc
leaq 0xbe9(%rip), %rdi       # 0x3225
movl %eax, %esi
xorl %eax, %eax
callq 0x1040
leaq 0xc40(%rip), %rdi       # 0x328c
callq 0x1030
xorl %r14d, %r14d
leaq (%rsp,%r14), %rdi
addq $0x50, %rdi
callq 0x17cc
movq %r15, %rdi
movl %eax, %esi
xorl %eax, %eax
callq 0x1040
addq $0x38, %r14
cmpq $0x7e0, %r14            # imm = 0x7E0
jne 0x2654
leaq 0xb9c(%rip), %rdi       # 0x321d
callq 0x1030
movl %ebp, %r14d
imull $0x962fc963, %r14d, %eax # imm = 0x962FC963
addl $0x1b4e800, %eax        # imm = 0x1B4E800
rorl $0x5, %eax
cmpl $0x1b4e80, %eax         # imm = 0x1B4E80
ja 0x2710
movl (%rsp), %ebp
movl %ebp, 0x8(%rsp)
callq 0x10d0
cltq
imulq $0x38e38e39, %rax, %rcx # imm = 0x38E38E39
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x23, %rcx
addl %edx, %ecx
shll $0x2, %ecx
leal (%rcx,%rcx,8), %ecx
movl %eax, %edx
subl %ecx, %edx
leal 0x1(%rdx), %esi
movl %esi, 0x10(%rsp)
movl $0x61a8, 0x14(%rsp)     # imm = 0x61A8
negl %ecx
addl %ecx, %eax
addl $0x2, %eax
shrl $0x1f, %eax
addl %edx, %eax
addl $0x2, %eax
shrl %eax
imull $0x1f40, %eax, %eax     # imm = 0x1F40
movl %eax, 0xc(%rsp)
movq %r12, %rdi
leaq 0x8(%rsp), %rsi
callq 0x17d6
cmpl $0x1, %eax
je 0x2816
incl %ebp
movl %ebp, (%rsp)
movl %r14d, 0x4(%rsp)
movq %r12, %rdi
xorl %esi, %esi
callq 0x1b55
testq %rax, %rax
je 0x2771
xorl %r15d, %r15d
subl %ebx, 0x4(%rax)
jg 0x275e
movslq 0x8(%rax), %rcx
imulq $0x38, %rcx, %rcx
leaq (%rcx,%r13), %rdi
addq $-0x38, %rdi
movq %rax, %rsi
callq 0x17d6
testl %eax, %eax
jne 0x2816
leaq -0x1(%r15), %r14
movq %r12, %rdi
movq %r15, %rsi
callq 0x1ac3
movq %r14, %r15
incq %r15
movq %r12, %rdi
movq %r15, %rsi
callq 0x1b55
testq %rax, %rax
jne 0x2727
xorl %r14d, %r14d
imulq $0x38, %r14, %rax
leaq (%rsp,%rax), %r15
addq $0x50, %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x1b55
testq %rax, %rax
je 0x27c2
xorl %r13d, %r13d
subl %ebx, 0xc(%rax)
jg 0x27a9
leaq -0x1(%r13), %rbp
movq %r15, %rdi
movq %r13, %rsi
callq 0x1ac3
movq %rbp, %r13
incq %r13
movq %r15, %rdi
movq %r13, %rsi
callq 0x1b55
cmpq $0x2, %r13
ja 0x27c2
testq %rax, %rax
jne 0x2792
incq %r14
cmpq $0x24, %r14
jne 0x2774
movl 0x4(%rsp), %r14d
addl %ebx, %r14d
cmpl $0x240c8401, %r14d      # imm = 0x240C8401
leaq 0xa6b(%rip), %r15       # 0x324c
leaq 0x50(%rsp), %r13
jl 0x25da
leaq 0x18(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x285f
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 0xa34(%rip), %rdi       # 0x3251
callq 0x1030
leaq 0x18(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x285f
movl $0x1, %edi
callq 0x10c0