#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 */

/* Variables and functions */
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ grantpt(int);
int open(char *, int);
char *ptsname(int);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ unlockpt(int);
int openPty(int *master, int *slave, char *name) {
  strcpy(name, "/dev/ptmx");
  *master = open(name, O_RDWR | O_NONBLOCK);
  if (*master < 0) {
    return -1;
  }
  grantpt(*master);
  unlockpt(*master);
  strcpy(name, ptsname(*master));
  *slave = open(name, O_RDWR | O_NONBLOCK);
  if (*slave < 0) {
    close(*master);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<openPty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe81(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rdi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
mov    0x2e48(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
jge    11c0 <openPty+0x60>
movl   $0xffffffff,-0x4(%rbp)
jmp    1243 <openPty+0xe3>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1080 <grantpt@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1060 <unlockpt@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1030 <ptsname@plt>
mov    -0x28(%rbp),%rdi
mov    %rax,%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rdi
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
mov    0x2dd7(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
jge    123c <openPty+0xdc>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1243 <openPty+0xe3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openPty>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe87(%rip),%rsi
mov    %rdx,%rdi
call   1040 <strcpy@plt>
mov    0x2e60(%rip),%r12
mov    0x2e51(%rip),%r13
mov    0x0(%r13),%esi
or     (%r12),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
mov    %eax,%edi
mov    %eax,(%r15)
mov    $0xffffffff,%eax
test   %edi,%edi
js     11f5 <openPty+0x95>
call   1080 <grantpt@plt>
mov    (%r15),%edi
call   1060 <unlockpt@plt>
mov    (%r15),%edi
call   1030 <ptsname@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1040 <strcpy@plt>
mov    0x0(%r13),%esi
or     (%r12),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
mov    %eax,%ecx
mov    %eax,(%r14)
xor    %eax,%eax
test   %ecx,%ecx
jns    11f5 <openPty+0x95>
mov    (%r15),%edi
call   1050 <close@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/ptmx\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
define i32 @openPty(i32* %0, i32* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i8*
store i32* %0, i32** %5
store i32* %1, i32** %6
store i8* %2, i8** %7
%8 = load i8*, i8** %7
%9 = call i32 @strcpy(i8* %8, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%10 = load i8*, i8** %7
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_NONBLOCK
%13 = or i32 %11, %12
%14 = call i32 @open(i8* %10, i32 %13)
%15 = load i32*, i32** %5
store i32 %14, i32* %15
%16 = load i32*, i32** %5
%17 = load i32, i32* %16
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %3
store i32 -1, i32* %4
br label %46
20: ; preds = %3
%21 = load i32*, i32** %5
%22 = load i32, i32* %21
%23 = call i32 @grantpt(i32 %22)
%24 = load i32*, i32** %5
%25 = load i32, i32* %24
%26 = call i32 @unlockpt(i32 %25)
%27 = load i8*, i8** %7
%28 = load i32*, i32** %5
%29 = load i32, i32* %28
%30 = call i8* @ptsname(i32 %29)
%31 = call i32 @strcpy(i8* %27, i8* %30)
%32 = load i8*, i8** %7
%33 = load i32, i32* @O_RDWR
%34 = load i32, i32* @O_NONBLOCK
%35 = or i32 %33, %34
%36 = call i32 @open(i8* %32, i32 %35)
%37 = load i32*, i32** %6
store i32 %36, i32* %37
%38 = load i32*, i32** %6
%39 = load i32, i32* %38
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %20
%42 = load i32*, i32** %5
%43 = load i32, i32* %42
%44 = call i32 @close(i32 %43)
store i32 -1, i32* %4
br label %46
45: ; preds = %20
store i32 0, i32* %4
br label %46
46: ; preds = %45, %41, %19
%47 = load i32, i32* %4
ret i32 %47
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/ptmx\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
define i32 @openPty(i32* nocapture %0, i32* nocapture writeonly %1, i8* %2) {
%4 = tail call i32 @strcpy(i8* %2, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_NONBLOCK
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* %2, i32 %7)
store i32 %8, i32* %0
%9 = icmp slt i32 %8, 0
br i1 %9, label %25, label %10
10: ; preds = %3
%11 = tail call i32 @grantpt(i32 %8)
%12 = load i32, i32* %0
%13 = tail call i32 @unlockpt(i32 %12)
%14 = load i32, i32* %0
%15 = tail call i8* @ptsname(i32 %14)
%16 = tail call i32 @strcpy(i8* %2, i8* %15)
%17 = load i32, i32* @O_RDWR
%18 = load i32, i32* @O_NONBLOCK
%19 = or i32 %18, %17
%20 = tail call i32 @open(i8* %2, i32 %19)
store i32 %20, i32* %1
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %10
%23 = load i32, i32* %0
%24 = tail call i32 @close(i32 %23)
br label %25
25: ; preds = %10, %3, %22
%26 = phi i32 [ -1, %22 ], [ -1, %3 ], [ 0, %10 ]
ret i32 %26
}