// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl $0x1, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x1, %edi
leaq -0x77(%rip), %rsi       # 0x12e0
callq 0x10a0
movl $0x2, %edi
leaq -0x88(%rip), %rsi       # 0x12e0
callq 0x10a0
movl $0xf, %edi
leaq -0x99(%rip), %rsi       # 0x12e0
callq 0x10a0
movl $0x3, %edi
leaq -0xaa(%rip), %rsi       # 0x12e0
callq 0x10a0
movq -0x10(%rbp), %rax
movq %rax, %rcx
addq $0x8, %rcx
movq %rcx, -0x10(%rbp)
cmpq $0x0, (%rax)
je 0x14e2
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq 0xf12(%rip), %rsi       # 0x22c8
callq 0x1090
cmpl $0x0, %eax
jne 0x13e0
movq -0x10(%rbp), %rax
movq %rax, %rcx
addq $0x8, %rcx
movq %rcx, -0x10(%rbp)
movq 0x8(%rax), %rdi
callq 0x1120
movl %eax, -0x1c(%rbp)
jmp 0x14dd
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq 0xede(%rip), %rsi       # 0x22cc
callq 0x1090
cmpl $0x0, %eax
je 0x1410
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq 0xec9(%rip), %rsi       # 0x22cf
callq 0x1090
cmpl $0x0, %eax
jne 0x1428
movq 0x2ca9(%rip), %rdi      # 0x40c0
callq 0x1040
movl $0x0, -0x4(%rbp)
jmp 0x18ac
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq 0xea0(%rip), %rsi       # 0x22d6
callq 0x1090
cmpl $0x0, %eax
jne 0x1458
movq 0x2c71(%rip), %rdi      # 0x40b8
callq 0x1040
movl $0x0, -0x4(%rbp)
jmp 0x18ac
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq -0x110(%rbp), %rsi
callq 0x10c0
cmpl $0x0, %eax
je 0x14a2
movq 0x2b81(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
movq 0x2c38(%rip), %rcx      # 0x40c0
leaq 0xe51(%rip), %rsi       # 0x22e0
movb $0x0, %al
callq 0x10b0
movl $0x1, -0x4(%rbp)
jmp 0x18ac
movl -0xf8(%rbp), %eax
andl $0x40, %eax
cmpl $0x0, %eax
jne 0x14db
movq 0x2b41(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0xe34(%rip), %rsi       # 0x22fc
movb $0x0, %al
callq 0x10b0
movl $0x1, -0x4(%rbp)
jmp 0x18ac
jmp 0x14e2
jmp 0x138f
cmpl $0x0, -0x1c(%rbp)
jl 0x150c
movslq -0x1c(%rbp), %rax
movq %rax, -0x140(%rbp)
movl $0x1, %edi
callq 0x1110
movq %rax, %rcx
movq -0x140(%rbp), %rax
cmpq %rcx, %rax
jle 0x1533
movq 0x2ae5(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movl -0x1c(%rbp), %edx
leaq 0xdf2(%rip), %rsi       # 0x2312
movb $0x0, %al
callq 0x10b0
movl $0x1, -0x4(%rbp)
jmp 0x18ac
movl $0x0, -0x114(%rbp)
movq -0x18(%rbp), %rax
movl -0x114(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x114(%rbp)
movslq %ecx, %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x155e
jmp 0x153d
movl -0x114(%rbp), %eax
addl $0x3, %eax
movslq %eax, %rdi
movl $0x8, %esi
callq 0x1080
movq %rax, -0x120(%rbp)
movl $0x0, -0x114(%rbp)
movq -0x18(%rbp), %rax
cmpq $0x0, (%rax)
je 0x15c2
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x8, %rcx
movq %rcx, -0x18(%rbp)
movq (%rax), %rdx
movq -0x120(%rbp), %rax
movl -0x114(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x114(%rbp)
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0x1585
leaq -0x40(%rbp), %rdx
movq -0x120(%rbp), %rax
movl -0x114(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x114(%rbp)
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
leaq -0x60(%rbp), %rdx
movq -0x120(%rbp), %rax
movl -0x114(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x114(%rbp)
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
leaq -0x80(%rbp), %rdx
movq -0x120(%rbp), %rax
movl -0x114(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x114(%rbp)
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
leaq -0x40(%rbp), %rax
movq %rax, -0x148(%rbp)
callq 0x1050
movq -0x148(%rbp), %rdi
movl %eax, %ecx
movl $0x20, %esi
leaq 0xcdd(%rip), %rdx       # 0x232d
movb $0x0, %al
callq 0x1070
leaq -0x60(%rbp), %rdi
movl -0x1c(%rbp), %ecx
movl $0x20, %esi
leaq 0xcd3(%rip), %rdx       # 0x233d
movb $0x0, %al
callq 0x1070
movslq -0x1c(%rbp), %rdi
movl $0x4, %esi
callq 0x1080
movq %rax, -0x128(%rbp)
movl $0x0, -0x12c(%rbp)
movl -0x12c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x1719
callq 0x1140
movq -0x128(%rbp), %rcx
movslq -0x12c(%rbp), %rdx
movl %eax, (%rcx,%rdx,4)
cmpl $0x0, %eax
jne 0x1703
leaq -0x80(%rbp), %rdi
movl -0x12c(%rbp), %ecx
movl $0x20, %esi
leaq 0xc80(%rip), %rdx       # 0x234c
movb $0x0, %al
callq 0x1070
movl $0x1, %edi
movl $0xf, %esi
movb $0x0, %al
callq 0x10d0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rsi
movq -0x120(%rbp), %rdx
callq 0x1060
movl %eax, -0x4(%rbp)
jmp 0x18ac
jmp 0x1705
movl -0x12c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x12c(%rbp)
jmp 0x1690
movl $0x0, -0x130(%rbp)
movl -0x130(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x1866
movl $0x0, -0x138(%rbp)
movq -0x128(%rbp), %rax
movslq -0x130(%rbp), %rcx
movl (%rax,%rcx,4), %edi
leaq -0x134(%rbp), %rsi
xorl %edx, %edx
callq 0x10f0
cmpl $-0x1, %eax
jne 0x1778
leaq 0xbf4(%rip), %rdi       # 0x235b
callq 0x1100
movl $0x1, -0x4(%rbp)
jmp 0x18ac
movl -0x134(%rbp), %eax
andl $0x7f, %eax
cmpl $0x0, %eax
jne 0x179c
movl -0x134(%rbp), %eax
andl $0xff00, %eax           # imm = 0xFF00
sarl $0x8, %eax
movl %eax, -0x138(%rbp)
jmp 0x17e6
movl -0x134(%rbp), %eax
andl $0x7f, %eax
addl $0x1, %eax
movsbl %al, %eax
sarl %eax
cmpl $0x0, %eax
jle 0x17e4
movq 0x283f(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x128(%rbp), %rax
movslq -0x130(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movl -0x134(%rbp), %ecx
andl $0x7f, %ecx
leaq 0xb86(%rip), %rsi       # 0x2363
movb $0x0, %al
callq 0x10b0
jmp 0x17e6
jmp 0x17e8
movl -0x134(%rbp), %ecx
andl $0x7f, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x149(%rbp)
je 0x181d
movl -0x134(%rbp), %eax
andl $0x7f, %eax
addl $0x1, %eax
movsbl %al, %eax
sarl %eax
cmpl $0x0, %eax
setg %al
xorb $-0x1, %al
movb %al, -0x149(%rbp)
movb -0x149(%rbp), %al
testb $0x1, %al
jne 0x173c
cmpl $0x0, -0x138(%rbp)
je 0x183d
movl -0x138(%rbp), %eax
movl %eax, -0x20(%rbp)
movq -0x128(%rbp), %rax
movslq -0x130(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x130(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x130(%rbp)
jmp 0x1723
callq 0x1250
cmpl $0x0, %eax
je 0x1885
leaq 0xa41(%rip), %rdi       # 0x22b8
callq 0x1100
movl $0x1, -0x4(%rbp)
jmp 0x18ac
cmpl $0x0, -0x20(%rbp)
je 0x18a6
movq 0x2766(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movl -0x20(%rbp), %edx
leaq 0xaea(%rip), %rsi       # 0x2389
movb $0x0, %al
callq 0x10b0
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq