#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 /*<<< orphan*/ TCSAFLUSH;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
int getpt();
scalar_t__ grantpt(int);
int open(char *, int);
scalar_t__ pts_name(int, char **, int);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios const *);
scalar_t__ unlockpt(int);
int openpty(int *amaster, int *aslave, char *name, const struct termios *termp,
            const struct winsize *winp) {
  char _buf[512];
  char *buf = _buf;
  int master, slave;
  master = getpt();
  if (master == -1)
    return -1;
  if (grantpt(master))
    goto fail;
  if (unlockpt(master))
    goto fail;
  if (pts_name(master, &buf, sizeof(_buf)))
    goto fail;
  slave = open(buf, O_RDWR | O_NOCTTY);
  if (slave == -1) {
    if (buf != _buf)
      free(buf);
    goto fail;
  }
  if (termp)
    tcsetattr(slave, TCSAFLUSH, termp);
  *amaster = master;
  *aslave = slave;
  if (name != NULL)
    strcpy(name, buf);
  if (buf != _buf)
    free(buf);
  return 0;
fail:
  close(master);
  return -1;
}


x86 assembly (clang, -O0):
<openpty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
lea    -0x230(%rbp),%rax
mov    %rax,-0x238(%rbp)
mov    $0x0,%al
call   1070 <getpt@plt>
mov    %eax,-0x23c(%rbp)
cmpl   $0xffffffff,-0x23c(%rbp)
jne    11e3 <openpty+0x53>
movl   $0xffffffff,-0x4(%rbp)
jmp    132d <openpty+0x19d>
mov    -0x23c(%rbp),%edi
call   10b0 <grantpt@plt>
cmp    $0x0,%rax
je     11fd <openpty+0x6d>
jmp    131b <openpty+0x18b>
mov    -0x23c(%rbp),%edi
call   1080 <unlockpt@plt>
cmp    $0x0,%rax
je     1217 <openpty+0x87>
jmp    131b <openpty+0x18b>
mov    -0x23c(%rbp),%edi
lea    -0x238(%rbp),%rsi
mov    $0x200,%edx
call   1050 <pts_name@plt>
cmp    $0x0,%rax
je     123d <openpty+0xad>
jmp    131b <openpty+0x18b>
mov    -0x238(%rbp),%rdi
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
mov    0x2d7c(%rip),%rax
or     (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x240(%rbp)
cmpl   $0xffffffff,-0x240(%rbp)
jne    1296 <openpty+0x106>
mov    -0x238(%rbp),%rax
lea    -0x230(%rbp),%rcx
cmp    %rcx,%rax
je     1291 <openpty+0x101>
mov    -0x238(%rbp),%rdi
call   1030 <free@plt>
jmp    131b <openpty+0x18b>
cmpq   $0x0,-0x28(%rbp)
je     12b9 <openpty+0x129>
mov    -0x240(%rbp),%edi
mov    0x2d32(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rdx
call   1090 <tcsetattr@plt>
mov    -0x23c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x240(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
cmpq   $0x0,-0x20(%rbp)
je     12ec <openpty+0x15c>
mov    -0x20(%rbp),%rdi
mov    -0x238(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x238(%rbp),%rax
lea    -0x230(%rbp),%rcx
cmp    %rcx,%rax
je     130f <openpty+0x17f>
mov    -0x238(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    132d <openpty+0x19d>
mov    -0x23c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openpty>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %rcx,%r13
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
lea    0x10(%rsp),%rax
mov    %rax,0x8(%rsp)
xor    %eax,%eax
call   1070 <getpt@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     11ff <openpty+0x6f>
mov    %eax,%ebx
mov    %eax,%edi
call   10b0 <grantpt@plt>
test   %rax,%rax
jne    11f8 <openpty+0x68>
mov    %ebx,%edi
call   1080 <unlockpt@plt>
test   %rax,%rax
jne    11f8 <openpty+0x68>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x200,%edx
call   1050 <pts_name@plt>
test   %rax,%rax
je     1213 <openpty+0x83>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rsp),%rdi
mov    0x2dc9(%rip),%rax
mov    0x2daa(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     1284 <openpty+0xf4>
test   %r13,%r13
je     1250 <openpty+0xc0>
mov    0x2da0(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%ebp
mov    %eax,%edi
mov    %r13,%rdx
call   1090 <tcsetattr@plt>
mov    %ebp,%eax
mov    %ebx,(%r12)
mov    %eax,(%r15)
test   %r14,%r14
je     1269 <openpty+0xd9>
mov    0x8(%rsp),%rsi
mov    %r14,%rdi
call   1040 <strcpy@plt>
mov    0x8(%rsp),%rdi
xor    %ebp,%ebp
lea    0x10(%rsp),%rax
cmp    %rax,%rdi
je     11ff <openpty+0x6f>
call   1030 <free@plt>
jmp    11ff <openpty+0x6f>
mov    0x8(%rsp),%rdi
lea    0x10(%rsp),%rax
cmp    %rax,%rdi
je     11f8 <openpty+0x68>
call   1030 <free@plt>
jmp    11f8 <openpty+0x68>

LLVM IR (-O0):
%struct.termios = type { i32 }
%struct.winsize = type { i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@TCSAFLUSH = 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 [512 x i8]
%13 = alloca i8*
%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 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
store i8* %16, i8** %13
%17 = call i32 (...) @getpt()
store i32 %17, i32* %14
%18 = load i32, i32* %14
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %21
20: ; preds = %5
store i32 -1, i32* %6
br label %82
21: ; preds = %5
%22 = load i32, i32* %14
%23 = call i64 @grantpt(i32 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %21
br label %79
26: ; preds = %21
%27 = load i32, i32* %14
%28 = call i64 @unlockpt(i32 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %26
br label %79
31: ; preds = %26
%32 = load i32, i32* %14
%33 = call i64 @pts_name(i32 %32, i8** %13, i32 512)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %31
br label %79
36: ; preds = %31
%37 = load i8*, i8** %13
%38 = load i32, i32* @O_RDWR
%39 = load i32, i32* @O_NOCTTY
%40 = or i32 %38, %39
%41 = call i32 @open(i8* %37, i32 %40)
store i32 %41, i32* %15
%42 = load i32, i32* %15
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %52
44: ; preds = %36
%45 = load i8*, i8** %13
%46 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%47 = icmp ne i8* %45, %46
br i1 %47, label %48, label %51
48: ; preds = %44
%49 = load i8*, i8** %13
%50 = call i32 @free(i8* %49)
br label %51
51: ; preds = %48, %44
br label %79
52: ; preds = %36
%53 = load %struct.termios*, %struct.termios** %10
%54 = icmp ne %struct.termios* %53, null
br i1 %54, label %55, label %60
55: ; preds = %52
%56 = load i32, i32* %15
%57 = load i32, i32* @TCSAFLUSH
%58 = load %struct.termios*, %struct.termios** %10
%59 = call i32 @tcsetattr(i32 %56, i32 %57, %struct.termios* %58)
br label %60
60: ; preds = %55, %52
%61 = load i32, i32* %14
%62 = load i32*, i32** %7
store i32 %61, i32* %62
%63 = load i32, i32* %15
%64 = load i32*, i32** %8
store i32 %63, i32* %64
%65 = load i8*, i8** %9
%66 = icmp ne i8* %65, null
br i1 %66, label %67, label %71
67: ; preds = %60
%68 = load i8*, i8** %9
%69 = load i8*, i8** %13
%70 = call i32 @strcpy(i8* %68, i8* %69)
br label %71
71: ; preds = %67, %60
%72 = load i8*, i8** %13
%73 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%74 = icmp ne i8* %72, %73
br i1 %74, label %75, label %78
75: ; preds = %71
%76 = load i8*, i8** %13
%77 = call i32 @free(i8* %76)
br label %78
78: ; preds = %75, %71
store i32 0, i32* %6
br label %82
79: ; preds = %51, %35, %30, %25
%80 = load i32, i32* %14
%81 = call i32 @close(i32 %80)
store i32 -1, i32* %6
br label %82
82: ; preds = %79, %78, %20
%83 = load i32, i32* %6
ret i32 %83
}

LLVM IR (-O2):
%struct.termios = type { i32 }
%struct.winsize = type { i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@TCSAFLUSH = global i32 0
define i32 @openpty(i32* nocapture writeonly %0, i32* nocapture writeonly %1, i8* %2, %struct.termios* %3, %struct.winsize* nocapture readnone %4) {
%6 = alloca [512 x i8]
%7 = alloca i8*
%8 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%9 = bitcast i8** %7 to i8*
store i8* %8, i8** %7
%10 = call i32 (...) @getpt()
%11 = icmp eq i32 %10, -1
br i1 %11, label %50, label %12
12: ; preds = %5
%13 = call i64 @grantpt(i32 %10)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %48
15: ; preds = %12
%16 = call i64 @unlockpt(i32 %10)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %48
18: ; preds = %15
%19 = call i64 @pts_name(i32 %10, i8** %7, i32 512)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %48
21: ; preds = %18
%22 = load i8*, i8** %7
%23 = load i32, i32* @O_RDWR
%24 = load i32, i32* @O_NOCTTY
%25 = or i32 %24, %23
%26 = call i32 @open(i8* %22, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %33
28: ; preds = %21
%29 = load i8*, i8** %7
%30 = icmp eq i8* %29, %8
br i1 %30, label %48, label %31
31: ; preds = %28
%32 = call i32 @free(i8* %29)
br label %48
33: ; preds = %21
%34 = icmp eq %struct.termios* %3, null
br i1 %34, label %38, label %35
35: ; preds = %33
%36 = load i32, i32* @TCSAFLUSH
%37 = call i32 @tcsetattr(i32 %26, i32 %36, %struct.termios* %3)
br label %38
38: ; preds = %35, %33
store i32 %10, i32* %0
store i32 %26, i32* %1
%39 = icmp eq i8* %2, null
br i1 %39, label %43, label %40
40: ; preds = %38
%41 = load i8*, i8** %7
%42 = call i32 @strcpy(i8* %2, i8* %41)
br label %43
43: ; preds = %40, %38
%44 = load i8*, i8** %7
%45 = icmp eq i8* %44, %8
br i1 %45, label %50, label %46
46: ; preds = %43
%47 = call i32 @free(i8* %44)
br label %50
48: ; preds = %28, %31, %18, %15, %12
%49 = call i32 @close(i32 %10)
br label %50
50: ; preds = %43, %46, %5, %48
%51 = phi i32 [ -1, %48 ], [ -1, %5 ], [ 0, %46 ], [ 0, %43 ]
ret i32 %51
}