#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;
};
typedef struct winsize termios;

/* Variables and functions */
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TCSETS;
int /*<<< orphan*/ TIOCSWINSZ;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int grantpt(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct winsize const *);
int open(char *, int);
int posix_openpt(int);
int ptsname_r(int, char *, int);
int /*<<< orphan*/ stderr;
int unlockpt(int);
int openpty(int *mfd, int *sfd, char *slavename, const struct termios *tp,
            const struct winsize *wp) {
  char sbuf[256];
  if (!slavename)
    slavename = sbuf;
  *mfd = posix_openpt(O_RDWR | O_NOCTTY);
  if (*mfd == -1) {
    fprintf(stderr, "open master failed\n");
    return -1;
  }
  int rv = ptsname_r(*mfd, slavename, 256);
  if (rv != 0) {
    fprintf(stderr, "ptsname failed\n");
    return -1;
  }
  rv = grantpt(*mfd);
  if (rv != 0) {
    fprintf(stderr, "grantpt failed\n");
    return -1;
  }
  rv = unlockpt(*mfd);
  if (rv != 0) {
    fprintf(stderr, "unlockpt failed\n");
    return -1;
  }
  *sfd = open(slavename, O_RDWR | O_NOCTTY);
  if (*mfd == -1) {
    fprintf(stderr, "open slave failed\n");
    return -1;
  }
  if (tp) {
    if (ioctl(*sfd, TCSETS, tp) != 0) {
      fprintf(stderr, "TCSETS failed\n");
    }
  }
  if (wp) {
    if (ioctl(*sfd, TIOCSWINSZ, wp) != 0) {
      fprintf(stderr, "TIOCSWINSZ failed\n");
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<openpty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a5 <openpty+0x35>
lea    -0x130(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
mov    0x2e1b(%rip),%rax
or     (%rax),%edi
call   1090 <posix_openpt@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0xffffffff,(%rax)
jne    11f2 <openpty+0x82>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
lea    0xe1f(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    139e <openpty+0x22e>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
mov    $0x100,%edx
call   1040 <ptsname_r@plt>
mov    %eax,-0x134(%rbp)
cmpl   $0x0,-0x134(%rbp)
je     123a <openpty+0xca>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
lea    0xdeb(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    139e <openpty+0x22e>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1080 <grantpt@plt>
mov    %eax,-0x134(%rbp)
cmpl   $0x0,-0x134(%rbp)
je     1279 <openpty+0x109>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
lea    0xdbc(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    139e <openpty+0x22e>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1050 <unlockpt@plt>
mov    %eax,-0x134(%rbp)
cmpl   $0x0,-0x134(%rbp)
je     12b8 <openpty+0x148>
mov    0x2d2a(%rip),%rax
mov    (%rax),%edi
lea    0xd8d(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    139e <openpty+0x22e>
mov    -0x20(%rbp),%rdi
mov    0x2d25(%rip),%rax
mov    (%rax),%esi
mov    0x2d04(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0xffffffff,(%rax)
jne    1309 <openpty+0x199>
mov    0x2cd9(%rip),%rax
mov    (%rax),%edi
lea    0xd4d(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    139e <openpty+0x22e>
cmpq   $0x0,-0x28(%rbp)
je     1350 <openpty+0x1e0>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    0x2c9f(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     134b <openpty+0x1db>
mov    0x2c8b(%rip),%rax
mov    (%rax),%edi
lea    0xd12(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1350 <openpty+0x1e0>
cmpq   $0x0,-0x30(%rbp)
je     1397 <openpty+0x227>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    0x2c78(%rip),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     1392 <openpty+0x222>
mov    0x2c44(%rip),%rax
mov    (%rax),%edi
lea    0xcda(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1397 <openpty+0x227>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openpty>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %r8,0x8(%rsp)
mov    %rcx,%r12
mov    %rsi,%r15
mov    %rdi,%r13
test   %rdx,%rdx
lea    0x10(%rsp),%rbx
cmovne %rdx,%rbx
mov    0x2e46(%rip),%rbp
mov    0x2e27(%rip),%r14
mov    (%r14),%edi
or     0x0(%rbp),%edi
call   1090 <posix_openpt@plt>
mov    %eax,0x0(%r13)
cmp    $0xffffffff,%eax
je     11e2 <openpty+0x72>
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
call   1040 <ptsname_r@plt>
test   %eax,%eax
je     11f4 <openpty+0x84>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
lea    0xe34(%rip),%rsi
jmp    1230 <openpty+0xc0>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
lea    0xe0e(%rip),%rsi
jmp    1230 <openpty+0xc0>
mov    0x0(%r13),%edi
call   1080 <grantpt@plt>
test   %eax,%eax
je     1213 <openpty+0xa3>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
lea    0xe13(%rip),%rsi
jmp    1230 <openpty+0xc0>
mov    0x0(%r13),%edi
call   1050 <unlockpt@plt>
test   %eax,%eax
je     124e <openpty+0xde>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
lea    0xe04(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r14),%esi
or     0x0(%rbp),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
mov    %eax,(%r15)
cmpl   $0xffffffff,0x0(%r13)
je     12d4 <openpty+0x164>
test   %r12,%r12
je     1298 <openpty+0x128>
mov    0x2d4e(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
mov    %r12,%rdx
call   1030 <ioctl@plt>
test   %rax,%rax
je     1298 <openpty+0x128>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
lea    0xdc5(%rip),%rsi
call   1060 <fprintf@plt>
xor    %ebx,%ebx
mov    0x8(%rsp),%rdx
test   %rdx,%rdx
je     123a <openpty+0xca>
mov    (%r15),%edi
mov    0x2d32(%rip),%rax
mov    (%rax),%esi
call   1030 <ioctl@plt>
test   %rax,%rax
je     123a <openpty+0xca>
mov    0x2d07(%rip),%rax
mov    (%rax),%edi
lea    0xd9d(%rip),%rsi
call   1060 <fprintf@plt>
jmp    123a <openpty+0xca>
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
lea    0xd61(%rip),%rsi
jmp    1230 <openpty+0xc0>

LLVM IR (-O0):
%struct.termios = type opaque
%struct.winsize = type { i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"open master failed\0A\00"
@.str.1 = [16 x i8] c"ptsname failed\0A\00"
@.str.2 = [16 x i8] c"grantpt failed\0A\00"
@.str.3 = [17 x i8] c"unlockpt failed\0A\00"
@.str.4 = [19 x i8] c"open slave failed\0A\00"
@TCSETS = global i32 0
@.str.5 = [15 x i8] c"TCSETS failed\0A\00"
@TIOCSWINSZ = global i32 0
@.str.6 = [19 x i8] c"TIOCSWINSZ failed\0A\00"
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 [256 x i8]
%13 = 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
%14 = load i8*, i8** %9
%15 = icmp ne i8* %14, null
br i1 %15, label %18, label %16
16: ; preds = %5
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
store i8* %17, i8** %9
br label %18
18: ; preds = %16, %5
%19 = load i32, i32* @O_RDWR
%20 = load i32, i32* @O_NOCTTY
%21 = or i32 %19, %20
%22 = call i32 @posix_openpt(i32 %21)
%23 = load i32*, i32** %7
store i32 %22, i32* %23
%24 = load i32*, i32** %7
%25 = load i32, i32* %24
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %18
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %6
br label %101
30: ; preds = %18
%31 = load i32*, i32** %7
%32 = load i32, i32* %31
%33 = load i8*, i8** %9
%34 = call i32 @ptsname_r(i32 %32, i8* %33, i32 256)
store i32 %34, i32* %13
%35 = load i32, i32* %13
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %30
%38 = load i32, i32* @stderr
%39 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %6
br label %101
40: ; preds = %30
%41 = load i32*, i32** %7
%42 = load i32, i32* %41
%43 = call i32 @grantpt(i32 %42)
store i32 %43, i32* %13
%44 = load i32, i32* %13
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %40
%47 = load i32, i32* @stderr
%48 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %6
br label %101
49: ; preds = %40
%50 = load i32*, i32** %7
%51 = load i32, i32* %50
%52 = call i32 @unlockpt(i32 %51)
store i32 %52, i32* %13
%53 = load i32, i32* %13
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %49
%56 = load i32, i32* @stderr
%57 = call i32 @fprintf(i32 %56, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %6
br label %101
58: ; preds = %49
%59 = load i8*, i8** %9
%60 = load i32, i32* @O_RDWR
%61 = load i32, i32* @O_NOCTTY
%62 = or i32 %60, %61
%63 = call i32 @open(i8* %59, i32 %62)
%64 = load i32*, i32** %8
store i32 %63, i32* %64
%65 = load i32*, i32** %7
%66 = load i32, i32* %65
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %71
68: ; preds = %58
%69 = load i32, i32* @stderr
%70 = call i32 @fprintf(i32 %69, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %6
br label %101
71: ; preds = %58
%72 = load %struct.termios*, %struct.termios** %10
%73 = icmp ne %struct.termios* %72, null
br i1 %73, label %74, label %86
74: ; preds = %71
%75 = load i32*, i32** %8
%76 = load i32, i32* %75
%77 = load i32, i32* @TCSETS
%78 = load %struct.termios*, %struct.termios** %10
%79 = bitcast %struct.termios* %78 to %struct.winsize*
%80 = call i64 @ioctl(i32 %76, i32 %77, %struct.winsize* %79)
%81 = icmp ne i64 %80, 0
br i1 %81, label %82, label %85
82: ; preds = %74
%83 = load i32, i32* @stderr
%84 = call i32 @fprintf(i32 %83, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
br label %85
85: ; preds = %82, %74
br label %86
86: ; preds = %85, %71
%87 = load %struct.winsize*, %struct.winsize** %11
%88 = icmp ne %struct.winsize* %87, null
br i1 %88, label %89, label %100
89: ; preds = %86
%90 = load i32*, i32** %8
%91 = load i32, i32* %90
%92 = load i32, i32* @TIOCSWINSZ
%93 = load %struct.winsize*, %struct.winsize** %11
%94 = call i64 @ioctl(i32 %91, i32 %92, %struct.winsize* %93)
%95 = icmp ne i64 %94, 0
br i1 %95, label %96, label %99
96: ; preds = %89
%97 = load i32, i32* @stderr
%98 = call i32 @fprintf(i32 %97, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
br label %99
99: ; preds = %96, %89
br label %100
100: ; preds = %99, %86
store i32 0, i32* %6
br label %101
101: ; preds = %100, %68, %55, %46, %37, %27
%102 = load i32, i32* %6
ret i32 %102
}

LLVM IR (-O2):
%struct.termios = type opaque
%struct.winsize = type { i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"open master failed\0A\00"
@.str.1 = [16 x i8] c"ptsname failed\0A\00"
@.str.2 = [16 x i8] c"grantpt failed\0A\00"
@.str.3 = [17 x i8] c"unlockpt failed\0A\00"
@.str.4 = [19 x i8] c"open slave failed\0A\00"
@TCSETS = global i32 0
@.str.5 = [15 x i8] c"TCSETS failed\0A\00"
@TIOCSWINSZ = global i32 0
@.str.6 = [19 x i8] c"TIOCSWINSZ failed\0A\00"
define i32 @openpty(i32* nocapture %0, i32* nocapture %1, i8* %2, %struct.termios* %3, %struct.winsize* %4) {
%6 = alloca [256 x i8]
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%8 = icmp eq i8* %2, null
%9 = select i1 %8, i8* %7, i8* %2
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_NOCTTY
%12 = or i32 %11, %10
%13 = tail call i32 @posix_openpt(i32 %12)
store i32 %13, i32* %0
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %5
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %68
18: ; preds = %5
%19 = call i32 @ptsname_r(i32 %13, i8* %9, i32 256)
%20 = icmp eq i32 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %18
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %68
24: ; preds = %18
%25 = load i32, i32* %0
%26 = call i32 @grantpt(i32 %25)
%27 = icmp eq i32 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %24
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %68
31: ; preds = %24
%32 = load i32, i32* %0
%33 = call i32 @unlockpt(i32 %32)
%34 = icmp eq i32 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %31
%36 = load i32, i32* @stderr
%37 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %68
38: ; preds = %31
%39 = load i32, i32* @O_RDWR
%40 = load i32, i32* @O_NOCTTY
%41 = or i32 %40, %39
%42 = call i32 @open(i8* %9, i32 %41)
store i32 %42, i32* %1
%43 = load i32, i32* %0
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %48
45: ; preds = %38
%46 = load i32, i32* @stderr
%47 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
br label %68
48: ; preds = %38
%49 = icmp eq %struct.termios* %3, null
br i1 %49, label %58, label %50
50: ; preds = %48
%51 = load i32, i32* @TCSETS
%52 = bitcast %struct.termios* %3 to %struct.winsize*
%53 = call i64 @ioctl(i32 %42, i32 %51, %struct.winsize* %52)
%54 = icmp eq i64 %53, 0
br i1 %54, label %58, label %55
55: ; preds = %50
%56 = load i32, i32* @stderr
%57 = call i32 @fprintf(i32 %56, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
br label %58
58: ; preds = %50, %55, %48
%59 = icmp eq %struct.winsize* %4, null
br i1 %59, label %68, label %60
60: ; preds = %58
%61 = load i32, i32* %1
%62 = load i32, i32* @TIOCSWINSZ
%63 = call i64 @ioctl(i32 %61, i32 %62, %struct.winsize* %4)
%64 = icmp eq i64 %63, 0
br i1 %64, label %68, label %65
65: ; preds = %60
%66 = load i32, i32* @stderr
%67 = call i32 @fprintf(i32 %66, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
br label %68
68: ; preds = %21, %28, %35, %45, %60, %65, %58, %15
%69 = phi i32 [ -1, %15 ], [ -1, %21 ], [ -1, %28 ], [ -1, %35 ], [ -1, %45 ], [ 0, %60 ], [ 0, %65 ], [ 0, %58 ]
ret i32 %69
}