#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 stat {
  int st_mode;
  scalar_t__ st_rdev;
};
typedef scalar_t__ dev_t;

/* Variables and functions */
int ACCESSPERMS;
int S_ISCHR(int);
scalar_t__ fstatat(int, char const *, struct stat *, int /*<<< orphan*/);
scalar_t__ makedev(int, int);
bool ptmx_device_usable(int dirfd, const char *path) {
  struct stat st;
  int perms;
  bool world_readable, world_writable;
  bool is_char;
  bool dev_type;
  dev_t expected_dev;
  if (dirfd < 0 || !path) {
    return false;
  }
  expected_dev = makedev(5, 2);
  if (fstatat(dirfd, path, &st, 0) < 0) {
    return false;
  }
  is_char = S_ISCHR(st.st_mode);
  dev_type = (expected_dev == st.st_rdev);
  perms = (st.st_mode & ACCESSPERMS);
  world_readable = (perms & 0444) == 0444;
  world_writable = (perms & 0222) == 0222;
  return (is_char && dev_type && world_readable && world_writable);
}


x86 assembly (clang, -O0):
<ptmx_device_usable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1154 <ptmx_device_usable+0x24>
cmpq   $0x0,-0x10(%rbp)
jne    1160 <ptmx_device_usable+0x30>
movl   $0x0,-0x4(%rbp)
jmp    123a <ptmx_device_usable+0x10a>
mov    $0x5,%edi
mov    $0x2,%esi
call   1040 <makedev@plt>
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    -0x20(%rbp),%rdx
xor    %ecx,%ecx
call   1050 <fstatat@plt>
cmp    $0x0,%rax
jge    119b <ptmx_device_usable+0x6b>
movl   $0x0,-0x4(%rbp)
jmp    123a <ptmx_device_usable+0x10a>
mov    -0x20(%rbp),%edi
call   1030 <S_ISCHR@plt>
mov    %eax,-0x30(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x18(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%eax
mov    0x2e25(%rip),%rcx
and    (%rcx),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
and    $0x124,%eax
cmp    $0x124,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
and    $0x92,%eax
cmp    $0x92,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x2c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x30(%rbp)
mov    %al,-0x41(%rbp)
je     122f <ptmx_device_usable+0xff>
xor    %eax,%eax
cmpl   $0x0,-0x34(%rbp)
mov    %al,-0x41(%rbp)
je     122f <ptmx_device_usable+0xff>
xor    %eax,%eax
cmpl   $0x0,-0x28(%rbp)
mov    %al,-0x41(%rbp)
je     122f <ptmx_device_usable+0xff>
cmpl   $0x0,-0x2c(%rbp)
setne  %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ptmx_device_usable>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
xor    %ebx,%ebx
test   %edi,%edi
js     11b1 <ptmx_device_usable+0x81>
mov    %rsi,%r15
test   %rsi,%rsi
je     11b1 <ptmx_device_usable+0x81>
mov    %edi,%ebp
mov    $0x5,%edi
mov    $0x2,%esi
call   1040 <makedev@plt>
mov    %rax,%r14
lea    0x8(%rsp),%rdx
xor    %ebx,%ebx
mov    %ebp,%edi
mov    %r15,%rsi
xor    %ecx,%ecx
call   1050 <fstatat@plt>
test   %rax,%rax
js     11b1 <ptmx_device_usable+0x81>
mov    0x8(%rsp),%edi
call   1030 <S_ISCHR@plt>
cmp    0x10(%rsp),%r14
sete   %cl
mov    0x2e5c(%rip),%rdx
mov    (%rdx),%edx
and    0x8(%rsp),%edx
not    %edx
test   $0x124,%edx
sete   %bl
test   %eax,%eax
setne  %al
and    %cl,%al
and    %bl,%al
test   $0x92,%dl
sete   %cl
and    %al,%cl
movzbl %cl,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i64 }
@ACCESSPERMS = global i32 0
define i32 @ptmx_device_usable(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %2
%16 = load i8*, i8** %5
%17 = icmp ne i8* %16, null
br i1 %17, label %19, label %18
18: ; preds = %15, %2
store i32 0, i32* %3
br label %61
19: ; preds = %15
%20 = call i64 @makedev(i32 5, i32 2)
store i64 %20, i64* %12
%21 = load i32, i32* %4
%22 = load i8*, i8** %5
%23 = call i64 @fstatat(i32 %21, i8* %22, %struct.stat* %6, i32 0)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %19
store i32 0, i32* %3
br label %61
26: ; preds = %19
%27 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @S_ISCHR(i32 %28)
store i32 %29, i32* %10
%30 = load i64, i64* %12
%31 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 1
%32 = load i64, i64* %31
%33 = icmp eq i64 %30, %32
%34 = zext i1 %33 to i32
store i32 %34, i32* %11
%35 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%36 = load i32, i32* %35
%37 = load i32, i32* @ACCESSPERMS
%38 = and i32 %36, %37
store i32 %38, i32* %7
%39 = load i32, i32* %7
%40 = and i32 %39, 292
%41 = icmp eq i32 %40, 292
%42 = zext i1 %41 to i32
store i32 %42, i32* %8
%43 = load i32, i32* %7
%44 = and i32 %43, 146
%45 = icmp eq i32 %44, 146
%46 = zext i1 %45 to i32
store i32 %46, i32* %9
%47 = load i32, i32* %10
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %58
49: ; preds = %26
%50 = load i32, i32* %11
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %58
52: ; preds = %49
%53 = load i32, i32* %8
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %52
%56 = load i32, i32* %9
%57 = icmp ne i32 %56, 0
br label %58
58: ; preds = %55, %52, %49, %26
%59 = phi i1 [ false, %52 ], [ false, %49 ], [ false, %26 ], [ %57, %55 ]
%60 = zext i1 %59 to i32
store i32 %60, i32* %3
br label %61
61: ; preds = %58, %25, %18
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
%struct.stat = type { i32, i64 }
@ACCESSPERMS = global i32 0
define i32 @ptmx_device_usable(i32 %0, i8* %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp sgt i32 %0, -1
%6 = icmp ne i8* %1, null
%7 = and i1 %5, %6
br i1 %7, label %8, label %31
8: ; preds = %2
%9 = tail call i64 @makedev(i32 5, i32 2)
%10 = call i64 @fstatat(i32 %0, i8* %1, %struct.stat* %3, i32 0)
%11 = icmp slt i64 %10, 0
br i1 %11, label %31, label %12
12: ; preds = %8
%13 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%14 = load i32, i32* %13
%15 = call i32 @S_ISCHR(i32 %14)
%16 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%17 = load i64, i64* %16
%18 = icmp eq i64 %9, %17
%19 = load i32, i32* %13
%20 = load i32, i32* @ACCESSPERMS
%21 = and i32 %20, %19
%22 = and i32 %21, 292
%23 = icmp eq i32 %22, 292
%24 = icmp ne i32 %15, 0
%25 = select i1 %24, i1 %18, i1 false
%26 = select i1 %25, i1 %23, i1 false
%27 = and i32 %21, 146
%28 = icmp eq i32 %27, 146
%29 = select i1 %26, i1 %28, i1 false
%30 = zext i1 %29 to i32
br label %31
31: ; preds = %8, %2, %12
%32 = phi i32 [ %30, %12 ], [ 0, %2 ], [ 0, %8 ]
ret i32 %32
}