#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 O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ die(char *, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int grantpt(int);
int posix_openpt(int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int unlockpt(int);
int open_master_pty(const char *desc) {
  int fd;
  if ((fd = posix_openpt(O_RDWR | O_NOCTTY)) == -1) {
    die("Unable to open master %s pseudo-tty: %s\n", desc, strerror(errno));
  }
  if (grantpt(fd) == -1) {
    die("Unable to grant permissions to master %s pty: %s\n", desc,
        strerror(errno));
  }
  if (unlockpt(fd) == -1) {
    die("Unable to unlock master %s pty: %s\n", desc, strerror(errno));
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_master_pty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
mov    0x2e6c(%rip),%rax
or     (%rax),%edi
call   1070 <posix_openpt@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    11a7 <open_master_pty+0x57>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x18(%rbp),%rsi
mov    %eax,%edx
lea    0xe5e(%rip),%rdi
call   1040 <die@plt>
mov    -0xc(%rbp),%edi
call   1050 <grantpt@plt>
cmp    $0xffffffff,%eax
jne    11e0 <open_master_pty+0x90>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
lea    0xe4e(%rip),%rdi
call   1040 <die@plt>
mov    -0xc(%rbp),%edi
call   1030 <unlockpt@plt>
cmp    $0xffffffff,%eax
jne    1219 <open_master_pty+0xc9>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
lea    0xe47(%rip),%rdi
call   1040 <die@plt>
mov    -0xc(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_master_pty>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e8b(%rip),%rax
mov    0x2e74(%rip),%rcx
mov    (%rcx),%edi
or     (%rax),%edi
call   1070 <posix_openpt@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1193 <open_master_pty+0x43>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe77(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <die@plt>
mov    %ebp,%edi
call   1050 <grantpt@plt>
cmp    $0xffffffff,%eax
jne    11be <open_master_pty+0x6e>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe75(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <die@plt>
mov    %ebp,%edi
call   1030 <unlockpt@plt>
cmp    $0xffffffff,%eax
jne    11e9 <open_master_pty+0x99>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe7c(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <die@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str = [41 x i8] c"Unable to open master %s pseudo-tty: %s\0A\00"
@errno = global i32 0
@.str.1 = [50 x i8] c"Unable to grant permissions to master %s pty: %s\0A\00"
@.str.2 = [36 x i8] c"Unable to unlock master %s pty: %s\0A\00"
define i32 @open_master_pty(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_NOCTTY
%6 = or i32 %4, %5
%7 = call i32 @posix_openpt(i32 %6)
store i32 %7, i32* %3
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %14
9: ; preds = %1
%10 = load i8*, i8** %2
%11 = load i32, i32* @errno
%12 = call i32 @strerror(i32 %11)
%13 = call i32 @die(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %10, i32 %12)
br label %14
14: ; preds = %9, %1
%15 = load i32, i32* %3
%16 = call i32 @grantpt(i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %23
18: ; preds = %14
%19 = load i8*, i8** %2
%20 = load i32, i32* @errno
%21 = call i32 @strerror(i32 %20)
%22 = call i32 @die(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i8* %19, i32 %21)
br label %23
23: ; preds = %18, %14
%24 = load i32, i32* %3
%25 = call i32 @unlockpt(i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %32
27: ; preds = %23
%28 = load i8*, i8** %2
%29 = load i32, i32* @errno
%30 = call i32 @strerror(i32 %29)
%31 = call i32 @die(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %28, i32 %30)
br label %32
32: ; preds = %27, %23
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str = [41 x i8] c"Unable to open master %s pseudo-tty: %s\0A\00"
@errno = global i32 0
@.str.1 = [50 x i8] c"Unable to grant permissions to master %s pty: %s\0A\00"
@.str.2 = [36 x i8] c"Unable to unlock master %s pty: %s\0A\00"
define i32 @open_master_pty(i8* %0) {
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_NOCTTY
%4 = or i32 %3, %2
%5 = tail call i32 @posix_openpt(i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %11
7: ; preds = %1
%8 = load i32, i32* @errno
%9 = tail call i32 @strerror(i32 %8)
%10 = tail call i32 @die(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %9)
br label %11
11: ; preds = %7, %1
%12 = tail call i32 @grantpt(i32 %5)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i32, i32* @errno
%16 = tail call i32 @strerror(i32 %15)
%17 = tail call i32 @die(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %16)
br label %18
18: ; preds = %14, %11
%19 = tail call i32 @unlockpt(i32 %5)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = load i32, i32* @errno
%23 = tail call i32 @strerror(i32 %22)
%24 = tail call i32 @die(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %23)
br label %25
25: ; preds = %21, %18
ret i32 %5
}