#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ posix_spawnattr_t;
typedef int /*<<< orphan*/ posix_spawn_file_actions_t;
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int ENOMEM;
int FILENAME_MAX;
int /*<<< orphan*/ X_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
int posix_spawn(int /*<<< orphan*/ *, char const *, int /*<<< orphan*/ const *,
                int /*<<< orphan*/ const *, char *const *, char *const *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char const *);
int /*<<< orphan*/ *strchr(char const *, char);
char *strdup(char *);
char *strtok_r(char *, char *, char **);
int posix_spawnp(pid_t *__restrict pid, const char *__restrict file,
                 const posix_spawn_file_actions_t *fa,
                 const posix_spawnattr_t *__restrict sa,
                 char *const *__restrict cav, char *const *__restrict env) {
  char fpath[FILENAME_MAX], *last, *p;
  char *path;
  if (strchr(file, '/') != NULL || (path = getenv("PATH")) == NULL)
    return posix_spawn(pid, file, fa, sa, cav, env);
  path = strdup(path);
  if (path == NULL)
    return ENOMEM;
  for (p = strtok_r(path, ":", &last); p; p = strtok_r(NULL, ":", &last)) {
    snprintf(fpath, sizeof fpath, "%s/%s", p, file);
    fpath[FILENAME_MAX - 1] = 0;
    if (access(fpath, X_OK) == 0)
      break;
  }
  free(path);
  return posix_spawn(pid, fpath, fa, sa, cav, env);
}


x86 assembly (clang, -O0):
<posix_spawnp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x78(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x70(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
cmp    $0x0,%rax
jne    1205 <posix_spawnp+0x85>
lea    0xe0e(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x60(%rbp)
cmp    $0x0,%rax
jne    1231 <posix_spawnp+0xb1>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%r8
mov    -0x38(%rbp),%r9
call   1090 <posix_spawn@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1327 <posix_spawnp+0x1a7>
mov    -0x60(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
jne    1261 <posix_spawnp+0xe1>
mov    0x2d80(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1327 <posix_spawnp+0x1a7>
mov    -0x60(%rbp),%rdi
lea    0xd99(%rip),%rsi
lea    -0x50(%rbp),%rdx
call   1070 <strtok_r@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     12f7 <posix_spawnp+0x177>
mov    -0x70(%rbp),%rdi
mov    -0x78(%rbp),%rax
mov    %eax,%esi
mov    -0x58(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    0xd6a(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x70(%rbp),%rdi
mov    0x2d3b(%rip),%rax
mov    (%rax),%eax
sub    $0x1,%eax
cltq
movb   $0x0,(%rdi,%rax,1)
mov    0x2d21(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
cmp    $0x0,%rax
jne    12d5 <posix_spawnp+0x155>
jmp    12f7 <posix_spawnp+0x177>
jmp    12da <posix_spawnp+0x15a>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd20(%rip),%rsi
lea    -0x50(%rbp),%rdx
call   1070 <strtok_r@plt>
mov    %rax,-0x58(%rbp)
jmp    1279 <posix_spawnp+0xf9>
mov    -0x60(%rbp),%rdi
call   1040 <free@plt>
mov    -0x70(%rbp),%rsi
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%r8
mov    -0x38(%rbp),%r9
call   1090 <posix_spawn@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<posix_spawnp>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r9,-0x48(%rbp)
mov    %r8,-0x40(%rbp)
mov    %rcx,-0x38(%rbp)
mov    %rdx,-0x30(%rbp)
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e3a(%rip),%r13
mov    0x0(%r13),%r12d
mov    %rsp,%r15
lea    0xf(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    %rsi,%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11ff <posix_spawnp+0x7f>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    -0x30(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    -0x40(%rbp),%r8
mov    -0x48(%rbp),%r9
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <posix_spawn@plt>
lea    0xdfa(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11d6 <posix_spawnp+0x56>
mov    %rax,%rdi
call   10a0 <strdup@plt>
test   %rax,%rax
je     12b7 <posix_spawnp+0x137>
lea    0xddd(%rip),%rsi
lea    -0x58(%rbp),%rdx
mov    %rax,-0x50(%rbp)
mov    %rax,%rdi
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
call   1070 <strtok_r@plt>
test   %rax,%rax
je     1291 <posix_spawnp+0x111>
mov    %r15,%rdi
mov    %r12d,%esi
lea    0xdb0(%rip),%rdx
mov    %rax,%rcx
mov    %r14,%r8
call   1060 <snprintf@plt>
movslq 0x0(%r13),%rax
movb   $0x0,-0x1(%rax,%r15,1)
mov    0x2d6d(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1080 <access@plt>
test   %rax,%rax
je     1291 <posix_spawnp+0x111>
xor    %edi,%edi
lea    0xd7a(%rip),%rsi
lea    -0x58(%rbp),%rdx
jmp    1240 <posix_spawnp+0xc0>
mov    -0x50(%rbp),%rdi
call   1040 <free@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    -0x30(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    -0x40(%rbp),%r8
mov    -0x48(%rbp),%r9
call   1090 <posix_spawn@plt>
jmp    12c0 <posix_spawnp+0x140>
mov    0x2d12(%rip),%rax
mov    (%rax),%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@FILENAME_MAX = global i32 0
@.str = [5 x i8] c"PATH\00"
@ENOMEM = global i32 0
@.str.1 = [2 x i8] c":\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@X_OK = global i32 0
define i32 @posix_spawnp(i32* noalias %0, i8* noalias %1, i32* %2, i32* noalias %3, i8** noalias %4, i8** noalias %5) {
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i8**
%13 = alloca i8**
%14 = alloca i8*
%15 = alloca i64
%16 = alloca i8*
%17 = alloca i8*
%18 = alloca i8*
%19 = alloca i32
store i32* %0, i32** %8
store i8* %1, i8** %9
store i32* %2, i32** %10
store i32* %3, i32** %11
store i8** %4, i8*** %12
store i8** %5, i8*** %13
%20 = load i32, i32* @FILENAME_MAX
%21 = zext i32 %20 to i64
%22 = call i8* @llvm.stacksave()
store i8* %22, i8** %14
%23 = alloca i8, i64 %21
store i64 %21, i64* %15
%24 = load i8*, i8** %9
%25 = call i32* @strchr(i8* %24, i8 signext 47)
%26 = icmp ne i32* %25, null
br i1 %26, label %30, label %27
27: ; preds = %6
%28 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %28, i8** %18
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %38
30: ; preds = %27, %6
%31 = load i32*, i32** %8
%32 = load i8*, i8** %9
%33 = load i32*, i32** %10
%34 = load i32*, i32** %11
%35 = load i8**, i8*** %12
%36 = load i8**, i8*** %13
%37 = call i32 @posix_spawn(i32* %31, i8* %32, i32* %33, i32* %34, i8** %35, i8** %36)
store i32 %37, i32* %7
store i32 1, i32* %19
br label %76
38: ; preds = %27
%39 = load i8*, i8** %18
%40 = call i8* @strdup(i8* %39)
store i8* %40, i8** %18
%41 = load i8*, i8** %18
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %45
43: ; preds = %38
%44 = load i32, i32* @ENOMEM
store i32 %44, i32* %7
store i32 1, i32* %19
br label %76
45: ; preds = %38
%46 = load i8*, i8** %18
%47 = call i8* @strtok_r(i8* %46, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %16)
store i8* %47, i8** %17
br label %48
48: ; preds = %65, %45
%49 = load i8*, i8** %17
%50 = icmp ne i8* %49, null
br i1 %50, label %51, label %67
51: ; preds = %48
%52 = trunc i64 %21 to i32
%53 = load i8*, i8** %17
%54 = load i8*, i8** %9
%55 = call i32 @snprintf(i8* %23, i32 %52, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %53, i8* %54)
%56 = load i32, i32* @FILENAME_MAX
%57 = sub nsw i32 %56, 1
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %23, i64 %58
store i8 0, i8* %59
%60 = load i32, i32* @X_OK
%61 = call i64 @access(i8* %23, i32 %60)
%62 = icmp eq i64 %61, 0
br i1 %62, label %63, label %64
63: ; preds = %51
br label %67
64: ; preds = %51
br label %65
65: ; preds = %64
%66 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %16)
store i8* %66, i8** %17
br label %48
67: ; preds = %63, %48
%68 = load i8*, i8** %18
%69 = call i32 @free(i8* %68)
%70 = load i32*, i32** %8
%71 = load i32*, i32** %10
%72 = load i32*, i32** %11
%73 = load i8**, i8*** %12
%74 = load i8**, i8*** %13
%75 = call i32 @posix_spawn(i32* %70, i8* %23, i32* %71, i32* %72, i8** %73, i8** %74)
store i32 %75, i32* %7
store i32 1, i32* %19
br label %76
76: ; preds = %67, %43, %30
%77 = load i8*, i8** %14
%78 = load i32, i32* %7
ret i32 %78
}

LLVM IR (-O2):
@FILENAME_MAX = global i32 0
@.str = [5 x i8] c"PATH\00"
@ENOMEM = global i32 0
@.str.1 = [2 x i8] c":\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@X_OK = global i32 0
define i32 @posix_spawnp(i32* noalias %0, i8* noalias %1, i32* %2, i32* noalias %3, i8** noalias %4, i8** noalias %5) {
%7 = alloca i8*
%8 = load i32, i32* @FILENAME_MAX
%9 = zext i32 %8 to i64
%10 = alloca i8, i64 %9
%11 = bitcast i8** %7 to i8*
%12 = tail call i32* @strchr(i8* %1, i8 signext 47)
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %6
%15 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %14, %6
%18 = tail call i32 @posix_spawn(i32* %0, i8* %1, i32* %2, i32* %3, i8** %4, i8** %5)
br label %43
19: ; preds = %14
%20 = tail call i8* @strdup(i8* %15)
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = load i32, i32* @ENOMEM
br label %43
24: ; preds = %19
%25 = call i8* @strtok_r(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %7)
%26 = icmp eq i8* %25, null
br i1 %26, label %40, label %27
27: ; preds = %24, %37
%28 = phi i8* [ %38, %37 ], [ %25, %24 ]
%29 = call i32 @snprintf(i8* %10, i32 %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %28, i8* %1)
%30 = load i32, i32* @FILENAME_MAX
%31 = add nsw i32 %30, -1
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %10, i64 %32
store i8 0, i8* %33
%34 = load i32, i32* @X_OK
%35 = call i64 @access(i8* %10, i32 %34)
%36 = icmp eq i64 %35, 0
br i1 %36, label %40, label %37
37: ; preds = %27
%38 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %7)
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %27
40: ; preds = %37, %27, %24
%41 = call i32 @free(i8* %20)
%42 = call i32 @posix_spawn(i32* %0, i8* %10, i32* %2, i32* %3, i8** %4, i8** %5)
br label %43
43: ; preds = %40, %22, %17
%44 = phi i32 [ %18, %17 ], [ %23, %22 ], [ %42, %40 ]
ret i32 %44
}