#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 pid_t;

/* Variables and functions */
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ execl(char *, char *, char *, char const *,
                         int /*<<< orphan*/ *);
int fork();
int my_popen(const char *command, pid_t *pid) {
  int rc;
  rc = -1;
  {
    *pid = fork();
    switch (*pid) {
    case -1:
      break;
    case 0:
      execl("/bin/sh", "/bin/sh", "-c", command, NULL);
      _exit(1);
      break;
    default:
      rc = 0;
      break;
    }
  }
  return rc;
}


x86 assembly (clang, -O0):
<my_popen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
xor    %eax,%eax
call   1050 <fork@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
sub    $0xffffffff,%eax
je     117d <my_popen+0x4d>
jmp    116d <my_popen+0x3d>
mov    -0x18(%rbp),%eax
test   %eax,%eax
je     1182 <my_popen+0x52>
jmp    11ab <my_popen+0x7b>
jmp    11b2 <my_popen+0x82>
mov    -0x8(%rbp),%rcx
lea    0xe73(%rip),%rsi
lea    0xe74(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
mov    %rsi,%rdi
call   1040 <execl@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_popen>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
xor    %eax,%eax
call   1050 <fork@plt>
mov    %eax,(%rbx)
cmp    $0xffffffff,%eax
je     114e <my_popen+0x1e>
test   %eax,%eax
je     1156 <my_popen+0x26>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xea3(%rip),%rdi
lea    0xea4(%rip),%rdx
mov    %rdi,%rsi
mov    %r14,%rcx
xor    %r8d,%r8d
call   1040 <execl@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [3 x i8] c"-c\00"
define i32 @my_popen(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
store i8* %0, i8** %3
store i32* %1, i32** %4
store i32 -1, i32* %5
%6 = call i32 (...) @fork()
%7 = load i32*, i32** %4
store i32 %6, i32* %7
%8 = load i32*, i32** %4
%9 = load i32, i32* %8
switch i32 %9, label %15 [
i32 -1, label %10
i32 0, label %11
]
10: ; preds = %2
br label %16
11: ; preds = %2
%12 = load i8*, i8** %3
%13 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %12, i32* null)
%14 = call i32 @_exit(i32 1)
unreachable
15: ; preds = %2
store i32 0, i32* %5
br label %16
16: ; preds = %15, %10
%17 = load i32, i32* %5
ret i32 %17
}

LLVM IR (-O2):
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [3 x i8] c"-c\00"
define i32 @my_popen(i8* %0, i32* nocapture writeonly %1) {
%3 = tail call i32 (...) @fork()
store i32 %3, i32* %1
switch i32 %3, label %7 [
i32 -1, label %8
i32 0, label %4
]
4: ; preds = %2
%5 = tail call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32* null)
%6 = tail call i32 @_exit(i32 1)
unreachable
7: ; preds = %2
br label %8
8: ; preds = %7, %2
%9 = phi i32 [ 0, %7 ], [ %3, %2 ]
ret i32 %9
}