#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 /*<<< orphan*/ c_lflag;
};
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ ECHO;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ TIOCSCTTY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int errno;
int /*<<< orphan*/ exec_cmd(int, char **);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int grantpt(int);
int /*<<< orphan*/ ioctl(int /*<<< orphan*/, int /*<<< orphan*/, int);
int open(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int posix_openpt(int /*<<< orphan*/);
int /*<<< orphan*/ ptsname(int);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ stderr;
int tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int unlockpt(int);
pid_t popen_tty(int ac, char *av[], int *fd) {
  int fdm, fds;
  int rc;
  fdm = posix_openpt(O_RDWR);
  if (fdm < 0) {
    fprintf(stderr, "Error %d on posix_openpt()\n", errno);
    return 1;
  }
  rc = grantpt(fdm);
  if (rc != 0) {
    fprintf(stderr, "Error %d on grantpt()\n", errno);
    return 1;
  }
  rc = unlockpt(fdm);
  if (rc != 0) {
    fprintf(stderr, "Error %d on unlockpt()\n", errno);
    return 1;
  }
  fds = open(ptsname(fdm), O_RDWR);
  pid_t pid;
  if ((pid = fork()) == -1) {
    perror("Error while fork");
    exit(-1);
  } else if (pid == 0) {
    struct termios slave_orig_term_settings;
    struct termios new_term_settings;
    close(fdm);
    rc = tcgetattr(fds, &slave_orig_term_settings);
    new_term_settings = slave_orig_term_settings;
    new_term_settings.c_lflag &= ~ECHO;
    tcsetattr(fds, TCSANOW, &new_term_settings);
    close(0);
    close(1);
    close(2);
    dup2(fds, 0);
    dup2(fds, 1);
    dup2(fds, 2);
    close(fds);
    setsid();
    ioctl(0, TIOCSCTTY, 1);
    exec_cmd(ac, av);
  }
  close(fds);
  *fd = fdm;
  return pid;
}


x86 assembly (clang, -O0):
<popen_tty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   1110 <posix_openpt@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1258 <popen_tty+0x58>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
mov    0x2d82(%rip),%rax
mov    (%rax),%edx
lea    0xdb9(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13f3 <popen_tty+0x1f3>
mov    -0x1c(%rbp),%edi
call   10f0 <grantpt@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1297 <popen_tty+0x97>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
mov    0x2d43(%rip),%rax
mov    (%rax),%edx
lea    0xd96(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13f3 <popen_tty+0x1f3>
mov    -0x1c(%rbp),%edi
call   1080 <unlockpt@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     12d6 <popen_tty+0xd6>
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
mov    0x2d04(%rip),%rax
mov    (%rax),%edx
lea    0xd6e(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13f3 <popen_tty+0x1f3>
mov    -0x1c(%rbp),%edi
call   1030 <ptsname@plt>
mov    %eax,%edi
mov    0x2d01(%rip),%rax
mov    (%rax),%esi
call   10d0 <open@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1120 <fork@plt>
mov    %eax,-0x28(%rbp)
cmp    $0xffffffff,%eax
jne    131a <popen_tty+0x11a>
lea    0xd40(%rip),%rdi
call   10e0 <perror@plt>
mov    $0xffffffff,%edi
call   1100 <exit@plt>
cmpl   $0x0,-0x28(%rbp)
jne    13d7 <popen_tty+0x1d7>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   10b0 <tcgetattr@plt>
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2c90(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x20(%rbp),%edi
mov    0x2c83(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   10c0 <tcsetattr@plt>
xor    %edi,%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   1060 <close@plt>
mov    $0x2,%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    -0x20(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0x20(%rbp),%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
mov    $0x0,%al
call   1070 <setsid@plt>
mov    0x2bfb(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
mov    $0x1,%edx
call   1050 <ioctl@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   10a0 <exec_cmd@plt>
jmp    13dc <popen_tty+0x1dc>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<popen_tty>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%r12d
mov    0x2dca(%rip),%rbx
mov    (%rbx),%edi
call   1110 <posix_openpt@plt>
test   %eax,%eax
js     1251 <popen_tty+0x51>
mov    %eax,%ebp
mov    %eax,%edi
call   10f0 <grantpt@plt>
test   %eax,%eax
je     126c <popen_tty+0x6c>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
mov    0x2d7a(%rip),%rax
mov    (%rax),%edx
lea    0xdcd(%rip),%rsi
jmp    1290 <popen_tty+0x90>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
mov    0x2d5f(%rip),%rax
mov    (%rax),%edx
lea    0xd96(%rip),%rsi
jmp    1290 <popen_tty+0x90>
mov    %ebp,%edi
call   1080 <unlockpt@plt>
test   %eax,%eax
je     12ab <popen_tty+0xab>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
mov    0x2d39(%rip),%rax
mov    (%rax),%edx
lea    0xda3(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%edi
call   1030 <ptsname@plt>
mov    (%rbx),%esi
mov    %eax,%edi
call   10d0 <open@plt>
mov    %eax,%r13d
xor    %eax,%eax
call   1120 <fork@plt>
mov    %eax,%ebx
test   %eax,%eax
je     12ea <popen_tty+0xea>
cmp    $0xffffffff,%ebx
jne    1395 <popen_tty+0x195>
lea    0xd70(%rip),%rdi
call   10e0 <perror@plt>
mov    $0xffffffff,%edi
call   1100 <exit@plt>
mov    %ebp,%edi
call   1060 <close@plt>
lea    0x10(%rsp),%rsi
mov    %r13d,%edi
call   10b0 <tcgetattr@plt>
mov    0x2cd3(%rip),%rax
mov    (%rax),%eax
not    %eax
and    0x10(%rsp),%eax
mov    %eax,0x8(%rsp)
mov    0x2cc8(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %r13d,%edi
call   10c0 <tcsetattr@plt>
xor    %edi,%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   1060 <close@plt>
mov    $0x2,%edi
call   1060 <close@plt>
mov    %r13d,%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    %r13d,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    %r13d,%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
mov    %r13d,%edi
call   1060 <close@plt>
xor    %eax,%eax
call   1070 <setsid@plt>
mov    0x2c3c(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
mov    $0x1,%edx
call   1050 <ioctl@plt>
mov    %r12d,%edi
mov    %r15,%rsi
call   10a0 <exec_cmd@plt>
mov    %r13d,%edi
call   1060 <close@plt>
mov    %ebp,(%r14)
jmp    129a <popen_tty+0x9a>

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

LLVM IR (-O2):
%struct.termios = type { i32 }
@O_RDWR = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"Error %d on posix_openpt()\0A\00"
@errno = global i32 0
@.str.1 = [23 x i8] c"Error %d on grantpt()\0A\00"
@.str.2 = [24 x i8] c"Error %d on unlockpt()\0A\00"
@.str.3 = [17 x i8] c"Error while fork\00"
@ECHO = global i32 0
@TCSANOW = global i32 0
@TIOCSCTTY = global i32 0
define i32 @popen_tty(i32 %0, i8** %1, i32* nocapture writeonly %2) {
%4 = alloca %struct.termios
%5 = alloca %struct.termios
%6 = load i32, i32* @O_RDWR
%7 = tail call i32 @posix_openpt(i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %3
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %11)
br label %61
13: ; preds = %3
%14 = tail call i32 @grantpt(i32 %7)
%15 = icmp eq i32 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %18)
br label %61
20: ; preds = %13
%21 = tail call i32 @unlockpt(i32 %7)
%22 = icmp eq i32 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %20
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %25)
br label %61
27: ; preds = %20
%28 = tail call i32 @ptsname(i32 %7)
%29 = load i32, i32* @O_RDWR
%30 = tail call i32 @open(i32 %28, i32 %29)
%31 = tail call i32 (...) @fork()
switch i32 %31, label %59 [
i32 -1, label %32
i32 0, label %35
]
32: ; preds = %27
%33 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%34 = tail call i32 @exit(i32 -1)
unreachable
35: ; preds = %27
%36 = bitcast %struct.termios* %4 to i8*
%37 = bitcast %struct.termios* %5 to i8*
%38 = tail call i32 @close(i32 %7)
%39 = call i32 @tcgetattr(i32 %30, %struct.termios* %4)
%40 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%41 = getelementptr inbounds %struct.termios, %struct.termios* %5, i64 0, i32 0
%42 = load i32, i32* %40
%43 = load i32, i32* @ECHO
%44 = xor i32 %43, -1
%45 = and i32 %42, %44
store i32 %45, i32* %41
%46 = load i32, i32* @TCSANOW
%47 = call i32 @tcsetattr(i32 %30, i32 %46, %struct.termios* %5)
%48 = call i32 @close(i32 0)
%49 = call i32 @close(i32 1)
%50 = call i32 @close(i32 2)
%51 = call i32 @dup2(i32 %30, i32 0)
%52 = call i32 @dup2(i32 %30, i32 1)
%53 = call i32 @dup2(i32 %30, i32 2)
%54 = call i32 @close(i32 %30)
%55 = call i32 (...) @setsid()
%56 = load i32, i32* @TIOCSCTTY
%57 = call i32 @ioctl(i32 0, i32 %56, i32 1)
%58 = call i32 @exec_cmd(i32 %0, i8** %1)
br label %59
59: ; preds = %27, %35
%60 = call i32 @close(i32 %30)
store i32 %7, i32* %2
br label %61
61: ; preds = %59, %23, %16, %9
%62 = phi i32 [ 1, %9 ], [ 1, %16 ], [ 1, %23 ], [ %31, %59 ]
ret i32 %62
}