#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 STDERR_FILENO;
int /*<<< orphan*/ errno_exit(char *);
int /*<<< orphan*/ error_exit(char *);
scalar_t__ grantpt(int);
int posix_openpt(int);
scalar_t__ unlockpt(int);
int prepare_master_pseudo_terminal(void) {
  int fd = posix_openpt(O_RDWR | O_NOCTTY);
  if (fd < 0)
    errno_exit("cannot open master pseudo-terminal");
  if (fd <= STDERR_FILENO)
    error_exit("stdin/stdout/stderr are not open");
  if (grantpt(fd) < 0)
    errno_exit("pseudo-terminal permission not granted");
  if (unlockpt(fd) < 0)
    errno_exit("pseudo-terminal permission not unlocked");
  return fd;
}


x86 assembly (clang, -O0):
<prepare_master_pseudo_terminal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e89(%rip),%rax
mov    (%rax),%edi
mov    0x2e70(%rip),%rax
or     (%rax),%edi
call   1070 <posix_openpt@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    1188 <prepare_master_pseudo_terminal+0x38>
lea    0xe7d(%rip),%rdi
call   1030 <errno_exit@plt>
mov    -0x4(%rbp),%eax
mov    0x2e3e(%rip),%rcx
cmp    (%rcx),%eax
jg     11a6 <prepare_master_pseudo_terminal+0x56>
lea    0xe82(%rip),%rdi
call   1040 <error_exit@plt>
mov    -0x4(%rbp),%edi
call   1060 <grantpt@plt>
cmp    $0x0,%rax
jge    11c4 <prepare_master_pseudo_terminal+0x74>
lea    0xe85(%rip),%rdi
call   1030 <errno_exit@plt>
mov    -0x4(%rbp),%edi
call   1050 <unlockpt@plt>
cmp    $0x0,%rax
jge    11e2 <prepare_master_pseudo_terminal+0x92>
lea    0xe8e(%rip),%rdi
call   1030 <errno_exit@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prepare_master_pseudo_terminal>:
push   %rbx
mov    0x2e90(%rip),%rax
mov    0x2e79(%rip),%rcx
mov    (%rcx),%edi
or     (%rax),%edi
call   1070 <posix_openpt@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    117a <prepare_master_pseudo_terminal+0x2a>
lea    0xe8b(%rip),%rdi
call   1030 <errno_exit@plt>
mov    0x2e4f(%rip),%rax
cmp    (%rax),%ebx
jg     1191 <prepare_master_pseudo_terminal+0x41>
lea    0xe97(%rip),%rdi
call   1040 <error_exit@plt>
mov    %ebx,%edi
call   1060 <grantpt@plt>
test   %rax,%rax
jns    11a9 <prepare_master_pseudo_terminal+0x59>
lea    0xea0(%rip),%rdi
call   1030 <errno_exit@plt>
mov    %ebx,%edi
call   1050 <unlockpt@plt>
test   %rax,%rax
jns    11c1 <prepare_master_pseudo_terminal+0x71>
lea    0xeaf(%rip),%rdi
call   1030 <errno_exit@plt>
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str = [35 x i8] c"cannot open master pseudo-terminal\00"
@STDERR_FILENO = global i32 0
@.str.1 = [33 x i8] c"stdin/stdout/stderr are not open\00"
@.str.2 = [39 x i8] c"pseudo-terminal permission not granted\00"
@.str.3 = [40 x i8] c"pseudo-terminal permission not unlocked\00"
define i32 @prepare_master_pseudo_terminal() {
%1 = alloca i32
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_NOCTTY
%4 = or i32 %2, %3
%5 = call i32 @posix_openpt(i32 %4)
store i32 %5, i32* %1
%6 = load i32, i32* %1
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @errno_exit(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %0
%11 = load i32, i32* %1
%12 = load i32, i32* @STDERR_FILENO
%13 = icmp sle i32 %11, %12
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = call i32 @error_exit(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %10
%17 = load i32, i32* %1
%18 = call i64 @grantpt(i32 %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @errno_exit(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
br label %22
22: ; preds = %20, %16
%23 = load i32, i32* %1
%24 = call i64 @unlockpt(i32 %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 @errno_exit(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0))
br label %28
28: ; preds = %26, %22
%29 = load i32, i32* %1
ret i32 %29
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str = [35 x i8] c"cannot open master pseudo-terminal\00"
@STDERR_FILENO = global i32 0
@.str.1 = [33 x i8] c"stdin/stdout/stderr are not open\00"
@.str.2 = [39 x i8] c"pseudo-terminal permission not granted\00"
@.str.3 = [40 x i8] c"pseudo-terminal permission not unlocked\00"
define i32 @prepare_master_pseudo_terminal() {
%1 = load i32, i32* @O_RDWR
%2 = load i32, i32* @O_NOCTTY
%3 = or i32 %2, %1
%4 = tail call i32 @posix_openpt(i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @errno_exit(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %0
%9 = load i32, i32* @STDERR_FILENO
%10 = icmp sgt i32 %4, %9
br i1 %10, label %13, label %11
11: ; preds = %8
%12 = tail call i32 @error_exit(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %8
%14 = tail call i64 @grantpt(i32 %4)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = tail call i32 @errno_exit(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %16, %13
%19 = tail call i64 @unlockpt(i32 %4)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = tail call i32 @errno_exit(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0))
br label %23
23: ; preds = %21, %18
ret i32 %4
}