#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 getpt();
scalar_t__ grantpt(int);
int /*<<< orphan*/ perror(char *);
scalar_t__ unlockpt(int);
int pts_open(void) {
  int fd;
  if ((fd = getpt()) < 0) {
    perror("getpt()");
    return -1;
  }
  if (grantpt(fd) < 0) {
    perror("grantpt()");
    return -1;
  }
  if (unlockpt(fd) < 0) {
    perror("unlockpt()");
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<pts_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1030 <getpt@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    1173 <pts_open+0x33>
lea    0xe9e(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cd <pts_open+0x8d>
mov    -0x8(%rbp),%edi
call   1060 <grantpt@plt>
cmp    $0x0,%rax
jge    119d <pts_open+0x5d>
lea    0xe7c(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cd <pts_open+0x8d>
mov    -0x8(%rbp),%edi
call   1040 <unlockpt@plt>
cmp    $0x0,%rax
jge    11c7 <pts_open+0x87>
lea    0xe5c(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cd <pts_open+0x8d>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pts_open>:
push   %rbx
xor    %eax,%eax
call   1030 <getpt@plt>
test   %eax,%eax
js     116a <pts_open+0x2a>
mov    %eax,%ebx
mov    %eax,%edi
call   1060 <grantpt@plt>
test   %rax,%rax
js     1173 <pts_open+0x33>
mov    %ebx,%edi
call   1040 <unlockpt@plt>
test   %rax,%rax
js     117c <pts_open+0x3c>
mov    %ebx,%eax
pop    %rbx
ret
lea    0xe8f(%rip),%rdi
jmp    1183 <pts_open+0x43>
lea    0xe8e(%rip),%rdi
jmp    1183 <pts_open+0x43>
lea    0xe8f(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1166 <pts_open+0x26>

LLVM IR (-O0):
@.str = [8 x i8] c"getpt()\00"
@.str.1 = [10 x i8] c"grantpt()\00"
@.str.2 = [11 x i8] c"unlockpt()\00"
define i32 @pts_open() {
%1 = alloca i32
%2 = alloca i32
%3 = call i32 (...) @getpt()
store i32 %3, i32* %2
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %21
7: ; preds = %0
%8 = load i32, i32* %2
%9 = call i64 @grantpt(i32 %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %21
13: ; preds = %7
%14 = load i32, i32* %2
%15 = call i64 @unlockpt(i32 %14)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %21
19: ; preds = %13
%20 = load i32, i32* %2
store i32 %20, i32* %1
br label %21
21: ; preds = %19, %17, %11, %5
%22 = load i32, i32* %1
ret i32 %22
}

LLVM IR (-O2):
@.str = [8 x i8] c"getpt()\00"
@.str.1 = [10 x i8] c"grantpt()\00"
@.str.2 = [11 x i8] c"unlockpt()\00"
define i32 @pts_open() {
%1 = tail call i32 (...) @getpt()
%2 = icmp slt i32 %1, 0
br i1 %2, label %9, label %3
3: ; preds = %0
%4 = tail call i64 @grantpt(i32 %1)
%5 = icmp slt i64 %4, 0
br i1 %5, label %9, label %6
6: ; preds = %3
%7 = tail call i64 @unlockpt(i32 %1)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %6, %3, %0
%10 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), %0 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), %3 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), %6 ]
%11 = tail call i32 @perror(i8* %10)
br label %12
12: ; preds = %9, %6
%13 = phi i32 [ %1, %6 ], [ -1, %9 ]
ret i32 %13
}