// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x860, %rsp            # imm = 0x860
movl $0x0, -0x4(%rbp)
movl $0x0, -0x854(%rbp)
movl $0x0, -0x858(%rbp)
movl $0x3e8, -0x85c(%rbp)    # imm = 0x3E8
leaq -0x68(%rbp), %rdi
leaq -0xcb(%rip), %rsi       # 0x3960
leaq -0x92(%rip), %rdx       # 0x39a0
movq 0x3597(%rip), %rcx      # 0x6fd0
callq 0x1a50
movl %eax, %ecx
movl $0x1, %eax
cmpl %ecx, %eax
jne 0x3a55
movl $0x1, -0x4(%rbp)
jmp 0x3f5a
movq $0x0, -0x10(%rbp)
cmpq $0x24, -0x10(%rbp)
jae 0x3ab4
leaq -0x850(%rbp), %rdi
imulq $0x38, -0x10(%rbp), %rax
addq %rax, %rdi
leaq -0x11a(%rip), %rsi      # 0x3960
leaq -0xe1(%rip), %rdx       # 0x39a0
movq 0x3548(%rip), %rcx      # 0x6fd0
callq 0x1a50
movl %eax, %ecx
movl $0x1, %eax
cmpl %ecx, %eax
jne 0x3aa4
movl $0x1, -0x4(%rbp)
jmp 0x3f5a
jmp 0x3aa6
movq -0x10(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x10(%rbp)
jmp 0x3a5d
xorl %eax, %eax
movl %eax, %edi
callq 0x1080
movl %eax, %edi
callq 0x1050
movq $0x2, -0x10(%rbp)
movl $0x960, %eax            # imm = 0x960
cltd
idivl -0x85c(%rbp)
cmpl $0x0, %edx
jne 0x3b01
movl $0x36ee80, %eax         # imm = 0x36EE80
cltd
idivl -0x85c(%rbp)
cmpl $0x0, %edx
jne 0x3b01
movl $0x61a8, %eax           # imm = 0x61A8
cltd
idivl -0x85c(%rbp)
cmpl $0x0, %edx
jne 0x3b01
jmp 0x3b45
jmp 0x3b03
movl $0x3e8, %eax            # imm = 0x3E8
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x10(%rbp)
cmpq $0x0, %rdx
je 0x3b24
movq -0x10(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x10(%rbp)
jmp 0x3b03
movq -0x10(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x10(%rbp)
movl %eax, %ecx
movl $0x3e8, %eax            # imm = 0x3E8
cltd
idivl %ecx
movl %eax, -0x85c(%rbp)
jmp 0x3acc
leaq 0x14d4(%rip), %rdi      # 0x5020
movsd 0x14b4(%rip), %xmm0     # 0x5008
movb $0x1, %al
callq 0x1040
leaq 0x14dc(%rip), %rdi      # 0x503e
movl $0x7, %esi
movb $0x0, %al
callq 0x1040
leaq 0x14ea(%rip), %rdi      # 0x505f
movl $0x24, %esi
movb $0x0, %al
callq 0x1040
leaq 0x14fb(%rip), %rdi      # 0x5083
movl $0x2, %esi
movb $0x0, %al
callq 0x1040
leaq 0x1517(%rip), %rdi      # 0x50b2
movl $0x3, %esi
movb $0x0, %al
callq 0x1040
leaq 0x1531(%rip), %rdi      # 0x50df
movl $0x96, %esi
movb $0x0, %al
callq 0x1040
leaq 0x1543(%rip), %rdi      # 0x5104
movl $0x5dc, %esi            # imm = 0x5DC
movb $0x0, %al
callq 0x1040
leaq 0x1559(%rip), %rdi      # 0x512d
movl $0x61a8, %esi           # imm = 0x61A8
movb $0x0, %al
callq 0x1040
leaq 0x1579(%rip), %rdi      # 0x5160
movl $0x1, %esi
movb $0x0, %al
callq 0x1040
leaq 0x1590(%rip), %rdi      # 0x518a
movl $0x960, %esi            # imm = 0x960
movb $0x0, %al
callq 0x1040
leaq 0x1599(%rip), %rdi      # 0x51a6
movl $0x8, %esi
movb $0x0, %al
callq 0x1040
movl -0x85c(%rbp), %esi
leaq 0x15ab(%rip), %rdi      # 0x51d1
movb $0x0, %al
callq 0x1040
leaq 0x15bc(%rip), %rdi      # 0x51f0
movb $0x0, %al
callq 0x1040
callq 0x1070
movl $0x0, -0x854(%rbp)
cmpl $0x240c8400, -0x854(%rbp) # imm = 0x240C8400
jg 0x3f3e
movl -0x854(%rbp), %eax
movl $0x36ee80, %ecx         # imm = 0x36EE80
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x3d21
movl -0x854(%rbp), %eax
movl $0x3e8, %ecx            # imm = 0x3E8
cltd
idivl %ecx
movl $0xe10, %ecx            # imm = 0xE10
cltd
idivl %ecx
movl %eax, %esi
leaq 0x1579(%rip), %rdi      # 0x5209
movb $0x0, %al
callq 0x1040
movl -0x858(%rbp), %esi
leaq 0x1580(%rip), %rdi      # 0x5224
movb $0x0, %al
callq 0x1040
leaq -0x68(%rbp), %rdi
callq 0x1eb0
movl %eax, %esi
leaq 0x1581(%rip), %rdi      # 0x523e
movb $0x0, %al
callq 0x1040
leaq 0x159a(%rip), %rdi      # 0x5265
movb $0x0, %al
callq 0x1040
movq $0x0, -0x10(%rbp)
cmpq $0x24, -0x10(%rbp)
jae 0x3d13
leaq -0x850(%rbp), %rdi
imulq $0x38, -0x10(%rbp), %rax
addq %rax, %rdi
callq 0x1eb0
movl %eax, %esi
leaq 0x1583(%rip), %rdi      # 0x5281
movb $0x0, %al
callq 0x1040
movq -0x10(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x10(%rbp)
jmp 0x3cda
leaq 0x156c(%rip), %rdi      # 0x5286
movb $0x0, %al
callq 0x1040
movl -0x854(%rbp), %eax
movl $0x960, %ecx            # imm = 0x960
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x3db0
movl -0x858(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x858(%rbp)
movl %eax, -0x28(%rbp)
callq 0x10d0
movl $0x24, %ecx
cltd
idivl %ecx
addl $0x1, %edx
movl %edx, -0x20(%rbp)
movl $0x61a8, -0x1c(%rbp)    # imm = 0x61A8
movl -0x20(%rbp), %eax
addl $0x2, %eax
subl $0x1, %eax
movl $0x2, %ecx
cltd
idivl %ecx
shll $0x3, %eax
imull $0x3e8, %eax, %eax      # imm = 0x3E8
movl %eax, -0x24(%rbp)
leaq -0x68(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x1ef0
movl %eax, %ecx
movl $0x1, %eax
cmpl %ecx, %eax
jne 0x3dae
leaq -0x850(%rbp), %rdx
leaq 0x14ea(%rip), %rdi      # 0x528f
leaq -0x68(%rbp), %rsi
callq 0x3f70
jmp 0x3db0
movq $0x0, -0x10(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x68(%rbp), %rdi
callq 0x2700
movq %rax, %rcx
movq %rcx, -0x30(%rbp)
xorl %eax, %eax
cmpq %rcx, %rax
je 0x3e62
movl -0x85c(%rbp), %edx
shll $0x0, %edx
movq -0x30(%rbp), %rcx
movl 0x4(%rcx), %eax
subl %edx, %eax
movl %eax, 0x4(%rcx)
cmpl $0x0, %eax
jg 0x3e4f
movq -0x30(%rbp), %rax
movl 0x8(%rax), %eax
subl $0x1, %eax
cltq
leaq -0x850(%rbp), %rdi
imulq $0x38, %rax, %rax
addq %rax, %rdi
movq -0x30(%rbp), %rsi
callq 0x1ef0
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x3e36
movq -0x10(%rbp), %rsi
movq %rsi, %rax
addq $-0x1, %rax
movq %rax, -0x10(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x2560
jmp 0x3e4d
leaq -0x850(%rbp), %rdx
leaq 0x144b(%rip), %rdi      # 0x528f
leaq -0x68(%rbp), %rsi
callq 0x3f70
jmp 0x3e4f
jmp 0x3e51
movq -0x10(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x10(%rbp)
jmp 0x3db8
movq $0x0, -0x10(%rbp)
cmpq $0x24, -0x10(%rbp)
jae 0x3f25
movq $0x0, -0x18(%rbp)
leaq -0x850(%rbp), %rdi
imulq $0x38, -0x10(%rbp), %rax
addq %rax, %rdi
movq -0x18(%rbp), %rsi
callq 0x2700
movq %rax, %rdx
movq %rdx, -0x30(%rbp)
xorl %eax, %eax
xorl %ecx, %ecx
cmpq %rdx, %rcx
movb %al, -0x85d(%rbp)
je 0x3eb9
cmpq $0x3, -0x18(%rbp)
setb %al
movb %al, -0x85d(%rbp)
movb -0x85d(%rbp), %al
testb $0x1, %al
jne 0x3ec5
jmp 0x3f12
movl -0x85c(%rbp), %edx
movq -0x30(%rbp), %rcx
movl 0xc(%rcx), %eax
subl %edx, %eax
movl %eax, 0xc(%rcx)
cmpl $0x0, %eax
jg 0x3eff
leaq -0x850(%rbp), %rdi
imulq $0x38, -0x10(%rbp), %rax
addq %rax, %rdi
movq -0x18(%rbp), %rsi
movq %rsi, %rax
addq $-0x1, %rax
movq %rax, -0x18(%rbp)
callq 0x2560
jmp 0x3f01
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x3e7d
jmp 0x3f14
movq -0x10(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x10(%rbp)
jmp 0x3e6a
jmp 0x3f27
movl -0x85c(%rbp), %eax
addl -0x854(%rbp), %eax
movl %eax, -0x854(%rbp)
jmp 0x3c4a
leaq -0x850(%rbp), %rsi
leaq -0x68(%rbp), %rdi
callq 0x3fb0
callq 0x1070
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x860, %rsp            # imm = 0x860
popq %rbp
retq
nopw %cs:(%rax,%rax)