// File: /workspace/github_c_makefile_star2_O0/sharpyfox[P]citrusleaf-client/src/cl_shm.c
// Function: citrusleaf_use_shm
int citrusleaf_use_shm(int num_nodes, key_t key) 
{
	if (g_shm_initiated) {
		return SHM_OK;
	}

	g_max_nodes = (num_nodes > 0)? num_nodes : DEFAULT_NUM_NODES_FOR_SHM;
	
	if (0==key) {
		key = DEFAULT_SHM_KEY;
	}

	/* Time to use shared memory */
	g_shared_memory = true;
	
	cf_debug("Shared memory key is %d",key);

	if (key == -1) {
		return SHM_ERROR;
	}
	void * shm_pt = (void*)0;
	
	/* The size of the shared memory is the size of the shm structure*/
	g_shm_info.shm_sz = sizeof(shm) + sizeof(shm_ninfo) * g_max_nodes;
	
	/* Fix the update thread end condition to false and update period to 1*/
	g_shm_info.update_thread_end_cond = false;
	g_shm_info.update_period = 1;
	
	// Checking if the value of kernel.shmmax is less than g_shm_info.shm_sz
	// In that case we should exit specifying the error
	size_t shm_max;
	FILE *f = fopen(SHMMAX_SYS_FILE, "r");

	if (!f) {
		cf_error("Failed to open file: %s", SHMMAX_SYS_FILE);
		return SHM_ERROR;
	}

	if (fscanf(f, "%zu", &shm_max) != 1) {
		cf_error("Failed to read shmmax from file: %s", SHMMAX_SYS_FILE);
		fclose(f);
		return SHM_ERROR;
	}

	fclose(f);

	if(shm_max < g_shm_info.shm_sz) {
		cf_error("Shared memory size %zu exceeds system max %zu.", g_shm_info.shm_sz, shm_max);
		cf_error("You can increase shared memory size by: sysctl -w kernel.shmmax=<new_size>");
		return SHM_ERROR;
	}
	
	// First try to exclusively create a shared memory, for this only one process will succeed.
 	// others will fail giving an errno of EEXIST
	 if((g_shm_info.id = shmget(key,g_shm_info.shm_sz,IPC_CREAT | IPC_EXCL | 0666))<0) {
		// if there are any other errors apart from EEXIST, we can return gracefully 
		if(errno != EEXIST) {
			cf_error("Error in getting shared memory exclusively: %s", strerror(errno));
			return SHM_ERROR;	
		}
		else {
			// For all the processes that failed to create with EEXIST,
			// we try to get the shared memory again so that we
			// have a valid shmid
			if((g_shm_info.id = shmget(key,g_shm_info.shm_sz,IPC_CREAT | 0666))<0) {
				cf_error("Error in attaching to shared memory: %s", strerror(errno));
				return SHM_ERROR;
			}
	
			// Attach to the shared memory
			if((shm_pt = shmat(g_shm_info.id,NULL,0))==(void*)-1) {
				cf_error("Error in attaching to shared memory: %s pid: %d", strerror(errno), getpid());
				return SHM_ERROR;
			}
		
			// The shared memory base pointer
			g_shm_pt = (shm*)shm_pt;
		}
	}
	else {
		// The process who got the shared memory in the exclusive case
		cf_debug("Succeeded in creating shm : pid %d shmid %d", getpid(), g_shm_info.id);

		// Attach to the shared memory
		if ((shm_pt = shmat(g_shm_info.id,NULL,0))==(void*)-1) {
			cf_error("Error in attaching to shared memory: %s pid: %d", strerror(errno), getpid());
			return SHM_ERROR;
		}

		// The shared memory base pointer
		g_shm_pt = (shm*)shm_pt;
		memset(g_shm_pt,0,g_shm_info.shm_sz);

		// If you are the one who created the shared memory, only you can initialize the mutexes.
		// Seems fair!
		// because if we let everyone initialize the mutexes, we are in deep deep trouble
		pthread_mutexattr_t attr;
		pthread_mutexattr_init (&attr);
		pthread_mutexattr_setpshared (&attr, PTHREAD_PROCESS_SHARED);
		pthread_mutexattr_setrobust_np(&attr, PTHREAD_MUTEX_ROBUST_NP);
		if(pthread_mutex_init (&(g_shm_pt->shm_lock), &attr)!=0) {
			cf_error("Mutex init failed pid %d",getpid());
			pthread_mutexattr_destroy(&attr);
			return SHM_ERROR;
		}
		pthread_mutexattr_destroy(&attr);
	}
	pthread_create(&g_shm_update_thr, 0, cl_shm_updater_fn, 0);

	// all well? return aok!
	g_shm_initiated = true;
	return SHM_OK;
}

// Assembly:
subq $0x78, %rsp
movl %edi, 0x70(%rsp)
movl %esi, 0x6c(%rsp)
testb $0x1, 0xf43d(%rip)      # 0x205e0
je 0x111b2
movl $0x0, 0x74(%rsp)
jmp 0x116b8
cmpl $0x0, 0x70(%rsp)
jle 0x111c3
movl 0x70(%rsp), %eax
movl %eax, 0x48(%rsp)
jmp 0x111ce
movl $0x40, %eax
movl %eax, 0x48(%rsp)
jmp 0x111ce
movl 0x48(%rsp), %eax
movl %eax, 0xf40c(%rip)      # 0x205e4
xorl %eax, %eax
cmpl 0x6c(%rsp), %eax
jne 0x111e8
movl $0xdb35a5f, 0x6c(%rsp)  # imm = 0xDB35A5F
leaq 0xf3f9(%rip), %rax      # 0x205e8
movb $0x1, (%rax)
leaq 0xd0df(%rip), %rax      # 0x1e2d8
movl (%rax), %ecx
movl $0x3, %eax
cmpl %ecx, %eax
jg 0x11222
leaq 0xd0d5(%rip), %rax      # 0x1e2e0
movq (%rax), %rcx
movl 0x6c(%rsp), %edx
movl $0x3, %edi
leaq 0x81a3(%rip), %rsi      # 0x193c1
movb $0x0, %al
callq *%rcx
cmpl $-0x1, 0x6c(%rsp)
jne 0x11236
movl $0xffffffff, 0x74(%rsp) # imm = 0xFFFFFFFF
jmp 0x116b8
movq $0x0, 0x60(%rsp)
movslq 0xf39e(%rip), %rax      # 0x205e4
imulq $0x2f9068, %rax, %rax   # imm = 0x2F9068
addq $0x38, %rax
movq %rax, 0xf3a0(%rip)      # 0x205f8
movb $0x0, 0xf3a9(%rip)      # 0x20608
movl $0x1, 0xf3a3(%rip)      # 0x2060c
leaq 0x8169(%rip), %rdi      # 0x193d9
leaq 0x773a(%rip), %rsi      # 0x189b1
callq 0x22c0
movq %rax, 0x50(%rsp)
cmpq $0x0, 0x50(%rsp)
jne 0x112c3
leaq 0xd048(%rip), %rax      # 0x1e2d8
movl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0x112b6
leaq 0xd041(%rip), %rax      # 0x1e2e0
movq (%rax), %rcx
xorl %edi, %edi
leaq 0x8146(%rip), %rsi      # 0x193f1
leaq 0x8127(%rip), %rdx      # 0x193d9
movb $0x0, %al
callq *%rcx
movl $0xffffffff, 0x74(%rsp) # imm = 0xFFFFFFFF
jmp 0x116b8
movq 0x50(%rsp), %rdi
leaq 0x76b7(%rip), %rsi      # 0x18986
leaq 0x58(%rsp), %rdx
movb $0x0, %al
callq 0x2530
cmpl $0x1, %eax
je 0x11324
leaq 0xcff1(%rip), %rax      # 0x1e2d8
movl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0x1130d
leaq 0xcfea(%rip), %rax      # 0x1e2e0
movq (%rax), %rcx
xorl %edi, %edi
leaq 0x8107(%rip), %rsi      # 0x19409
leaq 0x80d0(%rip), %rdx      # 0x193d9
movb $0x0, %al
callq *%rcx
movq 0x50(%rsp), %rdi
callq 0x2280
movl $0xffffffff, 0x74(%rsp) # imm = 0xFFFFFFFF
jmp 0x116b8
movq 0x50(%rsp), %rdi
callq 0x2280
movq 0x58(%rsp), %rax
cmpq 0xf2be(%rip), %rax      # 0x205f8
jae 0x113a2
leaq 0xcf95(%rip), %rax      # 0x1e2d8
movl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0x1136f
leaq 0xcf8e(%rip), %rax      # 0x1e2e0
movq (%rax), %r8
movq 0xf29c(%rip), %rdx      # 0x205f8
movq 0x58(%rsp), %rcx
xorl %edi, %edi
leaq 0x80c3(%rip), %rsi      # 0x1942d
movb $0x0, %al
callq *%r8
leaq 0xcf62(%rip), %rax      # 0x1e2d8
movl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0x11395
leaq 0xcf5b(%rip), %rax      # 0x1e2e0
movq (%rax), %rcx
xorl %edi, %edi
leaq 0x80cb(%rip), %rsi      # 0x1945c
movb $0x0, %al
callq *%rcx
movl $0xffffffff, 0x74(%rsp) # imm = 0xFFFFFFFF
jmp 0x116b8
movl 0x6c(%rsp), %edi
movq 0xf24b(%rip), %rsi      # 0x205f8
movl $0x7b6, %edx            # imm = 0x7B6
callq 0x2190
movl %eax, 0xf233(%rip)      # 0x205f0
cmpl $0x0, %eax
jge 0x11516
callq 0x2040
cmpl $0x11, (%rax)
je 0x1141c
leaq 0xcf01(%rip), %rax      # 0x1e2d8
movl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0x1140f
leaq 0xcefa(%rip), %rax      # 0x1e2e0
movq (%rax), %rax
movq %rax, 0x40(%rsp)
callq 0x2040
movl (%rax), %edi
callq 0x20d0
movq 0x40(%rsp), %rcx
movq %rax, %rdx
xorl %edi, %edi
leaq 0x809c(%rip), %rsi      # 0x194a7
movb $0x0, %al
callq *%rcx
movl $0xffffffff, 0x74(%rsp) # imm = 0xFFFFFFFF
jmp 0x116b8
movl 0x6c(%rsp), %edi
movq 0xf1d1(%rip), %rsi      # 0x205f8
movl $0x3b6, %edx            # imm = 0x3B6
callq 0x2190
movl %eax, 0xf1b9(%rip)      # 0x205f0
cmpl $0x0, %eax
jge 0x11488
leaq 0xce95(%rip), %rax      # 0x1e2d8
movl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0x1147b
leaq 0xce8e(%rip), %rax      # 0x1e2e0
movq (%rax), %rax
movq %rax, 0x38(%rsp)
callq 0x2040
movl (%rax), %edi
callq 0x20d0
movq 0x38(%rsp), %rcx
movq %rax, %rdx
xorl %edi, %edi
leaq 0x805f(%rip), %rsi      # 0x194d6
movb $0x0, %al
callq *%rcx
movl $0xffffffff, 0x74(%rsp) # imm = 0xFFFFFFFF
jmp 0x116b8
movl 0xf162(%rip), %edi      # 0x205f0
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x2360
movq %rax, 0x60(%rsp)
movq $-0x1, %rcx
cmpq %rcx, %rax
jne 0x11505
leaq 0xce27(%rip), %rax      # 0x1e2d8
movl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0x114f8
leaq 0xce20(%rip), %rax      # 0x1e2e0
movq (%rax), %rax
movq %rax, 0x30(%rsp)
callq 0x2040
movl (%rax), %edi
callq 0x20d0
movq %rax, 0x28(%rsp)
callq 0x23d0
movq 0x28(%rsp), %rdx
movq 0x30(%rsp), %r8
movl %eax, %ecx
xorl %edi, %edi
leaq 0x800b(%rip), %rsi      # 0x194fe
movb $0x0, %al
callq *%r8
movl $0xffffffff, 0x74(%rsp) # imm = 0xFFFFFFFF
jmp 0x116b8
movq 0x60(%rsp), %rax
movq %rax, 0xf0ff(%rip)      # 0x20610
jmp 0x1168f
leaq 0xcdbb(%rip), %rax      # 0x1e2d8
movl (%rax), %ecx
movl $0x3, %eax
cmpl %ecx, %eax
jg 0x1155a
leaq 0xcdb1(%rip), %rax      # 0x1e2e0
movq (%rax), %rax
movq %rax, 0x20(%rsp)
callq 0x23d0
movq 0x20(%rsp), %r8
movl %eax, %edx
movl 0xf0a7(%rip), %ecx      # 0x205f0
movl $0x3, %edi
leaq 0x7fd9(%rip), %rsi      # 0x1952e
movb $0x0, %al
callq *%r8
movl 0xf090(%rip), %edi      # 0x205f0
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x2360
movq %rax, 0x60(%rsp)
movq $-0x1, %rcx
cmpq %rcx, %rax
jne 0x115d7
leaq 0xcd55(%rip), %rax      # 0x1e2d8
movl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0x115ca
leaq 0xcd4e(%rip), %rax      # 0x1e2e0
movq (%rax), %rax
movq %rax, 0x18(%rsp)
callq 0x2040
movl (%rax), %edi
callq 0x20d0
movq %rax, 0x10(%rsp)
callq 0x23d0
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %r8
movl %eax, %ecx
xorl %edi, %edi
leaq 0x7f39(%rip), %rsi      # 0x194fe
movb $0x0, %al
callq *%r8
movl $0xffffffff, 0x74(%rsp) # imm = 0xFFFFFFFF
jmp 0x116b8
movq 0x60(%rsp), %rax
movq %rax, 0xf02d(%rip)      # 0x20610
movq 0xf026(%rip), %rdi      # 0x20610
movq 0xf007(%rip), %rdx      # 0x205f8
xorl %esi, %esi
callq 0x2170
leaq 0x4c(%rsp), %rdi
callq 0x2060
leaq 0x4c(%rsp), %rdi
movl $0x1, %esi
callq 0x2330
leaq 0x4c(%rsp), %rdi
movl $0x1, %esi
callq 0x2210
movq 0xefe9(%rip), %rdi      # 0x20610
addq $0x10, %rdi
leaq 0x4c(%rsp), %rsi
callq 0x2430
cmpl $0x0, %eax
je 0x11685
leaq 0xcc97(%rip), %rax      # 0x1e2d8
movl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jg 0x11671
leaq 0xcc90(%rip), %rax      # 0x1e2e0
movq (%rax), %rax
movq %rax, 0x8(%rsp)
callq 0x23d0
movq 0x8(%rsp), %rcx
movl %eax, %edx
xorl %edi, %edi
leaq 0x7eed(%rip), %rsi      # 0x1955a
movb $0x0, %al
callq *%rcx
leaq 0x4c(%rsp), %rdi
callq 0x2420
movl $0xffffffff, 0x74(%rsp) # imm = 0xFFFFFFFF
jmp 0x116b8
leaq 0x4c(%rsp), %rdi
callq 0x2420
leaq 0xef82(%rip), %rdi      # 0x20618
xorl %eax, %eax
movl %eax, %ecx
leaq 0x2f(%rip), %rdx        # 0x116d0
movq %rcx, %rsi
callq 0x2500
movb $0x1, 0xef30(%rip)      # 0x205e0
movl $0x0, 0x74(%rsp)
movl 0x74(%rsp), %eax
addq $0x78, %rsp
retq
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)