#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 */
struct termios {
  int dummy;
};
typedef int pid_t;

/* Variables and functions */
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int fork();
scalar_t__ grantpt(int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int posix_openpt(int);
char *ptsname(int);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
scalar_t__ unlockpt(int);
pid_t popen2(int *tty, char *cmd[]) {
  int master_fd;
  char *slave_dev = NULL;
  pid_t pid;
  if ((master_fd = posix_openpt(O_RDWR | O_NOCTTY)) < 0 ||
      grantpt(master_fd) != 0 || unlockpt(master_fd) != 0 ||
      (slave_dev = ptsname(master_fd)) == NULL)
    return -1;
  int slave_fd = open(slave_dev, O_RDWR | O_NOCTTY);
  if (slave_fd == -1)
    return -1;
  pid = fork();
  if (pid < 0)
    return pid;
  if (pid == 0) {
    close(master_fd);
    struct termios slave_orig_term_settings;
    struct termios new_term_settings;
    tcgetattr(slave_fd, &slave_orig_term_settings);
    new_term_settings = slave_orig_term_settings;
    cfmakeraw(&new_term_settings);
    tcsetattr(slave_fd, TCSANOW, &new_term_settings);
    dup2(slave_fd, 0);
    dup2(slave_fd, 1);
    execvp(*cmd, cmd);
    perror("execvp");
    exit(1);
  }
  if (tty == NULL) {
    close(master_fd);
  } else {
    *tty = master_fd;
  }
  return pid;
}


x86 assembly (clang, -O0):
<popen2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
mov    0x2dc8(%rip),%rax
or     (%rax),%edi
call   10f0 <posix_openpt@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jl     1255 <popen2+0x75>
mov    -0x1c(%rbp),%edi
call   10d0 <grantpt@plt>
cmp    $0x0,%rax
jne    1255 <popen2+0x75>
mov    -0x1c(%rbp),%edi
call   1060 <unlockpt@plt>
cmp    $0x0,%rax
jne    1255 <popen2+0x75>
mov    -0x1c(%rbp),%edi
call   1030 <ptsname@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1261 <popen2+0x81>
movl   $0xffffffff,-0x4(%rbp)
jmp    135a <popen2+0x17a>
mov    -0x28(%rbp),%rdi
mov    0x2d7c(%rip),%rax
mov    (%rax),%esi
mov    0x2d5b(%rip),%rax
or     (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    1295 <popen2+0xb5>
movl   $0xffffffff,-0x4(%rbp)
jmp    135a <popen2+0x17a>
mov    $0x0,%al
call   1100 <fork@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    12b4 <popen2+0xd4>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    135a <popen2+0x17a>
cmpl   $0x0,-0x2c(%rbp)
jne    1333 <popen2+0x153>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x30(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1080 <tcgetattr@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x40(%rbp)
lea    -0x40(%rbp),%rdi
call   1070 <cfmakeraw@plt>
mov    -0x30(%rbp),%edi
mov    0x2cf5(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1090 <tcsetattr@plt>
mov    -0x30(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    -0x30(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   10c0 <execvp@plt>
lea    0xcdc(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    134b <popen2+0x16b>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
jmp    1354 <popen2+0x174>
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<popen2>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2ded(%rip),%r12
mov    0x2dce(%rip),%r13
mov    0x0(%r13),%edi
or     (%r12),%edi
call   10f0 <posix_openpt@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     1232 <popen2+0x52>
mov    %eax,%ebx
mov    %eax,%edi
call   10d0 <grantpt@plt>
test   %rax,%rax
jne    1232 <popen2+0x52>
mov    %ebx,%edi
call   1060 <unlockpt@plt>
test   %rax,%rax
je     1243 <popen2+0x63>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebx,%edi
call   1030 <ptsname@plt>
test   %rax,%rax
je     1232 <popen2+0x52>
mov    0x0(%r13),%esi
or     (%r12),%esi
mov    %rax,%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     1232 <popen2+0x52>
mov    %eax,%r12d
xor    %eax,%eax
call   1100 <fork@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1232 <popen2+0x52>
je     1289 <popen2+0xa9>
test   %r15,%r15
je     1280 <popen2+0xa0>
mov    %ebx,(%r15)
jmp    1232 <popen2+0x52>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    1232 <popen2+0x52>
mov    %ebx,%edi
call   1050 <close@plt>
lea    0x10(%rsp),%rsi
mov    %r12d,%edi
call   1080 <tcgetattr@plt>
mov    0x10(%rsp),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <cfmakeraw@plt>
mov    0x2d27(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
mov    %rbx,%rdx
call   1090 <tcsetattr@plt>
mov    %r12d,%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    %r12d,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    (%r14),%rdi
mov    %r14,%rsi
call   10c0 <execvp@plt>
lea    0xd11(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.termios = type { i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@TCSANOW = global i32 0
@.str = [7 x i8] c"execvp\00"
define i32 @popen2(i32* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.termios
%11 = alloca %struct.termios
store i32* %0, i32** %4
store i8** %1, i8*** %5
store i8* null, i8** %7
%12 = load i32, i32* @O_RDWR
%13 = load i32, i32* @O_NOCTTY
%14 = or i32 %12, %13
%15 = call i32 @posix_openpt(i32 %14)
store i32 %15, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %29, label %17
17: ; preds = %2
%18 = load i32, i32* %6
%19 = call i64 @grantpt(i32 %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %29, label %21
21: ; preds = %17
%22 = load i32, i32* %6
%23 = call i64 @unlockpt(i32 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %29, label %25
25: ; preds = %21
%26 = load i32, i32* %6
%27 = call i8* @ptsname(i32 %26)
store i8* %27, i8** %7
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %30
29: ; preds = %25, %21, %17, %2
store i32 -1, i32* %3
br label %80
30: ; preds = %25
%31 = load i8*, i8** %7
%32 = load i32, i32* @O_RDWR
%33 = load i32, i32* @O_NOCTTY
%34 = or i32 %32, %33
%35 = call i32 @open(i8* %31, i32 %34)
store i32 %35, i32* %9
%36 = load i32, i32* %9
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %39
38: ; preds = %30
store i32 -1, i32* %3
br label %80
39: ; preds = %30
%40 = call i32 (...) @fork()
store i32 %40, i32* %8
%41 = load i32, i32* %8
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %39
%44 = load i32, i32* %8
store i32 %44, i32* %3
br label %80
45: ; preds = %39
%46 = load i32, i32* %8
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %69
48: ; preds = %45
%49 = load i32, i32* %6
%50 = call i32 @close(i32 %49)
%51 = load i32, i32* %9
%52 = call i32 @tcgetattr(i32 %51, %struct.termios* %10)
%53 = bitcast %struct.termios* %11 to i8*
%54 = bitcast %struct.termios* %10 to i8*
%55 = call i32 @cfmakeraw(%struct.termios* %11)
%56 = load i32, i32* %9
%57 = load i32, i32* @TCSANOW
%58 = call i32 @tcsetattr(i32 %56, i32 %57, %struct.termios* %11)
%59 = load i32, i32* %9
%60 = call i32 @dup2(i32 %59, i32 0)
%61 = load i32, i32* %9
%62 = call i32 @dup2(i32 %61, i32 1)
%63 = load i8**, i8*** %5
%64 = load i8*, i8** %63
%65 = load i8**, i8*** %5
%66 = call i32 @execvp(i8* %64, i8** %65)
%67 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%68 = call i32 @exit(i32 1)
unreachable
69: ; preds = %45
%70 = load i32*, i32** %4
%71 = icmp eq i32* %70, null
br i1 %71, label %72, label %75
72: ; preds = %69
%73 = load i32, i32* %6
%74 = call i32 @close(i32 %73)
br label %78
75: ; preds = %69
%76 = load i32, i32* %6
%77 = load i32*, i32** %4
store i32 %76, i32* %77
br label %78
78: ; preds = %75, %72
%79 = load i32, i32* %8
store i32 %79, i32* %3
br label %80
80: ; preds = %78, %43, %38, %29
%81 = load i32, i32* %3
ret i32 %81
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@TCSANOW = global i32 0
@.str = [7 x i8] c"execvp\00"
define i32 @popen2(i32* writeonly %0, i8** %1) {
%3 = alloca %struct.termios
%4 = alloca %struct.termios
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_NOCTTY
%7 = or i32 %6, %5
%8 = tail call i32 @posix_openpt(i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %52, label %10
10: ; preds = %2
%11 = tail call i64 @grantpt(i32 %8)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %52
13: ; preds = %10
%14 = tail call i64 @unlockpt(i32 %8)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %52
16: ; preds = %13
%17 = tail call i8* @ptsname(i32 %8)
%18 = icmp eq i8* %17, null
br i1 %18, label %52, label %19
19: ; preds = %16
%20 = load i32, i32* @O_RDWR
%21 = load i32, i32* @O_NOCTTY
%22 = or i32 %21, %20
%23 = tail call i32 @open(i8* %17, i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %52, label %25
25: ; preds = %19
%26 = tail call i32 (...) @fork()
%27 = icmp slt i32 %26, 0
br i1 %27, label %52, label %28
28: ; preds = %25
%29 = icmp eq i32 %26, 0
br i1 %29, label %30, label %47
30: ; preds = %28
%31 = tail call i32 @close(i32 %8)
%32 = bitcast %struct.termios* %3 to i8*
%33 = bitcast %struct.termios* %4 to i8*
%34 = call i32 @tcgetattr(i32 %23, %struct.termios* %3)
%35 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%36 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%37 = load i32, i32* %35
store i32 %37, i32* %36
%38 = call i32 @cfmakeraw(%struct.termios* %4)
%39 = load i32, i32* @TCSANOW
%40 = call i32 @tcsetattr(i32 %23, i32 %39, %struct.termios* %4)
%41 = call i32 @dup2(i32 %23, i32 0)
%42 = call i32 @dup2(i32 %23, i32 1)
%43 = load i8*, i8** %1
%44 = call i32 @execvp(i8* %43, i8** %1)
%45 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %28
%48 = icmp eq i32* %0, null
br i1 %48, label %49, label %51
49: ; preds = %47
%50 = tail call i32 @close(i32 %8)
br label %52
51: ; preds = %47
store i32 %8, i32* %0
br label %52
52: ; preds = %19, %25, %51, %49, %2, %10, %13, %16
%53 = phi i32 [ -1, %16 ], [ -1, %13 ], [ -1, %10 ], [ -1, %2 ], [ -1, %19 ], [ %26, %25 ], [ %26, %51 ], [ %26, %49 ]
ret i32 %53
}