#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 {
  int /*<<< orphan*/ d_name;
};
typedef scalar_t__ dev_t;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ S_ISCHR(int /*<<< orphan*/);
scalar_t__ chdir(char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ lstat(int /*<<< orphan*/, struct stat *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int strlen(int /*<<< orphan*/);
int findtty(char *res, int rlen, dev_t dev) {
  DIR *dir;
  struct dirent *ent;
  struct stat st;
  int r = 0;
  if (chdir("/dev") < 0 || (dir = opendir(".")) == NULL) {
    perror("bootlogd: /dev");
    return -1;
  }
  while ((ent = readdir(dir)) != NULL) {
    if (lstat(ent->d_name, &st) != 0)
      continue;
    if (!S_ISCHR(st.st_mode))
      continue;
    if (st.st_rdev == dev) {
      break;
    }
  }
  if (ent == NULL) {
    fprintf(stderr, "bootlogd: cannot find console device\n");
    r = -1;
  } else if (strlen(ent->d_name) >= rlen) {
    fprintf(stderr, "bootlogd: console device name too long\n");
    r = -1;
  } else
    strcpy(res, ent->d_name);
  closedir(dir);
  return r;
}


x86 assembly (clang, -O0):
<findtty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x44(%rbp)
lea    0xe4b(%rip),%rdi
call   1060 <chdir@plt>
cmp    $0x0,%rax
jl     11ea <findtty+0x4a>
lea    0xe29(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1202 <findtty+0x62>
lea    0xe11(%rip),%rdi
call   10c0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ee <findtty+0x14e>
jmp    1207 <findtty+0x67>
mov    -0x28(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     126a <findtty+0xca>
mov    -0x30(%rbp),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
call   1080 <lstat@plt>
cmp    $0x0,%rax
je     123c <findtty+0x9c>
jmp    1207 <findtty+0x67>
mov    -0x38(%rbp),%edi
call   10b0 <S_ISCHR@plt>
cmp    $0x0,%eax
jne    1252 <findtty+0xb2>
jmp    1207 <findtty+0x67>
mov    -0x40(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    1265 <findtty+0xc5>
jmp    126a <findtty+0xca>
jmp    1207 <findtty+0x67>
cmpq   $0x0,-0x30(%rbp)
jne    1296 <findtty+0xf6>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
lea    0xd8c(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0xffffffff,-0x44(%rbp)
jmp    12df <findtty+0x13f>
mov    -0x30(%rbp),%rax
mov    (%rax),%edi
call   1050 <strlen@plt>
cmp    -0x14(%rbp),%eax
jl     12cb <findtty+0x12b>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
lea    0xd7d(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0xffffffff,-0x44(%rbp)
jmp    12da <findtty+0x13a>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%esi
call   1030 <strcpy@plt>
jmp    12df <findtty+0x13f>
mov    -0x28(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findtty>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r12
mov    %esi,%r15d
mov    %rdi,%r14
lea    0xe4e(%rip),%rdi
call   1060 <chdir@plt>
test   %rax,%rax
js     1283 <findtty+0xe3>
lea    0xe2d(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     1283 <findtty+0xe3>
mov    %rax,%rbx
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     1250 <findtty+0xb0>
mov    %rax,%rbp
lea    0x8(%rsp),%r13
jmp    1210 <findtty+0x70>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   10a0 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1250 <findtty+0xb0>
mov    0x0(%rbp),%edi
mov    %r13,%rsi
call   1080 <lstat@plt>
test   %rax,%rax
jne    1200 <findtty+0x60>
mov    0x10(%rsp),%edi
call   10b0 <S_ISCHR@plt>
test   %eax,%eax
je     1200 <findtty+0x60>
cmp    %r12,0x8(%rsp)
jne    1200 <findtty+0x60>
mov    0x0(%rbp),%edi
call   1050 <strlen@plt>
cmp    %r15d,%eax
jge    1296 <findtty+0xf6>
mov    0x0(%rbp),%esi
mov    %r14,%rdi
call   1030 <strcpy@plt>
xor    %ebp,%ebp
jmp    126a <findtty+0xca>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
lea    0xdb1(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1070 <closedir@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd78(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%ebp
jmp    1272 <findtty+0xd2>
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
lea    0xd91(%rip),%rsi
jmp    1260 <findtty+0xc0>

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

LLVM IR (-O2):
%struct.stat = type { i64, i32 }
%struct.dirent = type { i32 }
@.str = [5 x i8] c"/dev\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [15 x i8] c"bootlogd: /dev\00"
@stderr = global i32 0
@.str.3 = [38 x i8] c"bootlogd: cannot find console device\0A\00"
@.str.4 = [40 x i8] c"bootlogd: console device name too long\0A\00"
define i32 @findtty(i8* %0, i32 %1, i64 %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = tail call i64 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%7 = icmp slt i64 %6, 0
br i1 %7, label %17, label %8
8: ; preds = %3
%9 = tail call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %17, label %11
11: ; preds = %8
%12 = call %struct.dirent* @readdir(i32* %9)
%13 = icmp eq %struct.dirent* %12, null
br i1 %13, label %35, label %14
14: ; preds = %11
%15 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%16 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
br label %19
17: ; preds = %8, %3
%18 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %52
19: ; preds = %14, %25
%20 = phi %struct.dirent* [ %12, %14 ], [ %26, %25 ]
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i64 0, i32 0
%22 = load i32, i32* %21
%23 = call i64 @lstat(i32 %22, %struct.stat* %4)
%24 = icmp eq i64 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %19, %28
%26 = call %struct.dirent* @readdir(i32* %9)
%27 = icmp eq %struct.dirent* %26, null
br i1 %27, label %35, label %19
28: ; preds = %19
%29 = load i32, i32* %15
%30 = call i32 @S_ISCHR(i32 %29)
%31 = icmp ne i32 %30, 0
%32 = load i64, i64* %16
%33 = icmp eq i64 %32, %2
%34 = select i1 %31, i1 %33, i1 false
br i1 %34, label %38, label %25
35: ; preds = %25, %11
%36 = load i32, i32* @stderr
%37 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
br label %49
38: ; preds = %28
%39 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i64 0, i32 0
%40 = load i32, i32* %39
%41 = call i32 @strlen(i32 %40)
%42 = icmp slt i32 %41, %1
br i1 %42, label %46, label %43
43: ; preds = %38
%44 = load i32, i32* @stderr
%45 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0))
br label %49
46: ; preds = %38
%47 = load i32, i32* %39
%48 = call i32 @strcpy(i8* %0, i32 %47)
br label %49
49: ; preds = %43, %46, %35
%50 = phi i32 [ -1, %35 ], [ -1, %43 ], [ 0, %46 ]
%51 = call i32 @closedir(i32* %9)
br label %52
52: ; preds = %49, %17
%53 = phi i32 [ -1, %17 ], [ %50, %49 ]
ret i32 %53
}