// File: /workspace/github_c_makefile_star2_O3/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 %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
callq 0x10e0
leaq 0xb7e(%rip), %rsi       # 0x2008
leaq -0x40(%rbp), %rbx
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x1100
leaq 0xb74(%rip), %rsi       # 0x2015
movq %rbx, %rdi
callq 0x10f0
movl 0x2c15(%rip), %edi      # 0x40c4
movq %rax, %rsi
callq 0x1130
movl 0x2c07(%rip), %edi      # 0x40c4
callq 0x1030
leaq 0xb3b(%rip), %rbx       # 0x2004
leaq 0xb47(%rip), %rdx       # 0x2017
movq %rax, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1090
leaq -0x2c(%rbp), %r15
movl $0x1, %edi
movq %r15, %rsi
callq 0x1050
movl 0x2bcb(%rip), %r14d     # 0x40c0
leal 0x1(%r14), %eax
movl %eax, 0x2bc1(%rip)      # 0x40c0
movl (%r15), %edi
xorl %esi, %esi
callq 0x1050
leaq 0xb18(%rip), %rsi       # 0x2028
leaq -0xb0(%rbp), %r13
movq %r13, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0x1100
movl 0x2b9a(%rip), %edi      # 0x40c4
callq 0x1030
movq %rax, %rdi
movq %rbx, %rsi
movq %r13, %rdx
xorl %eax, %eax
callq 0x1090
leal (%r14,%r14,4), %r12d
leal (%r14,%r14,4), %r15d
addl $0x4, %r15d
leaq 0xaf7(%rip), %rsi       # 0x2049
movq %r13, %rdi
movl %r12d, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x1100
movl 0x2b5c(%rip), %edi      # 0x40c4
callq 0x1030
movq %rax, %rdi
movq %rbx, %rsi
movq %r13, %rdx
xorl %eax, %eax
callq 0x1090
movl 0x2b41(%rip), %edi      # 0x40c4
callq 0x1030
leaq 0xae0(%rip), %rdx       # 0x206f
movq %rax, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1090
leal (%r14,%r14,4), %r13d
addl $0x2, %r13d
movl %r12d, %edi
movl %r13d, %esi
callq 0x1418
leal (%r14,%r14,4), %edi
addl $0x3, %edi
movl %r15d, %esi
callq 0x1418
movl %r12d, %edi
movl %r13d, %esi
movl %r15d, %edx
callq 0x1274
movl 0x2af2(%rip), %edi      # 0x40c4
callq 0x1030
leaq 0xa9c(%rip), %rdx       # 0x207a
movq %rax, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1090
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq