#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*/ _exit(int);
int /*<<< orphan*/ close(int);
int dup(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ err_handler(char *);
int /*<<< orphan*/ execvp(char *, char **);
scalar_t__ fork();
scalar_t__ open_a_pty(int *, int *);
int /*<<< orphan*/ perror(char *);
int execute_ripper_encoder_with_plugin(char **program_argv, char **plugin_argv,
                                       pid_t *program_pid, pid_t *plugin_pid,
                                       int *read_fd) {
  int pty_fd0, tty_fd0, pty_fd1, tty_fd1;
  pid_t pid;
  if (open_a_pty(&pty_fd0, &tty_fd0) < 0) {
    err_handler("Cannot open pty/tty pair");
    return -1;
  }
  if (open_a_pty(&pty_fd1, &tty_fd1) < 0) {
    close(pty_fd0);
    close(tty_fd0);
    err_handler("Cannot open pty/tty pair");
    return -1;
  }
  if ((pid = fork()) < 0) {
    err_handler("Cannot fork");
    return -1;
  }
  *plugin_pid = pid;
  if (pid == 0) {
    int stderr_fd;
    stderr_fd = dup(2);
    dup2(pty_fd0, 0);
    dup2(tty_fd1, 1);
    execvp(plugin_argv[0], plugin_argv);
    dup2(stderr_fd, 2);
    perror("Failed to exec plugin");
    _exit(127);
  }
  close(pty_fd0);
  close(tty_fd1);
  *read_fd = pty_fd1;
  if ((pid = fork()) < 0) {
    err_handler("Cannot fork");
    return -1;
  }
  *program_pid = pid;
  if (pid == 0) {
    int stderr_fd;
    stderr_fd = dup(2);
    dup2(tty_fd0, 1);
    dup2(tty_fd0, 2);
    execvp(program_argv[0], program_argv);
    dup2(stderr_fd, 2);
    perror("Failed to exec the specified program");
    _exit(127);
  }
  close(tty_fd0);
  return 0;
}


x86 assembly (clang, -O0):
<execute_ripper_encoder_with_plugin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
lea    -0x34(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1050 <open_a_pty@plt>
cmp    $0x0,%rax
jge    11db <execute_ripper_encoder_with_plugin+0x4b>
lea    0xe36(%rip),%rdi
call   1040 <err_handler@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1379 <execute_ripper_encoder_with_plugin+0x1e9>
lea    -0x3c(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1050 <open_a_pty@plt>
cmp    $0x0,%rax
jge    121a <execute_ripper_encoder_with_plugin+0x8a>
mov    -0x34(%rbp),%edi
call   1080 <close@plt>
mov    -0x38(%rbp),%edi
call   1080 <close@plt>
lea    0xdf7(%rip),%rdi
call   1040 <err_handler@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1379 <execute_ripper_encoder_with_plugin+0x1e9>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
jge    1247 <execute_ripper_encoder_with_plugin+0xb7>
lea    0xde3(%rip),%rdi
call   1040 <err_handler@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1379 <execute_ripper_encoder_with_plugin+0x1e9>
mov    -0x48(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x48(%rbp)
jne    12b4 <execute_ripper_encoder_with_plugin+0x124>
mov    $0x2,%edi
call   1070 <dup@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x34(%rbp),%edi
xor    %esi,%esi
call   1060 <dup2@plt>
mov    -0x40(%rbp),%edi
mov    $0x1,%esi
call   1060 <dup2@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   10a0 <execvp@plt>
mov    -0x4c(%rbp),%edi
mov    $0x2,%esi
call   1060 <dup2@plt>
lea    0xd80(%rip),%rdi
call   1090 <perror@plt>
mov    $0x7f,%edi
call   1030 <_exit@plt>
mov    -0x34(%rbp),%edi
call   1080 <close@plt>
mov    -0x40(%rbp),%edi
call   1080 <close@plt>
mov    -0x3c(%rbp),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
jge    12fa <execute_ripper_encoder_with_plugin+0x16a>
lea    0xd30(%rip),%rdi
call   1040 <err_handler@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1379 <execute_ripper_encoder_with_plugin+0x1e9>
mov    -0x48(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x48(%rbp)
jne    136a <execute_ripper_encoder_with_plugin+0x1da>
mov    $0x2,%edi
call   1070 <dup@plt>
mov    %eax,-0x50(%rbp)
mov    -0x38(%rbp),%edi
mov    $0x1,%esi
call   1060 <dup2@plt>
mov    -0x38(%rbp),%edi
mov    $0x2,%esi
call   1060 <dup2@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   10a0 <execvp@plt>
mov    -0x50(%rbp),%edi
mov    $0x2,%esi
call   1060 <dup2@plt>
lea    0xce0(%rip),%rdi
call   1090 <perror@plt>
mov    $0x7f,%edi
call   1030 <_exit@plt>
mov    -0x38(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execute_ripper_encoder_with_plugin>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %r8,%r13
mov    %rcx,%rbx
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%r14
lea    0x4(%rsp),%rdi
mov    %rsp,%rsi
call   1050 <open_a_pty@plt>
test   %rax,%rax
js     1239 <execute_ripper_encoder_with_plugin+0xa9>
lea    0xc(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1050 <open_a_pty@plt>
test   %rax,%rax
js     1228 <execute_ripper_encoder_with_plugin+0x98>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     121f <execute_ripper_encoder_with_plugin+0x8f>
mov    %rax,(%rbx)
je     1258 <execute_ripper_encoder_with_plugin+0xc8>
mov    0x4(%rsp),%edi
call   1080 <close@plt>
mov    0x8(%rsp),%edi
call   1080 <close@plt>
mov    0xc(%rsp),%eax
mov    %eax,0x0(%r13)
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     121f <execute_ripper_encoder_with_plugin+0x8f>
mov    %rax,(%r12)
je     129d <execute_ripper_encoder_with_plugin+0x10d>
mov    (%rsp),%edi
call   1080 <close@plt>
xor    %eax,%eax
jmp    124a <execute_ripper_encoder_with_plugin+0xba>
lea    0xdf3(%rip),%rdi
jmp    1240 <execute_ripper_encoder_with_plugin+0xb0>
mov    0x4(%rsp),%edi
call   1080 <close@plt>
mov    (%rsp),%edi
call   1080 <close@plt>
lea    0xdc0(%rip),%rdi
call   1040 <err_handler@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    $0x2,%edi
call   1070 <dup@plt>
mov    %eax,%ebx
mov    0x4(%rsp),%edi
xor    %esi,%esi
call   1060 <dup2@plt>
mov    0x8(%rsp),%edi
mov    $0x1,%esi
call   1060 <dup2@plt>
mov    (%r15),%rdi
mov    %r15,%rsi
call   10a0 <execvp@plt>
mov    %ebx,%edi
mov    $0x2,%esi
call   1060 <dup2@plt>
lea    0xd8a(%rip),%rdi
jmp    12e1 <execute_ripper_encoder_with_plugin+0x151>
mov    $0x2,%edi
call   1070 <dup@plt>
mov    %eax,%ebx
mov    (%rsp),%edi
mov    $0x1,%esi
call   1060 <dup2@plt>
mov    (%rsp),%edi
mov    $0x2,%esi
call   1060 <dup2@plt>
mov    (%r14),%rdi
mov    %r14,%rsi
call   10a0 <execvp@plt>
mov    %ebx,%edi
mov    $0x2,%esi
call   1060 <dup2@plt>
lea    0xd5a(%rip),%rdi
call   1090 <perror@plt>
mov    $0x7f,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"Cannot open pty/tty pair\00"
@.str.1 = [12 x i8] c"Cannot fork\00"
@.str.2 = [22 x i8] c"Failed to exec plugin\00"
@.str.3 = [37 x i8] c"Failed to exec the specified program\00"
define i32 @execute_ripper_encoder_with_plugin(i8** %0, i8** %1, i64* %2, i64* %3, i32* %4) {
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i8**
%9 = alloca i64*
%10 = alloca i64*
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i64
%17 = alloca i32
%18 = alloca i32
store i8** %0, i8*** %7
store i8** %1, i8*** %8
store i64* %2, i64** %9
store i64* %3, i64** %10
store i32* %4, i32** %11
%19 = call i64 @open_a_pty(i32* %12, i32* %13)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %5
%22 = call i32 @err_handler(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %6
br label %91
23: ; preds = %5
%24 = call i64 @open_a_pty(i32* %14, i32* %15)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %23
%27 = load i32, i32* %12
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* %13
%30 = call i32 @close(i32 %29)
%31 = call i32 @err_handler(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %6
br label %91
32: ; preds = %23
%33 = call i64 (...) @fork()
store i64 %33, i64* %16
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = call i32 @err_handler(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %6
br label %91
37: ; preds = %32
%38 = load i64, i64* %16
%39 = load i64*, i64** %10
store i64 %38, i64* %39
%40 = load i64, i64* %16
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %57
42: ; preds = %37
%43 = call i32 @dup(i32 2)
store i32 %43, i32* %17
%44 = load i32, i32* %12
%45 = call i32 @dup2(i32 %44, i32 0)
%46 = load i32, i32* %15
%47 = call i32 @dup2(i32 %46, i32 1)
%48 = load i8**, i8*** %8
%49 = getelementptr inbounds i8*, i8** %48, i64 0
%50 = load i8*, i8** %49
%51 = load i8**, i8*** %8
%52 = call i32 @execvp(i8* %50, i8** %51)
%53 = load i32, i32* %17
%54 = call i32 @dup2(i32 %53, i32 2)
%55 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%56 = call i32 @_exit(i32 127)
unreachable
57: ; preds = %37
%58 = load i32, i32* %12
%59 = call i32 @close(i32 %58)
%60 = load i32, i32* %15
%61 = call i32 @close(i32 %60)
%62 = load i32, i32* %14
%63 = load i32*, i32** %11
store i32 %62, i32* %63
%64 = call i64 (...) @fork()
store i64 %64, i64* %16
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %57
%67 = call i32 @err_handler(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %6
br label %91
68: ; preds = %57
%69 = load i64, i64* %16
%70 = load i64*, i64** %9
store i64 %69, i64* %70
%71 = load i64, i64* %16
%72 = icmp eq i64 %71, 0
br i1 %72, label %73, label %88
73: ; preds = %68
%74 = call i32 @dup(i32 2)
store i32 %74, i32* %18
%75 = load i32, i32* %13
%76 = call i32 @dup2(i32 %75, i32 1)
%77 = load i32, i32* %13
%78 = call i32 @dup2(i32 %77, i32 2)
%79 = load i8**, i8*** %7
%80 = getelementptr inbounds i8*, i8** %79, i64 0
%81 = load i8*, i8** %80
%82 = load i8**, i8*** %7
%83 = call i32 @execvp(i8* %81, i8** %82)
%84 = load i32, i32* %18
%85 = call i32 @dup2(i32 %84, i32 2)
%86 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
%87 = call i32 @_exit(i32 127)
unreachable
88: ; preds = %68
%89 = load i32, i32* %13
%90 = call i32 @close(i32 %89)
store i32 0, i32* %6
br label %91
91: ; preds = %88, %66, %35, %26, %21
%92 = load i32, i32* %6
ret i32 %92
}

LLVM IR (-O2):
@.str = [25 x i8] c"Cannot open pty/tty pair\00"
@.str.1 = [12 x i8] c"Cannot fork\00"
@.str.2 = [22 x i8] c"Failed to exec plugin\00"
@.str.3 = [37 x i8] c"Failed to exec the specified program\00"
define i32 @execute_ripper_encoder_with_plugin(i8** %0, i8** %1, i64* nocapture writeonly %2, i64* nocapture writeonly %3, i32* nocapture writeonly %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = bitcast i32* %6 to i8*
%11 = bitcast i32* %7 to i8*
%12 = bitcast i32* %8 to i8*
%13 = bitcast i32* %9 to i8*
%14 = call i64 @open_a_pty(i32* %6, i32* %7)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %5
%17 = call i32 @err_handler(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %71
18: ; preds = %5
%19 = call i64 @open_a_pty(i32* %8, i32* %9)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %18
%22 = load i32, i32* %6
%23 = call i32 @close(i32 %22)
%24 = load i32, i32* %7
%25 = call i32 @close(i32 %24)
%26 = call i32 @err_handler(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %71
27: ; preds = %18
%28 = call i64 (...) @fork()
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = call i32 @err_handler(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %71
32: ; preds = %27
store i64 %28, i64* %3
%33 = icmp eq i64 %28, 0
br i1 %33, label %34, label %45
34: ; preds = %32
%35 = call i32 @dup(i32 2)
%36 = load i32, i32* %6
%37 = call i32 @dup2(i32 %36, i32 0)
%38 = load i32, i32* %9
%39 = call i32 @dup2(i32 %38, i32 1)
%40 = load i8*, i8** %1
%41 = call i32 @execvp(i8* %40, i8** %1)
%42 = call i32 @dup2(i32 %35, i32 2)
%43 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%44 = call i32 @_exit(i32 127)
unreachable
45: ; preds = %32
%46 = load i32, i32* %6
%47 = call i32 @close(i32 %46)
%48 = load i32, i32* %9
%49 = call i32 @close(i32 %48)
%50 = load i32, i32* %8
store i32 %50, i32* %4
%51 = call i64 (...) @fork()
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %45
%54 = call i32 @err_handler(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %71
55: ; preds = %45
store i64 %51, i64* %2
%56 = icmp eq i64 %51, 0
br i1 %56, label %57, label %68
57: ; preds = %55
%58 = call i32 @dup(i32 2)
%59 = load i32, i32* %7
%60 = call i32 @dup2(i32 %59, i32 1)
%61 = load i32, i32* %7
%62 = call i32 @dup2(i32 %61, i32 2)
%63 = load i8*, i8** %0
%64 = call i32 @execvp(i8* %63, i8** %0)
%65 = call i32 @dup2(i32 %58, i32 2)
%66 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
%67 = call i32 @_exit(i32 127)
unreachable
68: ; preds = %55
%69 = load i32, i32* %7
%70 = call i32 @close(i32 %69)
br label %71
71: ; preds = %68, %53, %30, %21, %16
%72 = phi i32 [ -1, %16 ], [ -1, %21 ], [ -1, %30 ], [ -1, %53 ], [ 0, %68 ]
ret i32 %72
}