#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*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execve(char *, char **, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
void testpipe_with_exec(char *envp[]) {
  int pipefd[2];
  int status;
  pipe(pipefd);
  int pid = fork();
  if (pid == 0) {
    printf("child \n");
    char cmd[100] = "bin/scanftest";
    dup2(pipefd[0], 0);
    char *argv[10];
    argv[0] = "shashi\0";
    argv[1] = "ranjan\0";
    execve(cmd, argv, envp);
    exit(0);
  } else {
    char cmd[100] = "bin/printftest";
    dup2(pipefd[1], 1);
    char *argv[10];
    argv[0] = "shashi\0";
    argv[1] = "ranjan\0";
    execve(cmd, argv, envp);
  }
  close(pipefd[1]);
  waitpid(pid, &status, 0);
  printf("child killed now exit !!! \n");
}


x86 assembly (clang, -O0):
<testpipe_with_exec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x190,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x10(%rbp),%rdi
call   1060 <pipe@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    121e <testpipe_with_exec+0x8e>
lea    0xe3d(%rip),%rdi
call   1040 <printf@plt>
lea    -0x80(%rbp),%rdi
lea    0xe5d(%rip),%rsi
mov    $0x64,%edx
call   1080 <memcpy@plt>
mov    -0x10(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
lea    0xf16(%rip),%rax
mov    %rax,-0xd0(%rbp)
lea    0xf10(%rip),%rax
mov    %rax,-0xc8(%rbp)
lea    -0x80(%rbp),%rdi
lea    -0xd0(%rbp),%rsi
mov    -0x8(%rbp),%rdx
call   1070 <execve@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
lea    -0x140(%rbp),%rdi
lea    0xe74(%rip),%rsi
mov    $0x64,%edx
call   1080 <memcpy@plt>
mov    -0xc(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
lea    0xeba(%rip),%rax
mov    %rax,-0x190(%rbp)
lea    0xeb4(%rip),%rax
mov    %rax,-0x188(%rbp)
lea    -0x140(%rbp),%rdi
lea    -0x190(%rbp),%rsi
mov    -0x8(%rbp),%rdx
call   1070 <execve@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
lea    0xd75(%rip),%rdi
call   1040 <printf@plt>
add    $0x190,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<testpipe_with_exec>:
push   %rbp
push   %rbx
sub    $0xd8,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     1265 <testpipe_with_exec+0xe5>
mov    %eax,%ebp
movaps 0xf12(%rip),%xmm0
movaps %xmm0,0x60(%rsp)
movaps 0xef6(%rip),%xmm0
movaps %xmm0,0x50(%rsp)
movaps 0xeda(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movaps 0xebe(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0xea2(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe86(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0x0,0x70(%rsp)
mov    0xc(%rsp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
lea    0xec8(%rip),%rax
mov    %rax,0x80(%rsp)
lea    0xec1(%rip),%rax
mov    %rax,0x88(%rsp)
lea    0x10(%rsp),%rdi
lea    0x80(%rsp),%rsi
mov    %rbx,%rdx
call   1070 <execve@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   1080 <waitpid@plt>
lea    0xe8e(%rip),%rdi
call   1030 <puts@plt>
add    $0xd8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe93(%rip),%rdi
call   1030 <puts@plt>
movaps 0xdd8(%rip),%xmm0
movaps %xmm0,0x60(%rsp)
movaps 0xdbc(%rip),%xmm0
movaps %xmm0,0x50(%rsp)
movaps 0xda0(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movaps 0xd84(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0xd68(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xd4c(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0x0,0x70(%rsp)
mov    0x8(%rsp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
lea    0xe01(%rip),%rax
mov    %rax,0x80(%rsp)
lea    0xdfa(%rip),%rax
mov    %rax,0x88(%rsp)
lea    0x10(%rsp),%rdi
lea    0x80(%rsp),%rsi
mov    %rbx,%rdx
call   1070 <execve@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"child \0A\00"
@__const.testpipe_with_exec.cmd = [100 x i8] c"bin/scanftest\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str.1 = [8 x i8] c"shashi\00\00"
@.str.2 = [8 x i8] c"ranjan\00\00"
@__const.testpipe_with_exec.cmd.3 = [100 x i8] c"bin/printftest\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str.4 = [28 x i8] c"child killed now exit !!! \0A\00"
define void @testpipe_with_exec(i8** %0) {
%2 = alloca i8**
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = alloca i32
%6 = alloca [100 x i8]
%7 = alloca [10 x i8*]
%8 = alloca [100 x i8]
%9 = alloca [10 x i8*]
store i8** %0, i8*** %2
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%11 = call i32 @pipe(i32* %10)
%12 = call i32 (...) @fork()
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %28
15: ; preds = %1
%16 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%17 = bitcast [100 x i8]* %6 to i8*
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%19 = load i32, i32* %18
%20 = call i32 @dup2(i32 %19, i32 0)
%21 = getelementptr inbounds [10 x i8*], [10 x i8*]* %7, i64 0, i64 0
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8** %21
%22 = getelementptr inbounds [10 x i8*], [10 x i8*]* %7, i64 0, i64 1
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8** %22
%23 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%24 = getelementptr inbounds [10 x i8*], [10 x i8*]* %7, i64 0, i64 0
%25 = load i8**, i8*** %2
%26 = call i32 @execve(i8* %23, i8** %24, i8** %25)
%27 = call i32 @exit(i32 0)
unreachable
28: ; preds = %1
%29 = bitcast [100 x i8]* %8 to i8*
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%31 = load i32, i32* %30
%32 = call i32 @dup2(i32 %31, i32 1)
%33 = getelementptr inbounds [10 x i8*], [10 x i8*]* %9, i64 0, i64 0
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8** %33
%34 = getelementptr inbounds [10 x i8*], [10 x i8*]* %9, i64 0, i64 1
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8** %34
%35 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%36 = getelementptr inbounds [10 x i8*], [10 x i8*]* %9, i64 0, i64 0
%37 = load i8**, i8*** %2
%38 = call i32 @execve(i8* %35, i8** %36, i8** %37)
br label %39
39: ; preds = %28
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%41 = load i32, i32* %40
%42 = call i32 @close(i32 %41)
%43 = load i32, i32* %5
%44 = call i32 @waitpid(i32 %43, i32* %4, i32 0)
%45 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@__const.testpipe_with_exec.cmd = [100 x i8] c"bin/scanftest\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str.1 = [8 x i8] c"shashi\00\00"
@.str.2 = [8 x i8] c"ranjan\00\00"
@__const.testpipe_with_exec.cmd.3 = [100 x i8] c"bin/printftest\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@str = [27 x i8] c"child killed now exit !!! \00"
@str.5 = [7 x i8] c"child \00"
define void @testpipe_with_exec(i8** %0) {
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca [100 x i8]
%5 = alloca [10 x i8*]
%6 = alloca [100 x i8]
%7 = alloca [10 x i8*]
%8 = bitcast [2 x i32]* %2 to i8*
%9 = bitcast i32* %3 to i8*
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%11 = call i32 @pipe(i32* %10)
%12 = call i32 (...) @fork()
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %24
14: ; preds = %1
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.5, i64 0, i64 0))
%16 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%17 = load i32, i32* %10
%18 = call i32 @dup2(i32 %17, i32 0)
%19 = bitcast [10 x i8*]* %5 to i8*
%20 = getelementptr inbounds [10 x i8*], [10 x i8*]* %5, i64 0, i64 0
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8** %20
%21 = getelementptr inbounds [10 x i8*], [10 x i8*]* %5, i64 0, i64 1
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8** %21
%22 = call i32 @execve(i8* %16, i8** %20, i8** %0)
%23 = call i32 @exit(i32 0)
unreachable
24: ; preds = %1
%25 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%27 = load i32, i32* %26
%28 = call i32 @dup2(i32 %27, i32 1)
%29 = bitcast [10 x i8*]* %7 to i8*
%30 = getelementptr inbounds [10 x i8*], [10 x i8*]* %7, i64 0, i64 0
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8** %30
%31 = getelementptr inbounds [10 x i8*], [10 x i8*]* %7, i64 0, i64 1
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8** %31
%32 = call i32 @execve(i8* %25, i8** %30, i8** %0)
%33 = load i32, i32* %26
%34 = call i32 @close(i32 %33)
%35 = call i32 @waitpid(i32 %12, i32* %3, i32 0)
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
ret void
}