#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 /*<<< orphan*/ st_rdev;
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int O_CLOEXEC;
int O_NOCTTY;
int O_NONBLOCK;
int O_WRONLY;
int /*<<< orphan*/ S_ISCHR(int /*<<< orphan*/);
scalar_t__ asprintf(char **, char *, char const *, ...);
int /*<<< orphan*/ close_nointr_nofail(int);
int /*<<< orphan*/ free(char *);
int lstat(char const *, struct stat *);
int /*<<< orphan*/ major(int /*<<< orphan*/);
int /*<<< orphan*/ minor(int /*<<< orphan*/);
int open(char *, int);
scalar_t__ path_is_absolute(char const *);
bool wall_tty_match(const char *path) {
  int fd, k;
  char *p;
  struct stat st;
  if (path_is_absolute(path))
    k = lstat(path, &st);
  else {
    if (asprintf(&p, "/dev/%s", path) < 0)
      return true;
    k = lstat(p, &st);
    free(p);
  }
  if (k < 0)
    return true;
  if (!S_ISCHR(st.st_mode))
    return true;
  if (asprintf(&p, "/run/systemd/ask-password-block/%u:%u", major(st.st_rdev),
               minor(st.st_rdev)) < 0)
    return true;
  fd = open(p, O_WRONLY | O_CLOEXEC | O_NONBLOCK | O_NOCTTY);
  free(p);
  if (fd < 0)
    return true;
  close_nointr_nofail(fd);
  return false;
}


x86 assembly (clang, -O0):
<wall_tty_match>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <path_is_absolute@plt>
cmp    $0x0,%rax
je     11c4 <wall_tty_match+0x34>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1070 <lstat@plt>
mov    %eax,-0x18(%rbp)
jmp    1209 <wall_tty_match+0x79>
mov    -0x10(%rbp),%rdx
lea    -0x20(%rbp),%rdi
lea    0xe2d(%rip),%rsi
mov    $0x0,%al
call   10a0 <asprintf@plt>
cmp    $0x0,%rax
jge    11f0 <wall_tty_match+0x60>
movl   $0x1,-0x4(%rbp)
jmp    12de <wall_tty_match+0x14e>
mov    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1070 <lstat@plt>
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x18(%rbp)
jge    121f <wall_tty_match+0x8f>
movl   $0x1,-0x4(%rbp)
jmp    12de <wall_tty_match+0x14e>
mov    -0x24(%rbp),%edi
call   1090 <S_ISCHR@plt>
cmp    $0x0,%eax
jne    123c <wall_tty_match+0xac>
movl   $0x1,-0x4(%rbp)
jmp    12de <wall_tty_match+0x14e>
mov    -0x28(%rbp),%edi
call   1050 <major@plt>
cltq
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%edi
call   1040 <minor@plt>
mov    -0x30(%rbp),%rdx
mov    %eax,%ecx
lea    -0x20(%rbp),%rdi
lea    0xda5(%rip),%rsi
mov    $0x0,%al
call   10a0 <asprintf@plt>
cmp    $0x0,%rax
jge    1280 <wall_tty_match+0xf0>
movl   $0x1,-0x4(%rbp)
jmp    12de <wall_tty_match+0x14e>
mov    -0x20(%rbp),%rdi
mov    0x2d3d(%rip),%rax
mov    (%rax),%esi
mov    0x2d2c(%rip),%rax
or     (%rax),%esi
mov    0x2d4b(%rip),%rax
or     (%rax),%esi
mov    0x2d32(%rip),%rax
or     (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x14(%rbp)
jge    12cf <wall_tty_match+0x13f>
movl   $0x1,-0x4(%rbp)
jmp    12de <wall_tty_match+0x14e>
mov    -0x14(%rbp),%edi
call   1060 <close_nointr_nofail@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wall_tty_match>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
call   1080 <path_is_absolute@plt>
test   %rax,%rax
je     11b6 <wall_tty_match+0x26>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1070 <lstat@plt>
mov    %eax,%ebp
jmp    11ec <wall_tty_match+0x5c>
lea    0xe43(%rip),%rsi
mov    %rsp,%rdi
mov    %rbx,%rdx
xor    %eax,%eax
call   10a0 <asprintf@plt>
test   %rax,%rax
js     127d <wall_tty_match+0xed>
mov    (%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1070 <lstat@plt>
mov    %eax,%ebp
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    $0x1,%ebx
test   %ebp,%ebp
js     1282 <wall_tty_match+0xf2>
mov    0xc(%rsp),%edi
call   1090 <S_ISCHR@plt>
test   %eax,%eax
je     1282 <wall_tty_match+0xf2>
mov    0x8(%rsp),%ebp
mov    %ebp,%edi
call   1050 <major@plt>
movslq %eax,%r14
mov    %ebp,%edi
call   1040 <minor@plt>
lea    0xde6(%rip),%rsi
mov    %rsp,%rdi
mov    %r14,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   10a0 <asprintf@plt>
test   %rax,%rax
js     1282 <wall_tty_match+0xf2>
mov    0x2d8b(%rip),%rax
mov    0x2d7c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d99(%rip),%rax
or     (%rax),%esi
mov    (%rsp),%rdi
mov    0x2d7c(%rip),%rax
or     (%rax),%esi
call   10b0 <open@plt>
mov    %eax,%ebp
mov    (%rsp),%rdi
call   1030 <free@plt>
test   %ebp,%ebp
js     1282 <wall_tty_match+0xf2>
mov    %ebp,%edi
call   1060 <close_nointr_nofail@plt>
xor    %ebx,%ebx
jmp    1282 <wall_tty_match+0xf2>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@.str = [8 x i8] c"/dev/%s\00"
@.str.1 = [38 x i8] c"/run/systemd/ask-password-block/%u:%u\00"
@O_WRONLY = global i32 0
@O_CLOEXEC = global i32 0
@O_NONBLOCK = global i32 0
@O_NOCTTY = global i32 0
define i32 @wall_tty_match(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca %struct.stat
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i64 @path_is_absolute(i8* %8)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = call i32 @lstat(i8* %12, %struct.stat* %7)
store i32 %13, i32* %5
br label %24
14: ; preds = %1
%15 = load i8*, i8** %3
%16 = call i64 (i8**, i8*, i8*, ...) @asprintf(i8** %6, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %14
store i32 1, i32* %2
br label %64
19: ; preds = %14
%20 = load i8*, i8** %6
%21 = call i32 @lstat(i8* %20, %struct.stat* %7)
store i32 %21, i32* %5
%22 = load i8*, i8** %6
%23 = call i32 @free(i8* %22)
br label %24
24: ; preds = %19, %11
%25 = load i32, i32* %5
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %24
store i32 1, i32* %2
br label %64
28: ; preds = %24
%29 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 1
%30 = load i32, i32* %29
%31 = call i32 @S_ISCHR(i32 %30)
%32 = icmp ne i32 %31, 0
br i1 %32, label %34, label %33
33: ; preds = %28
store i32 1, i32* %2
br label %64
34: ; preds = %28
%35 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%36 = load i32, i32* %35
%37 = call i32 @major(i32 %36)
%38 = sext i32 %37 to i64
%39 = inttoptr i64 %38 to i8*
%40 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%41 = load i32, i32* %40
%42 = call i32 @minor(i32 %41)
%43 = call i64 (i8**, i8*, i8*, ...) @asprintf(i8** %6, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %39, i32 %42)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %34
store i32 1, i32* %2
br label %64
46: ; preds = %34
%47 = load i8*, i8** %6
%48 = load i32, i32* @O_WRONLY
%49 = load i32, i32* @O_CLOEXEC
%50 = or i32 %48, %49
%51 = load i32, i32* @O_NONBLOCK
%52 = or i32 %50, %51
%53 = load i32, i32* @O_NOCTTY
%54 = or i32 %52, %53
%55 = call i32 @open(i8* %47, i32 %54)
store i32 %55, i32* %4
%56 = load i8*, i8** %6
%57 = call i32 @free(i8* %56)
%58 = load i32, i32* %4
%59 = icmp slt i32 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %46
store i32 1, i32* %2
br label %64
61: ; preds = %46
%62 = load i32, i32* %4
%63 = call i32 @close_nointr_nofail(i32 %62)
store i32 0, i32* %2
br label %64
64: ; preds = %61, %60, %45, %33, %27, %18
%65 = load i32, i32* %2
ret i32 %65
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@.str = [8 x i8] c"/dev/%s\00"
@.str.1 = [38 x i8] c"/run/systemd/ask-password-block/%u:%u\00"
@O_WRONLY = global i32 0
@O_CLOEXEC = global i32 0
@O_NONBLOCK = global i32 0
@O_NOCTTY = global i32 0
define i32 @wall_tty_match(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.stat
%4 = bitcast i8** %2 to i8*
%5 = bitcast %struct.stat* %3 to i8*
%6 = tail call i64 @path_is_absolute(i8* %0)
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %1
%9 = call i32 @lstat(i8* %0, %struct.stat* %3)
br label %18
10: ; preds = %1
%11 = call i64 (i8**, i8*, i8*, ...) @asprintf(i8** %2, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %0)
%12 = icmp slt i64 %11, 0
br i1 %12, label %50, label %13
13: ; preds = %10
%14 = load i8*, i8** %2
%15 = call i32 @lstat(i8* %14, %struct.stat* %3)
%16 = load i8*, i8** %2
%17 = call i32 @free(i8* %16)
br label %18
18: ; preds = %13, %8
%19 = phi i32 [ %9, %8 ], [ %15, %13 ]
%20 = icmp slt i32 %19, 0
br i1 %20, label %50, label %21
21: ; preds = %18
%22 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%23 = load i32, i32* %22
%24 = call i32 @S_ISCHR(i32 %23)
%25 = icmp eq i32 %24, 0
br i1 %25, label %50, label %26
26: ; preds = %21
%27 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @major(i32 %28)
%30 = sext i32 %29 to i64
%31 = inttoptr i64 %30 to i8*
%32 = call i32 @minor(i32 %28)
%33 = call i64 (i8**, i8*, i8*, ...) @asprintf(i8** %2, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %31, i32 %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %50, label %35
35: ; preds = %26
%36 = load i8*, i8** %2
%37 = load i32, i32* @O_WRONLY
%38 = load i32, i32* @O_CLOEXEC
%39 = or i32 %38, %37
%40 = load i32, i32* @O_NONBLOCK
%41 = or i32 %39, %40
%42 = load i32, i32* @O_NOCTTY
%43 = or i32 %41, %42
%44 = call i32 @open(i8* %36, i32 %43)
%45 = load i8*, i8** %2
%46 = call i32 @free(i8* %45)
%47 = icmp slt i32 %44, 0
br i1 %47, label %50, label %48
48: ; preds = %35
%49 = call i32 @close_nointr_nofail(i32 %44)
br label %50
50: ; preds = %35, %26, %21, %18, %10, %48
%51 = phi i32 [ 0, %48 ], [ 1, %10 ], [ 1, %18 ], [ 1, %21 ], [ 1, %26 ], [ 1, %35 ]
ret i32 %51
}