// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0x1d6c
movq $0x0, -0x8(%rbp)
jmp 0x1f8c
leaq -0xa0(%rbp), %rdi
movl -0x14(%rbp), %ecx
movl -0x18(%rbp), %r8d
movl $0x40, %esi
leaq 0x3440(%rip), %rdx      # 0x51c6
movb $0x0, %al
callq 0x1100
cmpl $0x0, 0x5460(%rip)      # 0x71f4
jne 0x1e46
leaq -0xa0(%rbp), %rdi
movl $0xc2, %esi
xorl %edx, %edx
callq 0x1240
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jge 0x1dd8
movq 0x523b(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x3411(%rip), %rsi      # 0x51d8
movb $0x0, %al
callq 0x1180
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1230
movl -0x24(%rbp), %edi
movq -0x10(%rbp), %rsi
callq 0x1110
movq -0x10(%rbp), %rsi
movl -0x24(%rbp), %r8d
xorl %eax, %eax
movl %eax, %r9d
movl $0x3, %edx
movl $0x1, %ecx
movq %r9, %rdi
callq 0x10e0
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x1e34
movl -0x24(%rbp), %edi
callq 0x1150
cmpq $0x0, -0x20(%rbp)
je 0x1e27
movq -0x20(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq $0x0, -0x8(%rbp)
jmp 0x1f8c
movl -0x24(%rbp), %edi
movl $0x180, %esi            # imm = 0x180
callq 0x11e0
jmp 0x1f74
movl $0x0, -0x54(%rbp)
leaq -0x40(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1120
movq -0x40(%rbp), %rax
addq $0xa, %rax
movq %rax, -0x40(%rbp)
leaq -0xa0(%rbp), %rdi
movl $0x2, %esi
xorl %edx, %edx
callq 0x1240
movl %eax, -0x24(%rbp)
leaq -0x50(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1120
movq -0x50(%rbp), %rcx
xorl %eax, %eax
cmpq -0x40(%rbp), %rcx
movb %al, -0xa1(%rbp)
jle 0x1eac
movq -0x48(%rbp), %rax
cmpq -0x38(%rbp), %rax
setg %al
movb %al, -0xa1(%rbp)
movb -0xa1(%rbp), %dl
xorl %eax, %eax
movl $0x1, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movl %eax, -0x54(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x24(%rbp)
movb %al, -0xa2(%rbp)
jge 0x1edf
cmpl $0x0, -0x54(%rbp)
setne %al
xorb $-0x1, %al
movb %al, -0xa2(%rbp)
movb -0xa2(%rbp), %al
testb $0x1, %al
jne 0x1e66
cmpl $0x0, -0x54(%rbp)
je 0x1f19
callq 0x1060
movl $0x7e, (%rax)
cmpq $0x0, -0x20(%rbp)
je 0x1f0f
movq -0x20(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq $0x0, -0x8(%rbp)
jmp 0x1f8c
movl -0x24(%rbp), %edi
movq -0x10(%rbp), %rsi
callq 0x1110
movq -0x10(%rbp), %rsi
movl -0x24(%rbp), %r8d
xorl %eax, %eax
movl %eax, %r9d
movl $0x3, %edx
movl $0x1, %ecx
movq %r9, %rdi
callq 0x10e0
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x1f72
movl -0x24(%rbp), %edi
callq 0x1150
cmpq $0x0, -0x20(%rbp)
je 0x1f68
movq -0x20(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq $0x0, -0x8(%rbp)
jmp 0x1f8c
jmp 0x1f74
cmpq $0x0, -0x20(%rbp)
je 0x1f84
movl -0x24(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xb0, %rsp
popq %rbp
retq
nopl (%rax)