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

/* Variables and functions */
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int /*<<< orphan*/);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execlp(char *, char *, char *, char const *,
                          int /*<<< orphan*/ *);
scalar_t__ fork();
pid_t exec(int stdout, int stdin, const char *cmd) {
  pid_t pid;
  pid = fork();
  if (pid == 0) {
    close(STDOUT_FILENO);
    dup2(stdout, STDOUT_FILENO);
    close(STDIN_FILENO);
    dup2(stdin, STDIN_FILENO);
    execlp("/bin/sh", "sh", "-c", cmd, NULL);
  }
  return pid;
}


x86 assembly (clang, -O0):
<exec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c9 <exec+0x89>
mov    0x2e81(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    -0x4(%rbp),%edi
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x10(%rbp),%rcx
lea    0xe4f(%rip),%rdi
lea    0xe4d(%rip),%rsi
lea    0xe49(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1050 <execlp@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exec>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %esi,%r15d
mov    %edi,%ebp
xor    %eax,%eax
call   1060 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11b4 <exec+0x74>
mov    0x2e8a(%rip),%r12
mov    (%r12),%edi
call   1040 <close@plt>
mov    (%r12),%esi
mov    %ebp,%edi
call   1030 <dup2@plt>
mov    0x2e57(%rip),%rbp
mov    0x0(%rbp),%edi
call   1040 <close@plt>
mov    0x0(%rbp),%esi
mov    %r15d,%edi
call   1030 <dup2@plt>
lea    0xe65(%rip),%rdi
lea    0xe63(%rip),%rsi
lea    0xe5f(%rip),%rdx
mov    %r14,%rcx
xor    %r8d,%r8d
call   1050 <execlp@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@STDOUT_FILENO = global i32 0
@STDIN_FILENO = global i32 0
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [3 x i8] c"sh\00"
@.str.2 = [3 x i8] c"-c\00"
define i64 @exec(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
store i32 %0, i32* %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%8 = call i64 (...) @fork()
store i64 %8, i64* %7
%9 = load i64, i64* %7
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %24
11: ; preds = %3
%12 = load i32, i32* @STDOUT_FILENO
%13 = call i32 @close(i32 %12)
%14 = load i32, i32* %4
%15 = load i32, i32* @STDOUT_FILENO
%16 = call i32 @dup2(i32 %14, i32 %15)
%17 = load i32, i32* @STDIN_FILENO
%18 = call i32 @close(i32 %17)
%19 = load i32, i32* %5
%20 = load i32, i32* @STDIN_FILENO
%21 = call i32 @dup2(i32 %19, i32 %20)
%22 = load i8*, i8** %6
%23 = call i32 @execlp(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* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %22, i32* null)
br label %24
24: ; preds = %11, %3
%25 = load i64, i64* %7
ret i64 %25
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@STDIN_FILENO = global i32 0
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [3 x i8] c"sh\00"
@.str.2 = [3 x i8] c"-c\00"
define i64 @exec(i32 %0, i32 %1, i8* %2) {
%4 = tail call i64 (...) @fork()
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %16
6: ; preds = %3
%7 = load i32, i32* @STDOUT_FILENO
%8 = tail call i32 @close(i32 %7)
%9 = load i32, i32* @STDOUT_FILENO
%10 = tail call i32 @dup2(i32 %0, i32 %9)
%11 = load i32, i32* @STDIN_FILENO
%12 = tail call i32 @close(i32 %11)
%13 = load i32, i32* @STDIN_FILENO
%14 = tail call i32 @dup2(i32 %1, i32 %13)
%15 = tail call i32 @execlp(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* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %2, i32* null)
br label %16
16: ; preds = %6, %3
ret i64 %4
}