#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 */
struct statfs {
  int f_type;
};
typedef int /*<<< orphan*/ _buf;

/* Variables and functions */
int PATH_MAX;
scalar_t__ __libc_statfs(char *, struct statfs *);
int __unix_grantpt(int);
scalar_t__ pts_name(int, char **, int);
int grantpt(int fd) {
  struct statfs fsbuf;
  char _buf[PATH_MAX];
  char *buf = _buf;
  if (pts_name(fd, &buf, sizeof(_buf)))
    return -1;
  if (__libc_statfs(buf, &fsbuf) < 0)
    return -1;
  if (fsbuf.f_type != 0x1cd1 && fsbuf.f_type != 0x1373)
    return __unix_grantpt(fd);
  return 0;
}


x86 assembly (clang, -O0):
<grantpt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2eae(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    %eax,%edx
lea    -0x28(%rbp),%rsi
call   1040 <pts_name@plt>
cmp    $0x0,%rax
je     1192 <grantpt+0x62>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    11fb <grantpt+0xcb>
mov    -0x28(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1030 <__libc_statfs@plt>
cmp    $0x0,%rax
jge    11bc <grantpt+0x8c>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    11fb <grantpt+0xcb>
cmpl   $0x1cd1,-0x10(%rbp)
je     11ed <grantpt+0xbd>
cmpl   $0x1373,-0x10(%rbp)
je     11ed <grantpt+0xbd>
mov    -0x8(%rbp),%edi
call   1050 <__unix_grantpt@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    11fb <grantpt+0xcb>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<grantpt>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r14d
mov    %rsp,%r15
mov    0x2ea6(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rax
mov    %rdx,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rax
mov    %rax,%rsp
mov    %rax,-0x28(%rbp)
lea    -0x28(%rbp),%rsi
call   1040 <pts_name@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
je     1187 <grantpt+0x57>
mov    %r15,%rsp
mov    %ebx,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    -0x28(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1030 <__libc_statfs@plt>
test   %rax,%rax
js     1177 <grantpt+0x47>
xor    %ebx,%ebx
mov    -0x20(%rbp),%eax
cmp    $0x1373,%eax
je     1177 <grantpt+0x47>
cmp    $0x1cd1,%eax
je     1177 <grantpt+0x47>
mov    %r14d,%edi
call   1050 <__unix_grantpt@plt>
mov    %eax,%ebx
jmp    1177 <grantpt+0x47>

LLVM IR (-O0):
%struct.statfs = type { i32 }
@PATH_MAX = global i32 0
define i32 @grantpt(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.statfs
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
store i32 %0, i32* %3
%9 = load i32, i32* @PATH_MAX
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %5
%12 = alloca i8, i64 %10
store i64 %10, i64* %6
store i8* %12, i8** %7
%13 = load i32, i32* %3
%14 = trunc i64 %10 to i32
%15 = call i64 @pts_name(i32 %13, i8** %7, i32 %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %1
store i32 -1, i32* %2
store i32 1, i32* %8
br label %35
18: ; preds = %1
%19 = load i8*, i8** %7
%20 = call i64 @__libc_statfs(i8* %19, %struct.statfs* %4)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %18
store i32 -1, i32* %2
store i32 1, i32* %8
br label %35
23: ; preds = %18
%24 = getelementptr inbounds %struct.statfs, %struct.statfs* %4, i32 0, i32 0
%25 = load i32, i32* %24
%26 = icmp ne i32 %25, 7377
br i1 %26, label %27, label %34
27: ; preds = %23
%28 = getelementptr inbounds %struct.statfs, %struct.statfs* %4, i32 0, i32 0
%29 = load i32, i32* %28
%30 = icmp ne i32 %29, 4979
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = load i32, i32* %3
%33 = call i32 @__unix_grantpt(i32 %32)
store i32 %33, i32* %2
store i32 1, i32* %8
br label %35
34: ; preds = %27, %23
store i32 0, i32* %2
store i32 1, i32* %8
br label %35
35: ; preds = %34, %31, %22, %17
%36 = load i8*, i8** %5
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
%struct.statfs = type { i32 }
@PATH_MAX = global i32 0
define i32 @grantpt(i32 %0) {
%2 = alloca %struct.statfs
%3 = alloca i8*
%4 = bitcast %struct.statfs* %2 to i8*
%5 = load i32, i32* @PATH_MAX
%6 = zext i32 %5 to i64
%7 = tail call i8* @llvm.stacksave()
%8 = alloca i8, i64 %6
%9 = bitcast i8** %3 to i8*
store i8* %8, i8** %3
%10 = call i64 @pts_name(i32 %0, i8** %3, i32 %5)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %21
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = call i64 @__libc_statfs(i8* %13, %struct.statfs* %2)
%15 = icmp slt i64 %14, 0
br i1 %15, label %21, label %16
16: ; preds = %12
%17 = getelementptr inbounds %struct.statfs, %struct.statfs* %2, i64 0, i32 0
%18 = load i32, i32* %17
switch i32 %18, label %19 [
i32 7377, label %21
i32 4979, label %21
]
19: ; preds = %16
%20 = call i32 @__unix_grantpt(i32 %0)
br label %21
21: ; preds = %16, %16, %12, %1, %19
%22 = phi i32 [ %20, %19 ], [ -1, %1 ], [ -1, %12 ], [ 0, %16 ], [ 0, %16 ]
ret i32 %22
}