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

/* Variables and functions */
int ICANON;
int ISIG;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ TIOCSCTTY;
int TOSTOP;
scalar_t__ ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
scalar_t__ openpty(int *, int *, int /*<<< orphan*/ *, struct termios *,
                   int /*<<< orphan*/ *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int make_pty_pair(int *fdmaster, int *fdslave) {
  struct termios tio;
  if (openpty(fdmaster, fdslave, NULL, &tio, NULL) < 0)
    return -1;
  if (ioctl(*fdslave, TIOCSCTTY, NULL) < 0)
    return -1;
  tio.c_lflag |= (ICANON | ISIG | TOSTOP);
  if (tcsetattr(*fdslave, TCSANOW, &tio) < 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<make_pty_pair>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
lea    -0x20(%rbp),%rcx
mov    %r8,%rdx
call   1040 <openpty@plt>
cmp    $0x0,%rax
jge    116f <make_pty_pair+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f3 <make_pty_pair+0xc3>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    119d <make_pty_pair+0x6d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f3 <make_pty_pair+0xc3>
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
mov    0x2e2b(%rip),%rcx
or     (%rcx),%eax
mov    0x2e32(%rip),%rcx
or     (%rcx),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <tcsetattr@plt>
cmp    $0x0,%rax
jge    11ec <make_pty_pair+0xbc>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f3 <make_pty_pair+0xc3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_pty_pair>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rsp,%rcx
xor    %edx,%edx
xor    %r8d,%r8d
call   1040 <openpty@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
js     119f <make_pty_pair+0x6f>
mov    (%r14),%edi
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <ioctl@plt>
test   %rax,%rax
js     119f <make_pty_pair+0x6f>
mov    0x2e53(%rip),%rax
mov    0x2e64(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e69(%rip),%rax
or     (%rax),%ecx
or     %ecx,(%rsp)
mov    (%r14),%edi
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1050 <tcsetattr@plt>
mov    %rax,%rbx
sar    $0x3f,%rbx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@TIOCSCTTY = global i32 0
@ICANON = global i32 0
@ISIG = global i32 0
@TOSTOP = global i32 0
@TCSANOW = global i32 0
define i32 @make_pty_pair(i32* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca %struct.termios
store i32* %0, i32** %4
store i32* %1, i32** %5
%7 = load i32*, i32** %4
%8 = load i32*, i32** %5
%9 = call i64 @openpty(i32* %7, i32* %8, i32* null, %struct.termios* %6, i32* null)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -1, i32* %3
br label %35
12: ; preds = %2
%13 = load i32*, i32** %5
%14 = load i32, i32* %13
%15 = load i32, i32* @TIOCSCTTY
%16 = call i64 @ioctl(i32 %14, i32 %15, i32* null)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %12
store i32 -1, i32* %3
br label %35
19: ; preds = %12
%20 = load i32, i32* @ICANON
%21 = load i32, i32* @ISIG
%22 = or i32 %20, %21
%23 = load i32, i32* @TOSTOP
%24 = or i32 %22, %23
%25 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%26 = load i32, i32* %25
%27 = or i32 %26, %24
store i32 %27, i32* %25
%28 = load i32*, i32** %5
%29 = load i32, i32* %28
%30 = load i32, i32* @TCSANOW
%31 = call i64 @tcsetattr(i32 %29, i32 %30, %struct.termios* %6)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %19
store i32 -1, i32* %3
br label %35
34: ; preds = %19
store i32 0, i32* %3
br label %35
35: ; preds = %34, %33, %18, %11
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@TIOCSCTTY = global i32 0
@ICANON = global i32 0
@ISIG = global i32 0
@TOSTOP = global i32 0
@TCSANOW = global i32 0
define i32 @make_pty_pair(i32* %0, i32* %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
%5 = call i64 @openpty(i32* %0, i32* %1, i32* null, %struct.termios* %3, i32* null)
%6 = icmp slt i64 %5, 0
br i1 %6, label %26, label %7
7: ; preds = %2
%8 = load i32, i32* %1
%9 = load i32, i32* @TIOCSCTTY
%10 = call i64 @ioctl(i32 %8, i32 %9, i32* null)
%11 = icmp slt i64 %10, 0
br i1 %11, label %26, label %12
12: ; preds = %7
%13 = load i32, i32* @ICANON
%14 = load i32, i32* @ISIG
%15 = or i32 %14, %13
%16 = load i32, i32* @TOSTOP
%17 = or i32 %15, %16
%18 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%19 = load i32, i32* %18
%20 = or i32 %17, %19
store i32 %20, i32* %18
%21 = load i32, i32* %1
%22 = load i32, i32* @TCSANOW
%23 = call i64 @tcsetattr(i32 %21, i32 %22, %struct.termios* %3)
%24 = ashr i64 %23, 63
%25 = trunc i64 %24 to i32
br label %26
26: ; preds = %12, %7, %2
%27 = phi i32 [ -1, %2 ], [ -1, %7 ], [ %25, %12 ]
ret i32 %27
}