// File: /workspace/github_c_makefile_star2_O2/USArmyResearchLab[P]ARL_OpenSHMEM/tools/oshrun.c
// Function: main
int main(int argc, char *argv[], char *envp[])
{
	int npes = 1;
	int rc = 0;
	char runid[32], n_pes[32], my_pe[32];

	signal(SIGHUP, trap);
	signal(SIGINT, trap);
	signal(SIGTERM, trap);
	signal(SIGQUIT, trap);

	while(*argv++) {
		if (!strcmp(*argv,"-np")) {
			npes = atoi(*++argv);
		} else if (!strcmp(*argv,"-h") || !strcmp(*argv, "--help")) {
			puts(help);
			return 0;
		} else if (!strcmp(*argv,"--version")) {
			puts(version);
			return 0;
		} else {
			struct stat sb;
			if (stat(*argv, &sb)) {
				fprintf(stderr, "Unrecognized option: %s\n%s\n", *argv, help);
				return 1;
			} else if (!(sb.st_mode & S_IXUSR)) {
				fprintf(stderr, "%s is not executable\n", *argv);
				return 1;
			} else break;
		}
	}

	if (npes < 0 || npes > sysconf(_SC_CHILD_MAX)) {
		fprintf(stderr, "Illegal number of PEs: %d\n", npes);
		return 1;
	}

	int ne = 0;
	while (envp[ne++]);
	char **e = calloc(ne + 3, sizeof(char*));
	ne = 0;
	while (*envp) e[ne++] = *envp++;
	e[ne++] = runid;
	e[ne++] = n_pes;
	e[ne++] = my_pe;
	snprintf(runid, sizeof(runid), "SHMEM_RUN_ID=%d", getpid());
	snprintf(n_pes, sizeof(n_pes), "SHMEM_N_PES=%d", npes);

	pid_t *pids = calloc(npes, sizeof(pid_t));
	for (int pe = 0; pe < npes; pe++) {
		if (!(pids[pe] = fork())) {
			snprintf(my_pe, sizeof(my_pe), "SHMEM_MY_PE=%d", pe);
			prctl(PR_SET_PDEATHSIG, SIGTERM);
			return execvpe(*argv, argv, e);
		}
	}

	for (int pe = 0; pe < npes; pe++) {
		int status;
		int ret = 0;
		do {
			if (waitpid(pids[pe], &status, 0) == -1) {
				perror("waitpid");
				return 1;
			}
			if (WIFEXITED(status)) {
				ret = WEXITSTATUS(status);
			} else if (WIFSIGNALED(status)) {
				fprintf(stderr, "oshrun: child %d killed by signal %d\n",
					pids[pe], WTERMSIG(status));
			}
		} while (!WIFEXITED(status) && !WIFSIGNALED(status));
		if (ret) rc = ret;
		pids[pe] = 0;
	}
	if (cleanup()) {
		perror("oshrun: cleanup");
		return 1;
	}
	if (rc) fprintf(stderr,"oshrun: child exited return code %d\n", rc);
	return rc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rdx, %r12
movq %rsi, %rbx
leaq -0x4b(%rip), %r14       # 0x12c4
pushq $0x1
popq %r15
movl %r15d, %edi
movq %r14, %rsi
callq 0x10a0
pushq $0x2
popq %rdi
movq %r14, %rsi
callq 0x10a0
pushq $0xf
popq %rdi
movq %r14, %rsi
callq 0x10a0
pushq $0x3
popq %rdi
movq %r14, %rsi
callq 0x10a0
movq (%rbx), %r14
addq $0x8, %rbx
leaq 0xf7b(%rip), %r13       # 0x22c8
testq %r14, %r14
je 0x13e3
movq (%rbx), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x1090
testl %eax, %eax
jne 0x137d
movq 0x8(%rbx), %r14
movq %r14, %rdi
callq 0x1120
movl %eax, %r15d
addq $0x10, %rbx
jmp 0x134d
cmpb $0x2d, (%r14)
jne 0x1395
cmpb $0x68, 0x1(%r14)
jne 0x1395
cmpb $0x0, 0x2(%r14)
je 0x1590
leaq 0xf30(%rip), %rsi       # 0x22cc
movq %r14, %rdi
callq 0x1090
testl %eax, %eax
je 0x1590
leaq 0xf20(%rip), %rsi       # 0x22d3
movq %r14, %rdi
callq 0x1090
testl %eax, %eax
je 0x15db
leaq 0x50(%rsp), %rsi
movq %r14, %rdi
callq 0x10c0
testl %eax, %eax
jne 0x161b
testb $0x40, 0x68(%rsp)
je 0x163f
testl %r15d, %r15d
js 0x15eb
movl %r15d, %r14d
pushq $0x1
popq %rdi
callq 0x1110
cmpq %r14, %rax
jl 0x15eb
pushq $0x3
popq %rax
cmpq $0x0, -0x18(%r12,%rax,8)
leaq 0x1(%rax), %rax
jne 0x1403
movl %eax, %edi
pushq $0x8
popq %rsi
callq 0x1080
movq %rax, %r13
xorl %eax, %eax
movq (%r12,%rax,8), %rcx
testq %rcx, %rcx
je 0x1431
movq %rcx, (%r13,%rax,8)
incq %rax
jmp 0x141e
movl %eax, %eax
leaq 0x50(%rsp), %rcx
movq %rcx, (%r13,%rax,8)
leaq 0x30(%rsp), %rcx
movq %rcx, 0x8(%r13,%rax,8)
leaq 0x10(%rsp), %rcx
movq %rcx, 0x10(%r13,%rax,8)
callq 0x1050
leaq 0xecd(%rip), %rdx       # 0x232a
xorl %r12d, %r12d
pushq $0x20
popq %rbp
leaq 0x50(%rsp), %rdi
movq %rbp, %rsi
movl %eax, %ecx
xorl %eax, %eax
callq 0x1070
leaq 0xebf(%rip), %rdx       # 0x233a
leaq 0x30(%rsp), %rdi
movq %rbp, %rsi
movl %r15d, %ecx
xorl %eax, %eax
callq 0x1070
pushq $0x4
popq %rsi
movq %r14, %rdi
callq 0x1080
movq %rax, %r15
cmpq %r12, %r14
je 0x14b6
callq 0x1140
movl %eax, (%r15,%r12,4)
testl %eax, %eax
je 0x155a
incq %r12
jmp 0x149b
movq %r14, 0x8(%rsp)
xorl %r13d, %r13d
leaq 0x4(%rsp), %r12
xorl %r14d, %r14d
cmpq 0x8(%rsp), %r13
je 0x1599
movl (%r15,%r13,4), %ebp
movl %ebp, %edi
movq %r12, %rsi
xorl %edx, %edx
callq 0x10f0
cmpl $-0x1, %eax
je 0x15cd
movl 0x4(%rsp), %eax
movl %eax, %ecx
andl $0x7f, %ecx
je 0x1540
movl %ecx, %eax
shll $0x18, %eax
addl $0x1000000, %eax        # imm = 0x1000000
cmpl $0x2000000, %eax        # imm = 0x2000000
jge 0x1519
shll $0x18, %ecx
addl $0x1000000, %ecx        # imm = 0x1000000
cmpl $0x2000000, %ecx        # imm = 0x2000000
jl 0x14d5
jmp 0x1545
movq 0x2ad8(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
xorl %ebx, %ebx
leaq 0xe34(%rip), %rsi       # 0x2360
movl %ebp, %edx
xorl %eax, %eax
callq 0x10b0
movl 0x4(%rsp), %ecx
andl $0x7f, %ecx
jne 0x1506
jmp 0x1547
movzbl %ah, %ebx
jmp 0x1547
xorl %ebx, %ebx
testl %ebx, %ebx
cmovnel %ebx, %r14d
andl $0x0, (%r15,%r13,4)
incq %r13
jmp 0x14c6
leaq 0xde8(%rip), %rdx       # 0x2349
leaq 0x10(%rsp), %rdi
pushq $0x20
popq %rsi
movl %r12d, %ecx
xorl %eax, %eax
callq 0x1070
pushq $0x1
popq %rdi
pushq $0xf
popq %rsi
xorl %eax, %eax
callq 0x10d0
movq (%rbx), %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x1060
jmp 0x1609
movq 0x2b29(%rip), %rdi      # 0x40c0
jmp 0x15e2
callq 0x124c
testl %eax, %eax
jne 0x165c
xorl %eax, %eax
testl %r14d, %r14d
je 0x1609
movq 0x2a44(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xdc8(%rip), %rsi       # 0x2386
movl %r14d, %edx
xorl %eax, %eax
callq 0x10b0
movl %r14d, %eax
jmp 0x1609
leaq 0xd84(%rip), %rdi       # 0x2358
callq 0x1100
jmp 0x1606
movq 0x2ad6(%rip), %rdi      # 0x40b8
callq 0x1040
xorl %eax, %eax
jmp 0x1609
movq 0x2a06(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xd13(%rip), %rsi       # 0x230f
movl %r15d, %edx
xorl %eax, %eax
callq 0x10b0
pushq $0x1
popq %rax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x29d6(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq (%rbx), %rdx
movq 0x2a91(%rip), %rcx      # 0x40c0
leaq 0xca7(%rip), %rsi       # 0x22dd
xorl %eax, %eax
callq 0x10b0
jmp 0x1606
movq 0x29b2(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq (%rbx), %rdx
leaq 0xca6(%rip), %rsi       # 0x22f9
xorl %eax, %eax
callq 0x10b0
jmp 0x1606
leaq 0xc55(%rip), %rdi       # 0x22b8
jmp 0x15d4