// File: /workspace/github_c_makefile_star2_O1/ramirezsebas[P]TP-LISTINGS/./src/capitulo_4/4.7/tsd.c
// Function: merge_sort_multithreading
void* merge_sort_multithreading(void* arg) 
{ 
	char thread_log_filename[20], log_message[100];
	FILE* thread_log;
	/* Generate the filename for this thread’s log file. */
	sprintf (thread_log_filename, "thread%d.log", (int) pthread_self ());
	/* Open the log file. */
	thread_log = fopen (thread_log_filename, "w");
	/* Store the file pointer in thread-specific data under thread_log_key.*/
	pthread_setspecific (thread_log_key, thread_log);
	
	write_to_thread_log ("Thread starting.");

	int old_cancel_state;

	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancel_state);

    // which part out of 4 parts 
    int thread_part = part++; 

    pthread_setcancelstate(old_cancel_state, NULL);

    sprintf (log_message, "Processing part %d of the array.", thread_part);
    write_to_thread_log (log_message);
  
    // calculating low and high 
    int low = thread_part * (MAX / 4); 
    int high = (thread_part + 1) * (MAX / 4) - 1; 

    sprintf (log_message, "The part starts at %d and ends at %d.", low, high);
    write_to_thread_log (log_message);
  
    // evaluating mid point 
    int mid = low + (high - low) / 2; 

    write_to_thread_log ("Sorting...");
    if (low < high) { 
        merge_sort(low, mid); 
        merge_sort(mid + 1, high); 
        merge(low, mid, high); 
    }
    write_to_thread_log ("Sorting finished succesfully!"); 
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x90, %rsp
callq 0x10e0
leaq 0xb73(%rip), %rsi       # 0x2008
leaq -0x40(%rbp), %rbx
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x1100
leaq 0xb69(%rip), %rsi       # 0x2015
movq %rbx, %rdi
callq 0x10f0
movl 0x2c0a(%rip), %edi      # 0x40c4
movq %rax, %rsi
callq 0x1130
movl 0x2bfc(%rip), %edi      # 0x40c4
callq 0x1030
leaq 0xb30(%rip), %rbx       # 0x2004
leaq 0xb3c(%rip), %rdx       # 0x2017
movq %rax, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1090
leaq -0x24(%rbp), %r15
movl $0x1, %edi
movq %r15, %rsi
callq 0x1050
movl 0x2bc0(%rip), %r14d     # 0x40c0
leal 0x1(%r14), %eax
movl %eax, 0x2bb6(%rip)      # 0x40c0
movl (%r15), %edi
xorl %esi, %esi
callq 0x1050
leaq 0xb0d(%rip), %rsi       # 0x2028
leaq -0xb0(%rbp), %r12
movq %r12, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0x1100
movl 0x2b8f(%rip), %edi      # 0x40c4
callq 0x1030
movq %rax, %rdi
movq %rbx, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x1090
leal (%r14,%r14,4), %r15d
leal (%r14,%r14,4), %r14d
addl $0x4, %r14d
leaq 0xaec(%rip), %rsi       # 0x2049
movq %r12, %rdi
movl %r15d, %edx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x1100
movl 0x2b51(%rip), %edi      # 0x40c4
callq 0x1030
movq %rax, %rdi
movq %rbx, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x1090
movl 0x2b36(%rip), %edi      # 0x40c4
callq 0x1030
leaq 0xad5(%rip), %rdx       # 0x206f
movq %rax, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1090
movl %r14d, %eax
subl %r15d, %eax
jle 0x15e5
movl %eax, %r12d
shrl $0x1f, %r12d
addl %eax, %r12d
sarl %r12d
leal (%r12,%r15), %ebx
movl %r15d, %edi
movl %ebx, %esi
callq 0x1425
leal (%r12,%r15), %edi
incl %edi
movl %r14d, %esi
callq 0x1425
movl %r15d, %edi
movl %ebx, %esi
movl %r14d, %edx
callq 0x1274
movl 0x2ad9(%rip), %edi      # 0x40c4
callq 0x1030
leaq 0xa0d(%rip), %rsi       # 0x2004
leaq 0xa7c(%rip), %rdx       # 0x207a
movq %rax, %rdi
xorl %eax, %eax
callq 0x1090
addq $0x90, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq