#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 /*<<< orphan*/ O_RDWR;
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
scalar_t__ grantpt(int);
int posix_openpt(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
scalar_t__ unlockpt(int);
int openpt(void) {
  int fd = posix_openpt(O_RDWR);
  if (fd < 0) {
    fprintf(stderr, "Error %d on posix_openpt()\n", errno);
    return -1;
  }
  if (grantpt(fd) != 0) {
    fprintf(stderr, "Error %d on grantpt()\n", errno);
    return -1;
  }
  if (unlockpt(fd) != 0) {
    fprintf(stderr, "Error %d on unlockpt()\n", errno);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<openpt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e99(%rip),%rax
mov    (%rax),%edi
call   1060 <posix_openpt@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    118d <openpt+0x4d>
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
mov    0x2e5d(%rip),%rax
mov    (%rax),%edx
lea    0xe84(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120b <openpt+0xcb>
mov    -0x8(%rbp),%edi
call   1050 <grantpt@plt>
cmp    $0x0,%rax
je     11c9 <openpt+0x89>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    0x2e21(%rip),%rax
mov    (%rax),%edx
lea    0xe64(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120b <openpt+0xcb>
mov    -0x8(%rbp),%edi
call   1030 <unlockpt@plt>
cmp    $0x0,%rax
je     1205 <openpt+0xc5>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
mov    0x2de5(%rip),%rax
mov    (%rax),%edx
lea    0xe3f(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120b <openpt+0xcb>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openpt>:
push   %rbx
mov    0x2ea0(%rip),%rax
mov    (%rax),%edi
call   1060 <posix_openpt@plt>
test   %eax,%eax
js     116a <openpt+0x2a>
mov    %eax,%ebx
mov    %eax,%edi
call   1050 <grantpt@plt>
test   %rax,%rax
je     1173 <openpt+0x33>
lea    0xeb4(%rip),%rsi
jmp    1186 <openpt+0x46>
lea    0xe8f(%rip),%rsi
jmp    1186 <openpt+0x46>
mov    %ebx,%edi
call   1030 <unlockpt@plt>
test   %rax,%rax
je     11a2 <openpt+0x62>
lea    0xead(%rip),%rsi
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"Error %d on posix_openpt()\0A\00"
@errno = global i32 0
@.str.1 = [23 x i8] c"Error %d on grantpt()\0A\00"
@.str.2 = [24 x i8] c"Error %d on unlockpt()\0A\00"
define i32 @openpt() {
%1 = alloca i32
%2 = alloca i32
%3 = load i32, i32* @O_RDWR
%4 = call i32 @posix_openpt(i32 %3)
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 -1, i32* %1
br label %29
11: ; preds = %0
%12 = load i32, i32* %2
%13 = call i64 @grantpt(i32 %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = load i32, i32* @errno
%18 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %17)
store i32 -1, i32* %1
br label %29
19: ; preds = %11
%20 = load i32, i32* %2
%21 = call i64 @unlockpt(i32 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %25)
store i32 -1, i32* %1
br label %29
27: ; preds = %19
%28 = load i32, i32* %2
store i32 %28, i32* %1
br label %29
29: ; preds = %27, %23, %15, %7
%30 = load i32, i32* %1
ret i32 %30
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"Error %d on posix_openpt()\0A\00"
@errno = global i32 0
@.str.1 = [23 x i8] c"Error %d on grantpt()\0A\00"
@.str.2 = [24 x i8] c"Error %d on unlockpt()\0A\00"
define i32 @openpt() {
%1 = load i32, i32* @O_RDWR
%2 = tail call i32 @posix_openpt(i32 %1)
%3 = icmp slt i32 %2, 0
br i1 %3, label %10, label %4
4: ; preds = %0
%5 = tail call i64 @grantpt(i32 %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %4
%8 = tail call i64 @unlockpt(i32 %2)
%9 = icmp eq i64 %8, 0
br i1 %9, label %15, label %10
10: ; preds = %7, %4, %0
%11 = phi i8* [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), %0 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), %4 ], [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), %7 ]
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = tail call i32 @fprintf(i32 %12, i8* %11, i32 %13)
br label %15
15: ; preds = %10, %7
%16 = phi i32 [ %2, %7 ], [ -1, %10 ]
ret i32 %16
}