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

/* Variables and functions */
int O_NOCTTY;
int O_RDWR;
int errno;
int grantpt(int);
int open(char *, int);
scalar_t__ ptsname_r(int, char *, int);
int unlockpt(int);
int openpty(int *amaster, int *aslave, char *name, const struct termios *termp,
            const struct winsize *winp) {
  char buf[128];
  int fd, pts_fd;
  int retval;
  fd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
  if (fd < 0)
    return -1;
  retval = grantpt(fd);
  if (retval) {
    errno = retval;
    return -1;
  }
  retval = unlockpt(fd);
  if (retval) {
    errno = retval;
    return -1;
  }
  if (ptsname_r(fd, buf, sizeof(buf)) < 0)
    return -1;
  pts_fd = open(buf, O_RDWR | O_NOCTTY);
  if (pts_fd < 0)
    return -1;
  *amaster = fd;
  *aslave = pts_fd;
  return 0;
}


x86 assembly (clang, -O0):
<openpty>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
mov    0x2e69(%rip),%rax
or     (%rax),%esi
lea    0xe88(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0xb4(%rbp)
cmpl   $0x0,-0xb4(%rbp)
jge    119c <openpty+0x5c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1297 <openpty+0x157>
mov    -0xb4(%rbp),%edi
call   1060 <grantpt@plt>
mov    %eax,-0xbc(%rbp)
cmpl   $0x0,-0xbc(%rbp)
je     11d5 <openpty+0x95>
mov    -0xbc(%rbp),%ecx
mov    0x2e09(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1297 <openpty+0x157>
mov    -0xb4(%rbp),%edi
call   1040 <unlockpt@plt>
mov    %eax,-0xbc(%rbp)
cmpl   $0x0,-0xbc(%rbp)
je     120e <openpty+0xce>
mov    -0xbc(%rbp),%ecx
mov    0x2dd0(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1297 <openpty+0x157>
mov    -0xb4(%rbp),%edi
lea    -0xb0(%rbp),%rsi
mov    $0x80,%edx
call   1030 <ptsname_r@plt>
cmp    $0x0,%rax
jge    123b <openpty+0xfb>
movl   $0xffffffff,-0x4(%rbp)
jmp    1297 <openpty+0x157>
lea    -0xb0(%rbp),%rdi
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
mov    0x2d86(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0xb8(%rbp)
cmpl   $0x0,-0xb8(%rbp)
jge    1278 <openpty+0x138>
movl   $0xffffffff,-0x4(%rbp)
jmp    1297 <openpty+0x157>
mov    -0xb4(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0xb8(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openpty>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e8a(%rip),%r12
mov    0x2e73(%rip),%r13
mov    0x0(%r13),%esi
or     (%r12),%esi
lea    0xe8c(%rip),%rdi
call   1050 <open@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     11a3 <openpty+0x63>
mov    %eax,%ebp
mov    %eax,%edi
call   1060 <grantpt@plt>
test   %eax,%eax
jne    119a <openpty+0x5a>
mov    %ebp,%edi
call   1040 <unlockpt@plt>
test   %eax,%eax
je     11b7 <openpty+0x77>
mov    0x2e2f(%rip),%rcx
mov    %eax,(%rcx)
mov    %ebx,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x80,%edx
call   1030 <ptsname_r@plt>
test   %rax,%rax
js     11a3 <openpty+0x63>
mov    0x0(%r13),%esi
or     (%r12),%esi
mov    %rsp,%rdi
call   1050 <open@plt>
test   %eax,%eax
js     11a3 <openpty+0x63>
mov    %ebp,(%r15)
mov    %eax,(%r14)
xor    %ebx,%ebx
jmp    11a3 <openpty+0x63>

LLVM IR (-O0):
%struct.termios = type { i32 }
%struct.winsize = type { i32 }
@.str = [10 x i8] c"/dev/ptmx\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@errno = global i32 0
define i32 @openpty(i32* %0, i32* %1, i8* %2, %struct.termios* %3, %struct.winsize* %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca %struct.termios*
%11 = alloca %struct.winsize*
%12 = alloca [128 x i8]
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32* %0, i32** %7
store i32* %1, i32** %8
store i8* %2, i8** %9
store %struct.termios* %3, %struct.termios** %10
store %struct.winsize* %4, %struct.winsize** %11
%16 = load i32, i32* @O_RDWR
%17 = load i32, i32* @O_NOCTTY
%18 = or i32 %16, %17
%19 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %18)
store i32 %19, i32* %13
%20 = load i32, i32* %13
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %5
store i32 -1, i32* %6
br label %57
23: ; preds = %5
%24 = load i32, i32* %13
%25 = call i32 @grantpt(i32 %24)
store i32 %25, i32* %15
%26 = load i32, i32* %15
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = load i32, i32* %15
store i32 %29, i32* @errno
store i32 -1, i32* %6
br label %57
30: ; preds = %23
%31 = load i32, i32* %13
%32 = call i32 @unlockpt(i32 %31)
store i32 %32, i32* %15
%33 = load i32, i32* %15
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %30
%36 = load i32, i32* %15
store i32 %36, i32* @errno
store i32 -1, i32* %6
br label %57
37: ; preds = %30
%38 = load i32, i32* %13
%39 = getelementptr inbounds [128 x i8], [128 x i8]* %12, i64 0, i64 0
%40 = call i64 @ptsname_r(i32 %38, i8* %39, i32 128)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %37
store i32 -1, i32* %6
br label %57
43: ; preds = %37
%44 = getelementptr inbounds [128 x i8], [128 x i8]* %12, i64 0, i64 0
%45 = load i32, i32* @O_RDWR
%46 = load i32, i32* @O_NOCTTY
%47 = or i32 %45, %46
%48 = call i32 @open(i8* %44, i32 %47)
store i32 %48, i32* %14
%49 = load i32, i32* %14
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %43
store i32 -1, i32* %6
br label %57
52: ; preds = %43
%53 = load i32, i32* %13
%54 = load i32*, i32** %7
store i32 %53, i32* %54
%55 = load i32, i32* %14
%56 = load i32*, i32** %8
store i32 %55, i32* %56
store i32 0, i32* %6
br label %57
57: ; preds = %52, %51, %42, %35, %28, %22
%58 = load i32, i32* %6
ret i32 %58
}

LLVM IR (-O2):
%struct.termios = type { i32 }
%struct.winsize = type { i32 }
@.str = [10 x i8] c"/dev/ptmx\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@errno = global i32 0
define i32 @openpty(i32* nocapture writeonly %0, i32* nocapture writeonly %1, i8* nocapture readnone %2, %struct.termios* nocapture readnone %3, %struct.winsize* nocapture readnone %4) {
%6 = alloca [128 x i8]
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_NOCTTY
%10 = or i32 %9, %8
%11 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %31, label %13
13: ; preds = %5
%14 = tail call i32 @grantpt(i32 %11)
%15 = icmp eq i32 %14, 0
br i1 %15, label %17, label %16
16: ; preds = %13
store i32 %14, i32* @errno
br label %31
17: ; preds = %13
%18 = tail call i32 @unlockpt(i32 %11)
%19 = icmp eq i32 %18, 0
br i1 %19, label %21, label %20
20: ; preds = %17
store i32 %18, i32* @errno
br label %31
21: ; preds = %17
%22 = call i64 @ptsname_r(i32 %11, i8* %7, i32 128)
%23 = icmp slt i64 %22, 0
br i1 %23, label %31, label %24
24: ; preds = %21
%25 = load i32, i32* @O_RDWR
%26 = load i32, i32* @O_NOCTTY
%27 = or i32 %26, %25
%28 = call i32 @open(i8* %7, i32 %27)
%29 = icmp slt i32 %28, 0
br i1 %29, label %31, label %30
30: ; preds = %24
store i32 %11, i32* %0
store i32 %28, i32* %1
br label %31
31: ; preds = %24, %21, %5, %30, %20, %16
%32 = phi i32 [ -1, %16 ], [ -1, %20 ], [ 0, %30 ], [ -1, %5 ], [ -1, %21 ], [ -1, %24 ]
ret i32 %32
}