#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 {
  scalar_t__ st_rdev;
  int /*<<< orphan*/ st_mode;
};
struct dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int ENOTTY;
scalar_t__ S_ISCHR(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int errno;
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ stat(char *, struct stat *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, size_t);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
int __check_dir_for_tty_match(char *dirname, struct stat *st, char *buf,
                              size_t buflen) {
  DIR *fp;
  int len;
  struct stat dst;
  struct dirent *d;
  fp = opendir(dirname);
  if (fp == NULL)
    return errno;
  strncpy(buf, dirname, buflen);
  strncat(buf, "/", buflen);
  len = strlen(dirname) + 1;
  while ((d = readdir(fp)) != 0) {
    strncpy(buf + len, d->d_name, buflen);
    buf[buflen] = '\0';
    if (stat(buf, &dst) == 0 && S_ISCHR(dst.st_mode) &&
        st->st_rdev == dst.st_rdev) {
      closedir(fp);
      return 0;
    }
  }
  closedir(fp);
  return ENOTTY;
}


x86 assembly (clang, -O0):
<__check_dir_for_tty_match>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11c1 <__check_dir_for_tty_match+0x41>
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    129c <__check_dir_for_tty_match+0x11c>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    0xe1f(%rip),%rsi
call   1060 <strncat@plt>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x50(%rbp)
cmp    $0x0,%rax
je     1287 <__check_dir_for_tty_match+0x107>
mov    -0x20(%rbp),%rdi
movslq -0x34(%rbp),%rax
add    %rax,%rdi
mov    -0x50(%rbp),%rax
mov    (%rax),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jne    1282 <__check_dir_for_tty_match+0x102>
mov    -0x40(%rbp),%edi
call   10a0 <S_ISCHR@plt>
cmp    $0x0,%rax
je     1282 <__check_dir_for_tty_match+0x102>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
cmp    -0x48(%rbp),%rax
jne    1282 <__check_dir_for_tty_match+0x102>
mov    -0x30(%rbp),%rdi
call   1070 <closedir@plt>
movl   $0x0,-0x4(%rbp)
jmp    129c <__check_dir_for_tty_match+0x11c>
jmp    11f5 <__check_dir_for_tty_match+0x75>
mov    -0x30(%rbp),%rdi
call   1070 <closedir@plt>
mov    0x2d51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__check_dir_for_tty_match>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%rbp
call   1040 <opendir@plt>
test   %rax,%rax
je     1265 <__check_dir_for_tty_match+0xe5>
mov    %rax,%r12
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r14,%rdx
call   1030 <strncpy@plt>
lea    0xe40(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1060 <strncat@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
mov    %r12,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1254 <__check_dir_for_tty_match+0xd4>
movslq %ebp,%rcx
lea    (%rcx,%rbx,1),%r13
add    $0x1,%r13
lea    0x8(%rsp),%rbp
jmp    120d <__check_dir_for_tty_match+0x8d>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r12,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1254 <__check_dir_for_tty_match+0xd4>
mov    (%rax),%rsi
mov    %r13,%rdi
mov    %r14,%rdx
call   1030 <strncpy@plt>
movb   $0x0,(%rbx,%r14,1)
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1080 <stat@plt>
test   %rax,%rax
jne    1200 <__check_dir_for_tty_match+0x80>
mov    0x10(%rsp),%edi
call   10a0 <S_ISCHR@plt>
test   %rax,%rax
je     1200 <__check_dir_for_tty_match+0x80>
mov    (%r15),%rax
cmp    0x8(%rsp),%rax
jne    1200 <__check_dir_for_tty_match+0x80>
mov    %r12,%rdi
call   1070 <closedir@plt>
xor    %eax,%eax
jmp    126e <__check_dir_for_tty_match+0xee>
mov    %r12,%rdi
call   1070 <closedir@plt>
mov    0x2d85(%rip),%rax
jmp    126c <__check_dir_for_tty_match+0xec>
mov    0x2d6c(%rip),%rax
mov    (%rax),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i64, i32 }
%struct.dirent = type { i8* }
@errno = global i32 0
@.str = [2 x i8] c"/\00"
@ENOTTY = global i32 0
define i32 @__check_dir_for_tty_match(i8* %0, %struct.stat* nocapture %1, i8* %2, i64 %3) {
%5 = alloca %struct.stat
%6 = bitcast %struct.stat* %5 to i8*
%7 = tail call i32* @opendir(i8* %0)
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %4
%10 = load i32, i32* @errno
br label %48
11: ; preds = %4
%12 = tail call i32 @strncpy(i8* %2, i8* %0, i64 %3)
%13 = tail call i32 @strncat(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i64 %3)
%14 = tail call i32 @strlen(i8* %0)
%15 = call %struct.dirent* @readdir(i32* %7)
%16 = icmp eq %struct.dirent* %15, null
br i1 %16, label %45, label %17
17: ; preds = %11
%18 = add nsw i32 %14, 1
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %2, i64 %19
%21 = getelementptr inbounds i8, i8* %2, i64 %3
%22 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 1
%23 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%24 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
br label %25
25: ; preds = %17, %42
%26 = phi %struct.dirent* [ %15, %17 ], [ %43, %42 ]
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i64 0, i32 0
%28 = load i8*, i8** %27
%29 = call i32 @strncpy(i8* %20, i8* %28, i64 %3)
store i8 0, i8* %21
%30 = call i64 @stat(i8* %2, %struct.stat* %5)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %42
32: ; preds = %25
%33 = load i32, i32* %22
%34 = call i64 @S_ISCHR(i32 %33)
%35 = icmp eq i64 %34, 0
br i1 %35, label %42, label %36
36: ; preds = %32
%37 = load i64, i64* %23
%38 = load i64, i64* %24
%39 = icmp eq i64 %37, %38
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 @closedir(i32* %7)
br label %48
42: ; preds = %36, %32, %25
%43 = call %struct.dirent* @readdir(i32* %7)
%44 = icmp eq %struct.dirent* %43, null
br i1 %44, label %45, label %25
45: ; preds = %42, %11
%46 = call i32 @closedir(i32* %7)
%47 = load i32, i32* @ENOTTY
br label %48
48: ; preds = %45, %40, %9
%49 = phi i32 [ %10, %9 ], [ 0, %40 ], [ %47, %45 ]
ret i32 %49
}