// File: /workspace/github_cpp_makefile_star2_O2/BenLangmead[P]bowtie/./shmem.h
// Function: bool allocSharedMem<unsigned char>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, unsigned long, unsigned char**, char const*, bool)
bool allocSharedMem(std::string fname,
                    size_t len,
                    T ** dst,
                    const char *memName,
                    bool verbose)
{
	using namespace std;
	int shmid = -1;
	// Calculate key given string
	key_t key = (key_t)hash_string(fname);
	shmid_ds ds;
	int ret;
	// Reserve 4 bytes at the end for silly synchronization
	size_t shmemLen = len + 4;
	if(verbose) {
		cerr << "Reading " << len << "+4 bytes into shared memory for " << memName << endl;
	}
	T *ptr = NULL;
	while(true) {
		// Create the shrared-memory block
		if((shmid = shmget(key, shmemLen, IPC_CREAT | 0666)) < 0) {
			if(errno == ENOMEM) {
				cerr << "Out of memory allocating shared area " << memName << endl;
			} else if(errno == EACCES) {
				cerr << "EACCES" << endl;
			} else if(errno == EEXIST) {
				cerr << "EEXIST" << endl;
			} else if(errno == EINVAL) {
				cerr << "Warning: shared-memory chunk's segment size doesn't match expected size (" << (shmemLen) << ")" << endl
					 << "Deleting old shared memory block and trying again." << endl;
				shmid = shmget(key, 0, 0);
				if((ret = shmctl(shmid, IPC_RMID, &ds)) < 0) {
					cerr << "shmctl returned " << ret
						 << " for IPC_RMID, errno is " << errno
						 << ", shmid is " << shmid << endl;
					throw 1;
				} else {
					cerr << "Deleted shared mem chunk with shmid " << shmid << endl;
				}
				continue;
			} else if(errno == ENOENT) {
				cerr << "ENOENT" << endl;
			} else if(errno == ENOSPC) {
				cerr << "ENOSPC" << endl;
			} else {
				cerr << "shmget returned " << shmid << " for and errno is " << errno << endl;
			}
			throw 1;
		}
		ptr = (T*)shmat(shmid, 0, 0);
		if(ptr == (void*)-1) {
			cerr << "Failed to attach " << memName << " to shared memory with shmat()." << endl;
			throw 1;
		}
		if(ptr == NULL) {
			cerr << memName << " pointer returned by shmat() was NULL." << endl;
			throw 1;
		}
		// Did I create it, or did I just attach to one created by
		// another process?
		if((ret = shmctl(shmid, IPC_STAT, &ds)) < 0) {
			cerr << "shmctl returned " << ret << " for IPC_STAT and errno is " << errno << endl;
			throw 1;
		}
		if((size_t)ds.shm_segsz != shmemLen) {
			cerr << "Warning: shared-memory chunk's segment size (" << ds.shm_segsz
				 << ") doesn't match expected size (" << shmemLen << ")" << endl
				 << "Deleting old shared memory block and trying again." << endl;
			if((ret = shmctl(shmid, IPC_RMID, &ds)) < 0) {
				cerr << "shmctl returned " << ret << " for IPC_RMID and errno is " << errno << endl;
				throw 1;
			}
		} else {
			break;
		}
	} // while(true)
	*dst = ptr;
	bool initid = (((volatile uint32_t*)((char*)ptr + len))[0] == SHMEM_INIT);
	if(ds.shm_cpid == getpid() && !initid) {
		if(verbose) {
			cerr << "  I (pid = " << getpid() << ") created the "
			     << "shared memory for " << memName << endl;
		}
		// Set this value just off the end of the chunk to
		// indicate that the data hasn't been read yet.
		((volatile uint32_t*)((char*)ptr + len))[0] = SHMEM_UNINIT;
		return true;
	} else {
		if(verbose) {
			cerr << "  I (pid = " << getpid()
			     << ") did not create the shared memory for "
			     << memName << ".  Pid " << ds.shm_cpid << " did." << endl;
		}
		return false;
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movl %r8d, %ebp
movq %rcx, %rbx
movq %rdx, 0x18(%rsp)
movq %rsi, %r14
callq 0xd27a
movl %eax, %r12d
movl %ebp, 0x14(%rsp)
testl %ebp, %ebp
je 0xee5c
movq 0x251cd(%rip), %rdi     # 0x33ff0
leaq 0x1cd9a(%rip), %rsi     # 0x2bbc4
callq 0x42d0
movq %rax, %rdi
movq %r14, %rsi
callq 0x41a0
leaq 0x1cd8c(%rip), %rsi     # 0x2bbcd
movq %rax, %rdi
callq 0x42d0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x42d0
movq %rax, %rdi
callq 0x4130
movq %rbx, 0x8(%rsp)
movq %r14, 0x20(%rsp)
leaq 0x4(%r14), %r13
movq 0x2517f(%rip), %r15     # 0x33ff0
movl %r12d, %edi
movq %r13, %rsi
movl $0x3b6, %edx            # imm = 0x3B6
callq 0x4170
movl %eax, %ebp
testl %eax, %eax
js 0xef4d
movl %ebp, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4450
testq %rax, %rax
je 0xf165
movq %rax, %r14
cmpq $-0x1, %rax
je 0xf1a7
movl %ebp, %edi
pushq $0x2
popq %rsi
leaq 0x28(%rsp), %rdx
callq 0x4230
testl %eax, %eax
js 0xf17f
cmpq %r13, 0x58(%rsp)
je 0xeff2
movq %r15, %rdi
leaq 0x1cec9(%rip), %rsi     # 0x2bda1
callq 0x42d0
movq 0x58(%rsp), %rsi
movq %rax, %rdi
callq 0x41a0
movq %rax, %rdi
leaq 0x1cedb(%rip), %rsi     # 0x2bdcf
callq 0x42d0
movq %rax, %rdi
movq %r13, %rsi
callq 0x41a0
movq %rax, %rdi
leaq 0x1b50a(%rip), %rsi     # 0x2a418
callq 0x42d0
movq %rax, %rdi
callq 0x4130
movq %rax, %rdi
leaq 0x1cd47(%rip), %rsi     # 0x2bc6c
callq 0x42d0
movq %rax, %rdi
callq 0x4130
movl %ebp, %edi
xorl %esi, %esi
leaq 0x28(%rsp), %rdx
callq 0x4230
testl %eax, %eax
jns 0xee71
jmp 0xf1d6
callq 0x4040
movq %rax, %r14
movl (%rax), %eax
cmpl $0x16, %eax
jne 0xf118
movq %r15, %rdi
leaq 0x1ccb8(%rip), %rsi     # 0x2bc22
callq 0x42d0
movq %rax, %rdi
movq %r13, %rsi
callq 0x41a0
movq %rax, %rdi
leaq 0x1b494(%rip), %rsi     # 0x2a418
callq 0x42d0
movq %rax, %rdi
callq 0x4130
movq %rax, %rdi
leaq 0x1ccd1(%rip), %rsi     # 0x2bc6c
callq 0x42d0
movq %rax, %rdi
callq 0x4130
movl %r12d, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4170
movl %eax, %ebp
movl %eax, %edi
xorl %esi, %esi
leaq 0x28(%rsp), %rdx
callq 0x4230
testl %eax, %eax
js 0xf26c
movq %r15, %rdi
leaq 0x1ccff(%rip), %rsi     # 0x2bcd5
callq 0x42d0
movq %rax, %rdi
movl %ebp, %esi
callq 0x45d0
movq %rax, %rdi
callq 0x4130
jmp 0xee71
movq 0x18(%rsp), %rax
movq %r14, (%rax)
movq 0x20(%rsp), %r12
movl (%r14,%r12), %ebx
movl 0x78(%rsp), %ebp
callq 0x4490
cmpl %eax, %ebp
sete %al
cmpl $0xffaa6161, %ebx       # imm = 0xFFAA6161
setne %bl
andb %al, %bl
cmpb $0x1, %bl
jne 0xf08a
cmpb $0x0, 0x14(%rsp)
je 0xf080
movq 0x24fc1(%rip), %rdi     # 0x33ff0
leaq 0x1cdd5(%rip), %rsi     # 0x2be0b
callq 0x42d0
movq %rax, %r15
callq 0x4490
movq %r15, %rdi
movl %eax, %esi
callq 0x45d0
leaq 0x1cdc3(%rip), %rsi     # 0x2be17
movq %rax, %rdi
callq 0x42d0
leaq 0x1cdd8(%rip), %rsi     # 0x2be3b
movq %rax, %rdi
callq 0x42d0
movq %rax, %rdi
movq 0x8(%rsp), %rsi
callq 0x42d0
movq %rax, %rdi
callq 0x4130
movl $0xafba4242, (%r14,%r12) # imm = 0xAFBA4242
jmp 0xf104
cmpb $0x0, 0x14(%rsp)
je 0xf104
movq 0x24f58(%rip), %rdi     # 0x33ff0
leaq 0x1cd6c(%rip), %rsi     # 0x2be0b
callq 0x42d0
movq %rax, %r14
callq 0x4490
movq %r14, %rdi
movl %eax, %esi
callq 0x45d0
leaq 0x1cd69(%rip), %rsi     # 0x2be26
movq %rax, %rdi
callq 0x42d0
movq %rax, %rdi
movq 0x8(%rsp), %rsi
callq 0x42d0
leaq 0x1cd75(%rip), %rsi     # 0x2be4e
movq %rax, %rdi
callq 0x42d0
movl 0x78(%rsp), %esi
movq %rax, %rdi
callq 0x45d0
leaq 0x1cd62(%rip), %rsi     # 0x2be56
movq %rax, %rdi
callq 0x42d0
movq %rax, %rdi
callq 0x4130
movl %ebx, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x24ed1(%rip), %rdi     # 0x33ff0
cmpl $0x2, %eax
je 0xf25e
cmpl $0x1c, %eax
je 0xf216
cmpl $0xd, %eax
je 0xf228
cmpl $0x11, %eax
je 0xf21f
cmpl $0xc, %eax
jne 0xf231
leaq 0x1ca9b(%rip), %rsi     # 0x2bbee
callq 0x42d0
movq %rax, %rdi
movq 0x8(%rsp), %rsi
jmp 0xf265
movq 0x24e84(%rip), %rdi     # 0x33ff0
movq 0x8(%rsp), %rsi
callq 0x42d0
leaq 0x1cbe1(%rip), %rsi     # 0x2bd5e
jmp 0xf1ce
movl %eax, %ebx
movq 0x24e68(%rip), %rdi     # 0x33ff0
leaq 0x1cb10(%rip), %rsi     # 0x2bc9f
callq 0x42d0
movq %rax, %rdi
movl %ebx, %esi
callq 0x45d0
leaq 0x1cbe0(%rip), %rsi     # 0x2bd85
jmp 0xf1fc
movq 0x24e42(%rip), %rdi     # 0x33ff0
leaq 0x1cb77(%rip), %rsi     # 0x2bd2c
callq 0x42d0
movq %rax, %rdi
movq 0x8(%rsp), %rsi
callq 0x42d0
leaq 0x1cb70(%rip), %rsi     # 0x2bd3e
movq %rax, %rdi
jmp 0xf265
movl %eax, %ebx
movq 0x24e11(%rip), %rdi     # 0x33ff0
leaq 0x1cab9(%rip), %rsi     # 0x2bc9f
callq 0x42d0
movq %rax, %rdi
movl %ebx, %esi
callq 0x45d0
leaq 0x1cbf3(%rip), %rsi     # 0x2bdef
movq %rax, %rdi
callq 0x42d0
movq %rax, %rbx
callq 0x4040
movl (%rax), %esi
movq %rbx, %rdi
jmp 0xf2b9
leaq 0x1cae4(%rip), %rsi     # 0x2bd01
jmp 0xf265
leaq 0x1c9f5(%rip), %rsi     # 0x2bc1b
jmp 0xf265
leaq 0x1c9e5(%rip), %rsi     # 0x2bc14
jmp 0xf265
leaq 0x1cad0(%rip), %rsi     # 0x2bd08
callq 0x42d0
movq %rax, %rdi
movl %ebp, %esi
callq 0x45d0
leaq 0x1cacb(%rip), %rsi     # 0x2bd19
movq %rax, %rdi
callq 0x42d0
movl (%r14), %esi
movq %rax, %rdi
jmp 0xf2b9
leaq 0x1ca95(%rip), %rsi     # 0x2bcfa
callq 0x42d0
jmp 0xf2be
movl %eax, %ebx
movq 0x24d7b(%rip), %rdi     # 0x33ff0
leaq 0x1ca23(%rip), %rsi     # 0x2bc9f
callq 0x42d0
movq %rax, %rdi
movl %ebx, %esi
callq 0x45d0
leaq 0x1ca1e(%rip), %rsi     # 0x2bcb0
movq %rax, %rdi
callq 0x42d0
movl (%r14), %esi
movq %rax, %rdi
callq 0x45d0
leaq 0x1ca1d(%rip), %rsi     # 0x2bcc9
movq %rax, %rdi
callq 0x42d0
movq %rax, %rdi
movl %ebp, %esi
callq 0x45d0
movq %rax, %rdi
callq 0x4130
pushq $0x4
popq %rdi
callq 0x4140
movl $0x1, (%rax)
movq 0x24c95(%rip), %rsi     # 0x33f70
movq %rax, %rdi
xorl %edx, %edx
callq 0x45b0