#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_iflag;
};

/* Variables and functions */
int /*<<< orphan*/ IXOFF;
int /*<<< orphan*/ IXON;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ getenv(char *);
int sf_open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int open_pty_slave(char *name) {
  int sfd;
  struct termios tty_rmode;
  if ((sfd = sf_open(name, O_RDWR, 0)) < 0) {
    return -1;
  }
  if (getenv("RUN_ERL_DISABLE_FLOWCNTRL")) {
    if (tcgetattr(sfd, &tty_rmode) < 0) {
      fprintf(stderr, "Cannot get terminal's current mode\n");
      exit(-1);
    }
    tty_rmode.c_iflag &= ~IXOFF;
    if (tcsetattr(sfd, TCSANOW, &tty_rmode) < 0) {
      fprintf(stderr, "Cannot disable terminal's flow control on input\n");
      exit(-1);
    }
    tty_rmode.c_iflag &= ~IXON;
    if (tcsetattr(sfd, TCSANOW, &tty_rmode) < 0) {
      fprintf(stderr, "Cannot disable terminal's flow control on output\n");
      exit(-1);
    }
  }
  return sfd;
}


x86 assembly (clang, -O0):
<open_pty_slave>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <sf_open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1198 <open_pty_slave+0x38>
movl   $0xffffffff,-0x4(%rbp)
jmp    128e <open_pty_slave+0x12e>
lea    0xe61(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     1288 <open_pty_slave+0x128>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1060 <tcgetattr@plt>
cmp    $0x0,%rax
jge    11e3 <open_pty_slave+0x83>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
lea    0xe46(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    0x2df6(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1070 <tcsetattr@plt>
cmp    $0x0,%rax
jge    1233 <open_pty_slave+0xd3>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
lea    0xe1a(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d81(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1070 <tcsetattr@plt>
cmp    $0x0,%rax
jge    1283 <open_pty_slave+0x123>
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
lea    0xdfb(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
jmp    1288 <open_pty_slave+0x128>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_pty_slave>:
push   %r14
push   %rbx
push   %rax
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <sf_open@plt>
test   %eax,%eax
js     11f3 <open_pty_slave+0x93>
mov    %eax,%ebx
lea    0xe7f(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11f8 <open_pty_slave+0x98>
mov    %rsp,%rsi
mov    %ebx,%edi
call   1060 <tcgetattr@plt>
test   %rax,%rax
js     1202 <open_pty_slave+0xa2>
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,(%rsp)
mov    0x2e21(%rip),%r14
mov    (%r14),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1070 <tcsetattr@plt>
test   %rax,%rax
js     1214 <open_pty_slave+0xb4>
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,(%rsp)
mov    (%r14),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1070 <tcsetattr@plt>
test   %rax,%rax
jns    11f8 <open_pty_slave+0x98>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
lea    0xe7e(%rip),%rsi
jmp    1224 <open_pty_slave+0xc4>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
lea    0xe08(%rip),%rsi
jmp    1224 <open_pty_slave+0xc4>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
lea    0xe1a(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.termios = type { i32 }
@O_RDWR = global i32 0
@.str = [26 x i8] c"RUN_ERL_DISABLE_FLOWCNTRL\00"
@stderr = global i32 0
@.str.1 = [36 x i8] c"Cannot get terminal's current mode\0A\00"
@IXOFF = global i32 0
@TCSANOW = global i32 0
@.str.2 = [49 x i8] c"Cannot disable terminal's flow control on input\0A\00"
@IXON = global i32 0
@.str.3 = [50 x i8] c"Cannot disable terminal's flow control on output\0A\00"
define i32 @open_pty_slave(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.termios
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = load i32, i32* @O_RDWR
%8 = call i32 @sf_open(i8* %6, i32 %7, i32 0)
store i32 %8, i32* %4
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 -1, i32* %2
br label %53
11: ; preds = %1
%12 = call i64 @getenv(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %51
14: ; preds = %11
%15 = load i32, i32* %4
%16 = call i64 @tcgetattr(i32 %15, %struct.termios* %5)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 -1)
unreachable
22: ; preds = %14
%23 = load i32, i32* @IXOFF
%24 = xor i32 %23, -1
%25 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%26 = load i32, i32* %25
%27 = and i32 %26, %24
store i32 %27, i32* %25
%28 = load i32, i32* %4
%29 = load i32, i32* @TCSANOW
%30 = call i64 @tcsetattr(i32 %28, i32 %29, %struct.termios* %5)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %22
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0))
%35 = call i32 @exit(i32 -1)
unreachable
36: ; preds = %22
%37 = load i32, i32* @IXON
%38 = xor i32 %37, -1
%39 = getelementptr inbounds %struct.termios, %struct.termios* %5, i32 0, i32 0
%40 = load i32, i32* %39
%41 = and i32 %40, %38
store i32 %41, i32* %39
%42 = load i32, i32* %4
%43 = load i32, i32* @TCSANOW
%44 = call i64 @tcsetattr(i32 %42, i32 %43, %struct.termios* %5)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %36
%47 = load i32, i32* @stderr
%48 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0))
%49 = call i32 @exit(i32 -1)
unreachable
50: ; preds = %36
br label %51
51: ; preds = %50, %11
%52 = load i32, i32* %4
store i32 %52, i32* %2
br label %53
53: ; preds = %51, %10
%54 = load i32, i32* %2
ret i32 %54
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@O_RDWR = global i32 0
@.str = [26 x i8] c"RUN_ERL_DISABLE_FLOWCNTRL\00"
@stderr = global i32 0
@.str.1 = [36 x i8] c"Cannot get terminal's current mode\0A\00"
@IXOFF = global i32 0
@TCSANOW = global i32 0
@.str.2 = [49 x i8] c"Cannot disable terminal's flow control on input\0A\00"
@IXON = global i32 0
@.str.3 = [50 x i8] c"Cannot disable terminal's flow control on output\0A\00"
define i32 @open_pty_slave(i8* %0) {
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %2 to i8*
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @sf_open(i8* %0, i32 %4, i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %42, label %7
7: ; preds = %1
%8 = tail call i64 @getenv(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i64 %8, 0
br i1 %9, label %42, label %10
10: ; preds = %7
%11 = call i64 @tcgetattr(i32 %5, %struct.termios* %2)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %10
%18 = load i32, i32* @IXOFF
%19 = xor i32 %18, -1
%20 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%21 = load i32, i32* %20
%22 = and i32 %21, %19
store i32 %22, i32* %20
%23 = load i32, i32* @TCSANOW
%24 = call i64 @tcsetattr(i32 %5, i32 %23, %struct.termios* %2)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %17
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @exit(i32 -1)
unreachable
30: ; preds = %17
%31 = load i32, i32* @IXON
%32 = xor i32 %31, -1
%33 = load i32, i32* %20
%34 = and i32 %33, %32
store i32 %34, i32* %20
%35 = load i32, i32* @TCSANOW
%36 = call i64 @tcsetattr(i32 %5, i32 %35, %struct.termios* %2)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %30
%39 = load i32, i32* @stderr
%40 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0))
%41 = call i32 @exit(i32 -1)
unreachable
42: ; preds = %7, %30, %1
%43 = phi i32 [ -1, %1 ], [ %5, %30 ], [ %5, %7 ]
ret i32 %43
}