// File: /workspace/github_c_makefile_star2_O3/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, %r15
movq %rsi, %r12
leaq -0x4f(%rip), %rbx       # 0x134e
movl $0x1, %r14d
movl $0x1, %edi
movq %rbx, %rsi
callq 0x10a0
movl $0x2, %edi
movq %rbx, %rsi
callq 0x10a0
movl $0xf, %edi
movq %rbx, %rsi
callq 0x10a0
movl $0x3, %edi
movq %rbx, %rsi
callq 0x10a0
leaq 0x8(%r12), %rbx
cmpq $0x0, (%r12)
je 0x14c4
movq (%rbx), %r14
leaq 0xed7(%rip), %rsi       # 0x22c8
movq %r14, %rdi
callq 0x1090
movl $0x1, %ebp
testl %eax, %eax
jne 0x1441
addq $0x18, %r12
leaq 0xebb(%rip), %r13       # 0x22c8
movq %r12, %rbx
movq -0x8(%rbx), %r12
testq %r12, %r12
je 0x14b0
movq (%rbx), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x1090
addq $0x10, %rbx
testl %eax, %eax
je 0x1410
movq %r12, %rdi
callq 0x1120
movl %eax, %ebp
addq $-0x10, %rbx
cmpb $0x2d, (%r14)
jne 0x1455
cmpb $0x68, 0x1(%r14)
jne 0x1455
cmpb $0x0, 0x2(%r14)
je 0x14a4
leaq 0xe70(%rip), %rsi       # 0x22cc
movq %r14, %rdi
callq 0x1090
testl %eax, %eax
je 0x14a4
leaq 0xe64(%rip), %rsi       # 0x22d3
movq %r14, %rdi
callq 0x1090
testl %eax, %eax
je 0x1696
leaq 0x50(%rsp), %rsi
movq %r14, %rdi
callq 0x10c0
testl %eax, %eax
jne 0x1722
testb $0x40, 0x68(%rsp)
je 0x172c
movl %ebp, %r14d
jmp 0x14bb
movq 0x2c15(%rip), %rdi      # 0x40c0
jmp 0x169d
movq %r12, %rdi
callq 0x1120
movl %eax, %r14d
testl %r14d, %r14d
js 0x16e2
movl %r14d, %ebp
movl $0x1, %edi
callq 0x1110
cmpq %rbp, %rax
jl 0x16e2
movl $0x3, %eax
cmpq $0x0, -0x18(%r15,%rax,8)
leaq 0x1(%rax), %rax
jne 0x14df
movl %eax, %edi
movl $0x8, %esi
callq 0x1080
movq %rax, %r13
movq (%r15), %rax
xorl %r12d, %r12d
testq %rax, %rax
movq %rbp, 0x8(%rsp)
je 0x1522
xorl %ecx, %ecx
movq %rax, (%r13,%rcx,8)
movq 0x8(%r15,%rcx,8), %rax
incq %rcx
testq %rax, %rax
jne 0x150c
movl %ecx, %eax
jmp 0x1524
xorl %eax, %eax
leaq 0x50(%rsp), %r15
movq %r15, (%r13,%rax,8)
leaq 0x30(%rsp), %rbp
movq %rbp, 0x8(%r13,%rax,8)
leaq 0x10(%rsp), %rcx
movq %rcx, 0x10(%r13,%rax,8)
callq 0x1050
leaq 0xddc(%rip), %rdx       # 0x232a
movl $0x20, %esi
movq %r15, %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0x1070
leaq 0xdd4(%rip), %rdx       # 0x233a
movl $0x20, %esi
movq %rbp, %rdi
movl %r14d, %ecx
xorl %eax, %eax
callq 0x1070
movl $0x4, %esi
movq 0x8(%rsp), %rbp
movq %rbp, %rdi
callq 0x1080
testl %r14d, %r14d
je 0x16a9
movq %rax, %r15
xorl %r12d, %r12d
callq 0x1140
movl %eax, (%r15,%r12,4)
testl %eax, %eax
je 0x165a
incq %r12
cmpq %r12, %rbp
jne 0x1599
testl %r14d, %r14d
je 0x16a6
xorl %r13d, %r13d
leaq 0x4(%rsp), %r14
xorl %r12d, %r12d
movl (%r15,%r13,4), %ebp
movl %ebp, %edi
movq %r14, %rsi
xorl %edx, %edx
callq 0x10f0
cmpl $-0x1, %eax
je 0x16d9
movl 0x4(%rsp), %eax
movl %eax, %ecx
andl $0x7f, %ecx
je 0x1635
movl %ecx, %eax
shll $0x18, %eax
addl $0x1000000, %eax        # imm = 0x1000000
cmpl $0x2000000, %eax        # imm = 0x2000000
jge 0x160e
shll $0x18, %ecx
addl $0x1000000, %ecx        # imm = 0x1000000
cmpl $0x2000000, %ecx        # imm = 0x2000000
jl 0x15ca
jmp 0x163a
movq 0x29e3(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
xorl %ebx, %ebx
leaq 0xd3f(%rip), %rsi       # 0x2360
movl %ebp, %edx
xorl %eax, %eax
callq 0x10b0
movl 0x4(%rsp), %ecx
andl $0x7f, %ecx
jne 0x15fb
jmp 0x163c
movzbl %ah, %ebx
jmp 0x163c
xorl %ebx, %ebx
testl %ebx, %ebx
cmovnel %ebx, %r12d
movl $0x0, (%r15,%r13,4)
incq %r13
cmpq 0x8(%rsp), %r13
jne 0x15c6
jmp 0x16a9
leaq 0xce8(%rip), %rdx       # 0x2349
leaq 0x10(%rsp), %rdi
movl $0x20, %esi
movl %r12d, %ecx
xorl %eax, %eax
callq 0x1070
movl $0x1, %edi
movl $0xf, %esi
xorl %eax, %eax
callq 0x10d0
movq (%rbx), %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x1060
jmp 0x1710
movq 0x2a1b(%rip), %rdi      # 0x40b8
callq 0x1040
xorl %eax, %eax
jmp 0x1710
xorl %r12d, %r12d
callq 0x129c
testl %eax, %eax
jne 0x16ff
xorl %eax, %eax
testl %r12d, %r12d
je 0x1710
movq 0x2938(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xcbc(%rip), %rsi       # 0x2386
movl %r12d, %edx
xorl %eax, %eax
callq 0x10b0
movl %r12d, %eax
jmp 0x1710
leaq 0xc78(%rip), %rdi       # 0x2358
jmp 0x1706
movq 0x290f(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xc1c(%rip), %rsi       # 0x230f
movl %r14d, %edx
xorl %eax, %eax
callq 0x10b0
jmp 0x170b
leaq 0xbb2(%rip), %rdi       # 0x22b8
callq 0x1100
movl $0x1, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x1160
jmp 0x170b
movq %rbx, %rdi
callq 0x1185
jmp 0x170b