#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 /*<<< orphan*/ buf;

/* Variables and functions */
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TIOCGPTN;
int /*<<< orphan*/ TIOCSPTLCK;
int ioctl(int, int /*<<< orphan*/, int *);
int open(char *, int);
int snprintf(char *, int, char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int __wshd_openpty(int *master, int *slave, char *slave_name) {
  int rv;
  int lock;
  int pty;
  char buf[32];
  rv = open("/dev/ptmx", O_RDWR | O_NOCTTY);
  if (rv == -1) {
    return -1;
  }
  *master = rv;
  pty = 0;
  rv = ioctl(*master, TIOCGPTN, &pty);
  if (rv == -1) {
    return -1;
  }
  rv = snprintf(buf, sizeof(buf), "/dev/pts/%d", pty);
  if (rv >= sizeof(buf)) {
    return -1;
  }
  lock = 0;
  rv = ioctl(*master, TIOCSPTLCK, &lock);
  if (rv == -1) {
    return -1;
  }
  rv = open(buf, O_RDWR | O_NOCTTY);
  if (rv == -1) {
    return -1;
  }
  *slave = rv;
  if (slave_name != NULL) {
    strcpy(slave_name, buf);
  }
  return 0;
}


x86 assembly (clang, -O0):
<__wshd_openpty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
mov    0x2e6c(%rip),%rax
or     (%rax),%esi
lea    0xe93(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    118b <__wshd_openpty+0x4b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1295 <__wshd_openpty+0x155>
mov    -0x24(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
lea    -0x2c(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11cc <__wshd_openpty+0x8c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1295 <__wshd_openpty+0x155>
lea    -0x50(%rbp),%rdi
mov    -0x2c(%rbp),%ecx
mov    $0x20,%esi
lea    0xe2b(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x24(%rbp)
movslq -0x24(%rbp),%rax
cmp    $0x20,%rax
jb     1201 <__wshd_openpty+0xc1>
movl   $0xffffffff,-0x4(%rbp)
jmp    1295 <__wshd_openpty+0x155>
movl   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1239 <__wshd_openpty+0xf9>
movl   $0xffffffff,-0x4(%rbp)
jmp    1295 <__wshd_openpty+0x155>
lea    -0x50(%rbp),%rdi
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
mov    0x2d83(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    126d <__wshd_openpty+0x12d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1295 <__wshd_openpty+0x155>
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
cmpq   $0x0,-0x20(%rbp)
je     128e <__wshd_openpty+0x14e>
mov    -0x20(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__wshd_openpty>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    0x2e8a(%rip),%r12
mov    0x2e6b(%rip),%r13
mov    0x0(%r13),%esi
or     (%r12),%esi
lea    0xe8c(%rip),%rdi
call   1060 <open@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     121a <__wshd_openpty+0xda>
mov    %eax,(%rbx)
movl   $0x0,0x8(%rsp)
mov    0x2e48(%rip),%rcx
mov    (%rcx),%esi
lea    0x8(%rsp),%rdx
mov    %eax,%edi
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
je     121a <__wshd_openpty+0xda>
mov    0x8(%rsp),%ecx
lea    0xe54(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x20,%esi
call   1040 <snprintf@plt>
cmp    $0x1f,%eax
ja     121a <__wshd_openpty+0xda>
movl   $0x0,0xc(%rsp)
mov    (%rbx),%edi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
je     121a <__wshd_openpty+0xda>
mov    0x0(%r13),%esi
or     (%r12),%esi
lea    0x10(%rsp),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     121a <__wshd_openpty+0xda>
mov    %eax,(%r15)
xor    %ebp,%ebp
test   %r14,%r14
je     121a <__wshd_openpty+0xda>
lea    0x10(%rsp),%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/ptmx\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@TIOCGPTN = global i32 0
@.str.1 = [12 x i8] c"/dev/pts/%d\00"
@TIOCSPTLCK = global i32 0
define i32 @__wshd_openpty(i32* %0, i32* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [32 x i8]
store i32* %0, i32** %5
store i32* %1, i32** %6
store i8* %2, i8** %7
%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)
store i32 %15, i32* %8
%16 = load i32, i32* %8
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %3
store i32 -1, i32* %4
br label %64
19: ; preds = %3
%20 = load i32, i32* %8
%21 = load i32*, i32** %5
store i32 %20, i32* %21
store i32 0, i32* %10
%22 = load i32*, i32** %5
%23 = load i32, i32* %22
%24 = load i32, i32* @TIOCGPTN
%25 = call i32 @ioctl(i32 %23, i32 %24, i32* %10)
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %29
28: ; preds = %19
store i32 -1, i32* %4
br label %64
29: ; preds = %19
%30 = getelementptr inbounds [32 x i8], [32 x i8]* %11, i64 0, i64 0
%31 = load i32, i32* %10
%32 = call i32 @snprintf(i8* %30, i32 32, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %31)
store i32 %32, i32* %8
%33 = load i32, i32* %8
%34 = sext i32 %33 to i64
%35 = icmp uge i64 %34, 32
br i1 %35, label %36, label %37
36: ; preds = %29
store i32 -1, i32* %4
br label %64
37: ; preds = %29
store i32 0, i32* %9
%38 = load i32*, i32** %5
%39 = load i32, i32* %38
%40 = load i32, i32* @TIOCSPTLCK
%41 = call i32 @ioctl(i32 %39, i32 %40, i32* %9)
store i32 %41, i32* %8
%42 = load i32, i32* %8
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %45
44: ; preds = %37
store i32 -1, i32* %4
br label %64
45: ; preds = %37
%46 = getelementptr inbounds [32 x i8], [32 x i8]* %11, i64 0, i64 0
%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* %8
%51 = load i32, i32* %8
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %54
53: ; preds = %45
store i32 -1, i32* %4
br label %64
54: ; preds = %45
%55 = load i32, i32* %8
%56 = load i32*, i32** %6
store i32 %55, i32* %56
%57 = load i8*, i8** %7
%58 = icmp ne i8* %57, null
br i1 %58, label %59, label %63
59: ; preds = %54
%60 = load i8*, i8** %7
%61 = getelementptr inbounds [32 x i8], [32 x i8]* %11, i64 0, i64 0
%62 = call i32 @strcpy(i8* %60, i8* %61)
br label %63
63: ; preds = %59, %54
store i32 0, i32* %4
br label %64
64: ; preds = %63, %53, %44, %36, %28, %18
%65 = load i32, i32* %4
ret i32 %65
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/ptmx\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@TIOCGPTN = global i32 0
@.str.1 = [12 x i8] c"/dev/pts/%d\00"
@TIOCSPTLCK = global i32 0
define i32 @__wshd_openpty(i32* nocapture %0, i32* nocapture writeonly %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca [32 x i8]
%7 = bitcast i32* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = getelementptr inbounds [32 x i8], [32 x i8]* %6, 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)
%14 = icmp eq i32 %13, -1
br i1 %14, label %38, label %15
15: ; preds = %3
store i32 %13, i32* %0
store i32 0, i32* %5
%16 = load i32, i32* @TIOCGPTN
%17 = call i32 @ioctl(i32 %13, i32 %16, i32* %5)
%18 = icmp eq i32 %17, -1
br i1 %18, label %38, label %19
19: ; preds = %15
%20 = load i32, i32* %5
%21 = call i32 @snprintf(i8* %9, i32 32, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = icmp ugt i32 %21, 31
br i1 %22, label %38, label %23
23: ; preds = %19
store i32 0, i32* %4
%24 = load i32, i32* %0
%25 = load i32, i32* @TIOCSPTLCK
%26 = call i32 @ioctl(i32 %24, i32 %25, i32* %4)
%27 = icmp eq i32 %26, -1
br i1 %27, label %38, label %28
28: ; preds = %23
%29 = load i32, i32* @O_RDWR
%30 = load i32, i32* @O_NOCTTY
%31 = or i32 %30, %29
%32 = call i32 @open(i8* %9, i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %38, label %34
34: ; preds = %28
store i32 %32, i32* %1
%35 = icmp eq i8* %2, null
br i1 %35, label %38, label %36
36: ; preds = %34
%37 = call i32 @strcpy(i8* %2, i8* %9)
br label %38
38: ; preds = %34, %36, %28, %23, %19, %15, %3
%39 = phi i32 [ -1, %3 ], [ -1, %15 ], [ -1, %19 ], [ -1, %23 ], [ -1, %28 ], [ 0, %36 ], [ 0, %34 ]
ret i32 %39
}