// File: /workspace/github_c_makefile_star2_O2/USArmyResearchLab[P]ARL_OpenSHMEM/src/shmem_init.c
// Function: shm_alloc
static void* shm_alloc(size_t size, unsigned int gkey, int id, int* ret_fd)
{
	if (gkey == 0) return(0);

	int fd;
	void* p0;

	struct timeval t0,t1;
	int timeout;

	char shmobj0[64];

	snprintf(shmobj0,64,"/" SHM_ALLOC_SHMOBJ_TAG ".%d.%d",gkey,id);

	if (__shmem.my_pe == 0) {

		fd = shm_open( shmobj0,O_RDWR|O_CREAT|O_EXCL,0);

		if (fd < 0) {
			fprintf(stderr,"shmem error: master shm_open failed\n");
			exit(-1);
		}

		ftruncate(fd,size);

		p0 = mmap( 0,size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);

		if (!p0) {
			close(fd);
			if (ret_fd) *ret_fd = -1;
			return(0);
		}

		fchmod(fd,S_IRUSR|S_IWUSR);

	} else {

		timeout = 0;
		gettimeofday(&t0,0);
		t0.tv_sec += SHM_ALLOC_TIMEOUT;

		do {
			fd = shm_open( shmobj0, O_RDWR,0);
			gettimeofday(&t1,0);
			timeout = (t1.tv_sec > t0.tv_sec && t1.tv_usec > t0.tv_usec)? 1:0;
		} while( (fd < 0) && !timeout );
	
		if (timeout) {
			errno = ENOKEY;
			if(ret_fd) *ret_fd = -1;
			return(0);
		}

		ftruncate(fd,size);

		p0 = mmap( 0,size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);

		if (!p0) {
			close(fd);
			if (ret_fd) *ret_fd = -1;
			return(0);
		}

	}

	if (ret_fd) *ret_fd = fd;

	return(p0);	

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
testl %esi, %esi
je 0x1b9c
movq %rcx, %rbx
movl %edx, %r8d
movl %esi, %ecx
movq %rdi, %r14
leaq 0x26dd(%rip), %rdx      # 0x41be
leaq 0x30(%rsp), %rdi
pushq $0x40
popq %rsi
xorl %eax, %eax
callq 0x1100
cmpl $0x0, 0x46fd(%rip)      # 0x61f4
je 0x1ba1
movq %r14, 0x8(%rsp)
movq %rbx, (%rsp)
leaq 0x20(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x1120
movq (%r15), %rbx
movq 0x8(%r15), %r14
addq $0xa, %rbx
leaq 0x30(%rsp), %r15
pushq $0x2
popq %r12
leaq 0x10(%rsp), %r13
movq %r15, %rdi
movl %r12d, %esi
xorl %edx, %edx
callq 0x1250
movl %eax, %ebp
movq %r13, %rdi
xorl %esi, %esi
callq 0x1120
cmpq %rbx, 0x10(%rsp)
setle %cl
cmpq %r14, 0x18(%rsp)
setle %al
orb %cl, %al
testl %ebp, %ebp
jns 0x1b61
testb %al, %al
jne 0x1b2e
testb %al, %al
je 0x1bf3
movl %ebp, %edi
movq 0x8(%rsp), %rbx
movq %rbx, %rsi
callq 0x1110
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
xorl %edi, %edi
movq %rbx, %rsi
movl %ebp, %r8d
xorl %r9d, %r9d
callq 0x10e0
testq %rax, %rax
movq (%rsp), %rbx
je 0x1c07
movq %rax, %r14
jmp 0x1bec
xorl %r14d, %r14d
jmp 0x1c1b
leaq 0x30(%rsp), %rdi
movl $0xc2, %esi
xorl %edx, %edx
callq 0x1250
testl %eax, %eax
js 0x1c2d
movl %eax, %ebp
movl %eax, %edi
movq %r14, %rsi
callq 0x1110
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
xorl %edi, %edi
movq %r14, %rsi
movl %ebp, %r8d
xorl %r9d, %r9d
callq 0x10e0
testq %rax, %rax
je 0x1c07
movq %rax, %r14
movl %ebp, %edi
movl $0x180, %esi            # imm = 0x180
callq 0x11e0
testq %rbx, %rbx
jne 0x1c19
jmp 0x1c1b
callq 0x1060
movl $0x7e, (%rax)
xorl %r14d, %r14d
movq (%rsp), %rbx
jmp 0x1c11
movl %ebp, %edi
callq 0x1150
xorl %r14d, %r14d
testq %rbx, %rbx
je 0x1c1b
pushq $-0x1
popq %rbp
movl %ebp, (%rbx)
movq %r14, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x43c4(%rip), %rax      # 0x5ff8
movq (%rax), %rcx
leaq 0x2592(%rip), %rdi      # 0x41d0
pushq $0x24
popq %rsi
pushq $0x1
popq %rdx
callq 0x1240
pushq $-0x1
popq %rdi
callq 0x1230