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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execve(char *, char **, char **);
int /*<<< orphan*/ *fdopen(int, char *);
int fork();
int /*<<< orphan*/ pipe(int *);
FILE *my_popen_write(char *command, char *argv[], char *envp[]) {
  int fd[2];
  int read_fd, write_fd;
  int pid;
  pipe(fd);
  read_fd = fd[0];
  write_fd = fd[1];
  pid = fork();
  if (pid == 0) {
    close(write_fd);
    dup2(read_fd, 0);
    close(read_fd);
    execve(command, argv, envp);
    return NULL;
  } else {
    close(read_fd);
    return fdopen(write_fd, "w");
  }
}


x86 assembly (clang, -O0):
<my_popen_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x28(%rbp),%rdi
call   1050 <pipe@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    11d5 <my_popen_write+0x75>
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
mov    -0x2c(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1060 <execve@plt>
movq   $0x0,-0x8(%rbp)
jmp    11f0 <my_popen_write+0x90>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x30(%rbp),%edi
lea    0xe19(%rip),%rsi
call   1070 <fdopen@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_popen_write>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
mov    0x8(%rsp),%ebp
mov    0xc(%rsp),%ebx
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11ac <my_popen_write+0x4c>
mov    %ebp,%edi
call   1040 <close@plt>
lea    0xe60(%rip),%rsi
mov    %ebx,%edi
call   1070 <fdopen@plt>
mov    %rax,%rbx
jmp    11d3 <my_popen_write+0x73>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %ebx,%ebx
mov    %ebp,%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1060 <execve@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
define i32* @my_popen_write(i8* %0, i8** %1, i8** %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca [2 x i32]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%13 = call i32 @pipe(i32* %12)
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%15 = load i32, i32* %14
store i32 %15, i32* %9
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%17 = load i32, i32* %16
store i32 %17, i32* %10
%18 = call i32 (...) @fork()
store i32 %18, i32* %11
%19 = load i32, i32* %11
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %32
21: ; preds = %3
%22 = load i32, i32* %10
%23 = call i32 @close(i32 %22)
%24 = load i32, i32* %9
%25 = call i32 @dup2(i32 %24, i32 0)
%26 = load i32, i32* %9
%27 = call i32 @close(i32 %26)
%28 = load i8*, i8** %5
%29 = load i8**, i8*** %6
%30 = load i8**, i8*** %7
%31 = call i32 @execve(i8* %28, i8** %29, i8** %30)
store i32* null, i32** %4
br label %37
32: ; preds = %3
%33 = load i32, i32* %9
%34 = call i32 @close(i32 %33)
%35 = load i32, i32* %10
%36 = call i32* @fdopen(i32 %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %36, i32** %4
br label %37
37: ; preds = %32, %21
%38 = load i32*, i32** %4
ret i32* %38
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
define noalias i32* @my_popen_write(i8* %0, i8** %1, i8** %2) {
%4 = alloca [2 x i32]
%5 = bitcast [2 x i32]* %4 to i8*
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%7 = call i32 @pipe(i32* %6)
%8 = load i32, i32* %6
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%10 = load i32, i32* %9
%11 = call i32 (...) @fork()
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %3
%14 = call i32 @close(i32 %10)
%15 = call i32 @dup2(i32 %8, i32 0)
%16 = call i32 @close(i32 %8)
%17 = call i32 @execve(i8* %0, i8** %1, i8** %2)
br label %21
18: ; preds = %3
%19 = call i32 @close(i32 %8)
%20 = call i32* @fdopen(i32 %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %18, %13
%22 = phi i32* [ null, %13 ], [ %20, %18 ]
ret i32* %22
}