#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 */
typedef scalar_t__ uid_t;
struct stat {
  scalar_t__ st_uid;
};

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ LOG_ERR;
scalar_t__ errno;
int /*<<< orphan*/ snprintf(char *, int, char *, int);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char *);
int console_owner(uid_t uid, int cons) {
  char name[256];
  struct stat st;
  snprintf(name, sizeof name, "/dev/tty%d", cons);
  if (stat(name, &st) < 0) {
    if (errno != ENOENT)
      syslog(LOG_ERR, "stat(%s): %m", name);
    return 0;
  }
  return uid == st.st_uid;
}


x86 assembly (clang, -O0):
<console_owner>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x100,%esi
lea    0xea8(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x120(%rbp),%rdi
lea    -0x128(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jge    11bc <console_owner+0x8c>
mov    0x2e5f(%rip),%rax
mov    (%rax),%rax
mov    0x2e45(%rip),%rcx
cmp    (%rcx),%rax
je     11b0 <console_owner+0x80>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
lea    -0x120(%rbp),%rdx
lea    0xe60(%rip),%rsi
call   1050 <syslog@plt>
movl   $0x0,-0x4(%rbp)
jmp    11d2 <console_owner+0xa2>
mov    -0x10(%rbp),%rax
cmp    -0x128(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<console_owner>:
push   %r14
push   %rbx
sub    $0x118,%rsp
mov    %esi,%ecx
mov    %rdi,%r14
lea    0xeba(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1030 <snprintf@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
test   %rax,%rax
js     1176 <console_owner+0x46>
xor    %ebx,%ebx
cmp    %r14,0x8(%rsp)
sete   %bl
jmp    11a8 <console_owner+0x78>
mov    0x2e63(%rip),%rax
mov    (%rax),%rax
mov    0x2e49(%rip),%rcx
xor    %ebx,%ebx
cmp    (%rcx),%rax
je     11a8 <console_owner+0x78>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
lea    0xe6d(%rip),%rsi
lea    0x10(%rsp),%rdx
call   1050 <syslog@plt>
mov    %ebx,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [11 x i8] c"/dev/tty%d\00"
@errno = global i64 0
@ENOENT = global i64 0
@LOG_ERR = global i32 0
@.str.1 = [13 x i8] c"stat(%s): %m\00"
define i32 @console_owner(i64 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i32
%6 = alloca [256 x i8]
%7 = alloca %struct.stat
store i64 %0, i64* %4
store i32 %1, i32* %5
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%9 = load i32, i32* %5
%10 = call i32 @snprintf(i8* %8, i32 256, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%12 = call i64 @stat(i8* %11, %struct.stat* %7)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %23
14: ; preds = %2
%15 = load i64, i64* @errno
%16 = load i64, i64* @ENOENT
%17 = icmp ne i64 %15, %16
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = load i32, i32* @LOG_ERR
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%21 = call i32 @syslog(i32 %19, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %20)
br label %22
22: ; preds = %18, %14
store i32 0, i32* %3
br label %29
23: ; preds = %2
%24 = load i64, i64* %4
%25 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%26 = load i64, i64* %25
%27 = icmp eq i64 %24, %26
%28 = zext i1 %27 to i32
store i32 %28, i32* %3
br label %29
29: ; preds = %23, %22
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [11 x i8] c"/dev/tty%d\00"
@errno = global i64 0
@ENOENT = global i64 0
@LOG_ERR = global i32 0
@.str.1 = [13 x i8] c"stat(%s): %m\00"
define i32 @console_owner(i64 %0, i32 %1) {
%3 = alloca [256 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.stat* %4 to i8*
%7 = call i32 @snprintf(i8* %5, i32 256, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %1)
%8 = call i64 @stat(i8* %5, %struct.stat* %4)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %17
10: ; preds = %2
%11 = load i64, i64* @errno
%12 = load i64, i64* @ENOENT
%13 = icmp eq i64 %11, %12
br i1 %13, label %22, label %14
14: ; preds = %10
%15 = load i32, i32* @LOG_ERR
%16 = call i32 @syslog(i32 %15, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %5)
br label %22
17: ; preds = %2
%18 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%19 = load i64, i64* %18
%20 = icmp eq i64 %19, %0
%21 = zext i1 %20 to i32
br label %22
22: ; preds = %10, %14, %17
%23 = phi i32 [ %21, %17 ], [ 0, %14 ], [ 0, %10 ]
ret i32 %23
}