// File: /workspace/github_c_makefile_star2_O2/sharpyfox[P]citrusleaf-client/src/cl_shm.c
// Function: cl_shm_updater_fn
void*
cl_shm_updater_fn(void* gas)
{
	//Take Lock - only one process can update
	int status = pthread_mutex_lock(&(g_shm_pt->shm_lock));

	if (g_shm_info.update_thread_end_cond) {
		pthread_exit(0);
	}

	/* There are only two valid cases when we can access the protected data
	 * 1. We get the lock without any error and
	 * 2. The thread holding the lock died without releasing it */
	if (status != 0) {
		/* Make the lock consistent if the last owner of the lock died while holding the lock */
		/* We should ideally clean the memory here and then make the lock consistent */
		if (status == EOWNERDEAD) {
			pthread_mutex_consistent_np(&(g_shm_pt->shm_lock));
		}
		else {
			/* With Robust mutexes the lock gets in not recoverable state when the process holding the lock
			 * dies and the other process that gets the lock unlocks it without making it consistent
			 * (via pthread_mutex_consistent function) */
			cf_error("Failed to lock shared memory in tend thread. Exiting thread.");
			pthread_exit(0);
		}
	}

	cf_debug("Process %d took over control with pthread_mutex_lock returning %d", getpid(), status);
	g_shm_updater = true;

	do {
		/* Check if the thread is set to exit or not. If it is, gracefully exit*/
		if (g_shm_info.update_thread_end_cond) {
			cl_shm_free_seed_array();
			pthread_exit(0);
		}

		sleep(g_shm_info.update_period);

		if (g_shm_info.update_thread_end_cond) {
			cl_shm_free_seed_array();
			pthread_exit(0);
		}

		// Tend all clusters.
		cf_ll_element *e = cf_ll_get_head(&cluster_ll);

		while(e) {
			cl_shm_update((cl_cluster*)e);
			e = cf_ll_get_next(e);
		}
	} while(1);

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq 0xcd56(%rip), %rdi      # 0x17600
addq $0x10, %rdi
callq 0x23b0
cmpb $0x1, 0xcd36(%rip)      # 0x175f0
je 0xae72
movl %eax, %ebx
testl %eax, %eax
je 0xa8e2
cmpl $0x82, %ebx
jne 0xae79
movq 0xcd27(%rip), %rdi      # 0x17600
addq $0x10, %rdi
callq 0x21f0
leaq 0xa9df(%rip), %rax      # 0x152c8
movl (%rax), %eax
cmpl $0x3, %eax
jl 0xa912
leaq 0xa9d9(%rip), %rax      # 0x152d0
movq (%rax), %r14
callq 0x23a0
leaq 0x4c76(%rip), %rsi      # 0xf57c
pushq $0x3
popq %rdi
movl %eax, %edx
movl %ebx, %ecx
xorl %eax, %eax
callq *%r14
movb $0x1, 0xccf7(%rip)      # 0x17610
cmpb $0x1, 0xccd0(%rip)      # 0x175f0
je 0xae6d
movzbl 0xcccb(%rip), %edi      # 0x175f8
callq 0x2180
cmpb $0x0, 0xccb7(%rip)      # 0x175f0
jne 0xae6d
leaq 0xca92(%rip), %rax      # 0x173d8
movq (%rax), %rax
testq %rax, %rax
je 0xa919
movq %rax, 0x8(%rsp)
callq 0x23a0
movq 0xcca1(%rip), %rcx      # 0x17600
cltq
movq %rax, (%rcx)
movq 0x8(%rsp), %rax
leaq 0x158(%rax), %r14
movq %r14, %rdi
callq 0x23b0
movq 0x8(%rsp), %rax
movslq 0x104(%rax), %rbx
testq %rbx, %rbx
jle 0xae5b
cmpl %ebx, 0xcc8d(%rip)      # 0x17620
movq 0xcc7e(%rip), %rdi      # 0x17618
movq %r14, 0x10(%rsp)
jge 0xa9c4
callq 0x22c0
imulq $0x58, %rbx, %rdi
callq 0x22e0
movq %rax, %rdi
movq 0x8(%rsp), %rax
movl %ebx, 0xcc63(%rip)      # 0x17620
movq %rdi, 0xcc54(%rip)      # 0x17618
addq $0xf8, %rax
leaq 0x14(%rdi), %r12
xorl %r15d, %r15d
movq %rdi, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
movq %rax, 0x18(%rsp)
movq %r12, (%rsp)
cmpq %rbx, %r15
je 0xaaa9
movq 0x18(%rsp), %rdi
movl %r15d, %esi
leaq 0x48(%rsp), %rdx
callq 0xd967
movq 0x20(%rsp), %r13
movq 0x48(%rsp), %rbp
imulq $0x58, %r15, %r14
leaq (%r13,%r14), %rbx
movq %rbp, %rsi
movq %rbx, %rdi
pushq $0x14
popq %rdx
callq 0xb090
movl 0x2c(%rbp), %ecx
cmpl $0x5, %ecx
movl %ecx, 0x54(%r13,%r14)
jl 0xaa61
leaq 0xa894(%rip), %rax      # 0x152c8
movl (%rax), %eax
cmpl $0x3, %eax
jl 0xaa57
leaq 0xa88e(%rip), %rax      # 0x152d0
movq (%rax), %r8
pushq $0x3
popq %rdi
leaq 0x4b6f(%rip), %rsi      # 0xf5be
movq %rbp, %rdx
xorl %eax, %eax
callq *%r8
movl $0x4, 0x54(%rbx)
pushq $0x4
popq %rcx
addq $0x20, %rbp
movq %r12, %r14
xorl %r13d, %r13d
movslq %ecx, %rax
cmpq %rax, %r13
jge 0xaa98
movq %rbp, %rdi
movl %r13d, %esi
callq 0xd9c7
movq (%rax), %rcx
movq 0x8(%rax), %rax
incq %r13
movq %rax, 0x8(%r14)
movq %rcx, (%r14)
addq $0x10, %r14
movl 0x54(%rbx), %ecx
jmp 0xaa6b
movq 0x28(%rsp), %rbx
incq %r15
addq $0x58, %r12
jmp 0xa9e4
movq 0x10(%rsp), %rdi
callq 0x2190
movq 0x20(%rsp), %rax
movq (%rsp), %r12
xorl %ecx, %ecx
cmpq %rbx, %rcx
je 0xae63
movq 0xcb32(%rip), %rbx      # 0x17600
imulq $0x58, %rcx, %rbp
movq %rcx, 0x18(%rsp)
addq %rax, %rbp
cmpl $0x0, 0xc(%rbx)
jne 0xab12
movl 0x54(%rbp), %r14d
addq $0xc, %rbx
movl $0x0, %eax
movq %r12, %rdi
testl %r14d, %r14d
cmovlel %eax, %r14d
incq %r14
decq %r14
je 0xab12
movq %rbx, %rsi
leaq 0x10(%rdi), %r15
callq 0xb604
testl %eax, %eax
movq %r15, %rdi
jne 0xaafa
movslq 0x54(%rbp), %r14
movl $0x0, %r15d
movq %r12, %rbx
movq %r12, (%rsp)
testl %r14d, %r14d
cmovgl %r14d, %r15d
movq %r15, %r13
negq %r13
pushq $-0x1
popq %r12
leaq (%r13,%r12), %rax
cmpq $-0x1, %rax
je 0xab59
movq %rbx, %rdi
leaq 0x48(%rsp), %rsi
callq 0xb134
incq %r12
addq $0x10, %rbx
testl %eax, %eax
jne 0xab34
jmp 0xab5c
movq %r15, %r12
cmpq %r14, %r12
movq (%rsp), %r12
jge 0xae2f
movq %rbp, %rdi
callq 0xaf10
movq %rax, %r15
testq %rax, %rax
movq %rax, %rbx
je 0xac85
movq %rbx, %rdi
callq 0xae9e
movl 0x54(%rbp), %eax
cmpl 0x68(%rbx), %eax
je 0xab9b
movq %rbp, %rdi
movq %rbx, %rsi
callq 0xb055
leaq 0x70(%rbx), %rdi
leaq 0x50(%rsp), %rsi
pushq $0x14
popq %rdx
callq 0xb090
movl 0x6c(%rbx), %r14d
movl 0x70(%rsp), %r13d
cmpl %r13d, %r14d
je 0xabbe
movl %r13d, 0x6c(%rbx)
movq 0x68(%rsp), %rsi
leaq 0x84(%rbx), %rdi
movl $0xfe1, %edx            # imm = 0xFE1
callq 0xb090
testb %al, %al
je 0xac01
leaq 0xa6e9(%rip), %rax      # 0x152c8
cmpl $0x0, (%rax)
jle 0xac01
leaq 0xa6e5(%rip), %rax      # 0x152d0
movl $0xfe1, %edx            # imm = 0xFE1
movq (%rax), %rcx
pushq $0x1
popq %rdi
leaq 0x49f0(%rip), %rsi      # 0xf5ed
xorl %eax, %eax
callq *%rcx
movb 0x74(%rsp), %al
leaq 0x40(%rbx), %rdi
movq %rdi, 0x10(%rsp)
movb %al, 0x2f9067(%rbx)
callq 0x2190
leaq 0x48(%rsp), %rdi
callq 0xb2ae
testq %r15, %r15
jne 0xac32
movq 0xc9d1(%rip), %rax      # 0x17600
incl 0x8(%rax)
cmpl %r13d, %r14d
je 0xae2f
movslq 0x54(%rbp), %r14
movq %r12, %r15
movl $0x0, %r12d
testl %r14d, %r14d
cmovgl %r14d, %r12d
movq %r12, %rbp
negq %rbp
pushq $-0x1
popq %r13
leaq (%rbp,%r13), %rax
cmpq $-0x1, %rax
je 0xad45
movq %r15, %rdi
leaq 0x30(%rsp), %rsi
callq 0xb3a3
incq %r13
addq $0x10, %r15
testl %eax, %eax
jne 0xac59
jmp 0xad48
movq 0xc974(%rip), %rax      # 0x17600
movl 0xc942(%rip), %edx      # 0x175d4
movslq 0x8(%rax), %rcx
cmpl %edx, %ecx
jge 0xae02
imulq $0x2f9068, %rcx, %rcx   # imm = 0x2F9068
movq %rbp, %rdi
leaq 0x38(%rax,%rcx), %rbx
movq %rbx, %rsi
callq 0xb055
callq 0x23a0
movq 0xc93f(%rip), %rcx      # 0x17600
cltq
cmpq %rax, (%rcx)
jne 0xab80
leaq 0x30(%rsp), %r14
movq %r14, %rdi
callq 0x2050
movq %r14, %rdi
pushq $0x1
popq %r12
movl %r12d, %esi
callq 0x2300
movq %r14, %rdi
movl %r12d, %esi
callq 0x21e0
leaq 0x40(%rbx), %rdi
movq %r14, %rsi
callq 0x2400
testl %eax, %eax
je 0xae4a
leaq 0xa5ba(%rip), %rax      # 0x152c8
cmpl $0x0, (%rax)
movq (%rsp), %r12
jle 0xad36
leaq 0xa5b2(%rip), %rax      # 0x152d0
movq (%rax), %rbx
callq 0x23a0
pushq $0x1
popq %rdi
movl %eax, %edx
leaq 0x4969(%rip), %rsi      # 0xf69b
xorl %eax, %eax
callq *%rbx
leaq 0x30(%rsp), %rdi
callq 0x23f0
jmp 0xae25
movq %r12, %r13
cmpq %r14, %r13
jge 0xadfc
movq %rbx, %rdi
callq 0xae9e
movq 0x38(%rsp), %rsi
leaq 0x1065(%rbx), %rdi
movl $0x17c001, %edx         # imm = 0x17C001
callq 0xb090
movq (%rsp), %r12
testb %al, %al
je 0xada0
leaq 0xa54a(%rip), %rax      # 0x152c8
cmpl $0x0, (%rax)
jle 0xada0
leaq 0xa546(%rip), %rax      # 0x152d0
movl $0x17c001, %edx         # imm = 0x17C001
movq (%rax), %rcx
pushq $0x1
popq %rdi
leaq 0x4876(%rip), %rsi      # 0xf612
xorl %eax, %eax
callq *%rcx
movq 0x40(%rsp), %rsi
addq $0x17d066, %rbx         # imm = 0x17D066
movl $0x17c001, %edx         # imm = 0x17C001
movq %rbx, %rdi
callq 0xb090
testb %al, %al
je 0xade6
leaq 0xa504(%rip), %rax      # 0x152c8
cmpl $0x0, (%rax)
jle 0xade6
leaq 0xa500(%rip), %rax      # 0x152d0
movl $0x17c001, %edx         # imm = 0x17C001
movq (%rax), %rcx
pushq $0x1
popq %rdi
leaq 0x4862(%rip), %rsi      # 0xf644
xorl %eax, %eax
callq *%rcx
movq 0x10(%rsp), %rdi
callq 0x2190
leaq 0x30(%rsp), %rdi
callq 0xb491
jmp 0xae2f
movq (%rsp), %r12
jmp 0xae2f
leaq 0xa4bf(%rip), %rax      # 0x152c8
cmpl $0x0, (%rax)
js 0xae25
leaq 0xa4bb(%rip), %rax      # 0x152d0
xorl %edi, %edi
leaq 0x4857(%rip), %rsi      # 0xf675
movq (%rax), %rcx
xorl %eax, %eax
callq *%rcx
leaq 0x48(%rsp), %rdi
callq 0xb2ae
movq 0x18(%rsp), %rcx
movq 0x28(%rsp), %rbx
movq 0x20(%rsp), %rax
addq $0x58, %r12
incq %rcx
jmp 0xaabe
movq %r14, %rdi
callq 0x23f0
movq (%rsp), %r12
jmp 0xab80
movq %r14, %rdi
callq 0x2190
movq 0x8(%rsp), %rax
jmp 0xa946
callq 0xb02f
xorl %edi, %edi
callq 0x24c0
leaq 0xa448(%rip), %rax      # 0x152c8
cmpl $0x0, (%rax)
js 0xae72
leaq 0xa444(%rip), %rax      # 0x152d0
leaq 0x46ac(%rip), %rsi      # 0xf53f
xorl %edi, %edi
movq (%rax), %rcx
xorl %eax, %eax
callq *%rcx
jmp 0xae72