#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
typedef int uint;
typedef scalar_t__ time_t;
struct TYPE_2__ {
  scalar_t__ tv_sec;
};
struct stat {
  long long st_size;
  TYPE_1__ st_mtim;
  int /*<<< orphan*/ st_mode;
};
struct dirent {
  char *d_name;
  scalar_t__ d_type;
};
struct buf {
  int dummy;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ DT_DIR;
scalar_t__ DT_LNK;
scalar_t__ DT_REG;
scalar_t__ DT_UNKNOWN;
int EBADF;
int EINVAL;
int EOVERFLOW;
int ERANGE;
int SIZE_MAX;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
scalar_t__ S_ISLNK(int /*<<< orphan*/);
scalar_t__ S_ISREG(int /*<<< orphan*/);
int UINT_MAX;
int /*<<< orphan*/ assert(int);
char *buf_add(struct buf **const, int const, int);
int closedir(int /*<<< orphan*/ *);
int dirfd(int /*<<< orphan*/ *);
int errno;
int fchdir(int);
int lstat(char *, struct stat *);
int /*<<< orphan*/ memcpy(char *, char *, size_t const);
int ndigits(unsigned long long);
int /*<<< orphan*/ *opendir(char const *const);
struct dirent *readdir(int /*<<< orphan*/ *);
int readlink(char *, char *, size_t const);
int stat(char *, struct stat *);
int /*<<< orphan*/ strcmp(char *, char *);
size_t strlen(char *);
int /*<<< orphan*/ ulltoa(char *, unsigned long long, int const);
bool lsdir(const char *const name, struct buf **const buf) {
  assert(name != NULL);
  DIR *dir = opendir(name);
  if (dir == NULL)
    return (false);
  int fd = dirfd(dir);
  if (fd == -1) {
    assert(errno != EINVAL);
    goto fail;
  }
  if (fchdir(fd) == -1) {
    assert(errno != EBADF);
    goto fail;
  }
  errno = 0;
  struct dirent *ent;
  while ((ent = readdir(dir)) != NULL) {
    char *p;
    struct stat st;
    bool st_valid = false;
    if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
      continue;
    if (ent->d_type == DT_UNKNOWN) {
      if (stat(ent->d_name, &st) == -1)
        goto fail;
      else
        st_valid = true;
      if (S_ISDIR(st.st_mode))
        ent->d_type = DT_DIR;
      else if (S_ISLNK(st.st_mode))
        ent->d_type = DT_LNK;
      else if (S_ISREG(st.st_mode))
        ent->d_type = DT_REG;
    }
    const size_t dname_len = strlen(ent->d_name);
    if (dname_len > SIZE_MAX - 1) {
      errno = EOVERFLOW;
      goto fail;
    }
    const size_t dname_size = dname_len + 1;
    if ((p = buf_add(buf, 2 + dname_size, 1)) == NULL)
      goto fail;
    *p++ = ent->d_type == DT_DIR   ? 'D'
           : ent->d_type == DT_LNK ? 'L'
           : ent->d_type == DT_REG ? 'F'
                                   : 'U';
    *p++ = '\0';
    (void)memcpy(p, ent->d_name, dname_size);
    if (ent->d_type == DT_DIR) {
      if (lsdir(ent->d_name, buf) == false)
        goto fail;
      if (fchdir(fd) == -1) {
        assert(errno != EBADF);
        goto fail;
      }
      if ((p = buf_add(buf, 2, 1)) == NULL)
        goto fail;
      *p++ = '-';
      *p = '\0';
    } else if (ent->d_type == DT_LNK) {
      if (lstat(ent->d_name, &st) == -1)
        goto fail;
      if (st.st_size < 0) {
        errno = ERANGE;
        goto fail;
      }
      const size_t tsize = (size_t)st.st_size;
      if (tsize > SIZE_MAX - 1) {
        errno = EOVERFLOW;
        goto fail;
      }
      const size_t tlen = tsize + 1;
      if ((p = buf_add(buf, tlen + 1, 1)) == NULL)
        goto fail;
      const ssize_t llen = readlink(ent->d_name, p, tlen);
      *(p + tlen) = '\0';
      if (llen == -1)
        goto fail;
      if (llen < 0) {
        errno = ERANGE;
        goto fail;
      }
      if ((size_t)llen != tlen) {
        errno = EINVAL;
        goto fail;
      }
    } else if (ent->d_type == DT_REG) {
      if (st_valid == false) {
        if (stat(ent->d_name, &st) == -1)
          goto fail;
        st_valid = true;
      }
      if (st.st_mtim.tv_sec < 0 || st.st_size < 0) {
        errno = ERANGE;
        goto fail;
      }
      time_t mtime = st.st_mtim.tv_sec;
      long long size = st.st_size;
      const uint mtime_digits_size = ndigits((unsigned long long)mtime);
      const uint size_digits_size = ndigits((unsigned long long)size);
      if (mtime_digits_size > UINT_MAX - 1 || size_digits_size > UINT_MAX - 1) {
        errno = EOVERFLOW;
        goto fail;
      }
      const uint mtime_digits_len = mtime_digits_size + 1;
      const uint size_digits_len = size_digits_size + 1;
      if (mtime_digits_len > UINT_MAX - size_digits_len) {
        errno = EOVERFLOW;
        goto fail;
      }
      const uint total_digits_len = mtime_digits_len + size_digits_len;
      if ((p = buf_add(buf, total_digits_len, 1)) == NULL)
        goto fail;
      ulltoa(p, (unsigned long long)mtime, mtime_digits_len);
      *(p + mtime_digits_len) = '\0';
      p += mtime_digits_len;
      ulltoa(p, (unsigned long long)size, size_digits_len);
      *(p + size_digits_len) = '\0';
    }
  }
  if (errno != 0)
    goto fail;
  if (closedir(dir) == -1)
    assert(errno != EBADF);
  return (true);
fail : {
  int _errno = errno;
  if (closedir(dir) == -1)
    assert(errno != EBADF);
  errno = _errno;
  return (false);
}
}


x86 assembly (clang, -O0):
<lsdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   10f0 <assert@plt>
mov    -0x10(%rbp),%rdi
call   1080 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1279 <lsdir+0x49>
movl   $0x0,-0x4(%rbp)
jmp    19de <lsdir+0x7ae>
mov    -0x20(%rbp),%rdi
call   10e0 <dirfd@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    12b3 <lsdir+0x83>
mov    0x2d1a(%rip),%rax
mov    (%rax),%eax
mov    0x2d09(%rip),%rcx
cmp    (%rcx),%eax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   10f0 <assert@plt>
jmp    1988 <lsdir+0x758>
mov    -0x24(%rbp),%edi
call   1140 <fchdir@plt>
cmp    $0xffffffff,%eax
jne    12e8 <lsdir+0xb8>
mov    0x2ce5(%rip),%rax
mov    (%rax),%eax
mov    0x2ccc(%rip),%rcx
cmp    (%rcx),%eax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   10f0 <assert@plt>
jmp    1988 <lsdir+0x758>
mov    0x2cc1(%rip),%rax
movl   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
call   1130 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1936 <lsdir+0x706>
movl   $0x0,-0x54(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    0xce0(%rip),%rsi
call   10d0 <strcmp@plt>
cmp    $0x0,%eax
je     134b <lsdir+0x11b>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    0xcc3(%rip),%rsi
call   10d0 <strcmp@plt>
cmp    $0x0,%eax
jne    1350 <lsdir+0x120>
jmp    12f5 <lsdir+0xc5>
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2c81(%rip),%rcx
cmp    (%rcx),%rax
jne    1412 <lsdir+0x1e2>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    -0x50(%rbp),%rsi
call   1100 <stat@plt>
cmp    $0xffffffff,%eax
jne    1386 <lsdir+0x156>
jmp    1988 <lsdir+0x758>
movl   $0x1,-0x54(%rbp)
mov    -0x40(%rbp),%edi
call   10a0 <S_ISDIR@plt>
cmp    $0x0,%rax
je     13b6 <lsdir+0x186>
mov    0x2c12(%rip),%rax
mov    (%rax),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,0x8(%rax)
jmp    140d <lsdir+0x1dd>
mov    -0x40(%rbp),%edi
call   1040 <S_ISLNK@plt>
cmp    $0x0,%rax
je     13df <lsdir+0x1af>
mov    0x2bc9(%rip),%rax
mov    (%rax),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,0x8(%rax)
jmp    1408 <lsdir+0x1d8>
mov    -0x40(%rbp),%edi
call   1120 <S_ISREG@plt>
cmp    $0x0,%rax
je     1403 <lsdir+0x1d3>
mov    0x2bc8(%rip),%rax
mov    (%rax),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,0x8(%rax)
jmp    1408 <lsdir+0x1d8>
jmp    140d <lsdir+0x1dd>
jmp    1412 <lsdir+0x1e2>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
call   1090 <strlen@plt>
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rax
mov    0x2b5b(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
cmp    %rcx,%rax
jbe    1455 <lsdir+0x225>
mov    0x2bab(%rip),%rax
mov    (%rax),%ecx
mov    0x2b62(%rip),%rax
mov    %ecx,(%rax)
jmp    1988 <lsdir+0x758>
mov    -0x60(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x68(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x68(%rbp),%rax
add    $0x2,%rax
mov    %eax,%esi
mov    $0x1,%edx
call   1150 <buf_add@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    148c <lsdir+0x25c>
jmp    1988 <lsdir+0x758>
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2b1d(%rip),%rcx
cmp    (%rcx),%rax
jne    14b4 <lsdir+0x284>
mov    $0x44,%eax
mov    %eax,-0xac(%rbp)
jmp    1510 <lsdir+0x2e0>
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2ad5(%rip),%rcx
cmp    (%rcx),%rax
jne    14dc <lsdir+0x2ac>
mov    $0x4c,%eax
mov    %eax,-0xb0(%rbp)
jmp    1504 <lsdir+0x2d4>
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rdx
mov    0x2ad5(%rip),%rax
mov    (%rax),%rsi
mov    $0x55,%eax
mov    $0x46,%ecx
cmp    %rsi,%rdx
cmove  %ecx,%eax
mov    %eax,-0xb0(%rbp)
mov    -0xb0(%rbp),%eax
mov    %eax,-0xac(%rbp)
mov    -0xac(%rbp),%eax
mov    %al,%cl
mov    -0x38(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x38(%rbp)
mov    %cl,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
movb   $0x0,(%rax)
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
mov    -0x68(%rbp),%rdx
call   1110 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2a5a(%rip),%rcx
cmp    (%rcx),%rax
jne    15fe <lsdir+0x3ce>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <lsdir@plt>
cmp    $0x0,%eax
jne    1585 <lsdir+0x355>
jmp    1988 <lsdir+0x758>
mov    -0x24(%rbp),%edi
call   1140 <fchdir@plt>
cmp    $0xffffffff,%eax
jne    15ba <lsdir+0x38a>
mov    0x2a13(%rip),%rax
mov    (%rax),%eax
mov    0x29fa(%rip),%rcx
cmp    (%rcx),%eax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   10f0 <assert@plt>
jmp    1988 <lsdir+0x758>
mov    -0x18(%rbp),%rdi
mov    $0x2,%esi
mov    $0x1,%edx
call   1150 <buf_add@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    15e0 <lsdir+0x3b0>
jmp    1988 <lsdir+0x758>
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
movb   $0x2d,(%rax)
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
jmp    1931 <lsdir+0x701>
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x298b(%rip),%rcx
cmp    (%rcx),%rax
jne    1745 <lsdir+0x515>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    -0x50(%rbp),%rsi
call   10c0 <lstat@plt>
cmp    $0xffffffff,%eax
jne    1634 <lsdir+0x404>
jmp    1988 <lsdir+0x758>
cmpq   $0x0,-0x50(%rbp)
jge    1656 <lsdir+0x426>
mov    0x298a(%rip),%rax
mov    (%rax),%ecx
mov    0x2961(%rip),%rax
mov    %ecx,(%rax)
jmp    1988 <lsdir+0x758>
mov    -0x50(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x70(%rbp),%rax
mov    0x291f(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
cmp    %rcx,%rax
jbe    1691 <lsdir+0x461>
mov    0x296f(%rip),%rax
mov    (%rax),%ecx
mov    0x2926(%rip),%rax
mov    %ecx,(%rax)
jmp    1988 <lsdir+0x758>
mov    -0x70(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x78(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x78(%rbp),%rax
add    $0x1,%rax
mov    %eax,%esi
mov    $0x1,%edx
call   1150 <buf_add@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    16c8 <lsdir+0x498>
jmp    1988 <lsdir+0x758>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x78(%rbp),%rdx
call   1070 <readlink@plt>
mov    %eax,-0x7c(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x78(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpl   $0xffffffff,-0x7c(%rbp)
jne    16fa <lsdir+0x4ca>
jmp    1988 <lsdir+0x758>
cmpl   $0x0,-0x7c(%rbp)
jge    171b <lsdir+0x4eb>
mov    0x28c5(%rip),%rax
mov    (%rax),%ecx
mov    0x289c(%rip),%rax
mov    %ecx,(%rax)
jmp    1988 <lsdir+0x758>
movslq -0x7c(%rbp),%rax
cmp    -0x78(%rbp),%rax
je     1740 <lsdir+0x510>
mov    0x2878(%rip),%rax
mov    (%rax),%ecx
mov    0x2877(%rip),%rax
mov    %ecx,(%rax)
jmp    1988 <lsdir+0x758>
jmp    192c <lsdir+0x6fc>
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x286c(%rip),%rcx
cmp    (%rcx),%rax
jne    1927 <lsdir+0x6f7>
cmpl   $0x0,-0x54(%rbp)
jne    178c <lsdir+0x55c>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    -0x50(%rbp),%rsi
call   1100 <stat@plt>
cmp    $0xffffffff,%eax
jne    1785 <lsdir+0x555>
jmp    1988 <lsdir+0x758>
movl   $0x1,-0x54(%rbp)
cmpq   $0x0,-0x48(%rbp)
jl     17a2 <lsdir+0x572>
cmpq   $0x0,-0x50(%rbp)
jge    17b9 <lsdir+0x589>
mov    0x2827(%rip),%rax
mov    (%rax),%ecx
mov    0x27fe(%rip),%rax
mov    %ecx,(%rax)
jmp    1988 <lsdir+0x758>
mov    -0x48(%rbp),%rax
mov    %rax,-0x88(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,-0x90(%rbp)
mov    -0x88(%rbp),%rdi
call   1060 <ndigits@plt>
mov    %eax,-0x94(%rbp)
mov    -0x90(%rbp),%rdi
call   1060 <ndigits@plt>
mov    %eax,-0x98(%rbp)
mov    -0x94(%rbp),%eax
mov    0x27d8(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jg     1827 <lsdir+0x5f7>
mov    -0x98(%rbp),%eax
mov    0x27be(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jle    183e <lsdir+0x60e>
mov    0x27c2(%rip),%rax
mov    (%rax),%ecx
mov    0x2779(%rip),%rax
mov    %ecx,(%rax)
jmp    1988 <lsdir+0x758>
mov    -0x94(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x9c(%rbp)
mov    -0x98(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xa0(%rbp)
mov    -0x9c(%rbp),%eax
mov    0x276f(%rip),%rcx
mov    (%rcx),%ecx
sub    -0xa0(%rbp),%ecx
cmp    %ecx,%eax
jle    1890 <lsdir+0x660>
mov    0x2770(%rip),%rax
mov    (%rax),%ecx
mov    0x2727(%rip),%rax
mov    %ecx,(%rax)
jmp    1988 <lsdir+0x758>
mov    -0x9c(%rbp),%eax
add    -0xa0(%rbp),%eax
mov    %eax,-0xa4(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0xa4(%rbp),%esi
mov    $0x1,%edx
call   1150 <buf_add@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    18c9 <lsdir+0x699>
jmp    1988 <lsdir+0x758>
mov    -0x38(%rbp),%rdi
mov    -0x88(%rbp),%rsi
mov    -0x9c(%rbp),%edx
call   1030 <ulltoa@plt>
mov    -0x38(%rbp),%rax
movslq -0x9c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x9c(%rbp),%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x90(%rbp),%rsi
mov    -0xa0(%rbp),%edx
call   1030 <ulltoa@plt>
mov    -0x38(%rbp),%rax
movslq -0xa0(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    192c <lsdir+0x6fc>
jmp    1931 <lsdir+0x701>
jmp    12f5 <lsdir+0xc5>
mov    0x2673(%rip),%rax
cmpl   $0x0,(%rax)
je     194b <lsdir+0x71b>
jmp    1988 <lsdir+0x758>
mov    -0x20(%rbp),%rdi
call   10b0 <closedir@plt>
cmp    $0xffffffff,%eax
jne    197c <lsdir+0x74c>
mov    0x264c(%rip),%rax
mov    (%rax),%eax
mov    0x2633(%rip),%rcx
cmp    (%rcx),%eax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   10f0 <assert@plt>
movl   $0x1,-0x4(%rbp)
jmp    19de <lsdir+0x7ae>
mov    0x2621(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xa8(%rbp)
mov    -0x20(%rbp),%rdi
call   10b0 <closedir@plt>
cmp    $0xffffffff,%eax
jne    19c8 <lsdir+0x798>
mov    0x2600(%rip),%rax
mov    (%rax),%eax
mov    0x25e7(%rip),%rcx
cmp    (%rcx),%eax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   10f0 <assert@plt>
mov    -0xa8(%rbp),%ecx
mov    0x25db(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lsdir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rsi,0x8(%rsp)
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   10f0 <assert@plt>
mov    %rbx,%rdi
call   1080 <opendir@plt>
xor    %r15d,%r15d
test   %rax,%rax
je     16c1 <lsdir+0x491>
mov    %rax,%r12
mov    %rax,%rdi
call   10e0 <dirfd@plt>
cmp    $0xffffffff,%eax
je     12b0 <lsdir+0x80>
mov    %eax,%ebx
mov    %eax,%edi
call   1140 <fchdir@plt>
cmp    $0xffffffff,%eax
je     12c2 <lsdir+0x92>
mov    %ebx,0x14(%rsp)
mov    0x2d1f(%rip),%rax
movl   $0x0,(%rax)
mov    %r12,%rdi
call   1130 <readdir@plt>
test   %rax,%rax
je     160c <lsdir+0x3dc>
mov    %rax,%r13
jmp    1337 <lsdir+0x107>
mov    0x2cf9(%rip),%rax
mov    (%rax),%eax
mov    0x2ce8(%rip),%rcx
jmp    12d2 <lsdir+0xa2>
mov    0x2ce7(%rip),%rax
mov    (%rax),%eax
mov    0x2cce(%rip),%rcx
xor    %edi,%edi
cmp    (%rcx),%eax
setne  %dil
call   10f0 <assert@plt>
jmp    1693 <lsdir+0x463>
mov    %rax,%r13
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1030 <ulltoa@plt>
mov    %r13,%rbx
add    %r15,%rbx
movb   $0x0,0x0(%r13,%r15,1)
mov    %rbx,%rdi
mov    0x30(%rsp),%rsi
mov    %ebp,%edx
call   1030 <ulltoa@plt>
movslq %ebp,%rcx
add    %rbx,%rcx
cs nopw 0x0(%rax,%rax,1)
movb   $0x0,(%rcx)
mov    %r12,%rdi
call   1130 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     160c <lsdir+0x3dc>
mov    0x0(%r13),%rbp
mov    %rbp,%rdi
lea    0xcbc(%rip),%rsi
call   10d0 <strcmp@plt>
test   %eax,%eax
je     1323 <lsdir+0xf3>
mov    %rbp,%rdi
lea    0xca8(%rip),%rsi
call   10d0 <strcmp@plt>
test   %eax,%eax
je     1323 <lsdir+0xf3>
mov    0x8(%r13),%rax
mov    $0x1,%bl
mov    0x2c72(%rip),%rcx
cmp    (%rcx),%rax
jne    13cf <lsdir+0x19f>
mov    %rbp,%rdi
lea    0x18(%rsp),%rsi
call   1100 <stat@plt>
cmp    $0xffffffff,%eax
je     1690 <lsdir+0x460>
mov    0x28(%rsp),%ebp
mov    %ebp,%edi
call   10a0 <S_ISDIR@plt>
mov    0x2c1d(%rip),%rcx
test   %rax,%rax
jne    13c6 <lsdir+0x196>
mov    %ebp,%edi
call   1040 <S_ISLNK@plt>
mov    0x2bea(%rip),%rcx
test   %rax,%rax
jne    13c6 <lsdir+0x196>
mov    %ebp,%edi
call   1120 <S_ISREG@plt>
mov    0x2bff(%rip),%rcx
test   %rax,%rax
je     13cd <lsdir+0x19d>
mov    (%rcx),%rax
mov    %rax,0x8(%r13)
xor    %ebx,%ebx
mov    0x0(%r13),%rdi
call   1090 <strlen@plt>
mov    %rax,%rbp
mov    0x2ba6(%rip),%rax
movslq (%rax),%rax
add    $0xffffffffffffffff,%rax
cmp    %rax,%rbp
ja     164d <lsdir+0x41d>
lea    0x3(%rbp),%esi
mov    0x8(%rsp),%rdi
mov    $0x1,%edx
call   1150 <buf_add@plt>
test   %rax,%rax
je     1690 <lsdir+0x460>
add    $0x1,%rbp
mov    0x8(%r13),%rdx
mov    $0x44,%cl
mov    0x2b9a(%rip),%rsi
cmp    (%rsi),%rdx
je     1448 <lsdir+0x218>
mov    $0x4c,%cl
mov    0x2b6c(%rip),%rsi
cmp    (%rsi),%rdx
je     1448 <lsdir+0x218>
mov    0x2b88(%rip),%rcx
cmp    (%rcx),%rdx
mov    $0x55,%ecx
mov    $0x46,%edx
cmove  %edx,%ecx
mov    %cl,(%rax)
mov    %rax,%rdi
add    $0x2,%rdi
movb   $0x0,0x1(%rax)
mov    0x0(%r13),%rsi
mov    %rbp,%rdx
call   1110 <memcpy@plt>
mov    0x8(%r13),%rax
mov    0x2b4c(%rip),%rcx
cmp    (%rcx),%rax
jne    14c5 <lsdir+0x295>
mov    0x0(%r13),%rdi
mov    0x8(%rsp),%rsi
call   1050 <lsdir@plt>
test   %eax,%eax
je     1690 <lsdir+0x460>
mov    0x14(%rsp),%edi
call   1140 <fchdir@plt>
cmp    $0xffffffff,%eax
je     165f <lsdir+0x42f>
mov    0x8(%rsp),%rdi
mov    $0x2,%esi
mov    $0x1,%edx
call   1150 <buf_add@plt>
test   %rax,%rax
je     1690 <lsdir+0x460>
mov    %rax,%rcx
add    $0x1,%rcx
movb   $0x2d,(%rax)
jmp    1320 <lsdir+0xf0>
mov    0x2acc(%rip),%rcx
cmp    (%rcx),%rax
jne    1567 <lsdir+0x337>
mov    0x0(%r13),%rdi
lea    0x18(%rsp),%rsi
call   10c0 <lstat@plt>
cmp    $0xffffffff,%eax
je     1690 <lsdir+0x460>
mov    0x18(%rsp),%rbp
test   %rbp,%rbp
js     1656 <lsdir+0x426>
mov    0x2a87(%rip),%rax
movslq (%rax),%rax
add    $0xffffffffffffffff,%rax
cmp    %rax,%rbp
ja     164d <lsdir+0x41d>
lea    0x2(%rbp),%esi
mov    0x8(%rsp),%rdi
mov    $0x1,%edx
call   1150 <buf_add@plt>
test   %rax,%rax
je     1690 <lsdir+0x460>
mov    %rax,%rbx
add    $0x1,%rbp
mov    0x0(%r13),%rdi
mov    %rax,%rsi
mov    %rbp,%rdx
call   1070 <readlink@plt>
movb   $0x0,(%rbx,%rbp,1)
cmp    $0xffffffff,%eax
je     1690 <lsdir+0x460>
test   %eax,%eax
js     1656 <lsdir+0x426>
mov    %eax,%eax
cmp    %rax,%rbp
je     1323 <lsdir+0xf3>
jmp    167e <lsdir+0x44e>
mov    0x2a52(%rip),%rcx
cmp    (%rcx),%rax
jne    1323 <lsdir+0xf3>
test   %bl,%bl
je     1592 <lsdir+0x362>
mov    0x0(%r13),%rdi
lea    0x18(%rsp),%rsi
call   1100 <stat@plt>
cmp    $0xffffffff,%eax
je     1690 <lsdir+0x460>
mov    0x20(%rsp),%r14
test   %r14,%r14
js     1656 <lsdir+0x426>
mov    0x18(%rsp),%rbx
test   %rbx,%rbx
js     1656 <lsdir+0x426>
mov    %r14,%rdi
call   1060 <ndigits@plt>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1060 <ndigits@plt>
mov    %eax,%ebp
mov    0x2a0e(%rip),%rax
mov    (%rax),%eax
cmp    %eax,%r15d
jge    164d <lsdir+0x41d>
cmp    %eax,%ebp
jge    164d <lsdir+0x41d>
add    $0x1,%ebp
sub    %ebp,%eax
cmp    %eax,%r15d
jge    164d <lsdir+0x41d>
mov    %rbx,0x30(%rsp)
movslq %r15d,%r15
add    $0x1,%r15
lea    (%r15,%rbp,1),%esi
mov    0x8(%rsp),%rdi
mov    $0x1,%edx
call   1150 <buf_add@plt>
test   %rax,%rax
jne    12e4 <lsdir+0xb4>
jmp    1690 <lsdir+0x460>
mov    0x299d(%rip),%rbx
cmpl   $0x0,(%rbx)
mov    $0x0,%r15d
jne    1693 <lsdir+0x463>
mov    %r12,%rdi
call   10b0 <closedir@plt>
mov    $0x1,%r15d
cmp    $0xffffffff,%eax
jne    16c1 <lsdir+0x491>
mov    (%rbx),%eax
mov    0x2962(%rip),%rcx
xor    %edi,%edi
cmp    (%rcx),%eax
setne  %dil
call   10f0 <assert@plt>
jmp    16c1 <lsdir+0x491>
mov    0x299c(%rip),%rax
jmp    1685 <lsdir+0x455>
mov    0x2973(%rip),%rax
jmp    1685 <lsdir+0x455>
mov    0x294a(%rip),%rax
mov    (%rax),%eax
mov    0x2931(%rip),%rcx
xor    %edi,%edi
cmp    (%rcx),%eax
setne  %dil
call   10f0 <assert@plt>
jmp    1690 <lsdir+0x460>
mov    0x2923(%rip),%rax
mov    (%rax),%eax
mov    0x2922(%rip),%rcx
mov    %eax,(%rcx)
xor    %r15d,%r15d
mov    0x2916(%rip),%rbx
mov    (%rbx),%ebp
mov    %r12,%rdi
call   10b0 <closedir@plt>
cmp    $0xffffffff,%eax
jne    16bf <lsdir+0x48f>
mov    (%rbx),%eax
mov    0x28ee(%rip),%rcx
xor    %edi,%edi
cmp    (%rcx),%eax
setne  %dil
call   10f0 <assert@plt>
mov    %ebp,(%rbx)
mov    %r15d,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.buf = type { i32 }
%struct.dirent = type { i8*, i64 }
%struct.stat = type { i64, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i64 }
@errno = global i32 0
@EINVAL = global i32 0
@EBADF = global i32 0
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@DT_UNKNOWN = global i64 0
@DT_DIR = global i64 0
@DT_LNK = global i64 0
@DT_REG = global i64 0
@SIZE_MAX = global i32 0
@EOVERFLOW = global i32 0
@ERANGE = global i32 0
@UINT_MAX = global i32 0
define i32 @lsdir(i8* %0, %struct.buf** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.buf**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca %struct.dirent*
%9 = alloca i8*
%10 = alloca %struct.stat
%11 = alloca i32
%12 = alloca i64
%13 = alloca i64
%14 = alloca i64
%15 = alloca i64
%16 = alloca i32
%17 = alloca i64
%18 = alloca i64
%19 = alloca i32
%20 = alloca i32
%21 = alloca i32
%22 = alloca i32
%23 = alloca i32
%24 = alloca i32
store i8* %0, i8** %4
store %struct.buf** %1, %struct.buf*** %5
%25 = load i8*, i8** %4
%26 = icmp ne i8* %25, null
%27 = zext i1 %26 to i32
%28 = call i32 @assert(i32 %27)
%29 = load i8*, i8** %4
%30 = call i32* @opendir(i8* %29)
store i32* %30, i32** %6
%31 = load i32*, i32** %6
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %34
33: ; preds = %2
store i32 0, i32* %3
br label %395
34: ; preds = %2
%35 = load i32*, i32** %6
%36 = call i32 @dirfd(i32* %35)
store i32 %36, i32* %7
%37 = load i32, i32* %7
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %45
39: ; preds = %34
%40 = load i32, i32* @errno
%41 = load i32, i32* @EINVAL
%42 = icmp ne i32 %40, %41
%43 = zext i1 %42 to i32
%44 = call i32 @assert(i32 %43)
br label %382
45: ; preds = %34
%46 = load i32, i32* %7
%47 = call i32 @fchdir(i32 %46)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %55
49: ; preds = %45
%50 = load i32, i32* @errno
%51 = load i32, i32* @EBADF
%52 = icmp ne i32 %50, %51
%53 = zext i1 %52 to i32
%54 = call i32 @assert(i32 %53)
br label %382
55: ; preds = %45
store i32 0, i32* @errno
br label %56
56: ; preds = %366, %72, %55
%57 = load i32*, i32** %6
%58 = call %struct.dirent* @readdir(i32* %57)
store %struct.dirent* %58, %struct.dirent** %8
%59 = icmp ne %struct.dirent* %58, null
br i1 %59, label %60, label %367
60: ; preds = %56
store i32 0, i32* %11
%61 = load %struct.dirent*, %struct.dirent** %8
%62 = getelementptr inbounds %struct.dirent, %struct.dirent* %61, i32 0, i32 0
%63 = load i8*, i8** %62
%64 = call i32 @strcmp(i8* %63, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %72
66: ; preds = %60
%67 = load %struct.dirent*, %struct.dirent** %8
%68 = getelementptr inbounds %struct.dirent, %struct.dirent* %67, i32 0, i32 0
%69 = load i8*, i8** %68
%70 = call i32 @strcmp(i8* %69, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%71 = icmp ne i32 %70, 0
br i1 %71, label %73, label %72
72: ; preds = %66, %60
br label %56
73: ; preds = %66
%74 = load %struct.dirent*, %struct.dirent** %8
%75 = getelementptr inbounds %struct.dirent, %struct.dirent* %74, i32 0, i32 1
%76 = load i64, i64* %75
%77 = load i64, i64* @DT_UNKNOWN
%78 = icmp eq i64 %76, %77
br i1 %78, label %79, label %117
79: ; preds = %73
%80 = load %struct.dirent*, %struct.dirent** %8
%81 = getelementptr inbounds %struct.dirent, %struct.dirent* %80, i32 0, i32 0
%82 = load i8*, i8** %81
%83 = call i32 @stat(i8* %82, %struct.stat* %10)
%84 = icmp eq i32 %83, -1
br i1 %84, label %85, label %86
85: ; preds = %79
br label %382
86: ; preds = %79
store i32 1, i32* %11
br label %87
87: ; preds = %86
%88 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 2
%89 = load i32, i32* %88
%90 = call i64 @S_ISDIR(i32 %89)
%91 = icmp ne i64 %90, 0
br i1 %91, label %92, label %96
92: ; preds = %87
%93 = load i64, i64* @DT_DIR
%94 = load %struct.dirent*, %struct.dirent** %8
%95 = getelementptr inbounds %struct.dirent, %struct.dirent* %94, i32 0, i32 1
store i64 %93, i64* %95
br label %116
96: ; preds = %87
%97 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 2
%98 = load i32, i32* %97
%99 = call i64 @S_ISLNK(i32 %98)
%100 = icmp ne i64 %99, 0
br i1 %100, label %101, label %105
101: ; preds = %96
%102 = load i64, i64* @DT_LNK
%103 = load %struct.dirent*, %struct.dirent** %8
%104 = getelementptr inbounds %struct.dirent, %struct.dirent* %103, i32 0, i32 1
store i64 %102, i64* %104
br label %115
105: ; preds = %96
%106 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 2
%107 = load i32, i32* %106
%108 = call i64 @S_ISREG(i32 %107)
%109 = icmp ne i64 %108, 0
br i1 %109, label %110, label %114
110: ; preds = %105
%111 = load i64, i64* @DT_REG
%112 = load %struct.dirent*, %struct.dirent** %8
%113 = getelementptr inbounds %struct.dirent, %struct.dirent* %112, i32 0, i32 1
store i64 %111, i64* %113
br label %114
114: ; preds = %110, %105
br label %115
115: ; preds = %114, %101
br label %116
116: ; preds = %115, %92
br label %117
117: ; preds = %116, %73
%118 = load %struct.dirent*, %struct.dirent** %8
%119 = getelementptr inbounds %struct.dirent, %struct.dirent* %118, i32 0, i32 0
%120 = load i8*, i8** %119
%121 = call i64 @strlen(i8* %120)
store i64 %121, i64* %12
%122 = load i64, i64* %12
%123 = load i32, i32* @SIZE_MAX
%124 = sub nsw i32 %123, 1
%125 = sext i32 %124 to i64
%126 = icmp ugt i64 %122, %125
br i1 %126, label %127, label %129
127: ; preds = %117
%128 = load i32, i32* @EOVERFLOW
store i32 %128, i32* @errno
br label %382
129: ; preds = %117
%130 = load i64, i64* %12
%131 = add i64 %130, 1
store i64 %131, i64* %13
%132 = load %struct.buf**, %struct.buf*** %5
%133 = load i64, i64* %13
%134 = add i64 2, %133
%135 = trunc i64 %134 to i32
%136 = call i8* @buf_add(%struct.buf** %132, i32 %135, i32 1)
store i8* %136, i8** %9
%137 = icmp eq i8* %136, null
br i1 %137, label %138, label %139
138: ; preds = %129
br label %382
139: ; preds = %129
%140 = load %struct.dirent*, %struct.dirent** %8
%141 = getelementptr inbounds %struct.dirent, %struct.dirent* %140, i32 0, i32 1
%142 = load i64, i64* %141
%143 = load i64, i64* @DT_DIR
%144 = icmp eq i64 %142, %143
br i1 %144, label %145, label %146
145: ; preds = %139
br label %163
146: ; preds = %139
%147 = load %struct.dirent*, %struct.dirent** %8
%148 = getelementptr inbounds %struct.dirent, %struct.dirent* %147, i32 0, i32 1
%149 = load i64, i64* %148
%150 = load i64, i64* @DT_LNK
%151 = icmp eq i64 %149, %150
br i1 %151, label %152, label %153
152: ; preds = %146
br label %161
153: ; preds = %146
%154 = load %struct.dirent*, %struct.dirent** %8
%155 = getelementptr inbounds %struct.dirent, %struct.dirent* %154, i32 0, i32 1
%156 = load i64, i64* %155
%157 = load i64, i64* @DT_REG
%158 = icmp eq i64 %156, %157
%159 = zext i1 %158 to i64
%160 = select i1 %158, i32 70, i32 85
br label %161
161: ; preds = %153, %152
%162 = phi i32 [ 76, %152 ], [ %160, %153 ]
br label %163
163: ; preds = %161, %145
%164 = phi i32 [ 68, %145 ], [ %162, %161 ]
%165 = trunc i32 %164 to i8
%166 = load i8*, i8** %9
%167 = getelementptr inbounds i8, i8* %166, i32 1
store i8* %167, i8** %9
store i8 %165, i8* %166
%168 = load i8*, i8** %9
%169 = getelementptr inbounds i8, i8* %168, i32 1
store i8* %169, i8** %9
store i8 0, i8* %168
%170 = load i8*, i8** %9
%171 = load %struct.dirent*, %struct.dirent** %8
%172 = getelementptr inbounds %struct.dirent, %struct.dirent* %171, i32 0, i32 0
%173 = load i8*, i8** %172
%174 = load i64, i64* %13
%175 = call i32 @memcpy(i8* %170, i8* %173, i64 %174)
%176 = load %struct.dirent*, %struct.dirent** %8
%177 = getelementptr inbounds %struct.dirent, %struct.dirent* %176, i32 0, i32 1
%178 = load i64, i64* %177
%179 = load i64, i64* @DT_DIR
%180 = icmp eq i64 %178, %179
br i1 %180, label %181, label %208
181: ; preds = %163
%182 = load %struct.dirent*, %struct.dirent** %8
%183 = getelementptr inbounds %struct.dirent, %struct.dirent* %182, i32 0, i32 0
%184 = load i8*, i8** %183
%185 = load %struct.buf**, %struct.buf*** %5
%186 = call i32 @lsdir(i8* %184, %struct.buf** %185)
%187 = icmp eq i32 %186, 0
br i1 %187, label %188, label %189
188: ; preds = %181
br label %382
189: ; preds = %181
%190 = load i32, i32* %7
%191 = call i32 @fchdir(i32 %190)
%192 = icmp eq i32 %191, -1
br i1 %192, label %193, label %199
193: ; preds = %189
%194 = load i32, i32* @errno
%195 = load i32, i32* @EBADF
%196 = icmp ne i32 %194, %195
%197 = zext i1 %196 to i32
%198 = call i32 @assert(i32 %197)
br label %382
199: ; preds = %189
%200 = load %struct.buf**, %struct.buf*** %5
%201 = call i8* @buf_add(%struct.buf** %200, i32 2, i32 1)
store i8* %201, i8** %9
%202 = icmp eq i8* %201, null
br i1 %202, label %203, label %204
203: ; preds = %199
br label %382
204: ; preds = %199
%205 = load i8*, i8** %9
%206 = getelementptr inbounds i8, i8* %205, i32 1
store i8* %206, i8** %9
store i8 45, i8* %205
%207 = load i8*, i8** %9
store i8 0, i8* %207
br label %366
208: ; preds = %163
%209 = load %struct.dirent*, %struct.dirent** %8
%210 = getelementptr inbounds %struct.dirent, %struct.dirent* %209, i32 0, i32 1
%211 = load i64, i64* %210
%212 = load i64, i64* @DT_LNK
%213 = icmp eq i64 %211, %212
br i1 %213, label %214, label %273
214: ; preds = %208
%215 = load %struct.dirent*, %struct.dirent** %8
%216 = getelementptr inbounds %struct.dirent, %struct.dirent* %215, i32 0, i32 0
%217 = load i8*, i8** %216
%218 = call i32 @lstat(i8* %217, %struct.stat* %10)
%219 = icmp eq i32 %218, -1
br i1 %219, label %220, label %221
220: ; preds = %214
br label %382
221: ; preds = %214
%222 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%223 = load i64, i64* %222
%224 = icmp slt i64 %223, 0
br i1 %224, label %225, label %227
225: ; preds = %221
%226 = load i32, i32* @ERANGE
store i32 %226, i32* @errno
br label %382
227: ; preds = %221
%228 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%229 = load i64, i64* %228
store i64 %229, i64* %14
%230 = load i64, i64* %14
%231 = load i32, i32* @SIZE_MAX
%232 = sub nsw i32 %231, 1
%233 = sext i32 %232 to i64
%234 = icmp ugt i64 %230, %233
br i1 %234, label %235, label %237
235: ; preds = %227
%236 = load i32, i32* @EOVERFLOW
store i32 %236, i32* @errno
br label %382
237: ; preds = %227
%238 = load i64, i64* %14
%239 = add i64 %238, 1
store i64 %239, i64* %15
%240 = load %struct.buf**, %struct.buf*** %5
%241 = load i64, i64* %15
%242 = add i64 %241, 1
%243 = trunc i64 %242 to i32
%244 = call i8* @buf_add(%struct.buf** %240, i32 %243, i32 1)
store i8* %244, i8** %9
%245 = icmp eq i8* %244, null
br i1 %245, label %246, label %247
246: ; preds = %237
br label %382
247: ; preds = %237
%248 = load %struct.dirent*, %struct.dirent** %8
%249 = getelementptr inbounds %struct.dirent, %struct.dirent* %248, i32 0, i32 0
%250 = load i8*, i8** %249
%251 = load i8*, i8** %9
%252 = load i64, i64* %15
%253 = call i32 @readlink(i8* %250, i8* %251, i64 %252)
store i32 %253, i32* %16
%254 = load i8*, i8** %9
%255 = load i64, i64* %15
%256 = getelementptr inbounds i8, i8* %254, i64 %255
store i8 0, i8* %256
%257 = load i32, i32* %16
%258 = icmp eq i32 %257, -1
br i1 %258, label %259, label %260
259: ; preds = %247
br label %382
260: ; preds = %247
%261 = load i32, i32* %16
%262 = icmp slt i32 %261, 0
br i1 %262, label %263, label %265
263: ; preds = %260
%264 = load i32, i32* @ERANGE
store i32 %264, i32* @errno
br label %382
265: ; preds = %260
%266 = load i32, i32* %16
%267 = sext i32 %266 to i64
%268 = load i64, i64* %15
%269 = icmp ne i64 %267, %268
br i1 %269, label %270, label %272
270: ; preds = %265
%271 = load i32, i32* @EINVAL
store i32 %271, i32* @errno
br label %382
272: ; preds = %265
br label %365
273: ; preds = %208
%274 = load %struct.dirent*, %struct.dirent** %8
%275 = getelementptr inbounds %struct.dirent, %struct.dirent* %274, i32 0, i32 1
%276 = load i64, i64* %275
%277 = load i64, i64* @DT_REG
%278 = icmp eq i64 %276, %277
br i1 %278, label %279, label %364
279: ; preds = %273
%280 = load i32, i32* %11
%281 = icmp eq i32 %280, 0
br i1 %281, label %282, label %290
282: ; preds = %279
%283 = load %struct.dirent*, %struct.dirent** %8
%284 = getelementptr inbounds %struct.dirent, %struct.dirent* %283, i32 0, i32 0
%285 = load i8*, i8** %284
%286 = call i32 @stat(i8* %285, %struct.stat* %10)
%287 = icmp eq i32 %286, -1
br i1 %287, label %288, label %289
288: ; preds = %282
br label %382
289: ; preds = %282
store i32 1, i32* %11
br label %290
290: ; preds = %289, %279
%291 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 1
%292 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %291, i32 0, i32 0
%293 = load i64, i64* %292
%294 = icmp slt i64 %293, 0
br i1 %294, label %299, label %295
295: ; preds = %290
%296 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%297 = load i64, i64* %296
%298 = icmp slt i64 %297, 0
br i1 %298, label %299, label %301
299: ; preds = %295, %290
%300 = load i32, i32* @ERANGE
store i32 %300, i32* @errno
br label %382
301: ; preds = %295
%302 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 1
%303 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %302, i32 0, i32 0
%304 = load i64, i64* %303
store i64 %304, i64* %17
%305 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%306 = load i64, i64* %305
store i64 %306, i64* %18
%307 = load i64, i64* %17
%308 = call i32 @ndigits(i64 %307)
store i32 %308, i32* %19
%309 = load i64, i64* %18
%310 = call i32 @ndigits(i64 %309)
store i32 %310, i32* %20
%311 = load i32, i32* %19
%312 = load i32, i32* @UINT_MAX
%313 = sub nsw i32 %312, 1
%314 = icmp sgt i32 %311, %313
br i1 %314, label %320, label %315
315: ; preds = %301
%316 = load i32, i32* %20
%317 = load i32, i32* @UINT_MAX
%318 = sub nsw i32 %317, 1
%319 = icmp sgt i32 %316, %318
br i1 %319, label %320, label %322
320: ; preds = %315, %301
%321 = load i32, i32* @EOVERFLOW
store i32 %321, i32* @errno
br label %382
322: ; preds = %315
%323 = load i32, i32* %19
%324 = add nsw i32 %323, 1
store i32 %324, i32* %21
%325 = load i32, i32* %20
%326 = add nsw i32 %325, 1
store i32 %326, i32* %22
%327 = load i32, i32* %21
%328 = load i32, i32* @UINT_MAX
%329 = load i32, i32* %22
%330 = sub nsw i32 %328, %329
%331 = icmp sgt i32 %327, %330
br i1 %331, label %332, label %334
332: ; preds = %322
%333 = load i32, i32* @EOVERFLOW
store i32 %333, i32* @errno
br label %382
334: ; preds = %322
%335 = load i32, i32* %21
%336 = load i32, i32* %22
%337 = add nsw i32 %335, %336
store i32 %337, i32* %23
%338 = load %struct.buf**, %struct.buf*** %5
%339 = load i32, i32* %23
%340 = call i8* @buf_add(%struct.buf** %338, i32 %339, i32 1)
store i8* %340, i8** %9
%341 = icmp eq i8* %340, null
br i1 %341, label %342, label %343
342: ; preds = %334
br label %382
343: ; preds = %334
%344 = load i8*, i8** %9
%345 = load i64, i64* %17
%346 = load i32, i32* %21
%347 = call i32 @ulltoa(i8* %344, i64 %345, i32 %346)
%348 = load i8*, i8** %9
%349 = load i32, i32* %21
%350 = sext i32 %349 to i64
%351 = getelementptr inbounds i8, i8* %348, i64 %350
store i8 0, i8* %351
%352 = load i32, i32* %21
%353 = load i8*, i8** %9
%354 = sext i32 %352 to i64
%355 = getelementptr inbounds i8, i8* %353, i64 %354
store i8* %355, i8** %9
%356 = load i8*, i8** %9
%357 = load i64, i64* %18
%358 = load i32, i32* %22
%359 = call i32 @ulltoa(i8* %356, i64 %357, i32 %358)
%360 = load i8*, i8** %9
%361 = load i32, i32* %22
%362 = sext i32 %361 to i64
%363 = getelementptr inbounds i8, i8* %360, i64 %362
store i8 0, i8* %363
br label %364
364: ; preds = %343, %273
br label %365
365: ; preds = %364, %272
br label %366
366: ; preds = %365, %204
br label %56
367: ; preds = %56
%368 = load i32, i32* @errno
%369 = icmp ne i32 %368, 0
br i1 %369, label %370, label %371
370: ; preds = %367
br label %382
371: ; preds = %367
%372 = load i32*, i32** %6
%373 = call i32 @closedir(i32* %372)
%374 = icmp eq i32 %373, -1
br i1 %374, label %375, label %381
375: ; preds = %371
%376 = load i32, i32* @errno
%377 = load i32, i32* @EBADF
%378 = icmp ne i32 %376, %377
%379 = zext i1 %378 to i32
%380 = call i32 @assert(i32 %379)
br label %381
381: ; preds = %375, %371
store i32 1, i32* %3
br label %395
382: ; preds = %370, %342, %332, %320, %299, %288, %270, %263, %259, %246, %235, %225, %220, %203, %193, %188, %138, %127, %85, %49, %39
%383 = load i32, i32* @errno
store i32 %383, i32* %24
%384 = load i32*, i32** %6
%385 = call i32 @closedir(i32* %384)
%386 = icmp eq i32 %385, -1
br i1 %386, label %387, label %393
387: ; preds = %382
%388 = load i32, i32* @errno
%389 = load i32, i32* @EBADF
%390 = icmp ne i32 %388, %389
%391 = zext i1 %390 to i32
%392 = call i32 @assert(i32 %391)
br label %393
393: ; preds = %387, %382
%394 = load i32, i32* %24
store i32 %394, i32* @errno
store i32 0, i32* %3
br label %395
395: ; preds = %393, %381, %33
%396 = load i32, i32* %3
ret i32 %396
}

LLVM IR (-O2):
%struct.buf = type { i32 }
%struct.stat = type { i64, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.dirent = type { i8*, i64 }
@errno = global i32 0
@EINVAL = global i32 0
@EBADF = global i32 0
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@DT_UNKNOWN = global i64 0
@DT_DIR = global i64 0
@DT_LNK = global i64 0
@DT_REG = global i64 0
@SIZE_MAX = global i32 0
@EOVERFLOW = global i32 0
@ERANGE = global i32 0
@UINT_MAX = global i32 0
define i32 @lsdir(i8* %0, %struct.buf** %1) {
%3 = alloca %struct.stat
%4 = icmp ne i8* %0, null
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = tail call i32* @opendir(i8* %0)
%8 = icmp eq i32* %7, null
br i1 %8, label %230, label %9
9: ; preds = %2
%10 = tail call i32 @dirfd(i32* %7)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %18
12: ; preds = %9
%13 = load i32, i32* @errno
%14 = load i32, i32* @EINVAL
%15 = icmp ne i32 %13, %14
%16 = zext i1 %15 to i32
%17 = tail call i32 @assert(i32 %16)
br label %219
18: ; preds = %9
%19 = tail call i32 @fchdir(i32 %10)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %27
21: ; preds = %18
%22 = load i32, i32* @errno
%23 = load i32, i32* @EBADF
%24 = icmp ne i32 %22, %23
%25 = zext i1 %24 to i32
%26 = tail call i32 @assert(i32 %25)
br label %219
27: ; preds = %18
store i32 0, i32* @errno
%28 = tail call %struct.dirent* @readdir(i32* %7)
%29 = icmp eq %struct.dirent* %28, null
br i1 %29, label %207, label %30
30: ; preds = %27
%31 = bitcast %struct.stat* %3 to i8*
%32 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 2
%33 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1, i32 0
%34 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
br label %35
35: ; preds = %30, %204
%36 = phi %struct.dirent* [ %28, %30 ], [ %205, %204 ]
%37 = getelementptr inbounds %struct.dirent, %struct.dirent* %36, i64 0, i32 0
%38 = load i8*, i8** %37
%39 = tail call i32 @strcmp(i8* dereferenceable(1) %38, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%40 = icmp eq i32 %39, 0
br i1 %40, label %204, label %41
41: ; preds = %35
%42 = tail call i32 @strcmp(i8* dereferenceable(1) %38, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%43 = icmp eq i32 %42, 0
br i1 %43, label %204, label %44
44: ; preds = %41
%45 = getelementptr inbounds %struct.dirent, %struct.dirent* %36, i64 0, i32 1
%46 = load i64, i64* %45
%47 = load i64, i64* @DT_UNKNOWN
%48 = icmp eq i64 %46, %47
br i1 %48, label %49, label %65
49: ; preds = %44
%50 = call i32 @stat(i8* %38, %struct.stat* %3)
%51 = icmp eq i32 %50, -1
br i1 %51, label %201, label %52
52: ; preds = %49
%53 = load i32, i32* %32
%54 = tail call i64 @S_ISDIR(i32 %53)
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %62
56: ; preds = %52
%57 = tail call i64 @S_ISLNK(i32 %53)
%58 = icmp eq i64 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %56
%60 = tail call i64 @S_ISREG(i32 %53)
%61 = icmp eq i64 %60, 0
br i1 %61, label %65, label %62
62: ; preds = %59, %56, %52
%63 = phi i64* [ @DT_DIR, %52 ], [ @DT_LNK, %56 ], [ @DT_REG, %59 ]
%64 = load i64, i64* %63
store i64 %64, i64* %45
br label %65
65: ; preds = %62, %59, %44
%66 = phi i1 [ false, %59 ], [ true, %44 ], [ false, %62 ]
%67 = load i8*, i8** %37
%68 = tail call i64 @strlen(i8* dereferenceable(1) %67)
%69 = load i32, i32* @SIZE_MAX
%70 = add nsw i32 %69, -1
%71 = sext i32 %70 to i64
%72 = icmp ugt i64 %68, %71
br i1 %72, label %73, label %75
73: ; preds = %65
%74 = load i32, i32* @EOVERFLOW
store i32 %74, i32* @errno
br label %201
75: ; preds = %65
%76 = add i64 %68, 1
%77 = trunc i64 %68 to i32
%78 = add i32 %77, 3
%79 = tail call i8* @buf_add(%struct.buf** %1, i32 %78, i32 1)
%80 = icmp eq i8* %79, null
br i1 %80, label %201, label %81
81: ; preds = %75
%82 = load i64, i64* %45
%83 = load i64, i64* @DT_DIR
%84 = icmp eq i64 %82, %83
br i1 %84, label %92, label %85
85: ; preds = %81
%86 = load i64, i64* @DT_LNK
%87 = icmp eq i64 %82, %86
br i1 %87, label %92, label %88
88: ; preds = %85
%89 = load i64, i64* @DT_REG
%90 = icmp eq i64 %82, %89
%91 = select i1 %90, i8 70, i8 85
br label %92
92: ; preds = %88, %85, %81
%93 = phi i8 [ 68, %81 ], [ %91, %88 ], [ 76, %85 ]
%94 = getelementptr inbounds i8, i8* %79, i64 1
store i8 %93, i8* %79
%95 = getelementptr inbounds i8, i8* %79, i64 2
store i8 0, i8* %94
%96 = load i8*, i8** %37
%97 = tail call i32 @memcpy(i8* %95, i8* %96, i64 %76)
%98 = load i64, i64* %45
%99 = load i64, i64* @DT_DIR
%100 = icmp eq i64 %98, %99
br i1 %100, label %101, label %119
101: ; preds = %92
%102 = load i8*, i8** %37
%103 = tail call i32 @lsdir(i8* %102, %struct.buf** %1)
%104 = icmp eq i32 %103, 0
br i1 %104, label %201, label %105
105: ; preds = %101
%106 = tail call i32 @fchdir(i32 %10)
%107 = icmp eq i32 %106, -1
br i1 %107, label %108, label %114
108: ; preds = %105
%109 = load i32, i32* @errno
%110 = load i32, i32* @EBADF
%111 = icmp ne i32 %109, %110
%112 = zext i1 %111 to i32
%113 = tail call i32 @assert(i32 %112)
br label %201
114: ; preds = %105
%115 = tail call i8* @buf_add(%struct.buf** %1, i32 2, i32 1)
%116 = icmp eq i8* %115, null
br i1 %116, label %201, label %117
117: ; preds = %114
%118 = getelementptr inbounds i8, i8* %115, i64 1
store i8 45, i8* %115
br label %202
119: ; preds = %92
%120 = load i64, i64* @DT_LNK
%121 = icmp eq i64 %98, %120
br i1 %121, label %122, label %158
122: ; preds = %119
%123 = load i8*, i8** %37
%124 = call i32 @lstat(i8* %123, %struct.stat* %3)
%125 = icmp eq i32 %124, -1
br i1 %125, label %201, label %126
126: ; preds = %122
%127 = load i64, i64* %34
%128 = icmp slt i64 %127, 0
br i1 %128, label %129, label %131
129: ; preds = %126
%130 = load i32, i32* @ERANGE
store i32 %130, i32* @errno
br label %201
131: ; preds = %126
%132 = load i32, i32* @SIZE_MAX
%133 = add nsw i32 %132, -1
%134 = sext i32 %133 to i64
%135 = icmp ugt i64 %127, %134
br i1 %135, label %136, label %138
136: ; preds = %131
%137 = load i32, i32* @EOVERFLOW
store i32 %137, i32* @errno
br label %201
138: ; preds = %131
%139 = add nuw i64 %127, 1
%140 = trunc i64 %127 to i32
%141 = add i32 %140, 2
%142 = tail call i8* @buf_add(%struct.buf** %1, i32 %141, i32 1)
%143 = icmp eq i8* %142, null
br i1 %143, label %201, label %144
144: ; preds = %138
%145 = load i8*, i8** %37
%146 = tail call i32 @readlink(i8* %145, i8* %142, i64 %139)
%147 = getelementptr inbounds i8, i8* %142, i64 %139
store i8 0, i8* %147
%148 = icmp eq i32 %146, -1
br i1 %148, label %201, label %149
149: ; preds = %144
%150 = icmp slt i32 %146, 0
br i1 %150, label %151, label %153
151: ; preds = %149
%152 = load i32, i32* @ERANGE
store i32 %152, i32* @errno
br label %201
153: ; preds = %149
%154 = zext i32 %146 to i64
%155 = icmp eq i64 %139, %154
br i1 %155, label %204, label %156
156: ; preds = %153
%157 = load i32, i32* @EINVAL
store i32 %157, i32* @errno
br label %201
158: ; preds = %119
%159 = load i64, i64* @DT_REG
%160 = icmp eq i64 %98, %159
br i1 %160, label %161, label %204
161: ; preds = %158
br i1 %66, label %162, label %166
162: ; preds = %161
%163 = load i8*, i8** %37
%164 = call i32 @stat(i8* %163, %struct.stat* %3)
%165 = icmp eq i32 %164, -1
br i1 %165, label %201, label %166
166: ; preds = %162, %161
%167 = load i64, i64* %33
%168 = icmp slt i64 %167, 0
%169 = load i64, i64* %34
%170 = icmp slt i64 %169, 0
%171 = select i1 %168, i1 true, i1 %170
br i1 %171, label %172, label %174
172: ; preds = %166
%173 = load i32, i32* @ERANGE
store i32 %173, i32* @errno
br label %201
174: ; preds = %166
%175 = tail call i32 @ndigits(i64 %167)
%176 = tail call i32 @ndigits(i64 %169)
%177 = load i32, i32* @UINT_MAX
%178 = icmp slt i32 %175, %177
%179 = icmp slt i32 %176, %177
%180 = select i1 %178, i1 %179, i1 false
br i1 %180, label %183, label %181
181: ; preds = %174
%182 = load i32, i32* @EOVERFLOW
store i32 %182, i32* @errno
br label %201
183: ; preds = %174
%184 = add nsw i32 %175, 1
%185 = add nsw i32 %176, 1
%186 = sub nsw i32 %177, %185
%187 = icmp slt i32 %175, %186
br i1 %187, label %190, label %188
188: ; preds = %183
%189 = load i32, i32* @EOVERFLOW
store i32 %189, i32* @errno
br label %201
190: ; preds = %183
%191 = add nsw i32 %185, %184
%192 = tail call i8* @buf_add(%struct.buf** %1, i32 %191, i32 1)
%193 = icmp eq i8* %192, null
br i1 %193, label %201, label %194
194: ; preds = %190
%195 = tail call i32 @ulltoa(i8* %192, i64 %167, i32 %184)
%196 = sext i32 %184 to i64
%197 = getelementptr inbounds i8, i8* %192, i64 %196
store i8 0, i8* %197
%198 = tail call i32 @ulltoa(i8* %197, i64 %169, i32 %185)
%199 = sext i32 %185 to i64
%200 = getelementptr inbounds i8, i8* %197, i64 %199
br label %202
201: ; preds = %49, %75, %101, %114, %122, %162, %138, %144, %190, %73, %108, %129, %172, %136, %151, %156, %181, %188
br label %219
202: ; preds = %194, %117
%203 = phi i8* [ %118, %117 ], [ %200, %194 ]
store i8 0, i8* %203
br label %204
204: ; preds = %202, %158, %153, %35, %41
%205 = tail call %struct.dirent* @readdir(i32* %7)
%206 = icmp eq %struct.dirent* %205, null
br i1 %206, label %207, label %35
207: ; preds = %204, %27
%208 = load i32, i32* @errno
%209 = icmp eq i32 %208, 0
br i1 %209, label %210, label %219
210: ; preds = %207
%211 = tail call i32 @closedir(i32* %7)
%212 = icmp eq i32 %211, -1
br i1 %212, label %213, label %230
213: ; preds = %210
%214 = load i32, i32* @errno
%215 = load i32, i32* @EBADF
%216 = icmp ne i32 %214, %215
%217 = zext i1 %216 to i32
%218 = tail call i32 @assert(i32 %217)
br label %230
219: ; preds = %201, %207, %21, %12
%220 = load i32, i32* @errno
%221 = tail call i32 @closedir(i32* %7)
%222 = icmp eq i32 %221, -1
br i1 %222, label %223, label %229
223: ; preds = %219
%224 = load i32, i32* @errno
%225 = load i32, i32* @EBADF
%226 = icmp ne i32 %224, %225
%227 = zext i1 %226 to i32
%228 = tail call i32 @assert(i32 %227)
br label %229
229: ; preds = %223, %219
store i32 %220, i32* @errno
br label %230
230: ; preds = %229, %213, %210, %2
%231 = phi i32 [ 0, %2 ], [ 0, %229 ], [ 1, %213 ], [ 1, %210 ]
ret i32 %231
}