#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 */

/* Variables and functions */
int EFAULT;
int ENAMETOOLONG;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int errno;
scalar_t__ grantpt(int);
int posix_openpt(int /*<<< orphan*/);
char *ptsname(int);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
scalar_t__ unlockpt(int);
int ptym_open(char *pts_name, size_t buff_len) {
  if (pts_name == NULL) {
    errno = EFAULT;
    return -1;
  }
  int ptym_fd;
  if ((ptym_fd = posix_openpt(O_RDWR)) < 0)
    return -1;
  char *pts_ptr = ptsname(ptym_fd);
  strncpy(pts_name, pts_ptr, buff_len);
  if (buff_len == 0 || pts_name[buff_len - 1] != '\0') {
    errno = ENAMETOOLONG;
    goto errout;
  }
  if (grantpt(ptym_fd) < 0)
    goto errout;
  if (unlockpt(ptym_fd) < 0)
    goto errout;
  return ptym_fd;
errout:;
  int err_saved = errno;
  close(ptym_fd);
  errno = err_saved;
  return -1;
}


x86 assembly (clang, -O0):
<ptym_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1199 <ptym_open+0x39>
mov    0x2e76(%rip),%rax
mov    (%rax),%ecx
mov    0x2e45(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <ptym_open+0x117>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
call   1080 <posix_openpt@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11bf <ptym_open+0x5f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <ptym_open+0x117>
mov    -0x1c(%rbp),%edi
call   1030 <ptsname@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <strncpy@plt>
cmpq   $0x0,-0x18(%rbp)
je     1200 <ptym_open+0xa0>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     1217 <ptym_open+0xb7>
mov    0x2dc1(%rip),%rax
mov    (%rax),%ecx
mov    0x2dc0(%rip),%rax
mov    %ecx,(%rax)
jmp    1250 <ptym_open+0xf0>
mov    -0x1c(%rbp),%edi
call   1070 <grantpt@plt>
cmp    $0x0,%rax
jge    122e <ptym_open+0xce>
jmp    1250 <ptym_open+0xf0>
mov    -0x1c(%rbp),%edi
call   1060 <unlockpt@plt>
cmp    $0x0,%rax
jge    1245 <ptym_open+0xe5>
jmp    1250 <ptym_open+0xf0>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1277 <ptym_open+0x117>
mov    0x2d79(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x2c(%rbp),%ecx
mov    0x2d62(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ptym_open>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     11cc <ptym_open+0x6c>
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
call   1080 <posix_openpt@plt>
test   %eax,%eax
js     11de <ptym_open+0x7e>
mov    %eax,%ebx
mov    %eax,%edi
call   1030 <ptsname@plt>
mov    %r15,%rdi
mov    %rax,%rsi
mov    %r14,%rdx
call   1040 <strncpy@plt>
test   %r14,%r14
je     11a8 <ptym_open+0x48>
cmpb   $0x0,-0x1(%r14,%r15,1)
je     11f0 <ptym_open+0x90>
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
mov    0x2e18(%rip),%rcx
mov    %eax,(%rcx)
mov    0x2e0f(%rip),%rax
mov    (%rax),%ebp
mov    %ebx,%edi
call   1050 <close@plt>
jmp    11d5 <ptym_open+0x75>
mov    0x2e25(%rip),%rax
mov    (%rax),%ebp
mov    0x2df4(%rip),%rax
mov    %ebp,(%rax)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebx,%edi
call   1070 <grantpt@plt>
test   %rax,%rax
js     11ba <ptym_open+0x5a>
mov    %ebx,%edi
call   1060 <unlockpt@plt>
test   %rax,%rax
jns    11e3 <ptym_open+0x83>
jmp    11ba <ptym_open+0x5a>

LLVM IR (-O0):
@EFAULT = global i32 0
@errno = global i32 0
@O_RDWR = global i32 0
@ENAMETOOLONG = global i32 0
define i32 @ptym_open(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
%9 = load i8*, i8** %4
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = load i32, i32* @EFAULT
store i32 %12, i32* @errno
store i32 -1, i32* %3
br label %54
13: ; preds = %2
%14 = load i32, i32* @O_RDWR
%15 = call i32 @posix_openpt(i32 %14)
store i32 %15, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %13
store i32 -1, i32* %3
br label %54
18: ; preds = %13
%19 = load i32, i32* %6
%20 = call i8* @ptsname(i32 %19)
store i8* %20, i8** %7
%21 = load i8*, i8** %4
%22 = load i8*, i8** %7
%23 = load i64, i64* %5
%24 = call i32 @strncpy(i8* %21, i8* %22, i64 %23)
%25 = load i64, i64* %5
%26 = icmp eq i64 %25, 0
br i1 %26, label %35, label %27
27: ; preds = %18
%28 = load i8*, i8** %4
%29 = load i64, i64* %5
%30 = sub i64 %29, 1
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %27, %18
%36 = load i32, i32* @ENAMETOOLONG
store i32 %36, i32* @errno
br label %49
37: ; preds = %27
%38 = load i32, i32* %6
%39 = call i64 @grantpt(i32 %38)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %37
br label %49
42: ; preds = %37
%43 = load i32, i32* %6
%44 = call i64 @unlockpt(i32 %43)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %42
br label %49
47: ; preds = %42
%48 = load i32, i32* %6
store i32 %48, i32* %3
br label %54
49: ; preds = %46, %41, %35
%50 = load i32, i32* @errno
store i32 %50, i32* %8
%51 = load i32, i32* %6
%52 = call i32 @close(i32 %51)
%53 = load i32, i32* %8
store i32 %53, i32* @errno
store i32 -1, i32* %3
br label %54
54: ; preds = %49, %47, %17, %11
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
@EFAULT = global i32 0
@errno = global i32 0
@O_RDWR = global i32 0
@ENAMETOOLONG = global i32 0
define i32 @ptym_open(i8* %0, i64 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = load i32, i32* @EFAULT
br label %30
6: ; preds = %2
%7 = load i32, i32* @O_RDWR
%8 = tail call i32 @posix_openpt(i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %32, label %10
10: ; preds = %6
%11 = tail call i8* @ptsname(i32 %8)
%12 = tail call i32 @strncpy(i8* %0, i8* %11, i64 %1)
%13 = icmp eq i64 %1, 0
br i1 %13, label %19, label %14
14: ; preds = %10
%15 = add i64 %1, -1
%16 = getelementptr inbounds i8, i8* %0, i64 %15
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %14, %10
%20 = load i32, i32* @ENAMETOOLONG
store i32 %20, i32* @errno
br label %27
21: ; preds = %14
%22 = tail call i64 @grantpt(i32 %8)
%23 = icmp slt i64 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = tail call i64 @unlockpt(i32 %8)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %24, %21, %19
%28 = load i32, i32* @errno
%29 = tail call i32 @close(i32 %8)
br label %30
30: ; preds = %4, %27
%31 = phi i32 [ %28, %27 ], [ %5, %4 ]
store i32 %31, i32* @errno
br label %32
32: ; preds = %30, %6, %24
%33 = phi i32 [ -1, %6 ], [ %8, %24 ], [ -1, %30 ]
ret i32 %33
}