#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 EOVERFLOW;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ close(int);
int errno;
int grantpt(int);
int posix_openpt(int);
char *ptsname(int);
size_t strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
int unlockpt(int);
int pty_master_open(char *slave_name, size_t sn_len) {
  int master_fd;
  int last_errno;
  char *p;
  master_fd = posix_openpt(O_RDWR | O_NOCTTY);
  if (master_fd == -1) {
    return -1;
  }
  if (grantpt(master_fd) == -1) {
    last_errno = errno;
    close(master_fd);
    errno = last_errno;
    return -1;
  }
  if (unlockpt(master_fd) == -1) {
    last_errno = errno;
    close(master_fd);
    errno = last_errno;
    return -1;
  }
  p = ptsname(master_fd);
  if (p == NULL) {
    last_errno = errno;
    close(master_fd);
    errno = last_errno;
    return -1;
  }
  if (strlen(p) < sn_len) {
    strncpy(slave_name, p, sn_len);
  } else {
    close(master_fd);
    errno = EOVERFLOW;
    return -1;
  }
  return master_fd;
}


x86 assembly (clang, -O0):
<pty_master_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
mov    0x2e40(%rip),%rax
or     (%rax),%edi
call   1090 <posix_openpt@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11b0 <pty_master_open+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c2 <pty_master_open+0x152>
mov    -0x1c(%rbp),%edi
call   1080 <grantpt@plt>
cmp    $0xffffffff,%eax
jne    11ed <pty_master_open+0x7d>
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%ecx
mov    0x2de9(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12c2 <pty_master_open+0x152>
mov    -0x1c(%rbp),%edi
call   1070 <unlockpt@plt>
cmp    $0xffffffff,%eax
jne    122a <pty_master_open+0xba>
mov    0x2dc3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%ecx
mov    0x2dac(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12c2 <pty_master_open+0x152>
mov    -0x1c(%rbp),%edi
call   1030 <ptsname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    126d <pty_master_open+0xfd>
mov    0x2d80(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%ecx
mov    0x2d69(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12c2 <pty_master_open+0x152>
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
cmp    -0x18(%rbp),%rax
jae    1296 <pty_master_open+0x126>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <strncpy@plt>
jmp    12bc <pty_master_open+0x14c>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    0x2d4b(%rip),%rax
mov    (%rax),%ecx
mov    0x2d1a(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12c2 <pty_master_open+0x152>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pty_master_open>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e5b(%rip),%rax
mov    0x2e44(%rip),%rcx
mov    (%rcx),%edi
or     (%rax),%edi
call   1090 <posix_openpt@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     11fb <pty_master_open+0x8b>
mov    %eax,%ebp
mov    %eax,%edi
call   1080 <grantpt@plt>
cmp    $0xffffffff,%eax
je     11e7 <pty_master_open+0x77>
mov    %ebp,%edi
call   1070 <unlockpt@plt>
cmp    $0xffffffff,%eax
je     11e7 <pty_master_open+0x77>
mov    %ebp,%edi
call   1030 <ptsname@plt>
test   %rax,%rax
je     11e7 <pty_master_open+0x77>
mov    %rax,%r12
mov    %rax,%rdi
call   1050 <strlen@plt>
cmp    %r14,%rax
jae    1206 <pty_master_open+0x96>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
call   1040 <strncpy@plt>
mov    %ebp,%ebx
jmp    11fb <pty_master_open+0x8b>
mov    0x2dda(%rip),%r14
mov    (%r14),%r15d
mov    %ebp,%edi
call   1060 <close@plt>
mov    %r15d,(%r14)
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    0x2dab(%rip),%rcx
mov    %eax,(%rcx)
jmp    11fb <pty_master_open+0x8b>

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

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