#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 */
typedef int winsize;
typedef int termios;

/* Variables and functions */
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ PATH_MAX;
int /*<<< orphan*/ TCSETS;
int /*<<< orphan*/ TIOCGPTN;
int /*<<< orphan*/ TIOCSWINSZ;
int /*<<< orphan*/ close(int *);
scalar_t__ ioctl(int, int /*<<< orphan*/, int const *);
int open(char *, int);
int /*<<< orphan*/ ttyname_r(int, char *, int /*<<< orphan*/);
int openpty(int *ptm, int *pts, char *name, const struct termios *termios,
            const struct winsize *winsize) {
  *ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY);
  if (*ptm < 0) {
    return -1;
  }
  if (ioctl(*ptm, TIOCGPTN, pts) < 0) {
    close(ptm);
    return -1;
  }
  if (termios) {
    if (ioctl(*pts, TCSETS, termios) < 0) {
      close(ptm);
      return -1;
    }
  }
  if (winsize) {
    if (ioctl(*pts, TIOCSWINSZ, winsize) < 0) {
      close(ptm);
      return -1;
    }
  }
  if (name) {
    ttyname_r(*pts, name, PATH_MAX);
  }
  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    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
mov    0x2e54(%rip),%rax
or     (%rax),%esi
lea    0xe8b(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
jge    119b <openpty+0x5b>
movl   $0xffffffff,-0x4(%rbp)
jmp    128a <openpty+0x14a>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    11d2 <openpty+0x92>
mov    -0x10(%rbp),%rdi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128a <openpty+0x14a>
cmpq   $0x0,-0x28(%rbp)
je     1219 <openpty+0xd9>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    1214 <openpty+0xd4>
mov    -0x10(%rbp),%rdi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128a <openpty+0x14a>
jmp    1219 <openpty+0xd9>
cmpq   $0x0,-0x30(%rbp)
je     1260 <openpty+0x120>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    125b <openpty+0x11b>
mov    -0x10(%rbp),%rdi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128a <openpty+0x14a>
jmp    1260 <openpty+0x120>
cmpq   $0x0,-0x20(%rbp)
je     1283 <openpty+0x143>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
mov    0x2d74(%rip),%rax
mov    (%rax),%edx
call   1050 <ttyname_r@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openpty>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r12
mov    %rcx,%rbp
mov    %rdx,%r14
mov    %rsi,%r13
mov    %rdi,%rbx
mov    0x2e7f(%rip),%rax
mov    0x2e58(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe8d(%rip),%rdi
call   1060 <open@plt>
mov    %eax,(%rbx)
mov    $0xffffffff,%r15d
test   %eax,%eax
js     1201 <openpty+0xc1>
mov    0x2e4d(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
mov    %r13,%rdx
call   1030 <ioctl@plt>
test   %rax,%rax
js     11f9 <openpty+0xb9>
test   %rbp,%rbp
je     11bb <openpty+0x7b>
mov    0x0(%r13),%edi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdx
call   1030 <ioctl@plt>
test   %rax,%rax
js     11f9 <openpty+0xb9>
test   %r12,%r12
je     11da <openpty+0x9a>
mov    0x0(%r13),%edi
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdx
call   1030 <ioctl@plt>
test   %rax,%rax
js     11f9 <openpty+0xb9>
xor    %r15d,%r15d
test   %r14,%r14
je     1201 <openpty+0xc1>
mov    0x0(%r13),%edi
mov    0x2e03(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1050 <ttyname_r@plt>
jmp    1201 <openpty+0xc1>
mov    %rbx,%rdi
call   1040 <close@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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