#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*/ close(int);
scalar_t__ grantpt(int);
scalar_t__ ptsname(int);
int startpar_getpt();
scalar_t__ unlockpt(int);
int checkdevpts(void) {
  int ptsfd = startpar_getpt();
  if (ptsfd == -1) {
    return 0;
  } else if (ptsname(ptsfd) == 0 || grantpt(ptsfd) || unlockpt(ptsfd)) {
    close(ptsfd);
    return 0;
  } else {
    close(ptsfd);
    return 1;
  }
}


x86 assembly (clang, -O0):
<checkdevpts>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1040 <startpar_getpt@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1178 <checkdevpts+0x28>
movl   $0x0,-0x4(%rbp)
jmp    11d1 <checkdevpts+0x81>
mov    -0x8(%rbp),%edi
call   1030 <ptsname@plt>
cmp    $0x0,%rax
je     11ae <checkdevpts+0x5e>
mov    -0x8(%rbp),%edi
call   1070 <grantpt@plt>
cmp    $0x0,%rax
jne    11ae <checkdevpts+0x5e>
mov    -0x8(%rbp),%edi
call   1060 <unlockpt@plt>
cmp    $0x0,%rax
je     11c2 <checkdevpts+0x72>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    11d1 <checkdevpts+0x81>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checkdevpts>:
push   %rbp
push   %rbx
push   %rax
xor    %ebp,%ebp
xor    %eax,%eax
call   1040 <startpar_getpt@plt>
cmp    $0xffffffff,%eax
je     1194 <checkdevpts+0x44>
mov    %eax,%ebx
mov    %eax,%edi
call   1030 <ptsname@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     118d <checkdevpts+0x3d>
mov    %ebx,%edi
call   1070 <grantpt@plt>
test   %rax,%rax
jne    118d <checkdevpts+0x3d>
mov    %ebx,%edi
call   1060 <unlockpt@plt>
xor    %ebp,%ebp
test   %rax,%rax
sete   %bpl
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @checkdevpts() {
%1 = alloca i32
%2 = alloca i32
%3 = call i32 (...) @startpar_getpt()
store i32 %3, i32* %2
%4 = load i32, i32* %2
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %7
6: ; preds = %0
store i32 0, i32* %1
br label %25
7: ; preds = %0
%8 = load i32, i32* %2
%9 = call i64 @ptsname(i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %19, label %11
11: ; preds = %7
%12 = load i32, i32* %2
%13 = call i64 @grantpt(i32 %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %11
%16 = load i32, i32* %2
%17 = call i64 @unlockpt(i32 %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15, %11, %7
%20 = load i32, i32* %2
%21 = call i32 @close(i32 %20)
store i32 0, i32* %1
br label %25
22: ; preds = %15
%23 = load i32, i32* %2
%24 = call i32 @close(i32 %23)
store i32 1, i32* %1
br label %25
25: ; preds = %22, %19, %6
%26 = load i32, i32* %1
ret i32 %26
}

LLVM IR (-O2):
define i32 @checkdevpts() {
%1 = tail call i32 (...) @startpar_getpt()
%2 = icmp eq i32 %1, -1
br i1 %2, label %16, label %3
3: ; preds = %0
%4 = tail call i64 @ptsname(i32 %1)
%5 = icmp eq i64 %4, 0
br i1 %5, label %13, label %6
6: ; preds = %3
%7 = tail call i64 @grantpt(i32 %1)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %6
%10 = tail call i64 @unlockpt(i32 %1)
%11 = icmp eq i64 %10, 0
%12 = zext i1 %11 to i32
br label %13
13: ; preds = %9, %3, %6
%14 = phi i32 [ 0, %6 ], [ 0, %3 ], [ %12, %9 ]
%15 = tail call i32 @close(i32 %1)
br label %16
16: ; preds = %13, %0
%17 = phi i32 [ 0, %0 ], [ %14, %13 ]
ret i32 %17
}