// File: /workspace/github_c_makefile_star2_O1/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 $0x108, %rsp            # imm = 0x108
movq %rdx, %r12
movq %rsi, %r15
leaq -0x4f(%rip), %rbx       # 0x131e
movl $0x1, %ebp
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(%r15), %r14
xorl %ebx, %ebx
cmpq $0x0, (%r15)
je 0x149c
movq (%r14), %rbx
leaq 0xf08(%rip), %rsi       # 0x22c8
movq %rbx, %rdi
callq 0x1090
movl $0x1, %ebp
testl %eax, %eax
jne 0x1410
addq $0x18, %r15
leaq 0xeec(%rip), %r13       # 0x22c8
movq %r15, %r14
movq -0x8(%r14), %r15
testq %r15, %r15
je 0x1490
movq (%r14), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1090
addq $0x10, %r14
testl %eax, %eax
je 0x13df
movq %r15, %rdi
callq 0x1120
movl %eax, %ebp
addq $-0x10, %r14
leaq 0xeb5(%rip), %rsi       # 0x22cc
movq %rbx, %rdi
callq 0x1090
testl %eax, %eax
je 0x147d
leaq 0xea5(%rip), %rsi       # 0x22cf
movq %rbx, %rdi
callq 0x1090
testl %eax, %eax
je 0x147d
leaq 0xe99(%rip), %rsi       # 0x22d6
movq %rbx, %rdi
callq 0x1090
testl %eax, %eax
je 0x172b
leaq 0x70(%rsp), %rsi
movq %rbx, %rdi
callq 0x10c0
testl %eax, %eax
jne 0x176a
testb $0x40, 0x88(%rsp)
je 0x1782
xorl %ebx, %ebx
movb $0x1, %al
testb %al, %al
jne 0x149c
jmp 0x1756
movq 0x2c3c(%rip), %rdi      # 0x40c0
callq 0x1040
xorl %ebx, %ebx
jmp 0x1756
movq %r15, %rdi
callq 0x1120
movl %eax, %ebp
xorl %ebx, %ebx
testl %ebp, %ebp
js 0x1737
movl %ebp, %r15d
movl $0x1, %edi
callq 0x1110
movq %r15, 0x18(%rsp)
cmpq %r15, %rax
jl 0x1737
movl $0x3, %eax
cmpq $0x0, -0x18(%r12,%rax,8)
leaq 0x1(%rax), %rax
jne 0x14c4
movl %eax, %edi
movl $0x8, %esi
callq 0x1080
movq %rax, %r13
movq (%r12), %rax
testq %rax, %rax
movl %ebx, 0x10(%rsp)
je 0x1504
xorl %ecx, %ecx
movq %rax, (%r13,%rcx,8)
movq 0x8(%r12,%rcx,8), %rax
incq %rcx
testq %rax, %rax
jne 0x14ee
movl %ecx, %eax
jmp 0x1506
xorl %eax, %eax
leaq 0x70(%rsp), %r12
movq %r12, (%r13,%rax,8)
leaq 0x50(%rsp), %r15
movq %r15, 0x8(%r13,%rax,8)
leaq 0x30(%rsp), %rcx
movq %rcx, 0x10(%r13,%rax,8)
callq 0x1050
leaq 0xdfd(%rip), %rdx       # 0x232d
xorl %ebx, %ebx
movl $0x20, %esi
movq %r12, %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0x1070
leaq 0xdf3(%rip), %rdx       # 0x233d
movl $0x20, %esi
movq %r15, %rdi
movl %ebp, %ecx
xorl %eax, %eax
callq 0x1070
movl $0x4, %esi
movq 0x18(%rsp), %rdi
callq 0x1080
movq %rax, %r15
testl %ebp, %ebp
setne %al
movq %r15, 0x20(%rsp)
je 0x1610
movb %al, 0xf(%rsp)
cmpl $0x2, %ebp
movl $0x1, %r12d
cmovgel %ebp, %r12d
callq 0x1140
movl %eax, (%r15)
testl %eax, %eax
je 0x15c4
decq %r12
xorl %ebx, %ebx
movq 0x20(%rsp), %r15
cmpq %rbx, %r12
je 0x1605
callq 0x1140
movl %eax, 0x4(%r15,%rbx,4)
incq %rbx
testl %eax, %eax
jne 0x15a4
cmpq 0x18(%rsp), %rbx
setb 0xf(%rsp)
leaq 0xd81(%rip), %rdx       # 0x234c
leaq 0x30(%rsp), %rdi
movl $0x20, %esi
movl %ebx, %ecx
xorl %eax, %eax
callq 0x1070
movl $0x1, %edi
movl $0xf, %esi
xorl %eax, %eax
callq 0x10d0
movq (%r14), %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x1060
movl %eax, %ebx
movb 0xf(%rsp), %al
jmp 0x1614
incq %rbx
cmpq 0x18(%rsp), %rbx
setb %al
movl 0x10(%rsp), %ebx
testb %al, %al
jne 0x1756
movl %ebx, 0x10(%rsp)
testl %ebp, %ebp
setne %al
je 0x1707
cmpl $0x2, %ebp
movl $0x1, %ecx
cmovgel %ebp, %ecx
movq %rcx, 0x28(%rsp)
leaq 0x14(%rsp), %r14
xorl %r12d, %r12d
xorl %r15d, %r15d
movb %al, 0xf(%rsp)
movq 0x20(%rsp), %rax
movl (%rax,%r12,4), %r13d
xorl %ebp, %ebp
movl %r13d, %edi
movq %r14, %rsi
xorl %edx, %edx
callq 0x10f0
movl %eax, %ebx
cmpl $-0x1, %eax
je 0x16f1
movl 0x14(%rsp), %eax
movl %eax, %ecx
andl $0x7f, %ecx
je 0x16a6
movl %ecx, %eax
shll $0x18, %eax
addl $0x1000000, %eax        # imm = 0x1000000
cmpl $0x2000000, %eax        # imm = 0x2000000
jl 0x16a9
movq 0x2968(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xcc9(%rip), %rsi       # 0x2363
movl %r13d, %edx
xorl %eax, %eax
callq 0x10b0
jmp 0x16a9
movzbl %ah, %ebp
movl 0x14(%rsp), %eax
andl $0x7f, %eax
je 0x16c1
shll $0x18, %eax
addl $0x1000000, %eax        # imm = 0x1000000
cmpl $0x2000000, %eax        # imm = 0x2000000
jl 0x1655
testl %ebp, %ebp
cmovnel %ebp, %r15d
movq 0x20(%rsp), %rax
movl $0x0, (%rax,%r12,4)
cmpl $-0x1, %ebx
je 0x170c
incq %r12
cmpq 0x18(%rsp), %r12
setb %al
cmpq 0x28(%rsp), %r12
jne 0x1646
jmp 0x1710
leaq 0xc63(%rip), %rdi       # 0x235b
callq 0x1100
movl $0x1, 0x10(%rsp)
jmp 0x16d4
xorl %r15d, %r15d
jmp 0x1710
movb 0xf(%rsp), %al
testb $0x1, %al
movl 0x10(%rsp), %ebx
jne 0x1756
callq 0x129c
testl %eax, %eax
jne 0x1774
testl %r15d, %r15d
jne 0x1796
movl %r15d, %ebx
jmp 0x1756
movq 0x2986(%rip), %rdi      # 0x40b8
jmp 0x1484
movq 0x28ba(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xbca(%rip), %rsi       # 0x2312
movl %ebp, %edx
xorl %eax, %eax
callq 0x10b0
movl $0x1, %ebx
movl %ebx, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x1160
jmp 0x178a
leaq 0xb3d(%rip), %rdi       # 0x22b8
callq 0x1100
jmp 0x1751
movq %r14, %rdi
callq 0x1185
movl $0x1, %ebx
xorl %eax, %eax
jmp 0x1474
movq 0x285b(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xbe2(%rip), %rsi       # 0x2389
movl %r15d, %edx
xorl %eax, %eax
callq 0x10b0
jmp 0x1726