#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 errno;
scalar_t__ grantpt(int);
int posix_openpt(int);
scalar_t__ unlockpt(int);
int new_pty(void) {
  int fd = posix_openpt(O_RDWR | O_NOCTTY);
  if (fd < 0)
    return -errno;
  if (grantpt(fd) < 0)
    return -errno;
  if (unlockpt(fd) < 0)
    return -errno;
  return fd;
}


x86 assembly (clang, -O0):
<new_pty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2ea9(%rip),%rax
mov    (%rax),%edi
mov    0x2e90(%rip),%rax
or     (%rax),%edi
call   1050 <posix_openpt@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    116f <new_pty+0x3f>
xor    %eax,%eax
mov    0x2e6b(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11bf <new_pty+0x8f>
mov    -0x8(%rbp),%edi
call   1040 <grantpt@plt>
cmp    $0x0,%rax
jge    1194 <new_pty+0x64>
xor    %eax,%eax
mov    0x2e46(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11bf <new_pty+0x8f>
mov    -0x8(%rbp),%edi
call   1030 <unlockpt@plt>
cmp    $0x0,%rax
jge    11b9 <new_pty+0x89>
xor    %eax,%eax
mov    0x2e21(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11bf <new_pty+0x8f>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<new_pty>:
push   %rbx
mov    0x2eb0(%rip),%rax
mov    0x2e99(%rip),%rcx
mov    (%rcx),%edi
or     (%rax),%edi
call   1050 <posix_openpt@plt>
test   %eax,%eax
js     1166 <new_pty+0x36>
mov    %eax,%ebx
mov    %eax,%edi
call   1040 <grantpt@plt>
test   %rax,%rax
js     1166 <new_pty+0x36>
mov    %ebx,%edi
call   1030 <unlockpt@plt>
test   %rax,%rax
jns    1171 <new_pty+0x41>
mov    0x2e63(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@errno = global i32 0
define i32 @new_pty() {
%1 = alloca i32
%2 = alloca i32
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_NOCTTY
%5 = or i32 %3, %4
%6 = call i32 @posix_openpt(i32 %5)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = load i32, i32* @errno
%11 = sub nsw i32 0, %10
store i32 %11, i32* %1
br label %28
12: ; preds = %0
%13 = load i32, i32* %2
%14 = call i64 @grantpt(i32 %13)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i32, i32* @errno
%18 = sub nsw i32 0, %17
store i32 %18, i32* %1
br label %28
19: ; preds = %12
%20 = load i32, i32* %2
%21 = call i64 @unlockpt(i32 %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = load i32, i32* @errno
%25 = sub nsw i32 0, %24
store i32 %25, i32* %1
br label %28
26: ; preds = %19
%27 = load i32, i32* %2
store i32 %27, i32* %1
br label %28
28: ; preds = %26, %23, %16, %9
%29 = load i32, i32* %1
ret i32 %29
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@errno = global i32 0
define i32 @new_pty() {
%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 %9
6: ; preds = %0
%7 = load i32, i32* @errno
%8 = sub nsw i32 0, %7
br label %21
9: ; preds = %0
%10 = tail call i64 @grantpt(i32 %4)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i32, i32* @errno
%14 = sub nsw i32 0, %13
br label %21
15: ; preds = %9
%16 = tail call i64 @unlockpt(i32 %4)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load i32, i32* @errno
%20 = sub nsw i32 0, %19
br label %21
21: ; preds = %15, %18, %12, %6
%22 = phi i32 [ %8, %6 ], [ %14, %12 ], [ %20, %18 ], [ %4, %15 ]
ret i32 %22
}