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

/* Variables and functions */
int /*<<< orphan*/ S_ISCHR(int /*<<< orphan*/);
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ chdir(char const *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
char *getcwd(int /*<<< orphan*/ *, int /*<<< orphan*/);
scalar_t__ lstat(char *, struct stat *);
char *malloc(size_t);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char const *, ...);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
int strlen(char const *);
int findtty(char *res, const char *startdir, size_t rlen, dev_t dev) {
  DIR *dir;
  struct dirent *ent;
  struct stat st;
  int r = -1;
  char *olddir = getcwd(NULL, 0);
  if (chdir(startdir) < 0 || (dir = opendir(".")) == NULL) {
    int msglen = strlen(startdir) + 11;
    char *msg = malloc(msglen);
    snprintf(msg, msglen, "bootlogd: %s", startdir);
    perror(msg);
    free(msg);
    chdir(olddir);
    return -1;
  }
  while ((ent = readdir(dir)) != NULL) {
    if (lstat(ent->d_name, &st) != 0)
      continue;
    if (S_ISDIR(st.st_mode) && 0 != strcmp(".", ent->d_name) &&
        0 != strcmp("..", ent->d_name)) {
      char *path = malloc(rlen);
      snprintf(path, rlen, "%s/%s", startdir, ent->d_name);
      r = findtty(res, path, rlen, dev);
      free(path);
      if (0 == r) {
        closedir(dir);
        chdir(olddir);
        return 0;
      }
      continue;
    }
    if (!S_ISCHR(st.st_mode))
      continue;
    if (st.st_rdev == dev) {
      if ((strlen(ent->d_name) + strlen(startdir) + 1) >= rlen) {
        fprintf(stderr, "bootlogd: console device name too long\n");
        closedir(dir);
        chdir(olddir);
        return -1;
      } else {
        snprintf(res, rlen, "%s/%s", startdir, ent->d_name);
        closedir(dir);
        chdir(olddir);
        return 0;
      }
    }
  }
  closedir(dir);
  chdir(olddir);
  return r;
}


x86 assembly (clang, -O0):
<findtty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0xffffffff,-0x4c(%rbp)
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
call   1090 <getcwd@plt>
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <chdir@plt>
cmp    $0x0,%eax
jl     125d <findtty+0x5d>
lea    0xdc4(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    12ba <findtty+0xba>
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
add    $0xb,%eax
mov    %eax,-0x5c(%rbp)
movslq -0x5c(%rbp),%rdi
call   10f0 <malloc@plt>
mov    %rax,-0x68(%rbp)
mov    -0x68(%rbp),%rdi
movslq -0x5c(%rbp),%rsi
mov    -0x18(%rbp),%rcx
lea    0xd74(%rip),%rdx
mov    $0x0,%al
call   1080 <snprintf@plt>
mov    -0x68(%rbp),%rdi
call   1110 <perror@plt>
mov    -0x68(%rbp),%rdi
call   1030 <free@plt>
mov    -0x58(%rbp),%rdi
call   1060 <chdir@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    14a7 <findtty+0x2a7>
jmp    12bf <findtty+0xbf>
mov    -0x30(%rbp),%rdi
call   10e0 <readdir@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     148f <findtty+0x28f>
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
lea    -0x48(%rbp),%rsi
call   10b0 <lstat@plt>
cmp    $0x0,%rax
je     12f5 <findtty+0xf5>
jmp    12bf <findtty+0xbf>
mov    -0x40(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%rax
je     13c6 <findtty+0x1c6>
mov    -0x38(%rbp),%rax
mov    (%rax),%rsi
lea    0xcf9(%rip),%rdi
call   10c0 <strcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     13c6 <findtty+0x1c6>
mov    -0x38(%rbp),%rax
mov    (%rax),%rsi
lea    0xcd7(%rip),%rdi
call   10c0 <strcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     13c6 <findtty+0x1c6>
mov    -0x20(%rbp),%rdi
call   10f0 <malloc@plt>
mov    %rax,-0x70(%rbp)
mov    -0x70(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rcx
mov    -0x38(%rbp),%rax
mov    (%rax),%r8
lea    0xca0(%rip),%rdx
mov    $0x0,%al
call   1080 <snprintf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x70(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1120 <findtty@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x70(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
cmp    -0x4c(%rbp),%eax
jne    13c1 <findtty+0x1c1>
mov    -0x30(%rbp),%rdi
call   10a0 <closedir@plt>
mov    -0x58(%rbp),%rdi
call   1060 <chdir@plt>
movl   $0x0,-0x4(%rbp)
jmp    14a7 <findtty+0x2a7>
jmp    12bf <findtty+0xbf>
mov    -0x40(%rbp),%edi
call   1100 <S_ISCHR@plt>
cmp    $0x0,%eax
jne    13dc <findtty+0x1dc>
jmp    12bf <findtty+0xbf>
mov    -0x48(%rbp),%rax
cmp    -0x28(%rbp),%rax
jne    148a <findtty+0x28a>
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x74(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x74(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
cltq
cmp    -0x20(%rbp),%rax
jb     144b <findtty+0x24b>
mov    0x2bc1(%rip),%rax
mov    (%rax),%edi
lea    0xbee(%rip),%rsi
call   10d0 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   10a0 <closedir@plt>
mov    -0x58(%rbp),%rdi
call   1060 <chdir@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    14a7 <findtty+0x2a7>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rcx
mov    -0x38(%rbp),%rax
mov    (%rax),%r8
lea    0xbab(%rip),%rdx
mov    $0x0,%al
call   1080 <snprintf@plt>
mov    -0x30(%rbp),%rdi
call   10a0 <closedir@plt>
mov    -0x58(%rbp),%rdi
call   1060 <chdir@plt>
movl   $0x0,-0x4(%rbp)
jmp    14a7 <findtty+0x2a7>
jmp    12bf <findtty+0xbf>
mov    -0x30(%rbp),%rdi
call   10a0 <closedir@plt>
mov    -0x58(%rbp),%rdi
call   1060 <chdir@plt>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findtty>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rcx,%r15
mov    %rdx,0x20(%rsp)
mov    %rsi,%rbp
mov    %rdi,%r13
xor    %edi,%edi
xor    %esi,%esi
call   1090 <getcwd@plt>
mov    %rax,%r12
mov    %rbp,%rdi
call   1060 <chdir@plt>
test   %eax,%eax
js     13da <findtty+0x1da>
lea    0xdcf(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     13da <findtty+0x1da>
mov    %rax,%rbx
mov    %rbp,0x18(%rsp)
mov    %r12,0x8(%rsp)
mov    $0xffffffff,%ebp
lea    0x28(%rsp),%r14
mov    %r13,0x10(%rsp)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   10e0 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     1390 <findtty+0x190>
mov    0x0(%r13),%rdi
mov    %r14,%rsi
call   10b0 <lstat@plt>
test   %rax,%rax
jne    1270 <findtty+0x70>
mov    0x30(%rsp),%r12d
mov    %r12d,%edi
call   1070 <S_ISDIR@plt>
test   %rax,%rax
je     12d1 <findtty+0xd1>
mov    0x0(%r13),%rsi
lea    0xd5c(%rip),%rdi
call   10c0 <strcmp@plt>
test   %rax,%rax
je     12d1 <findtty+0xd1>
mov    0x0(%r13),%rsi
lea    0xd46(%rip),%rdi
call   10c0 <strcmp@plt>
test   %rax,%rax
jne    12e6 <findtty+0xe6>
mov    %r12d,%edi
call   1100 <S_ISCHR@plt>
test   %eax,%eax
je     1270 <findtty+0x70>
cmp    %r15,0x28(%rsp)
jne    1270 <findtty+0x70>
jmp    1349 <findtty+0x149>
mov    0x20(%rsp),%r12
mov    %r12,%rdi
call   10f0 <malloc@plt>
mov    %rax,%rbp
mov    0x0(%r13),%r8
mov    %rax,%rdi
mov    %r12,%rsi
lea    0xd09(%rip),%rdx
mov    0x18(%rsp),%rcx
xor    %eax,%eax
call   1080 <snprintf@plt>
mov    0x10(%rsp),%rdi
mov    %rbp,%rsi
mov    %r12,%rdx
mov    %r15,%rcx
call   1120 <findtty@plt>
mov    %eax,%r12d
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r12d,%ebp
test   %r12d,%r12d
jne    1270 <findtty+0x70>
mov    %rbx,%rdi
call   10a0 <closedir@plt>
xor    %ebp,%ebp
jmp    1398 <findtty+0x198>
mov    0x0(%r13),%rdi
call   1050 <strlen@plt>
mov    %eax,%r15d
mov    0x18(%rsp),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
add    %r15d,%eax
add    $0x1,%eax
cltq
mov    0x20(%rsp),%rsi
cmp    %rsi,%rax
jae    13b6 <findtty+0x1b6>
mov    0x0(%r13),%r8
lea    0xc91(%rip),%rdx
xor    %ebp,%ebp
mov    0x10(%rsp),%rdi
mov    %r14,%rcx
xor    %eax,%eax
call   1080 <snprintf@plt>
mov    %rbx,%rdi
call   10a0 <closedir@plt>
mov    0x8(%rsp),%r12
mov    %r12,%rdi
call   1060 <chdir@plt>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c23(%rip),%rax
mov    (%rax),%edi
lea    0xc50(%rip),%rsi
call   10d0 <fprintf@plt>
mov    %rbx,%rdi
call   10a0 <closedir@plt>
mov    $0xffffffff,%ebp
jmp    1398 <findtty+0x198>
mov    %rbp,%rdi
call   1050 <strlen@plt>
add    $0xb,%eax
movslq %eax,%r14
mov    %r14,%rdi
call   10f0 <malloc@plt>
mov    %rax,%rbx
lea    0xc06(%rip),%rdx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbp,%rcx
xor    %eax,%eax
call   1080 <snprintf@plt>
mov    %rbx,%rdi
call   1110 <perror@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%ebp
jmp    139d <findtty+0x19d>

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i64, i32 }
@.str = [2 x i8] c".\00"
@.str.1 = [13 x i8] c"bootlogd: %s\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [6 x i8] c"%s/%s\00"
@stderr = global i32 0
@.str.4 = [40 x i8] c"bootlogd: console device name too long\0A\00"
define i32 @findtty(i8* %0, i8* %1, i64 %2, i64 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32*
%11 = alloca %struct.dirent*
%12 = alloca %struct.stat
%13 = alloca i32
%14 = alloca i8*
%15 = alloca i32
%16 = alloca i8*
%17 = alloca i8*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i64 %3, i64* %9
store i32 -1, i32* %13
%18 = call i8* @getcwd(i32* null, i32 0)
store i8* %18, i8** %14
%19 = load i8*, i8** %7
%20 = call i32 @chdir(i8* %19)
%21 = icmp slt i32 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %4
%23 = call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %23, i32** %10
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %43
25: ; preds = %22, %4
%26 = load i8*, i8** %7
%27 = call i32 @strlen(i8* %26)
%28 = add nsw i32 %27, 11
store i32 %28, i32* %15
%29 = load i32, i32* %15
%30 = sext i32 %29 to i64
%31 = call i8* @malloc(i64 %30)
store i8* %31, i8** %16
%32 = load i8*, i8** %16
%33 = load i32, i32* %15
%34 = sext i32 %33 to i64
%35 = load i8*, i8** %7
%36 = call i32 (i8*, i64, i8*, i8*, ...) @snprintf(i8* %32, i64 %34, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %35)
%37 = load i8*, i8** %16
%38 = call i32 @perror(i8* %37)
%39 = load i8*, i8** %16
%40 = call i32 @free(i8* %39)
%41 = load i8*, i8** %14
%42 = call i32 @chdir(i8* %41)
store i32 -1, i32* %5
br label %146
43: ; preds = %22
br label %44
44: ; preds = %139, %102, %96, %54, %43
%45 = load i32*, i32** %10
%46 = call %struct.dirent* @readdir(i32* %45)
store %struct.dirent* %46, %struct.dirent** %11
%47 = icmp ne %struct.dirent* %46, null
br i1 %47, label %48, label %140
48: ; preds = %44
%49 = load %struct.dirent*, %struct.dirent** %11
%50 = getelementptr inbounds %struct.dirent, %struct.dirent* %49, i32 0, i32 0
%51 = load i8*, i8** %50
%52 = call i64 @lstat(i8* %51, %struct.stat* %12)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %48
br label %44
55: ; preds = %48
%56 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 1
%57 = load i32, i32* %56
%58 = call i64 @S_ISDIR(i32 %57)
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %97
60: ; preds = %55
%61 = load %struct.dirent*, %struct.dirent** %11
%62 = getelementptr inbounds %struct.dirent, %struct.dirent* %61, i32 0, i32 0
%63 = load i8*, i8** %62
%64 = call i64 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %63)
%65 = icmp ne i64 0, %64
br i1 %65, label %66, label %97
66: ; preds = %60
%67 = load %struct.dirent*, %struct.dirent** %11
%68 = getelementptr inbounds %struct.dirent, %struct.dirent* %67, i32 0, i32 0
%69 = load i8*, i8** %68
%70 = call i64 @strcmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %69)
%71 = icmp ne i64 0, %70
br i1 %71, label %72, label %97
72: ; preds = %66
%73 = load i64, i64* %8
%74 = call i8* @malloc(i64 %73)
store i8* %74, i8** %17
%75 = load i8*, i8** %17
%76 = load i64, i64* %8
%77 = load i8*, i8** %7
%78 = load %struct.dirent*, %struct.dirent** %11
%79 = getelementptr inbounds %struct.dirent, %struct.dirent* %78, i32 0, i32 0
%80 = load i8*, i8** %79
%81 = call i32 (i8*, i64, i8*, i8*, ...) @snprintf(i8* %75, i64 %76, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %77, i8* %80)
%82 = load i8*, i8** %6
%83 = load i8*, i8** %17
%84 = load i64, i64* %8
%85 = load i64, i64* %9
%86 = call i32 @findtty(i8* %82, i8* %83, i64 %84, i64 %85)
store i32 %86, i32* %13
%87 = load i8*, i8** %17
%88 = call i32 @free(i8* %87)
%89 = load i32, i32* %13
%90 = icmp eq i32 0, %89
br i1 %90, label %91, label %96
91: ; preds = %72
%92 = load i32*, i32** %10
%93 = call i32 @closedir(i32* %92)
%94 = load i8*, i8** %14
%95 = call i32 @chdir(i8* %94)
store i32 0, i32* %5
br label %146
96: ; preds = %72
br label %44
97: ; preds = %66, %60, %55
%98 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 1
%99 = load i32, i32* %98
%100 = call i32 @S_ISCHR(i32 %99)
%101 = icmp ne i32 %100, 0
br i1 %101, label %103, label %102
102: ; preds = %97
br label %44
103: ; preds = %97
%104 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%105 = load i64, i64* %104
%106 = load i64, i64* %9
%107 = icmp eq i64 %105, %106
br i1 %107, label %108, label %139
108: ; preds = %103
%109 = load %struct.dirent*, %struct.dirent** %11
%110 = getelementptr inbounds %struct.dirent, %struct.dirent* %109, i32 0, i32 0
%111 = load i8*, i8** %110
%112 = call i32 @strlen(i8* %111)
%113 = load i8*, i8** %7
%114 = call i32 @strlen(i8* %113)
%115 = add nsw i32 %112, %114
%116 = add nsw i32 %115, 1
%117 = sext i32 %116 to i64
%118 = load i64, i64* %8
%119 = icmp uge i64 %117, %118
br i1 %119, label %120, label %127
120: ; preds = %108
%121 = load i32, i32* @stderr
%122 = call i32 @fprintf(i32 %121, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0))
%123 = load i32*, i32** %10
%124 = call i32 @closedir(i32* %123)
%125 = load i8*, i8** %14
%126 = call i32 @chdir(i8* %125)
store i32 -1, i32* %5
br label %146
127: ; preds = %108
%128 = load i8*, i8** %6
%129 = load i64, i64* %8
%130 = load i8*, i8** %7
%131 = load %struct.dirent*, %struct.dirent** %11
%132 = getelementptr inbounds %struct.dirent, %struct.dirent* %131, i32 0, i32 0
%133 = load i8*, i8** %132
%134 = call i32 (i8*, i64, i8*, i8*, ...) @snprintf(i8* %128, i64 %129, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %130, i8* %133)
%135 = load i32*, i32** %10
%136 = call i32 @closedir(i32* %135)
%137 = load i8*, i8** %14
%138 = call i32 @chdir(i8* %137)
store i32 0, i32* %5
br label %146
139: ; preds = %103
br label %44
140: ; preds = %44
%141 = load i32*, i32** %10
%142 = call i32 @closedir(i32* %141)
%143 = load i8*, i8** %14
%144 = call i32 @chdir(i8* %143)
%145 = load i32, i32* %13
store i32 %145, i32* %5
br label %146
146: ; preds = %140, %127, %120, %91, %25
%147 = load i32, i32* %5
ret i32 %147
}

LLVM IR (-O2):
%struct.stat = type { i64, i32 }
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [13 x i8] c"bootlogd: %s\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [6 x i8] c"%s/%s\00"
@stderr = global i32 0
@.str.4 = [40 x i8] c"bootlogd: console device name too long\0A\00"
define i32 @findtty(i8* %0, i8* %1, i64 %2, i64 %3) {
%5 = alloca %struct.stat
%6 = bitcast %struct.stat* %5 to i8*
%7 = tail call i8* @getcwd(i32* null, i32 0)
%8 = tail call i32 @chdir(i8* %1)
%9 = icmp slt i32 %8, 0
br i1 %9, label %16, label %10
10: ; preds = %4
%11 = tail call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 1
%15 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
br label %52
16: ; preds = %10, %4
%17 = tail call i32 @strlen(i8* %1)
%18 = add nsw i32 %17, 11
%19 = sext i32 %18 to i64
%20 = tail call i8* @malloc(i64 %19)
%21 = tail call i32 (i8*, i64, i8*, i8*, ...) @snprintf(i8* %20, i64 %19, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%22 = tail call i32 @perror(i8* %20)
%23 = tail call i32 @free(i8* %20)
br label %82
24: ; preds = %52, %30
%25 = phi %struct.dirent* [ %31, %30 ], [ %54, %52 ]
%26 = getelementptr inbounds %struct.dirent, %struct.dirent* %25, i64 0, i32 0
%27 = load i8*, i8** %26
%28 = call i64 @lstat(i8* %27, %struct.stat* %5)
%29 = icmp eq i64 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %24, %58
%31 = tail call %struct.dirent* @readdir(i32* %11)
%32 = icmp eq %struct.dirent* %31, null
br i1 %32, label %80, label %24
33: ; preds = %24
%34 = load i32, i32* %14
%35 = tail call i64 @S_ISDIR(i32 %34)
%36 = icmp eq i64 %35, 0
br i1 %36, label %58, label %37
37: ; preds = %33
%38 = load i8*, i8** %26
%39 = tail call i64 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %38)
%40 = icmp eq i64 %39, 0
br i1 %40, label %58, label %41
41: ; preds = %37
%42 = load i8*, i8** %26
%43 = tail call i64 @strcmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %42)
%44 = icmp eq i64 %43, 0
br i1 %44, label %58, label %45
45: ; preds = %41
%46 = tail call i8* @malloc(i64 %2)
%47 = load i8*, i8** %26
%48 = tail call i32 (i8*, i64, i8*, i8*, ...) @snprintf(i8* %46, i64 %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %1, i8* %47)
%49 = tail call i32 @findtty(i8* %0, i8* %46, i64 %2, i64 %3)
%50 = tail call i32 @free(i8* %46)
%51 = icmp eq i32 %49, 0
br i1 %51, label %56, label %52
52: ; preds = %13, %45
%53 = phi i32 [ -1, %13 ], [ %49, %45 ]
%54 = tail call %struct.dirent* @readdir(i32* %11)
%55 = icmp eq %struct.dirent* %54, null
br i1 %55, label %80, label %24
56: ; preds = %45
%57 = tail call i32 @closedir(i32* %11)
br label %82
58: ; preds = %41, %37, %33
%59 = tail call i32 @S_ISCHR(i32 %34)
%60 = icmp ne i32 %59, 0
%61 = load i64, i64* %15
%62 = icmp eq i64 %61, %3
%63 = select i1 %60, i1 %62, i1 false
br i1 %63, label %64, label %30
64: ; preds = %58
%65 = load i8*, i8** %26
%66 = tail call i32 @strlen(i8* %65)
%67 = tail call i32 @strlen(i8* %1)
%68 = add i32 %66, 1
%69 = add i32 %68, %67
%70 = sext i32 %69 to i64
%71 = icmp ult i64 %70, %2
br i1 %71, label %76, label %72
72: ; preds = %64
%73 = load i32, i32* @stderr
%74 = tail call i32 @fprintf(i32 %73, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0))
%75 = tail call i32 @closedir(i32* %11)
br label %82
76: ; preds = %64
%77 = load i8*, i8** %26
%78 = tail call i32 (i8*, i64, i8*, i8*, ...) @snprintf(i8* %0, i64 %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %1, i8* %77)
%79 = tail call i32 @closedir(i32* %11)
br label %82
80: ; preds = %52, %30
%81 = tail call i32 @closedir(i32* %11)
br label %82
82: ; preds = %56, %80, %76, %72, %16
%83 = phi i32 [ -1, %16 ], [ 0, %56 ], [ -1, %72 ], [ 0, %76 ], [ %53, %80 ]
%84 = tail call i32 @chdir(i8* %7)
ret i32 %83
}