#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 */

/* Variables and functions */
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ chdir(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ close_fds();
int /*<<< orphan*/ dirname(int /*<<< orphan*/);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execv(char *, char **);
char **malloc(int);
int /*<<< orphan*/ strdup(char *);
void run_child(char *run_ptest, int fd_stdout, int fd_stderr) {
  char **argv = malloc(sizeof(char) * 2);
  chdir(dirname(strdup(run_ptest)));
  argv[0] = run_ptest;
  argv[1] = NULL;
  dup2(fd_stdout, STDOUT_FILENO);
  dup2(fd_stdout, STDERR_FILENO);
  close(fd_stderr);
  close_fds();
  execv(run_ptest, argv);
}


x86 assembly (clang, -O0):
<run_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    $0x2,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <strdup@plt>
mov    %eax,%edi
call   1050 <dirname@plt>
mov    %eax,%edi
call   1030 <chdir@plt>
mov    -0x8(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
movq   $0x0,0x8(%rax)
mov    -0xc(%rbp),%edi
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0xc(%rbp),%edi
mov    0x2def(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
mov    $0x0,%al
call   1090 <close_fds@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10a0 <execv@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_child>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %esi,%ebp
mov    %rdi,%r14
mov    $0x2,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1080 <strdup@plt>
mov    %eax,%edi
call   1050 <dirname@plt>
mov    %eax,%edi
call   1030 <chdir@plt>
mov    %r14,(%rbx)
movq   $0x0,0x8(%rbx)
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <dup2@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <dup2@plt>
mov    %r15d,%edi
call   1060 <close@plt>
xor    %eax,%eax
call   1090 <close_fds@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    10a0 <execv@plt>

LLVM IR (-O0):
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define void @run_child(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8**
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%8 = call i8** @malloc(i32 2)
store i8** %8, i8*** %7
%9 = load i8*, i8** %4
%10 = call i32 @strdup(i8* %9)
%11 = call i32 @dirname(i32 %10)
%12 = call i32 @chdir(i32 %11)
%13 = load i8*, i8** %4
%14 = load i8**, i8*** %7
%15 = getelementptr inbounds i8*, i8** %14, i64 0
store i8* %13, i8** %15
%16 = load i8**, i8*** %7
%17 = getelementptr inbounds i8*, i8** %16, i64 1
store i8* null, i8** %17
%18 = load i32, i32* %5
%19 = load i32, i32* @STDOUT_FILENO
%20 = call i32 @dup2(i32 %18, i32 %19)
%21 = load i32, i32* %5
%22 = load i32, i32* @STDERR_FILENO
%23 = call i32 @dup2(i32 %21, i32 %22)
%24 = load i32, i32* %6
%25 = call i32 @close(i32 %24)
%26 = call i32 (...) @close_fds()
%27 = load i8*, i8** %4
%28 = load i8**, i8*** %7
%29 = call i32 @execv(i8* %27, i8** %28)
ret void
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
define void @run_child(i8* %0, i32 %1, i32 %2) {
%4 = tail call i8** @malloc(i32 2)
%5 = tail call i32 @strdup(i8* %0)
%6 = tail call i32 @dirname(i32 %5)
%7 = tail call i32 @chdir(i32 %6)
store i8* %0, i8** %4
%8 = getelementptr inbounds i8*, i8** %4, i64 1
store i8* null, i8** %8
%9 = load i32, i32* @STDOUT_FILENO
%10 = tail call i32 @dup2(i32 %1, i32 %9)
%11 = load i32, i32* @STDERR_FILENO
%12 = tail call i32 @dup2(i32 %1, i32 %11)
%13 = tail call i32 @close(i32 %2)
%14 = tail call i32 (...) @close_fds()
%15 = tail call i32 @execv(i8* %0, i8** %4)
ret void
}