#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 dummy;
};

/* Variables and functions */
int EINVAL;
int ENOTTY;
int ERANGE;
int __check_dir_for_tty_match(char *, struct stat *, char *, size_t);
int /*<<< orphan*/ __set_errno(int);
int errno;
scalar_t__ fstat(int, struct stat *);
int /*<<< orphan*/ isatty(int);
int ttyname_r(int fd, char *buf, size_t buflen) {
  int noerr;
  struct stat st;
  noerr = errno;
  if (buf == NULL) {
    noerr = EINVAL;
    goto cool_found_it;
  }
  if (buflen < 10) {
    noerr = ERANGE;
    goto cool_found_it;
  }
  if (!isatty(fd)) {
    noerr = ENOTTY;
    goto cool_found_it;
  }
  if (fstat(fd, &st) < 0)
    return errno;
  if (!isatty(fd)) {
    noerr = ENOTTY;
    goto cool_found_it;
  }
  if ((noerr = __check_dir_for_tty_match("/dev/vc", &st, buf, buflen)) == 0)
    goto cool_found_it;
  if ((noerr = __check_dir_for_tty_match("/dev/tts", &st, buf, buflen)) == 0)
    goto cool_found_it;
  if ((noerr = __check_dir_for_tty_match("/dev/pts", &st, buf, buflen)) == 0)
    goto cool_found_it;
  if ((noerr = __check_dir_for_tty_match("/dev", &st, buf, buflen)) == 0)
    goto cool_found_it;
cool_found_it:
  __set_errno(noerr);
  return noerr;
}


x86 assembly (clang, -O0):
<ttyname_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    117b <ttyname_r+0x3b>
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    12ab <ttyname_r+0x16b>
cmpq   $0xa,-0x18(%rbp)
jae    1197 <ttyname_r+0x57>
mov    0x2e53(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    12ab <ttyname_r+0x16b>
mov    -0x8(%rbp),%edi
call   1040 <isatty@plt>
cmp    $0x0,%eax
jne    11b9 <ttyname_r+0x79>
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    12ab <ttyname_r+0x16b>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1060 <fstat@plt>
cmp    $0x0,%rax
jge    11e0 <ttyname_r+0xa0>
mov    0x2dfa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12b9 <ttyname_r+0x179>
mov    -0x8(%rbp),%edi
call   1040 <isatty@plt>
cmp    $0x0,%eax
jne    1202 <ttyname_r+0xc2>
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    12ab <ttyname_r+0x16b>
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xdef(%rip),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <__check_dir_for_tty_match@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jne    122b <ttyname_r+0xeb>
jmp    12ab <ttyname_r+0x16b>
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xdce(%rip),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <__check_dir_for_tty_match@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jne    1254 <ttyname_r+0x114>
jmp    12ab <ttyname_r+0x16b>
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xdae(%rip),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <__check_dir_for_tty_match@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jne    127d <ttyname_r+0x13d>
jmp    12ab <ttyname_r+0x16b>
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xd8e(%rip),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <__check_dir_for_tty_match@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jne    12a6 <ttyname_r+0x166>
jmp    12ab <ttyname_r+0x16b>
jmp    12ab <ttyname_r+0x16b>
mov    -0x1c(%rbp),%edi
call   1030 <__set_errno@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ttyname_r>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
test   %rsi,%rsi
je     1162 <ttyname_r+0x22>
mov    %rdx,%r14
cmp    $0x9,%rdx
ja     116e <ttyname_r+0x2e>
mov    0x2e83(%rip),%rax
jmp    1215 <ttyname_r+0xd5>
mov    0x2e5f(%rip),%rax
jmp    1215 <ttyname_r+0xd5>
mov    %rsi,%rbx
mov    %edi,%ebp
call   1040 <isatty@plt>
test   %eax,%eax
je     120e <ttyname_r+0xce>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
call   1060 <fstat@plt>
test   %rax,%rax
js     1229 <ttyname_r+0xe9>
mov    %ebp,%edi
call   1040 <isatty@plt>
test   %eax,%eax
je     120e <ttyname_r+0xce>
lea    0xe59(%rip),%rdi
lea    0x8(%rsp),%rsi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1050 <__check_dir_for_tty_match@plt>
xor    %ebp,%ebp
test   %eax,%eax
je     1217 <ttyname_r+0xd7>
lea    0xe44(%rip),%rdi
lea    0x8(%rsp),%rsi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1050 <__check_dir_for_tty_match@plt>
test   %eax,%eax
je     1217 <ttyname_r+0xd7>
lea    0xe32(%rip),%rdi
lea    0x8(%rsp),%rsi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1050 <__check_dir_for_tty_match@plt>
test   %eax,%eax
je     1217 <ttyname_r+0xd7>
lea    0xe20(%rip),%rdi
lea    0x8(%rsp),%rsi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1050 <__check_dir_for_tty_match@plt>
mov    %eax,%ebp
jmp    1217 <ttyname_r+0xd7>
mov    0x2dd3(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   1030 <__set_errno@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2da0(%rip),%rax
mov    (%rax),%ebp
jmp    121e <ttyname_r+0xde>

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i32 0
@EINVAL = global i32 0
@ERANGE = global i32 0
@ENOTTY = global i32 0
@.str = [8 x i8] c"/dev/vc\00"
@.str.1 = [9 x i8] c"/dev/tts\00"
@.str.2 = [9 x i8] c"/dev/pts\00"
@.str.3 = [5 x i8] c"/dev\00"
define i32 @ttyname_r(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca %struct.stat
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i32, i32* @errno
store i32 %10, i32* %8
%11 = load i8*, i8** %6
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = load i32, i32* @EINVAL
store i32 %14, i32* %8
br label %63
15: ; preds = %3
%16 = load i64, i64* %7
%17 = icmp ult i64 %16, 10
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = load i32, i32* @ERANGE
store i32 %19, i32* %8
br label %63
20: ; preds = %15
%21 = load i32, i32* %5
%22 = call i32 @isatty(i32 %21)
%23 = icmp ne i32 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %20
%25 = load i32, i32* @ENOTTY
store i32 %25, i32* %8
br label %63
26: ; preds = %20
%27 = load i32, i32* %5
%28 = call i64 @fstat(i32 %27, %struct.stat* %9)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = load i32, i32* @errno
store i32 %31, i32* %4
br label %67
32: ; preds = %26
%33 = load i32, i32* %5
%34 = call i32 @isatty(i32 %33)
%35 = icmp ne i32 %34, 0
br i1 %35, label %38, label %36
36: ; preds = %32
%37 = load i32, i32* @ENOTTY
store i32 %37, i32* %8
br label %63
38: ; preds = %32
%39 = load i8*, i8** %6
%40 = load i64, i64* %7
%41 = call i32 @__check_dir_for_tty_match(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), %struct.stat* %9, i8* %39, i64 %40)
store i32 %41, i32* %8
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %38
br label %63
44: ; preds = %38
%45 = load i8*, i8** %6
%46 = load i64, i64* %7
%47 = call i32 @__check_dir_for_tty_match(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %9, i8* %45, i64 %46)
store i32 %47, i32* %8
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %44
br label %63
50: ; preds = %44
%51 = load i8*, i8** %6
%52 = load i64, i64* %7
%53 = call i32 @__check_dir_for_tty_match(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), %struct.stat* %9, i8* %51, i64 %52)
store i32 %53, i32* %8
%54 = icmp eq i32 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %50
br label %63
56: ; preds = %50
%57 = load i8*, i8** %6
%58 = load i64, i64* %7
%59 = call i32 @__check_dir_for_tty_match(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), %struct.stat* %9, i8* %57, i64 %58)
store i32 %59, i32* %8
%60 = icmp eq i32 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %56
br label %63
62: ; preds = %56
br label %63
63: ; preds = %62, %61, %55, %49, %43, %36, %24, %18, %13
%64 = load i32, i32* %8
%65 = call i32 @__set_errno(i32 %64)
%66 = load i32, i32* %8
store i32 %66, i32* %4
br label %67
67: ; preds = %63, %30
%68 = load i32, i32* %4
ret i32 %68
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i32 0
@EINVAL = global i32 0
@ERANGE = global i32 0
@ENOTTY = global i32 0
@.str = [8 x i8] c"/dev/vc\00"
@.str.1 = [9 x i8] c"/dev/tts\00"
@.str.2 = [9 x i8] c"/dev/pts\00"
@.str.3 = [5 x i8] c"/dev\00"
define i32 @ttyname_r(i32 %0, i8* %1, i64 %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = icmp eq i8* %1, null
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = load i32, i32* @EINVAL
br label %39
9: ; preds = %3
%10 = icmp ult i64 %2, 10
br i1 %10, label %11, label %13
11: ; preds = %9
%12 = load i32, i32* @ERANGE
br label %39
13: ; preds = %9
%14 = tail call i32 @isatty(i32 %0)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = load i32, i32* @ENOTTY
br label %39
18: ; preds = %13
%19 = call i64 @fstat(i32 %0, %struct.stat* %4)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = load i32, i32* @errno
br label %42
23: ; preds = %18
%24 = tail call i32 @isatty(i32 %0)
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = load i32, i32* @ENOTTY
br label %39
28: ; preds = %23
%29 = call i32 @__check_dir_for_tty_match(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), %struct.stat* %4, i8* %1, i64 %2)
%30 = icmp eq i32 %29, 0
br i1 %30, label %39, label %31
31: ; preds = %28
%32 = call i32 @__check_dir_for_tty_match(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %4, i8* %1, i64 %2)
%33 = icmp eq i32 %32, 0
br i1 %33, label %39, label %34
34: ; preds = %31
%35 = call i32 @__check_dir_for_tty_match(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), %struct.stat* %4, i8* %1, i64 %2)
%36 = icmp eq i32 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %34
%38 = call i32 @__check_dir_for_tty_match(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), %struct.stat* %4, i8* %1, i64 %2)
br label %39
39: ; preds = %37, %34, %31, %28, %26, %16, %11, %7
%40 = phi i32 [ %8, %7 ], [ %12, %11 ], [ 0, %28 ], [ 0, %31 ], [ 0, %34 ], [ %38, %37 ], [ %27, %26 ], [ %17, %16 ]
%41 = call i32 @__set_errno(i32 %40)
br label %42
42: ; preds = %39, %21
%43 = phi i32 [ %40, %39 ], [ %22, %21 ]
ret i32 %43
}