#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 PATH_MAX;
int /*<<< orphan*/ TCSAFLUSH;
scalar_t__ __unlockpt(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
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 *);
int openpty(int *ptx, int *pty, char *name, const struct termios *termp,
            const struct winsize *winp) {
  char _buf[PATH_MAX];
  char *buf = _buf;
  int ptx_fd, ret = -1, pty_fd = -1;
  *buf = '\0';
  ptx_fd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
  if (ptx_fd == -1)
    return -1;
  if (__unlockpt(ptx_fd))
    goto on_error;
  if (pty_fd == -1) {
    if (pts_name(ptx_fd, &buf, sizeof(_buf)))
      goto on_error;
    pty_fd = open(buf, O_RDWR | O_NOCTTY);
    if (pty_fd == -1)
      goto on_error;
  }
  if (termp)
    tcsetattr(pty_fd, TCSAFLUSH, termp);
  *ptx = ptx_fd;
  *pty = pty_fd;
  if (name != NULL) {
    if (*buf == '\0')
      if (pts_name(ptx_fd, &buf, sizeof(_buf)))
        goto on_error;
    strcpy(name, buf);
  }
  ret = 0;
on_error:
  if (ret == -1) {
    close(ptx_fd);
    if (pty_fd != -1)
      close(pty_fd);
  }
  if (buf != _buf)
    free(buf);
  return ret;
}


x86 assembly (clang, -O0):
<openpty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,%ecx
mov    %rcx,-0x68(%rbp)
mov    %rsp,%rax
mov    %rax,-0x38(%rbp)
mov    %rcx,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rax
sub    %rdx,%rax
mov    %rax,-0x60(%rbp)
mov    %rax,%rsp
mov    %rcx,-0x40(%rbp)
mov    %rax,-0x48(%rbp)
movl   $0xffffffff,-0x50(%rbp)
movl   $0xffffffff,-0x54(%rbp)
mov    -0x48(%rbp),%rax
movb   $0x0,(%rax)
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
mov    0x2de1(%rip),%rax
or     (%rax),%esi
lea    0xe10(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0xffffffff,-0x4c(%rbp)
jne    1215 <openpty+0xa5>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x58(%rbp)
jmp    135d <openpty+0x1ed>
mov    -0x4c(%rbp),%edi
call   1040 <__unlockpt@plt>
cmp    $0x0,%rax
je     122c <openpty+0xbc>
jmp    1310 <openpty+0x1a0>
cmpl   $0xffffffff,-0x54(%rbp)
jne    1289 <openpty+0x119>
mov    -0x68(%rbp),%rax
mov    -0x4c(%rbp),%edi
mov    %eax,%edx
lea    -0x48(%rbp),%rsi
call   1060 <pts_name@plt>
cmp    $0x0,%rax
je     1257 <openpty+0xe7>
jmp    1310 <openpty+0x1a0>
mov    -0x48(%rbp),%rdi
mov    0x2d7e(%rip),%rax
mov    (%rax),%esi
mov    0x2d5d(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0xffffffff,-0x54(%rbp)
jne    1284 <openpty+0x114>
jmp    1310 <openpty+0x1a0>
jmp    1289 <openpty+0x119>
cmpq   $0x0,-0x28(%rbp)
je     12a9 <openpty+0x139>
mov    -0x54(%rbp),%edi
mov    0x2d3a(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rdx
call   1080 <tcsetattr@plt>
mov    -0x4c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x54(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
cmpq   $0x0,-0x20(%rbp)
je     1309 <openpty+0x199>
mov    -0x48(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    12fc <openpty+0x18c>
mov    -0x68(%rbp),%rax
mov    -0x4c(%rbp),%edi
mov    %eax,%edx
lea    -0x48(%rbp),%rsi
call   1060 <pts_name@plt>
cmp    $0x0,%rax
je     12f7 <openpty+0x187>
jmp    1310 <openpty+0x1a0>
jmp    12fc <openpty+0x18c>
mov    -0x20(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   1050 <strcpy@plt>
movl   $0x0,-0x50(%rbp)
cmpl   $0xffffffff,-0x50(%rbp)
jne    1339 <openpty+0x1c9>
mov    -0x4c(%rbp),%edi
call   1070 <close@plt>
cmpl   $0xffffffff,-0x54(%rbp)
je     1334 <openpty+0x1c4>
mov    -0x54(%rbp),%edi
call   1070 <close@plt>
jmp    1339 <openpty+0x1c9>
mov    -0x60(%rbp),%rax
cmp    %rax,-0x48(%rbp)
je     1350 <openpty+0x1e0>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x50(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x58(%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    %rcx,-0x40(%rbp)
mov    %rdx,-0x50(%rbp)
mov    %rsi,-0x48(%rbp)
mov    %rdi,-0x38(%rbp)
mov    0x2e58(%rip),%rax
mov    (%rax),%r13d
mov    %rsp,%rax
lea    0xf(%r13),%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rax,%r15
sub    %rcx,%r15
mov    %r15,%rsp
neg    %rcx
mov    %r15,-0x30(%rbp)
movb   $0x0,(%rax,%rcx,1)
mov    0x2e1f(%rip),%r12
mov    0x2e00(%rip),%r14
mov    (%r14),%esi
or     (%r12),%esi
lea    0xe2a(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     121e <openpty+0xae>
mov    %eax,%ebx
mov    %eax,%edi
call   1040 <__unlockpt@plt>
test   %rax,%rax
jne    1201 <openpty+0x91>
lea    -0x30(%rbp),%rsi
mov    %ebx,%edi
mov    %r13d,%edx
call   1060 <pts_name@plt>
test   %rax,%rax
je     1236 <openpty+0xc6>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0xffffffff,%r14d
mov    -0x30(%rbp),%rdi
cmp    %r15,%rdi
je     1224 <openpty+0xb4>
call   1030 <free@plt>
jmp    1224 <openpty+0xb4>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    -0x30(%rbp),%rdi
mov    (%r14),%esi
or     (%r12),%esi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1201 <openpty+0x91>
mov    %eax,%r14d
mov    -0x40(%rbp),%rdx
test   %rdx,%rdx
je     1268 <openpty+0xf8>
mov    0x2d7a(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
call   1080 <tcsetattr@plt>
mov    -0x38(%rbp),%rax
mov    %ebx,(%rax)
mov    -0x48(%rbp),%rax
mov    %r14d,-0x38(%rbp)
mov    %r14d,(%rax)
xor    %r14d,%r14d
mov    -0x50(%rbp),%r12
test   %r12,%r12
je     120e <openpty+0x9e>
mov    -0x30(%rbp),%rsi
cmpb   $0x0,(%rsi)
je     129b <openpty+0x12b>
mov    %r12,%rdi
call   1050 <strcpy@plt>
jmp    120e <openpty+0x9e>
lea    -0x30(%rbp),%rsi
mov    %ebx,%edi
mov    %r13d,%edx
call   1060 <pts_name@plt>
test   %rax,%rax
je     12bd <openpty+0x14d>
mov    %ebx,%edi
call   1070 <close@plt>
mov    -0x38(%rbp),%edi
jmp    1203 <openpty+0x93>
mov    -0x30(%rbp),%rsi
jmp    128e <openpty+0x11e>

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
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 i8*
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = 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
%19 = load i32, i32* @PATH_MAX
%20 = zext i32 %19 to i64
%21 = call i8* @llvm.stacksave()
store i8* %21, i8** %12
%22 = alloca i8, i64 %20
store i64 %20, i64* %13
store i8* %22, i8** %14
store i32 -1, i32* %16
store i32 -1, i32* %17
%23 = load i8*, i8** %14
store i8 0, i8* %23
%24 = load i32, i32* @O_RDWR
%25 = load i32, i32* @O_NOCTTY
%26 = or i32 %24, %25
%27 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %26)
store i32 %27, i32* %15
%28 = load i32, i32* %15
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %31
30: ; preds = %5
store i32 -1, i32* %6
store i32 1, i32* %18
br label %107
31: ; preds = %5
%32 = load i32, i32* %15
%33 = call i64 @__unlockpt(i32 %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %31
br label %87
36: ; preds = %31
%37 = load i32, i32* %17
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %55
39: ; preds = %36
%40 = load i32, i32* %15
%41 = trunc i64 %20 to i32
%42 = call i64 @pts_name(i32 %40, i8** %14, i32 %41)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %39
br label %87
45: ; preds = %39
%46 = load i8*, i8** %14
%47 = load i32, i32* @O_RDWR
%48 = load i32, i32* @O_NOCTTY
%49 = or i32 %47, %48
%50 = call i32 @open(i8* %46, i32 %49)
store i32 %50, i32* %17
%51 = load i32, i32* %17
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %54
53: ; preds = %45
br label %87
54: ; preds = %45
br label %55
55: ; preds = %54, %36
%56 = load %struct.termios*, %struct.termios** %10
%57 = icmp ne %struct.termios* %56, null
br i1 %57, label %58, label %63
58: ; preds = %55
%59 = load i32, i32* %17
%60 = load i32, i32* @TCSAFLUSH
%61 = load %struct.termios*, %struct.termios** %10
%62 = call i32 @tcsetattr(i32 %59, i32 %60, %struct.termios* %61)
br label %63
63: ; preds = %58, %55
%64 = load i32, i32* %15
%65 = load i32*, i32** %7
store i32 %64, i32* %65
%66 = load i32, i32* %17
%67 = load i32*, i32** %8
store i32 %66, i32* %67
%68 = load i8*, i8** %9
%69 = icmp ne i8* %68, null
br i1 %69, label %70, label %86
70: ; preds = %63
%71 = load i8*, i8** %14
%72 = load i8, i8* %71
%73 = sext i8 %72 to i32
%74 = icmp eq i32 %73, 0
br i1 %74, label %75, label %82
75: ; preds = %70
%76 = load i32, i32* %15
%77 = trunc i64 %20 to i32
%78 = call i64 @pts_name(i32 %76, i8** %14, i32 %77)
%79 = icmp ne i64 %78, 0
br i1 %79, label %80, label %81
80: ; preds = %75
br label %87
81: ; preds = %75
br label %82
82: ; preds = %81, %70
%83 = load i8*, i8** %9
%84 = load i8*, i8** %14
%85 = call i32 @strcpy(i8* %83, i8* %84)
br label %86
86: ; preds = %82, %63
store i32 0, i32* %16
br label %87
87: ; preds = %86, %80, %53, %44, %35
%88 = load i32, i32* %16
%89 = icmp eq i32 %88, -1
br i1 %89, label %90, label %99
90: ; preds = %87
%91 = load i32, i32* %15
%92 = call i32 @close(i32 %91)
%93 = load i32, i32* %17
%94 = icmp ne i32 %93, -1
br i1 %94, label %95, label %98
95: ; preds = %90
%96 = load i32, i32* %17
%97 = call i32 @close(i32 %96)
br label %98
98: ; preds = %95, %90
br label %99
99: ; preds = %98, %87
%100 = load i8*, i8** %14
%101 = icmp ne i8* %100, %22
br i1 %101, label %102, label %105
102: ; preds = %99
%103 = load i8*, i8** %14
%104 = call i32 @free(i8* %103)
br label %105
105: ; preds = %102, %99
%106 = load i32, i32* %16
store i32 %106, i32* %6
store i32 1, i32* %18
br label %107
107: ; preds = %105, %30
%108 = load i8*, i8** %12
%109 = load i32, i32* %6
ret i32 %109
}

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
define i32 @openpty(i32* nocapture writeonly %0, i32* nocapture writeonly %1, i8* %2, %struct.termios* %3, %struct.winsize* nocapture readnone %4) {
%6 = alloca i8*
%7 = load i32, i32* @PATH_MAX
%8 = zext i32 %7 to i64
%9 = alloca i8, i64 %8
%10 = bitcast i8** %6 to i8*
store i8* %9, i8** %6
store i8 0, i8* %9
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_NOCTTY
%13 = or i32 %12, %11
%14 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %59, label %16
16: ; preds = %5
%17 = call i64 @__unlockpt(i32 %14)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %48
19: ; preds = %16
%20 = call i64 @pts_name(i32 %14, i8** %6, i32 %7)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %48
22: ; preds = %19
%23 = load i8*, i8** %6
%24 = load i32, i32* @O_RDWR
%25 = load i32, i32* @O_NOCTTY
%26 = or i32 %25, %24
%27 = call i32 @open(i8* %23, i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %48, label %29
29: ; preds = %22
%30 = icmp eq %struct.termios* %3, null
br i1 %30, label %34, label %31
31: ; preds = %29
%32 = load i32, i32* @TCSAFLUSH
%33 = call i32 @tcsetattr(i32 %27, i32 %32, %struct.termios* %3)
br label %34
34: ; preds = %31, %29
store i32 %14, i32* %0
store i32 %27, i32* %1
%35 = icmp eq i8* %2, null
br i1 %35, label %53, label %36
36: ; preds = %34
%37 = load i8*, i8** %6
%38 = load i8, i8* %37
%39 = icmp eq i8 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %36
%41 = call i64 @pts_name(i32 %14, i8** %6, i32 %7)
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %50
43: ; preds = %40
%44 = load i8*, i8** %6
br label %45
45: ; preds = %43, %36
%46 = phi i8* [ %44, %43 ], [ %37, %36 ]
%47 = call i32 @strcpy(i8* %2, i8* %46)
br label %53
48: ; preds = %16, %19, %22
%49 = call i32 @close(i32 %14)
br label %53
50: ; preds = %40
%51 = call i32 @close(i32 %14)
%52 = call i32 @close(i32 %27)
br label %53
53: ; preds = %48, %45, %34, %50
%54 = phi i32 [ -1, %50 ], [ 0, %45 ], [ 0, %34 ], [ -1, %48 ]
%55 = load i8*, i8** %6
%56 = icmp eq i8* %55, %9
br i1 %56, label %59, label %57
57: ; preds = %53
%58 = call i32 @free(i8* %55)
br label %59
59: ; preds = %53, %57, %5
%60 = phi i32 [ -1, %5 ], [ %54, %57 ], [ %54, %53 ]
ret i32 %60
}