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

/* Variables and functions */
int O_NONBLOCK;
int O_RDONLY;
int PATH_MAX;
int /*<<< orphan*/ close(int);
scalar_t__ closedir(int /*<<< orphan*/ *);
scalar_t__ isatty(int);
scalar_t__ major(int /*<<< orphan*/);
scalar_t__ minor(int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, size_t, char *, int);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strcmp(char *, char *);
int strlcat(char *, char *, int);
int strlcpy(char *, char *, size_t);
int /*<<< orphan*/ weprintf(char *, ...);
int ttytostr(int tty_maj, int tty_min, char *str, size_t n) {
  struct stat sb;
  struct dirent *dp;
  DIR *dirp;
  char path[PATH_MAX];
  int fd;
  int r = 0;
  switch (tty_maj) {
  case 136:
    snprintf(str, n, "pts/%d", tty_min);
    return 0;
  case 4:
    snprintf(str, n, "tty%d", tty_min);
    return 0;
  default:
    str[0] = '?';
    str[1] = '\0';
    break;
  }
  dirp = opendir("/dev");
  if (!dirp) {
    weprintf("opendir /dev:");
    return -1;
  }
  while ((dp = readdir(dirp))) {
    if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
      continue;
    if (strlcpy(path, "/dev/", sizeof(path)) >= sizeof(path)) {
      weprintf("path too long\n");
      r = -1;
      goto err0;
    }
    if (strlcat(path, dp->d_name, sizeof(path)) >= sizeof(path)) {
      weprintf("path too long\n");
      r = -1;
      goto err0;
    }
    if (stat(path, &sb) < 0) {
      weprintf("stat %s:", path);
      r = -1;
      goto err0;
    }
    if ((int)major(sb.st_rdev) == tty_maj &&
        (int)minor(sb.st_rdev) == tty_min) {
      fd = open(path, O_RDONLY | O_NONBLOCK);
      if (fd < 0)
        continue;
      if (isatty(fd)) {
        strlcpy(str, dp->d_name, n);
        close(fd);
        break;
      } else {
        close(fd);
        r = -1;
        goto err0;
      }
    }
  }
err0:
  if (closedir(dirp) < 0) {
    weprintf("closedir /dev:");
    r = -1;
  }
  return r;
}


x86 assembly (clang, -O0):
<ttytostr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x68(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x48(%rbp)
movl   $0x0,-0x50(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x58(%rbp)
sub    $0x4,%eax
je     127e <ttytostr+0x9e>
jmp    1241 <ttytostr+0x61>
mov    -0x58(%rbp),%eax
sub    $0x88,%eax
jne    12a8 <ttytostr+0xc8>
jmp    1254 <ttytostr+0x74>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0xc(%rbp),%ecx
lea    0xd9a(%rip),%rdx
call   1070 <snprintf@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    14d2 <ttytostr+0x2f2>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0xc(%rbp),%ecx
lea    0xd77(%rip),%rdx
call   1070 <snprintf@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    14d2 <ttytostr+0x2f2>
mov    -0x18(%rbp),%rax
movb   $0x3f,(%rax)
mov    -0x18(%rbp),%rax
movb   $0x0,0x1(%rax)
lea    0xd4f(%rip),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12f3 <ttytostr+0x113>
lea    0xd39(%rip),%rdi
mov    $0x0,%al
call   10e0 <weprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    14d2 <ttytostr+0x2f2>
jmp    12f8 <ttytostr+0x118>
mov    -0x38(%rbp),%rdi
call   10d0 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1498 <ttytostr+0x2b8>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    0xd04(%rip),%rsi
call   10b0 <strcmp@plt>
cmp    $0x0,%eax
je     1347 <ttytostr+0x167>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    0xce7(%rip),%rsi
call   10b0 <strcmp@plt>
cmp    $0x0,%eax
jne    134c <ttytostr+0x16c>
jmp    12f8 <ttytostr+0x118>
mov    -0x68(%rbp),%rdx
mov    -0x60(%rbp),%rdi
lea    0xcc8(%rip),%rsi
call   10f0 <strlcpy@plt>
mov    -0x68(%rbp),%rcx
cltq
cmp    %rcx,%rax
jb     1389 <ttytostr+0x1a9>
lea    0xcb3(%rip),%rdi
mov    $0x0,%al
call   10e0 <weprintf@plt>
movl   $0xffffffff,-0x50(%rbp)
jmp    149d <ttytostr+0x2bd>
mov    -0x68(%rbp),%rax
mov    -0x60(%rbp),%rdi
mov    -0x30(%rbp),%rcx
mov    (%rcx),%rsi
mov    %eax,%edx
call   1040 <strlcat@plt>
mov    -0x68(%rbp),%rcx
cltq
cmp    %rcx,%rax
jb     13c8 <ttytostr+0x1e8>
lea    0xc74(%rip),%rdi
mov    $0x0,%al
call   10e0 <weprintf@plt>
movl   $0xffffffff,-0x50(%rbp)
jmp    149d <ttytostr+0x2bd>
mov    -0x60(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   10c0 <stat@plt>
cmp    $0x0,%rax
jge    13fd <ttytostr+0x21d>
mov    -0x60(%rbp),%rsi
lea    0xc4e(%rip),%rdi
mov    $0x0,%al
call   10e0 <weprintf@plt>
movl   $0xffffffff,-0x50(%rbp)
jmp    149d <ttytostr+0x2bd>
mov    -0x28(%rbp),%edi
call   1080 <major@plt>
cmp    -0x8(%rbp),%eax
jne    1493 <ttytostr+0x2b3>
mov    -0x28(%rbp),%edi
call   1060 <minor@plt>
cmp    -0xc(%rbp),%eax
jne    1493 <ttytostr+0x2b3>
mov    -0x60(%rbp),%rdi
mov    0x2bae(%rip),%rax
mov    (%rax),%esi
mov    0x2bad(%rip),%rax
or     (%rax),%esi
call   1100 <open@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jge    144c <ttytostr+0x26c>
jmp    12f8 <ttytostr+0x118>
mov    -0x4c(%rbp),%edi
call   1030 <isatty@plt>
cmp    $0x0,%rax
je     147f <ttytostr+0x29f>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
mov    -0x20(%rbp),%rdx
call   10f0 <strlcpy@plt>
mov    -0x4c(%rbp),%edi
call   1090 <close@plt>
jmp    1498 <ttytostr+0x2b8>
mov    -0x4c(%rbp),%edi
call   1090 <close@plt>
movl   $0xffffffff,-0x50(%rbp)
jmp    149d <ttytostr+0x2bd>
jmp    12f8 <ttytostr+0x118>
jmp    149d <ttytostr+0x2bd>
mov    -0x38(%rbp),%rdi
call   10a0 <closedir@plt>
cmp    $0x0,%rax
jge    14c5 <ttytostr+0x2e5>
lea    0xb8a(%rip),%rdi
mov    $0x0,%al
call   10e0 <weprintf@plt>
movl   $0xffffffff,-0x50(%rbp)
mov    -0x50(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ttytostr>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rcx,%r14
mov    %rdx,%rcx
mov    %esi,%ebx
mov    %rsp,%r15
mov    0x2ded(%rip),%rax
mov    (%rax),%r13d
mov    %rsp,%r12
lea    0xf(%r13),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
cmp    $0x4,%edi
je     122d <ttytostr+0x4d>
cmp    $0x88,%edi
jne    1248 <ttytostr+0x68>
lea    0xdd5(%rip),%rdx
jmp    1234 <ttytostr+0x54>
lea    0xdd3(%rip),%rdx
mov    %rcx,%rdi
mov    %r14,%rsi
mov    %ebx,%ecx
call   1070 <snprintf@plt>
xor    %ebx,%ebx
jmp    13f2 <ttytostr+0x212>
mov    %edi,-0x48(%rbp)
mov    %ebx,-0x44(%rbp)
mov    %rcx,-0x58(%rbp)
movw   $0x3f,(%rcx)
lea    0xdaf(%rip),%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     139e <ttytostr+0x1be>
mov    %r14,-0x50(%rbp)
mov    %r15,-0x38(%rbp)
mov    %rax,-0x30(%rbp)
mov    %rax,%rdi
call   10d0 <readdir@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     13cd <ttytostr+0x1ed>
mov    %rax,%r15
lea    0xd8c(%rip),%r14
jmp    12b5 <ttytostr+0xd5>
nopw   0x0(%rax,%rax,1)
mov    -0x30(%rbp),%rdi
call   10d0 <readdir@plt>
mov    %rax,%r15
test   %rax,%rax
je     1392 <ttytostr+0x1b2>
mov    (%r15),%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   10b0 <strcmp@plt>
test   %eax,%eax
je     12a0 <ttytostr+0xc0>
mov    %rbx,%rdi
lea    0xd4f(%rip),%rsi
call   10b0 <strcmp@plt>
test   %eax,%eax
je     12a0 <ttytostr+0xc0>
mov    %r12,%rdi
lea    0xd3f(%rip),%rsi
mov    %r13,%rdx
call   10f0 <strlcpy@plt>
cltq
cmp    %r13,%rax
jae    13a7 <ttytostr+0x1c7>
mov    (%r15),%rsi
mov    %r12,%rdi
mov    %r13d,%edx
call   1040 <strlcat@plt>
cltq
cmp    %r13,%rax
jae    13a7 <ttytostr+0x1c7>
mov    %r12,%rdi
lea    -0x40(%rbp),%rsi
call   10c0 <stat@plt>
test   %rax,%rax
js     13b7 <ttytostr+0x1d7>
mov    -0x40(%rbp),%edi
call   1080 <major@plt>
cmp    -0x48(%rbp),%eax
jne    12a0 <ttytostr+0xc0>
mov    -0x40(%rbp),%edi
call   1060 <minor@plt>
cmp    -0x44(%rbp),%eax
jne    12a0 <ttytostr+0xc0>
mov    0x2c92(%rip),%rax
mov    (%rax),%esi
mov    0x2c81(%rip),%rax
or     (%rax),%esi
mov    %r12,%rdi
call   1100 <open@plt>
test   %eax,%eax
js     12a0 <ttytostr+0xc0>
mov    %eax,%ebx
mov    %eax,%edi
call   1030 <isatty@plt>
test   %rax,%rax
je     1406 <ttytostr+0x226>
mov    (%r15),%rsi
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rdx
call   10f0 <strlcpy@plt>
mov    %ebx,%edi
call   1090 <close@plt>
mov    -0x38(%rbp),%r15
mov    -0x30(%rbp),%rdi
xor    %ebx,%ebx
jmp    13d5 <ttytostr+0x1f5>
lea    0xc6d(%rip),%rdi
jmp    13e6 <ttytostr+0x206>
lea    0xc7b(%rip),%rdi
xor    %eax,%eax
call   10e0 <weprintf@plt>
jmp    13c8 <ttytostr+0x1e8>
lea    0xc7a(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   10e0 <weprintf@plt>
mov    $0xffffffff,%ebx
mov    -0x38(%rbp),%r15
mov    -0x30(%rbp),%rdi
call   10a0 <closedir@plt>
test   %rax,%rax
jns    13f2 <ttytostr+0x212>
lea    0xc5b(%rip),%rdi
xor    %eax,%eax
call   10e0 <weprintf@plt>
mov    $0xffffffff,%ebx
mov    %r15,%rsp
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebx,%edi
call   1090 <close@plt>
jmp    13c8 <ttytostr+0x1e8>

LLVM IR (-O0):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [7 x i8] c"pts/%d\00"
@.str.1 = [6 x i8] c"tty%d\00"
@.str.2 = [5 x i8] c"/dev\00"
@.str.3 = [14 x i8] c"opendir /dev:\00"
@.str.4 = [2 x i8] c".\00"
@.str.5 = [3 x i8] c"..\00"
@.str.6 = [6 x i8] c"/dev/\00"
@.str.7 = [15 x i8] c"path too long\0A\00"
@.str.8 = [9 x i8] c"stat %s:\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.9 = [15 x i8] c"closedir /dev:\00"
define i32 @ttytostr(i32 %0, i32 %1, i8* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca %struct.stat
%11 = alloca %struct.dirent*
%12 = alloca i32*
%13 = alloca i8*
%14 = alloca i64
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i64 %3, i64* %9
%18 = load i32, i32* @PATH_MAX
%19 = zext i32 %18 to i64
%20 = call i8* @llvm.stacksave()
store i8* %20, i8** %13
%21 = alloca i8, i64 %19
store i64 %19, i64* %14
store i32 0, i32* %16
%22 = load i32, i32* %6
switch i32 %22, label %33 [
i32 136, label %23
i32 4, label %28
]
23: ; preds = %4
%24 = load i8*, i8** %8
%25 = load i64, i64* %9
%26 = load i32, i32* %7
%27 = call i32 @snprintf(i8* %24, i64 %25, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %26)
store i32 0, i32* %5
store i32 1, i32* %17
br label %131
28: ; preds = %4
%29 = load i8*, i8** %8
%30 = load i64, i64* %9
%31 = load i32, i32* %7
%32 = call i32 @snprintf(i8* %29, i64 %30, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %31)
store i32 0, i32* %5
store i32 1, i32* %17
br label %131
33: ; preds = %4
%34 = load i8*, i8** %8
%35 = getelementptr inbounds i8, i8* %34, i64 0
store i8 63, i8* %35
%36 = load i8*, i8** %8
%37 = getelementptr inbounds i8, i8* %36, i64 1
store i8 0, i8* %37
br label %38
38: ; preds = %33
%39 = call i32* @opendir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32* %39, i32** %12
%40 = load i32*, i32** %12
%41 = icmp ne i32* %40, null
br i1 %41, label %44, label %42
42: ; preds = %38
%43 = call i32 (i8*, ...) @weprintf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %5
store i32 1, i32* %17
br label %131
44: ; preds = %38
br label %45
45: ; preds = %121, %104, %61, %44
%46 = load i32*, i32** %12
%47 = call %struct.dirent* @readdir(i32* %46)
store %struct.dirent* %47, %struct.dirent** %11
%48 = icmp ne %struct.dirent* %47, null
br i1 %48, label %49, label %122
49: ; preds = %45
%50 = load %struct.dirent*, %struct.dirent** %11
%51 = getelementptr inbounds %struct.dirent, %struct.dirent* %50, i32 0, i32 0
%52 = load i8*, i8** %51
%53 = call i32 @strcmp(i8* %52, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %61
55: ; preds = %49
%56 = load %struct.dirent*, %struct.dirent** %11
%57 = getelementptr inbounds %struct.dirent, %struct.dirent* %56, i32 0, i32 0
%58 = load i8*, i8** %57
%59 = call i32 @strcmp(i8* %58, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%60 = icmp ne i32 %59, 0
br i1 %60, label %62, label %61
61: ; preds = %55, %49
br label %45
62: ; preds = %55
%63 = call i32 @strlcpy(i8* %21, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i64 %19)
%64 = sext i32 %63 to i64
%65 = icmp uge i64 %64, %19
br i1 %65, label %66, label %68
66: ; preds = %62
%67 = call i32 (i8*, ...) @weprintf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0))
store i32 -1, i32* %16
br label %123
68: ; preds = %62
%69 = load %struct.dirent*, %struct.dirent** %11
%70 = getelementptr inbounds %struct.dirent, %struct.dirent* %69, i32 0, i32 0
%71 = load i8*, i8** %70
%72 = trunc i64 %19 to i32
%73 = call i32 @strlcat(i8* %21, i8* %71, i32 %72)
%74 = sext i32 %73 to i64
%75 = icmp uge i64 %74, %19
br i1 %75, label %76, label %78
76: ; preds = %68
%77 = call i32 (i8*, ...) @weprintf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0))
store i32 -1, i32* %16
br label %123
78: ; preds = %68
%79 = call i64 @stat(i8* %21, %struct.stat* %10)
%80 = icmp slt i64 %79, 0
br i1 %80, label %81, label %83
81: ; preds = %78
%82 = call i32 (i8*, ...) @weprintf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i64 0, i64 0), i8* %21)
store i32 -1, i32* %16
br label %123
83: ; preds = %78
%84 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%85 = load i32, i32* %84
%86 = call i64 @major(i32 %85)
%87 = trunc i64 %86 to i32
%88 = load i32, i32* %6
%89 = icmp eq i32 %87, %88
br i1 %89, label %90, label %121
90: ; preds = %83
%91 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%92 = load i32, i32* %91
%93 = call i64 @minor(i32 %92)
%94 = trunc i64 %93 to i32
%95 = load i32, i32* %7
%96 = icmp eq i32 %94, %95
br i1 %96, label %97, label %121
97: ; preds = %90
%98 = load i32, i32* @O_RDONLY
%99 = load i32, i32* @O_NONBLOCK
%100 = or i32 %98, %99
%101 = call i32 @open(i8* %21, i32 %100)
store i32 %101, i32* %15
%102 = load i32, i32* %15
%103 = icmp slt i32 %102, 0
br i1 %103, label %104, label %105
104: ; preds = %97
br label %45
105: ; preds = %97
%106 = load i32, i32* %15
%107 = call i64 @isatty(i32 %106)
%108 = icmp ne i64 %107, 0
br i1 %108, label %109, label %118
109: ; preds = %105
%110 = load i8*, i8** %8
%111 = load %struct.dirent*, %struct.dirent** %11
%112 = getelementptr inbounds %struct.dirent, %struct.dirent* %111, i32 0, i32 0
%113 = load i8*, i8** %112
%114 = load i64, i64* %9
%115 = call i32 @strlcpy(i8* %110, i8* %113, i64 %114)
%116 = load i32, i32* %15
%117 = call i32 @close(i32 %116)
br label %122
118: ; preds = %105
%119 = load i32, i32* %15
%120 = call i32 @close(i32 %119)
store i32 -1, i32* %16
br label %123
121: ; preds = %90, %83
br label %45
122: ; preds = %109, %45
br label %123
123: ; preds = %122, %118, %81, %76, %66
%124 = load i32*, i32** %12
%125 = call i64 @closedir(i32* %124)
%126 = icmp slt i64 %125, 0
br i1 %126, label %127, label %129
127: ; preds = %123
%128 = call i32 (i8*, ...) @weprintf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.9, i64 0, i64 0))
store i32 -1, i32* %16
br label %129
129: ; preds = %127, %123
%130 = load i32, i32* %16
store i32 %130, i32* %5
store i32 1, i32* %17
br label %131
131: ; preds = %129, %42, %28, %23
%132 = load i8*, i8** %13
%133 = load i32, i32* %5
ret i32 %133
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@.str = [7 x i8] c"pts/%d\00"
@.str.1 = [6 x i8] c"tty%d\00"
@.str.2 = [5 x i8] c"/dev\00"
@.str.3 = [14 x i8] c"opendir /dev:\00"
@.str.4 = [2 x i8] c".\00"
@.str.5 = [3 x i8] c"..\00"
@.str.6 = [6 x i8] c"/dev/\00"
@.str.7 = [15 x i8] c"path too long\0A\00"
@.str.8 = [9 x i8] c"stat %s:\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.9 = [15 x i8] c"closedir /dev:\00"
define i32 @ttytostr(i32 %0, i32 %1, i8* %2, i64 %3) {
%5 = alloca %struct.stat
%6 = bitcast %struct.stat* %5 to i8*
%7 = load i32, i32* @PATH_MAX
%8 = zext i32 %7 to i64
%9 = tail call i8* @llvm.stacksave()
%10 = alloca i8, i64 %8
switch i32 %0, label %15 [
i32 136, label %11
i32 4, label %13
]
11: ; preds = %4
%12 = tail call i32 @snprintf(i8* %2, i64 %3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %1)
br label %87
13: ; preds = %4
%14 = tail call i32 @snprintf(i8* %2, i64 %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %1)
br label %87
15: ; preds = %4
store i8 63, i8* %2
%16 = getelementptr inbounds i8, i8* %2, i64 1
store i8 0, i8* %16
%17 = tail call i32* @opendir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %24, label %19
19: ; preds = %15
%20 = call %struct.dirent* @readdir(i32* %17)
%21 = icmp eq %struct.dirent* %20, null
br i1 %21, label %81, label %22
22: ; preds = %19
%23 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
br label %26
24: ; preds = %15
%25 = tail call i32 (i8*, ...) @weprintf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %87
26: ; preds = %22, %35
%27 = phi %struct.dirent* [ %20, %22 ], [ %36, %35 ]
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i64 0, i32 0
%29 = load i8*, i8** %28
%30 = call i32 @strcmp(i8* dereferenceable(1) %29, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%31 = icmp eq i32 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %26
%33 = call i32 @strcmp(i8* dereferenceable(1) %29, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %56, %61, %26, %32, %66
%36 = call %struct.dirent* @readdir(i32* %17)
%37 = icmp eq %struct.dirent* %36, null
br i1 %37, label %81, label %26
38: ; preds = %32
%39 = call i32 @strlcpy(i8* %10, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i64 %8)
%40 = sext i32 %39 to i64
%41 = icmp ult i64 %40, %8
br i1 %41, label %44, label %42
42: ; preds = %38
%43 = call i32 (i8*, ...) @weprintf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0))
br label %81
44: ; preds = %38
%45 = load i8*, i8** %28
%46 = call i32 @strlcat(i8* %10, i8* %45, i32 %7)
%47 = sext i32 %46 to i64
%48 = icmp ult i64 %47, %8
br i1 %48, label %51, label %49
49: ; preds = %44
%50 = call i32 (i8*, ...) @weprintf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0))
br label %81
51: ; preds = %44
%52 = call i64 @stat(i8* %10, %struct.stat* %5)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %51
%55 = call i32 (i8*, ...) @weprintf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i64 0, i64 0), i8* %10)
br label %81
56: ; preds = %51
%57 = load i32, i32* %23
%58 = call i64 @major(i32 %57)
%59 = trunc i64 %58 to i32
%60 = icmp eq i32 %59, %0
br i1 %60, label %61, label %35
61: ; preds = %56
%62 = load i32, i32* %23
%63 = call i64 @minor(i32 %62)
%64 = trunc i64 %63 to i32
%65 = icmp eq i32 %64, %1
br i1 %65, label %66, label %35
66: ; preds = %61
%67 = load i32, i32* @O_RDONLY
%68 = load i32, i32* @O_NONBLOCK
%69 = or i32 %68, %67
%70 = call i32 @open(i8* %10, i32 %69)
%71 = icmp slt i32 %70, 0
br i1 %71, label %35, label %72
72: ; preds = %66
%73 = call i64 @isatty(i32 %70)
%74 = icmp eq i64 %73, 0
br i1 %74, label %79, label %75
75: ; preds = %72
%76 = load i8*, i8** %28
%77 = call i32 @strlcpy(i8* %2, i8* %76, i64 %3)
%78 = call i32 @close(i32 %70)
br label %81
79: ; preds = %72
%80 = call i32 @close(i32 %70)
br label %81
81: ; preds = %35, %19, %75, %79, %54, %49, %42
%82 = phi i32 [ -1, %42 ], [ -1, %49 ], [ -1, %54 ], [ 0, %75 ], [ -1, %79 ], [ 0, %19 ], [ 0, %35 ]
%83 = call i64 @closedir(i32* %17)
%84 = icmp slt i64 %83, 0
br i1 %84, label %85, label %87
85: ; preds = %81
%86 = call i32 (i8*, ...) @weprintf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.9, i64 0, i64 0))
br label %87
87: ; preds = %81, %85, %24, %13, %11
%88 = phi i32 [ -1, %24 ], [ 0, %13 ], [ 0, %11 ], [ -1, %85 ], [ %82, %81 ]
ret i32 %88
}