// File: /workspace/github_c_makefile_star2_O0/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
subq $0xd0, %rsp
movq %rdi, -0x10(%rbp)
leaq -0x30(%rbp), %rax
movq %rax, -0xc8(%rbp)
callq 0x10d0
movq -0xc8(%rbp), %rdi
movl %eax, %edx
leaq 0xae9(%rip), %rsi       # 0x2008
movb $0x0, %al
callq 0x10f0
leaq -0x30(%rbp), %rdi
leaq 0xae4(%rip), %rsi       # 0x2015
callq 0x10e0
movq %rax, -0xa8(%rbp)
movl 0x2b81(%rip), %edi      # 0x40c4
movq -0xa8(%rbp), %rsi
callq 0x1120
leaq 0xac1(%rip), %rdi       # 0x2017
callq 0x1230
movl $0x1, %edi
leaq -0xac(%rbp), %rsi
callq 0x1050
movl 0x2b4e(%rip), %eax      # 0x40c0
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x2b43(%rip)      # 0x40c0
movl %eax, -0xb0(%rbp)
movl -0xac(%rbp), %edi
xorl %eax, %eax
movl %eax, %esi
callq 0x1050
leaq -0xa0(%rbp), %rdi
movl -0xb0(%rbp), %edx
leaq 0xa82(%rip), %rsi       # 0x2028
movb $0x0, %al
callq 0x10f0
leaq -0xa0(%rbp), %rdi
callq 0x1230
imull $0x5, -0xb0(%rbp), %eax
movl %eax, -0xb4(%rbp)
movl -0xb0(%rbp), %eax
addl $0x1, %eax
imull $0x5, %eax, %eax
subl $0x1, %eax
movl %eax, -0xb8(%rbp)
leaq -0xa0(%rbp), %rdi
movl -0xb4(%rbp), %edx
movl -0xb8(%rbp), %ecx
leaq 0xa54(%rip), %rsi       # 0x2049
movb $0x0, %al
callq 0x10f0
leaq -0xa0(%rbp), %rdi
callq 0x1230
movl -0xb4(%rbp), %eax
movl %eax, -0xc0(%rbp)
movl -0xb8(%rbp), %eax
subl -0xb4(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0xc0(%rbp), %eax
addl %ecx, %eax
movl %eax, -0xbc(%rbp)
leaq 0xa30(%rip), %rdi       # 0x206f
callq 0x1230
movl -0xb4(%rbp), %eax
cmpl -0xb8(%rbp), %eax
jge 0x168e
movl -0xb4(%rbp), %edi
movl -0xbc(%rbp), %esi
callq 0x1480
movl -0xbc(%rbp), %edi
addl $0x1, %edi
movl -0xb8(%rbp), %esi
callq 0x1480
movl -0xb4(%rbp), %edi
movl -0xbc(%rbp), %esi
movl -0xb8(%rbp), %edx
callq 0x1290
leaq 0x9e5(%rip), %rdi       # 0x207a
callq 0x1230
movq -0x8(%rbp), %rax
addq $0xd0, %rsp
popq %rbp
retq
nopw (%rax,%rax)