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

/* Variables and functions */
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ S_ISCHR(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
scalar_t__ lstat(char *, struct stat *);
int /*<<< orphan*/ sleep(int);
scalar_t__ strncmp(char *, char *, int);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
void check_ttyname(char *ttyn) {
  struct stat statbuf;
  if (lstat(ttyn, &statbuf) || !S_ISCHR(statbuf.st_mode) ||
      (statbuf.st_nlink > 1 && strncmp(ttyn, "/dev/", 5))) {
    syslog(LOG_ERR, "FATAL: bad tty");
    sleep(1);
    exit(1);
  }
}


x86 assembly (clang, -O0):
<check_ttyname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1040 <lstat@plt>
cmp    $0x0,%rax
jne    11bd <check_ttyname+0x5d>
mov    -0xc(%rbp),%edi
call   1060 <S_ISCHR@plt>
cmp    $0x0,%eax
je     11bd <check_ttyname+0x5d>
cmpl   $0x1,-0x10(%rbp)
jle    11e6 <check_ttyname+0x86>
mov    -0x8(%rbp),%rdi
lea    0xe57(%rip),%rsi
mov    $0x5,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     11e6 <check_ttyname+0x86>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
lea    0xe39(%rip),%rsi
call   1050 <syslog@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_ttyname>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1040 <lstat@plt>
test   %rax,%rax
jne    11aa <check_ttyname+0x4a>
mov    0xc(%rsp),%edi
call   1060 <S_ISCHR@plt>
test   %eax,%eax
je     11aa <check_ttyname+0x4a>
cmpl   $0x2,0x8(%rsp)
jl     11a4 <check_ttyname+0x44>
lea    0xe6e(%rip),%rsi
mov    %rbx,%rdi
mov    $0x5,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11aa <check_ttyname+0x4a>
add    $0x10,%rsp
pop    %rbx
ret
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
lea    0xe4c(%rip),%rsi
call   1050 <syslog@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@.str = [6 x i8] c"/dev/\00"
@LOG_ERR = global i32 0
@.str.1 = [15 x i8] c"FATAL: bad tty\00"
define void @check_ttyname(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.stat
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = call i64 @lstat(i8* %4, %struct.stat* %3)
%6 = icmp ne i64 %5, 0
br i1 %6, label %20, label %7
7: ; preds = %1
%8 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 1
%9 = load i32, i32* %8
%10 = call i32 @S_ISCHR(i32 %9)
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %20
12: ; preds = %7
%13 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%14 = load i32, i32* %13
%15 = icmp sgt i32 %14, 1
br i1 %15, label %16, label %25
16: ; preds = %12
%17 = load i8*, i8** %2
%18 = call i64 @strncmp(i8* %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %16, %7, %1
%21 = load i32, i32* @LOG_ERR
%22 = call i32 @syslog(i32 %21, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @sleep(i32 1)
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %16, %12
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@.str = [6 x i8] c"/dev/\00"
@LOG_ERR = global i32 0
@.str.1 = [15 x i8] c"FATAL: bad tty\00"
define void @check_ttyname(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @lstat(i8* %0, %struct.stat* %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %18
6: ; preds = %1
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%8 = load i32, i32* %7
%9 = tail call i32 @S_ISCHR(i32 %8)
%10 = icmp eq i32 %9, 0
br i1 %10, label %18, label %11
11: ; preds = %6
%12 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%13 = load i32, i32* %12
%14 = icmp sgt i32 %13, 1
br i1 %14, label %15, label %23
15: ; preds = %11
%16 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%17 = icmp eq i64 %16, 0
br i1 %17, label %23, label %18
18: ; preds = %15, %6, %1
%19 = load i32, i32* @LOG_ERR
%20 = tail call i32 @syslog(i32 %19, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%21 = tail call i32 @sleep(i32 1)
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %15, %11
ret void
}