#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;
struct termios {
  int dummy;
};

/* Variables and functions */
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ TIOCGPTN;
int /*<<< orphan*/ TIOCSPTLCK;
int /*<<< orphan*/ TIOCSWINSZ;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, int const *);
int open(char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios const *);
int openpty(int *m, int *s, char *name, const struct termios *tio,
            const struct winsize *ws) {
  int n = 0;
  char buf[20];
  *m = open("/dev/ptmx", O_RDWR | O_NOCTTY);
  if (!*m)
    return -1;
  if (ioctl(*m, TIOCSPTLCK, &n) || ioctl(*m, TIOCGPTN, &n)) {
    close(*m);
    return -1;
  }
  if (!name)
    name = buf;
  snprintf(name, sizeof buf, "/dev/pts/%d", n);
  if ((*s = open(name, O_RDWR | O_NOCTTY)) < 0) {
    close(*m);
    return -1;
  }
  if (tio)
    tcsetattr(*s, TCSANOW, tio);
  if (ws)
    ioctl(*s, TIOCSWINSZ, ws);
  return 0;
}


x86 assembly (clang, -O0):
<openpty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
mov    0x2e3d(%rip),%rax
or     (%rax),%esi
lea    0xe74(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
jne    11b2 <openpty+0x62>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c6 <openpty+0x176>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
lea    -0x34(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jne    11f6 <openpty+0xa6>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
lea    -0x34(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     120d <openpty+0xbd>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c6 <openpty+0x176>
cmpq   $0x0,-0x20(%rbp)
jne    1220 <openpty+0xd0>
lea    -0x50(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x34(%rbp),%ecx
mov    $0x14,%esi
lea    0xdd7(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x20(%rbp),%rdi
mov    0x2da5(%rip),%rax
mov    (%rax),%esi
mov    0x2d74(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    -0x18(%rbp),%rcx
mov    %eax,(%rcx)
cmp    $0x0,%eax
jge    1279 <openpty+0x129>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c6 <openpty+0x176>
cmpq   $0x0,-0x28(%rbp)
je     129c <openpty+0x14c>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    0x2d47(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmpq   $0x0,-0x30(%rbp)
je     12bf <openpty+0x16f>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    0x2d1c(%rip),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%rdx
call   1040 <ioctl@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openpty>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %r8,%r14
mov    %rcx,0x10(%rsp)
mov    %rdx,%r13
mov    %rsi,0x18(%rsp)
mov    %rdi,%rbx
movl   $0x0,0x4(%rsp)
mov    0x2e68(%rip),%r15
mov    0x2e39(%rip),%r12
mov    (%r12),%esi
or     (%r15),%esi
lea    0xe6b(%rip),%rdi
call   1070 <open@plt>
mov    %eax,(%rbx)
mov    $0xffffffff,%ebp
test   %eax,%eax
je     11e0 <openpty+0x90>
mov    0x2e0c(%rip),%rcx
mov    (%rcx),%esi
lea    0x4(%rsp),%rdx
mov    %eax,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
jne    11d9 <openpty+0x89>
mov    (%rbx),%edi
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
call   1040 <ioctl@plt>
test   %rax,%rax
je     11f1 <openpty+0xa1>
mov    (%rbx),%edi
call   1050 <close@plt>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,0x8(%rsp)
test   %r13,%r13
lea    0x20(%rsp),%r14
cmovne %r13,%r14
mov    0x4(%rsp),%ecx
lea    0xdfd(%rip),%rdx
mov    %r14,%rdi
mov    $0x14,%esi
call   1030 <snprintf@plt>
mov    (%r12),%esi
or     (%r15),%esi
mov    %r14,%rdi
call   1070 <open@plt>
mov    0x18(%rsp),%r14
mov    %eax,(%r14)
test   %eax,%eax
js     11d9 <openpty+0x89>
mov    0x10(%rsp),%rdx
test   %rdx,%rdx
je     124f <openpty+0xff>
mov    0x2d92(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   1060 <tcsetattr@plt>
xor    %ebp,%ebp
mov    0x8(%rsp),%rdx
test   %rdx,%rdx
je     11e0 <openpty+0x90>
mov    (%r14),%edi
mov    0x2d6b(%rip),%rax
mov    (%rax),%esi
call   1040 <ioctl@plt>
jmp    11e0 <openpty+0x90>

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

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