// File: /workspace/github_c_makefile_star2_O0/eric-lo[P]3150/8-Synchronization/pthread_race_condition.c
// Function: main
int main(int argc, char *argv[])
{
	pthread_t *tid;		/* the thread identifiers */
	int thread_num;		/* the total number of threads created */
	int *id, i;

	if(argc != 3)
	{
		fprintf(stderr, "usage: %s [max value] [num of threads]\n", argv[0]);
		exit(1);
	}

	if(atoi(argv[1]) < 0)
	{
		fprintf(stderr, "1st argument must be non-negative\n");
		exit(1);
	}

	if(atoi(argv[2]) < 0)
	{
		fprintf(stderr, "Second argument must be > 0\n");
		exit(1);
	}

/* variable initialization */

	srand(time(NULL));
	max = atoi(argv[1]);
	thread_num = atoi(argv[2]);

	printf("Number of threads created = %d\n", thread_num);
	if( (tid = (pthread_t *) malloc(sizeof(pthread_t) * thread_num)) == NULL)
	{
		perror("malloc()");
		exit(1);
	}

	if( (id = (int *) malloc(sizeof(int) * thread_num)) == NULL)
	{
		perror("malloc()");
		exit(1);
	}

/* create the threads */

	for(i = 0; i < thread_num; i++)
	{
		id[i] = i;
		if(pthread_create(&tid[i], NULL, do_your_job, &id[i]) != 0)
		{
			perror("pthread_create");
			exit(0);
		}
	}

/* now wait for the thread to exit */

	for(i = 0; i < thread_num; i++)
		pthread_join(tid[i], NULL);

/* free the memory */


	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x3, -0x8(%rbp)
je 0x12f5
movq 0x2d25(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0xd90(%rip), %rsi       # 0x2074
movb $0x0, %al
callq 0x1050
movl $0x1, %edi
callq 0x10c0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x10b0
cmpl $0x0, %eax
jge 0x1329
movq 0x2cea(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xd84(%rip), %rsi       # 0x209c
movb $0x0, %al
callq 0x1050
movl $0x1, %edi
callq 0x10c0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x10b0
cmpl $0x0, %eax
jge 0x135d
movq 0x2cb6(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xd73(%rip), %rsi       # 0x20bf
movb $0x0, %al
callq 0x1050
movl $0x1, %edi
callq 0x10c0
xorl %eax, %eax
movl %eax, %edi
callq 0x1060
movl %eax, %edi
callq 0x1040
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x10b0
movl %eax, 0x2d44(%rip)      # 0x40c4
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x10b0
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %esi
leaq 0xd42(%rip), %rdi       # 0x20dc
movb $0x0, %al
callq 0x1030
movslq -0x1c(%rbp), %rdi
shlq $0x3, %rdi
callq 0x1080
movq %rax, -0x18(%rbp)
cmpq $0x0, %rax
jne 0x13ce
leaq 0xd3d(%rip), %rdi       # 0x20fc
callq 0x10a0
movl $0x1, %edi
callq 0x10c0
movslq -0x1c(%rbp), %rdi
shlq $0x2, %rdi
callq 0x1080
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
jne 0x13fb
leaq 0xd10(%rip), %rdi       # 0x20fc
callq 0x10a0
movl $0x1, %edi
callq 0x10c0
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x146b
movl -0x2c(%rbp), %edx
movq -0x28(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x18(%rbp), %rdi
movslq -0x2c(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
movq -0x28(%rbp), %rcx
movslq -0x2c(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rcx
xorl %eax, %eax
movl %eax, %esi
leaq -0x241(%rip), %rdx      # 0x1200
callq 0x1090
cmpl $0x0, %eax
je 0x145e
leaq 0xcb3(%rip), %rdi       # 0x2105
callq 0x10a0
xorl %edi, %edi
callq 0x10c0
jmp 0x1460
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1402
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x149a
movq -0x18(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x10d0
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1472
xorl %eax, %eax
addq $0x30, %rsp
popq %rbp
retq