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

/* Variables and functions */
int O_NOCTTY;
int O_RDWR;
int PATH_MAX;
int /*<<< orphan*/ TCSAFLUSH;
int /*<<< orphan*/ TIOCSWINSZ;
int /*<<< orphan*/ close(int);
scalar_t__ grantpt(int);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, struct winsize *);
int open(char *, int);
scalar_t__ ptsname_r(int, char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
scalar_t__ unlockpt(int);
int openpty(int *amaster, int *aslave, char *name, struct termios *termp,
            struct winsize *winp) {
  char buf[PATH_MAX];
  int master, slave;
  master = open("/dev/ptmx", O_RDWR);
  if (master == -1)
    return -1;
  if (grantpt(master))
    goto fail;
  if (unlockpt(master))
    goto fail;
  if (ptsname_r(master, buf, sizeof buf))
    goto fail;
  slave = open(buf, O_RDWR | O_NOCTTY);
  if (slave == -1)
    goto fail;
  if (termp)
    tcsetattr(slave, TCSAFLUSH, termp);
  if (winp)
    ioctl(slave, TIOCSWINSZ, winp);
  *amaster = master;
  *aslave = slave;
  if (name != NULL)
    strcpy(name, buf);
  return 0;
fail:
  close(master);
  return -1;
}


x86 assembly (clang, -O0):
<openpty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x60(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
lea    0xe24(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    1201 <openpty+0x81>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    1310 <openpty+0x190>
mov    -0x44(%rbp),%edi
call   10a0 <grantpt@plt>
cmp    $0x0,%rax
je     1218 <openpty+0x98>
jmp    12fa <openpty+0x17a>
mov    -0x44(%rbp),%edi
call   1070 <unlockpt@plt>
cmp    $0x0,%rax
je     122f <openpty+0xaf>
jmp    12fa <openpty+0x17a>
mov    -0x58(%rbp),%rsi
mov    -0x60(%rbp),%rax
mov    -0x44(%rbp),%edi
mov    %eax,%edx
call   1060 <ptsname_r@plt>
cmp    $0x0,%rax
je     1250 <openpty+0xd0>
jmp    12fa <openpty+0x17a>
mov    -0x58(%rbp),%rdi
mov    0x2d85(%rip),%rax
mov    (%rax),%esi
mov    0x2d5c(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0xffffffff,-0x48(%rbp)
jne    127d <openpty+0xfd>
jmp    12fa <openpty+0x17a>
cmpq   $0x0,-0x28(%rbp)
je     129d <openpty+0x11d>
mov    -0x48(%rbp),%edi
mov    0x2d46(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rdx
call   1080 <tcsetattr@plt>
cmpq   $0x0,-0x30(%rbp)
je     12bd <openpty+0x13d>
mov    -0x48(%rbp),%edi
mov    0x2d1e(%rip),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%rdx
call   1040 <ioctl@plt>
mov    -0x44(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x48(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
cmpq   $0x0,-0x20(%rbp)
je     12e7 <openpty+0x167>
mov    -0x58(%rbp),%rsi
mov    -0x20(%rbp),%rdi
call   1030 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    1310 <openpty+0x190>
mov    -0x44(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openpty>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8,-0x38(%rbp)
mov    %rcx,-0x30(%rbp)
mov    %rdx,-0x50(%rbp)
mov    %rsi,-0x48(%rbp)
mov    %rdi,-0x40(%rbp)
mov    0x2e44(%rip),%rax
mov    (%rax),%r13d
mov    %rsp,%rbx
lea    0xf(%r13),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2e19(%rip),%r14
mov    (%r14),%esi
lea    0xe2f(%rip),%rdi
call   1090 <open@plt>
mov    $0xffffffff,%r12d
cmp    $0xffffffff,%eax
je     1218 <openpty+0x98>
mov    %eax,%r15d
mov    %eax,%edi
call   10a0 <grantpt@plt>
test   %rax,%rax
jne    1210 <openpty+0x90>
mov    %r15d,%edi
call   1070 <unlockpt@plt>
test   %rax,%rax
jne    1210 <openpty+0x90>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %r13d,%edx
call   1060 <ptsname_r@plt>
test   %rax,%rax
je     122a <openpty+0xaa>
mov    %r15d,%edi
call   1050 <close@plt>
mov    %r12d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d8f(%rip),%rax
mov    (%rax),%esi
or     (%r14),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1210 <openpty+0x90>
mov    %eax,%r13d
mov    -0x30(%rbp),%rdx
test   %rdx,%rdx
je     1260 <openpty+0xe0>
mov    0x2d82(%rip),%rax
mov    (%rax),%esi
mov    %r13d,%edi
call   1080 <tcsetattr@plt>
mov    -0x38(%rbp),%rdx
test   %rdx,%rdx
je     127a <openpty+0xfa>
mov    0x2d60(%rip),%rax
mov    (%rax),%esi
mov    %r13d,%edi
call   1040 <ioctl@plt>
mov    -0x40(%rbp),%rax
mov    %r15d,(%rax)
mov    -0x48(%rbp),%rax
mov    %r13d,(%rax)
xor    %r12d,%r12d
mov    -0x50(%rbp),%rdi
test   %rdi,%rdi
je     1218 <openpty+0x98>
mov    %rbx,%rsi
call   1030 <strcpy@plt>
jmp    1218 <openpty+0x98>

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

LLVM IR (-O2):
%struct.termios = type { i32 }
%struct.winsize = type { i32 }
@PATH_MAX = global i32 0
@.str = [10 x i8] c"/dev/ptmx\00"
@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 = load i32, i32* @PATH_MAX
%7 = zext i32 %6 to i64
%8 = alloca i8, i64 %7
%9 = load i32, i32* @O_RDWR
%10 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %43, label %12
12: ; preds = %5
%13 = tail call i64 @grantpt(i32 %10)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %41
15: ; preds = %12
%16 = tail call i64 @unlockpt(i32 %10)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %41
18: ; preds = %15
%19 = call i64 @ptsname_r(i32 %10, i8* %8, i32 %6)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %41
21: ; preds = %18
%22 = load i32, i32* @O_RDWR
%23 = load i32, i32* @O_NOCTTY
%24 = or i32 %23, %22
%25 = call i32 @open(i8* %8, i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %41, label %27
27: ; preds = %21
%28 = icmp eq %struct.termios* %3, null
br i1 %28, label %32, label %29
29: ; preds = %27
%30 = load i32, i32* @TCSAFLUSH
%31 = call i32 @tcsetattr(i32 %25, i32 %30, %struct.termios* %3)
br label %32
32: ; preds = %29, %27
%33 = icmp eq %struct.winsize* %4, null
br i1 %33, label %37, label %34
34: ; preds = %32
%35 = load i32, i32* @TIOCSWINSZ
%36 = call i32 @ioctl(i32 %25, i32 %35, %struct.winsize* %4)
br label %37
37: ; preds = %34, %32
store i32 %10, i32* %0
store i32 %25, i32* %1
%38 = icmp eq i8* %2, null
br i1 %38, label %43, label %39
39: ; preds = %37
%40 = call i32 @strcpy(i8* %2, i8* %8)
br label %43
41: ; preds = %21, %18, %15, %12
%42 = call i32 @close(i32 %10)
br label %43
43: ; preds = %37, %39, %5, %41
%44 = phi i32 [ -1, %41 ], [ -1, %5 ], [ 0, %39 ], [ 0, %37 ]
ret i32 %44
}