#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*/ TIOCSWINSZ;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
int getpt();
scalar_t__ grantpt(int);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, struct winsize const *);
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);
  if (winp)
    ioctl(slave, TIOCSWINSZ, winp);

  *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   1080 <getpt@plt>
mov    %eax,-0x23c(%rbp)
cmpl   $0xffffffff,-0x23c(%rbp)
jne    11f3 <openpty+0x53>
movl   $0xffffffff,-0x4(%rbp)
jmp    1360 <openpty+0x1c0>
mov    -0x23c(%rbp),%edi
call   10c0 <grantpt@plt>
cmp    $0x0,%rax
je     120d <openpty+0x6d>
jmp    134e <openpty+0x1ae>
mov    -0x23c(%rbp),%edi
call   1090 <unlockpt@plt>
cmp    $0x0,%rax
je     1227 <openpty+0x87>
jmp    134e <openpty+0x1ae>
mov    -0x23c(%rbp),%edi
lea    -0x238(%rbp),%rsi
mov    $0x200,%edx
call   1050 <pts_name@plt>
cmp    $0x0,%rax
je     124d <openpty+0xad>
jmp    134e <openpty+0x1ae>
mov    -0x238(%rbp),%rdi
mov    0x2d8d(%rip),%rax
mov    (%rax),%esi
mov    0x2d64(%rip),%rax
or     (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x240(%rbp)
cmpl   $0xffffffff,-0x240(%rbp)
jne    12a6 <openpty+0x106>
mov    -0x238(%rbp),%rax
lea    -0x230(%rbp),%rcx
cmp    %rcx,%rax
je     12a1 <openpty+0x101>
mov    -0x238(%rbp),%rdi
call   1030 <free@plt>
jmp    134e <openpty+0x1ae>
cmpq   $0x0,-0x28(%rbp)
je     12c9 <openpty+0x129>
mov    -0x240(%rbp),%edi
mov    0x2d22(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rdx
call   10a0 <tcsetattr@plt>
cmpq   $0x0,-0x30(%rbp)
je     12ec <openpty+0x14c>
mov    -0x240(%rbp),%edi
mov    0x2cf7(%rip),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%rdx
call   1060 <ioctl@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     131f <openpty+0x17f>
mov    -0x20(%rbp),%rdi
mov    -0x238(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x238(%rbp),%rax
lea    -0x230(%rbp),%rcx
cmp    %rcx,%rax
je     1342 <openpty+0x1a2>
mov    -0x238(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    1360 <openpty+0x1c0>
mov    -0x23c(%rbp),%edi
call   1070 <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    %r8,%r13
mov    %rcx,%r14
mov    %rdx,0x8(%rsp)
mov    %rsi,%r15
mov    %rdi,%r12
lea    0x10(%rsp),%rax
mov    %rax,(%rsp)
xor    %eax,%eax
call   1080 <getpt@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     1211 <openpty+0x71>
mov    %eax,%ebp
mov    %eax,%edi
call   10c0 <grantpt@plt>
test   %rax,%rax
jne    120a <openpty+0x6a>
mov    %ebp,%edi
call   1090 <unlockpt@plt>
test   %rax,%rax
jne    120a <openpty+0x6a>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x200,%edx
call   1050 <pts_name@plt>
test   %rax,%rax
je     1225 <openpty+0x85>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %ebx,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%rdi
mov    0x2db8(%rip),%rax
mov    0x2d91(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
je     12b5 <openpty+0x115>
test   %r14,%r14
je     1261 <openpty+0xc1>
mov    0x2d8f(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%ebx
mov    %eax,%edi
mov    %r14,%rdx
call   10a0 <tcsetattr@plt>
mov    %ebx,%eax
test   %r13,%r13
je     127d <openpty+0xdd>
mov    0x2d6b(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%ebx
mov    %eax,%edi
mov    %r13,%rdx
call   1060 <ioctl@plt>
mov    %ebx,%eax
mov    %ebp,(%r12)
mov    %eax,(%r15)
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     1297 <openpty+0xf7>
mov    (%rsp),%rsi
call   1040 <strcpy@plt>
mov    (%rsp),%rdi
xor    %ebx,%ebx
lea    0x10(%rsp),%rax
cmp    %rax,%rdi
je     1211 <openpty+0x71>
call   1030 <free@plt>
jmp    1211 <openpty+0x71>
mov    (%rsp),%rdi
lea    0x10(%rsp),%rax
cmp    %rax,%rdi
je     120a <openpty+0x6a>
call   1030 <free@plt>
jmp    120a <openpty+0x6a>

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
@TIOCSWINSZ = 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 %90
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 %87
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 %87
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 %87
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 %87
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 %struct.winsize*, %struct.winsize** %11
%62 = icmp ne %struct.winsize* %61, null
br i1 %62, label %63, label %68
63: ; preds = %60
%64 = load i32, i32* %15
%65 = load i32, i32* @TIOCSWINSZ
%66 = load %struct.winsize*, %struct.winsize** %11
%67 = call i32 @ioctl(i32 %64, i32 %65, %struct.winsize* %66)
br label %68
68: ; preds = %63, %60
%69 = load i32, i32* %14
%70 = load i32*, i32** %7
store i32 %69, i32* %70
%71 = load i32, i32* %15
%72 = load i32*, i32** %8
store i32 %71, i32* %72
%73 = load i8*, i8** %9
%74 = icmp ne i8* %73, null
br i1 %74, label %75, label %79
75: ; preds = %68
%76 = load i8*, i8** %9
%77 = load i8*, i8** %13
%78 = call i32 @strcpy(i8* %76, i8* %77)
br label %79
79: ; preds = %75, %68
%80 = load i8*, i8** %13
%81 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%82 = icmp ne i8* %80, %81
br i1 %82, label %83, label %86
83: ; preds = %79
%84 = load i8*, i8** %13
%85 = call i32 @free(i8* %84)
br label %86
86: ; preds = %83, %79
store i32 0, i32* %6
br label %90
87: ; preds = %51, %35, %30, %25
%88 = load i32, i32* %14
%89 = call i32 @close(i32 %88)
store i32 -1, i32* %6
br label %90
90: ; preds = %87, %86, %20
%91 = load i32, i32* %6
ret i32 %91
}

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
@TIOCSWINSZ = global i32 0
define i32 @openpty(i32* nocapture writeonly %0, i32* nocapture writeonly %1, i8* %2, %struct.termios* %3, %struct.winsize* %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 %55, label %12
12: ; preds = %5
%13 = call i64 @grantpt(i32 %10)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %53
15: ; preds = %12
%16 = call i64 @unlockpt(i32 %10)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %53
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 %53
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 %53, label %31
31: ; preds = %28
%32 = call i32 @free(i8* %29)
br label %53
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
%39 = icmp eq %struct.winsize* %4, null
br i1 %39, label %43, label %40
40: ; preds = %38
%41 = load i32, i32* @TIOCSWINSZ
%42 = call i32 @ioctl(i32 %26, i32 %41, %struct.winsize* %4)
br label %43
43: ; preds = %40, %38
store i32 %10, i32* %0
store i32 %26, i32* %1
%44 = icmp eq i8* %2, null
br i1 %44, label %48, label %45
45: ; preds = %43
%46 = load i8*, i8** %7
%47 = call i32 @strcpy(i8* %2, i8* %46)
br label %48
48: ; preds = %45, %43
%49 = load i8*, i8** %7
%50 = icmp eq i8* %49, %8
br i1 %50, label %55, label %51
51: ; preds = %48
%52 = call i32 @free(i8* %49)
br label %55
53: ; preds = %28, %31, %18, %15, %12
%54 = call i32 @close(i32 %10)
br label %55
55: ; preds = %48, %51, %5, %53
%56 = phi i32 [ -1, %53 ], [ -1, %5 ], [ 0, %51 ], [ 0, %48 ]
ret i32 %56
}