#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 */
typedef scalar_t__ pid_t;
typedef int mode_t;

/* Variables and functions */
int O_DIRECTORY;
int O_RDONLY;
int S_IROTH;
int S_IRWXG;
int S_IRWXU;
int S_IXOTH;
scalar_t__ WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
int close(int);
int creat(char *, int const);
int do_getdents(char *, int, int);
int do_getdents64(char const *, int, int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int mkdir(char *, int const);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ remove(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int waitpid(scalar_t__, int *, int /*<<< orphan*/);
int main(void) {
  int rv, fd, count;
  const mode_t perm = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH;
  rv = mkdir("root", perm);
  if (rv) {
    perror("mkdir 1");
    return 1;
  }
  rv = mkdir("root/testdir", perm);
  if (rv) {
    perror("mkdir 2");
    return 1;
  }
  rv = creat("root/testdir/file1", perm);
  if (rv < 0) {
    perror("creat 1");
    return 1;
  }
  rv = close(rv);
  if (rv) {
    perror("close 1");
    return 1;
  }
  rv = creat("root/testdir/file2", perm);
  if (rv < 0) {
    perror("creat 2");
    return 1;
  }
  rv = close(rv);
  if (rv) {
    perror("close 2");
    return 1;
  }
  rv = mkdir("root/testdir/dir3", perm);
  if (rv) {
    perror("mkdir 3");
    return 1;
  }
  printf("getdents: setup ok\n");
  fd = open("root/testdir", O_RDONLY | O_DIRECTORY);
  if (fd < 0) {
    perror("open 1");
    return 1;
  }
  do {
    count = do_getdents("getdents32", fd, 512);
    if (count < 0)
      return 1;
  } while (count > 0);
  rv = close(fd);
  if (rv) {
    perror("close 1");
    return 1;
  }
  fd = open("root/testdir", O_RDONLY | O_DIRECTORY);
  if (fd < 0) {
    perror("open 2");
    return 1;
  }
  do {
    count = do_getdents64("getdents64", fd, 512);
    if (count < 0)
      return 1;
  } while (count > 0);
  rv = close(fd);
  if (rv) {
    perror("close 2");
    return 1;
  }
  fd = open("root/testdir", O_RDONLY | O_DIRECTORY);
  count = do_getdents64("getdents64 before fork", fd, 64);
  if (count < 0)
    return 1;
  fflush(stdout);
  pid_t pid = fork();
  if (pid < 0) {
    perror("fork");
    return 1;
  }
  const char *process = pid ? "parent getdents64" : "child getdents64";
  count = do_getdents64(process, fd, 64);
  if (count < 0)
    return 1;
  if (count == 0) {
    fprintf(stderr, "%s: handle empty too soon\n", process);
    return 1;
  }
  rv = close(fd);
  if (rv) {
    perror("close after fork");
    return 1;
  }
  if (pid) {
    int status;
    rv = waitpid(pid, &status, 0);
    if (rv < 0) {
      perror("waitpid");
      return 1;
    }
    if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
      fprintf(stderr, "waitpid: got %d\n", status);
      return 1;
    }
    remove("root/testdir/file1");
    remove("root/testdir/file2");
    remove("root/testdir/dir3");
    remove("root/testdir");
    remove("root");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    0x2dc9(%rip),%rcx
or     (%rcx),%eax
mov    0x2d98(%rip),%rcx
or     (%rcx),%eax
mov    0x2dbf(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xdd0(%rip),%rdi
call   1040 <mkdir@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     125a <main+0x6a>
lea    0xdbc(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    16ed <main+0x4fd>
mov    -0x14(%rbp),%esi
lea    0xda9(%rip),%rdi
call   1040 <mkdir@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     128e <main+0x9e>
lea    0xd9d(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    16ed <main+0x4fd>
mov    -0x14(%rbp),%esi
lea    0xd8a(%rip),%rdi
call   10f0 <creat@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    12c2 <main+0xd2>
lea    0xd84(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    16ed <main+0x4fd>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     12ef <main+0xff>
lea    0xd5f(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    16ed <main+0x4fd>
mov    -0x14(%rbp),%esi
lea    0xd4c(%rip),%rdi
call   10f0 <creat@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1323 <main+0x133>
lea    0xd46(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    16ed <main+0x4fd>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1350 <main+0x160>
lea    0xd21(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    16ed <main+0x4fd>
mov    -0x14(%rbp),%esi
lea    0xd0e(%rip),%rdi
call   1040 <mkdir@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1384 <main+0x194>
lea    0xd07(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    16ed <main+0x4fd>
lea    0xcf7(%rip),%rdi
call   1060 <printf@plt>
mov    0x2c39(%rip),%rax
mov    (%rax),%esi
mov    0x2c20(%rip),%rax
or     (%rax),%esi
lea    0xc64(%rip),%rdi
call   10c0 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    13d3 <main+0x1e3>
lea    0xcd4(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    16ed <main+0x4fd>
jmp    13d8 <main+0x1e8>
mov    -0xc(%rbp),%esi
lea    0xcbb(%rip),%rdi
mov    $0x200,%edx
call   1050 <do_getdents@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1405 <main+0x215>
movl   $0x1,-0x4(%rbp)
jmp    16ed <main+0x4fd>
jmp    140a <main+0x21a>
cmpl   $0x0,-0x10(%rbp)
jg     13d8 <main+0x1e8>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1441 <main+0x251>
lea    0xc0d(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    16ed <main+0x4fd>
mov    0x2b88(%rip),%rax
mov    (%rax),%esi
mov    0x2b6f(%rip),%rax
or     (%rax),%esi
lea    0xbb3(%rip),%rdi
call   10c0 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1484 <main+0x294>
lea    0xc35(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    16ed <main+0x4fd>
jmp    1489 <main+0x299>
mov    -0xc(%rbp),%esi
lea    0xc4b(%rip),%rdi
mov    $0x200,%edx
call   10e0 <do_getdents64@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    14b6 <main+0x2c6>
movl   $0x1,-0x4(%rbp)
jmp    16ed <main+0x4fd>
jmp    14bb <main+0x2cb>
cmpl   $0x0,-0x10(%rbp)
jg     1489 <main+0x299>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     14f2 <main+0x302>
lea    0xb7f(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    16ed <main+0x4fd>
mov    0x2ad7(%rip),%rax
mov    (%rax),%esi
mov    0x2abe(%rip),%rax
or     (%rax),%esi
lea    0xb02(%rip),%rdi
call   10c0 <open@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xb92(%rip),%rdi
mov    $0x40,%edx
call   10e0 <do_getdents64@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1540 <main+0x350>
movl   $0x1,-0x4(%rbp)
jmp    16ed <main+0x4fd>
mov    0x2a61(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
mov    $0x0,%al
call   1110 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    157c <main+0x38c>
lea    0xb56(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    16ed <main+0x4fd>
mov    -0x20(%rbp),%rdx
lea    0xb51(%rip),%rax
lea    0xb38(%rip),%rcx
cmp    $0x0,%rdx
cmovne %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    $0x40,%edx
call   10e0 <do_getdents64@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    15c4 <main+0x3d4>
movl   $0x1,-0x4(%rbp)
jmp    16ed <main+0x4fd>
cmpl   $0x0,-0x10(%rbp)
jne    15f5 <main+0x405>
mov    0x29e3(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xb07(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    16ed <main+0x4fd>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1622 <main+0x432>
lea    0xaf3(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    16ed <main+0x4fd>
cmpq   $0x0,-0x20(%rbp)
je     16e6 <main+0x4f6>
mov    -0x20(%rbp),%rdi
lea    -0x2c(%rbp),%rsi
xor    %edx,%edx
call   10b0 <waitpid@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1661 <main+0x471>
lea    0xac5(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    16ed <main+0x4fd>
mov    -0x2c(%rbp),%edi
call   1100 <WIFEXITED@plt>
cmp    $0x0,%eax
je     1684 <main+0x494>
mov    -0x2c(%rbp),%edi
call   10a0 <WEXITSTATUS@plt>
cmp    $0x0,%rax
je     16aa <main+0x4ba>
mov    0x292d(%rip),%rax
mov    (%rax),%edi
mov    -0x2c(%rbp),%edx
lea    0xa86(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    16ed <main+0x4fd>
lea    0x971(%rip),%rdi
call   1030 <remove@plt>
lea    0x988(%rip),%rdi
call   1030 <remove@plt>
lea    0x99f(%rip),%rdi
call   1030 <remove@plt>
lea    0x938(%rip),%rdi
call   1030 <remove@plt>
lea    0x91f(%rip),%rdi
call   1030 <remove@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2de5(%rip),%rax
mov    0x2dce(%rip),%rcx
mov    (%rcx),%ebx
or     (%rax),%ebx
mov    0x2d9b(%rip),%rax
or     (%rax),%ebx
mov    0x2dc2(%rip),%rax
or     (%rax),%ebx
lea    0xdd9(%rip),%rdi
mov    %ebx,%esi
call   1040 <mkdir@plt>
test   %eax,%eax
jne    1455 <main+0x265>
lea    0xdd0(%rip),%rdi
mov    %ebx,%esi
call   1040 <mkdir@plt>
test   %eax,%eax
jne    145e <main+0x26e>
lea    0xdcf(%rip),%rdi
mov    %ebx,%esi
call   10f0 <creat@plt>
test   %eax,%eax
js     1467 <main+0x277>
mov    %eax,%edi
call   1070 <close@plt>
test   %eax,%eax
jne    1470 <main+0x280>
lea    0xdcd(%rip),%rdi
mov    %ebx,%esi
call   10f0 <creat@plt>
test   %eax,%eax
js     1479 <main+0x289>
mov    %eax,%edi
call   1070 <close@plt>
test   %eax,%eax
jne    1482 <main+0x292>
lea    0xdcb(%rip),%rdi
mov    %ebx,%esi
call   1040 <mkdir@plt>
test   %eax,%eax
jne    148b <main+0x29b>
lea    0xe67(%rip),%rdi
call   1050 <puts@plt>
mov    0x2d11(%rip),%r14
mov    0x2cfa(%rip),%r15
mov    (%r15),%esi
or     (%r14),%esi
lea    0xd3a(%rip),%rdi
call   10c0 <open@plt>
test   %eax,%eax
js     1494 <main+0x2a4>
mov    %eax,%ebp
lea    0xda0(%rip),%rbx
mov    $0x1,%r12d
nop
mov    %rbx,%rdi
mov    %ebp,%esi
mov    $0x200,%edx
call   1060 <do_getdents@plt>
test   %eax,%eax
js     14a6 <main+0x2b6>
jne    12f0 <main+0x100>
mov    %ebp,%edi
call   1070 <close@plt>
test   %eax,%eax
jne    14b6 <main+0x2c6>
mov    (%r15),%esi
or     (%r14),%esi
lea    0xce8(%rip),%rdi
call   10c0 <open@plt>
test   %eax,%eax
js     14e0 <main+0x2f0>
mov    %eax,%ebp
lea    0xd8f(%rip),%rbx
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %ebp,%esi
mov    $0x200,%edx
call   10e0 <do_getdents64@plt>
test   %eax,%eax
js     14a6 <main+0x2b6>
jne    1340 <main+0x150>
mov    %ebp,%edi
call   1070 <close@plt>
test   %eax,%eax
jne    14ee <main+0x2fe>
mov    (%r15),%esi
or     (%r14),%esi
lea    0xc98(%rip),%rdi
call   10c0 <open@plt>
mov    %eax,%r15d
lea    0xd17(%rip),%rdi
mov    %eax,%esi
mov    $0x40,%edx
call   10e0 <do_getdents64@plt>
test   %eax,%eax
js     14a6 <main+0x2b6>
mov    0x2c09(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
xor    %eax,%eax
call   1110 <fork@plt>
test   %rax,%rax
js     14fc <main+0x30c>
mov    %rax,%r14
lea    0xd04(%rip),%rax
lea    0xceb(%rip),%rbp
cmove  %rax,%rbp
mov    %rbp,%rdi
mov    %r15d,%esi
mov    $0x40,%edx
call   10e0 <do_getdents64@plt>
test   %eax,%eax
js     14a6 <main+0x2b6>
je     14c4 <main+0x2d4>
mov    %r15d,%edi
call   1070 <close@plt>
test   %eax,%eax
jne    154e <main+0x35e>
test   %r14,%r14
je     1546 <main+0x356>
lea    0xc(%rsp),%rsi
mov    %r14,%rdi
xor    %edx,%edx
call   10b0 <waitpid@plt>
test   %eax,%eax
js     155f <main+0x36f>
mov    0xc(%rsp),%edi
call   1100 <WIFEXITED@plt>
test   %eax,%eax
je     1438 <main+0x248>
mov    0xc(%rsp),%edi
call   10a0 <WEXITSTATUS@plt>
test   %rax,%rax
je     150a <main+0x31a>
mov    0x2b79(%rip),%rax
mov    (%rax),%edi
mov    0xc(%rsp),%edx
lea    0xcbd(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    14a6 <main+0x2b6>
lea    0xba9(%rip),%rdi
jmp    149b <main+0x2ab>
lea    0xbb5(%rip),%rdi
jmp    149b <main+0x2ab>
lea    0xbc7(%rip),%rdi
jmp    149b <main+0x2ab>
lea    0xbc6(%rip),%rdi
jmp    149b <main+0x2ab>
lea    0xbd8(%rip),%rdi
jmp    149b <main+0x2ab>
lea    0xbd7(%rip),%rdi
jmp    149b <main+0x2ab>
lea    0xbe8(%rip),%rdi
jmp    149b <main+0x2ab>
lea    0xbe7(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%r12d
mov    %r12d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xb80(%rip),%rdi
call   10d0 <perror@plt>
jmp    14a6 <main+0x2b6>
mov    0x2aed(%rip),%rax
mov    (%rax),%edi
lea    0xc01(%rip),%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    14a6 <main+0x2b6>
lea    0xbad(%rip),%rdi
call   10d0 <perror@plt>
jmp    14a6 <main+0x2b6>
lea    0xb6b(%rip),%rdi
call   10d0 <perror@plt>
jmp    14a6 <main+0x2b6>
lea    0xbaa(%rip),%rdi
call   10d0 <perror@plt>
jmp    14a6 <main+0x2b6>
lea    0xb11(%rip),%rdi
call   1030 <remove@plt>
lea    0xb28(%rip),%rdi
call   1030 <remove@plt>
lea    0xb3f(%rip),%rdi
call   1030 <remove@plt>
lea    0xad8(%rip),%rdi
call   1030 <remove@plt>
lea    0xabf(%rip),%rdi
call   1030 <remove@plt>
xor    %r12d,%r12d
jmp    14a6 <main+0x2b6>
lea    0xb9b(%rip),%rdi
call   10d0 <perror@plt>
jmp    14a6 <main+0x2b6>
lea    0xb9b(%rip),%rdi
call   10d0 <perror@plt>
jmp    14a6 <main+0x2b6>

LLVM IR (-O0):
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IROTH = global i32 0
@S_IXOTH = global i32 0
@.str = [5 x i8] c"root\00"
@.str.1 = [8 x i8] c"mkdir 1\00"
@.str.2 = [13 x i8] c"root/testdir\00"
@.str.3 = [8 x i8] c"mkdir 2\00"
@.str.4 = [19 x i8] c"root/testdir/file1\00"
@.str.5 = [8 x i8] c"creat 1\00"
@.str.6 = [8 x i8] c"close 1\00"
@.str.7 = [19 x i8] c"root/testdir/file2\00"
@.str.8 = [8 x i8] c"creat 2\00"
@.str.9 = [8 x i8] c"close 2\00"
@.str.10 = [18 x i8] c"root/testdir/dir3\00"
@.str.11 = [8 x i8] c"mkdir 3\00"
@.str.12 = [20 x i8] c"getdents: setup ok\0A\00"
@O_RDONLY = global i32 0
@O_DIRECTORY = global i32 0
@.str.13 = [7 x i8] c"open 1\00"
@.str.14 = [11 x i8] c"getdents32\00"
@.str.15 = [7 x i8] c"open 2\00"
@.str.16 = [11 x i8] c"getdents64\00"
@.str.17 = [23 x i8] c"getdents64 before fork\00"
@stdout = global i32 0
@.str.18 = [5 x i8] c"fork\00"
@.str.19 = [18 x i8] c"parent getdents64\00"
@.str.20 = [17 x i8] c"child getdents64\00"
@stderr = global i32 0
@.str.21 = [27 x i8] c"%s: handle empty too soon\0A\00"
@.str.22 = [17 x i8] c"close after fork\00"
@.str.23 = [8 x i8] c"waitpid\00"
@.str.24 = [17 x i8] c"waitpid: got %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
store i32 0, i32* %1
%9 = load i32, i32* @S_IRWXU
%10 = load i32, i32* @S_IRWXG
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IROTH
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IXOTH
%15 = or i32 %13, %14
store i32 %15, i32* %5
%16 = load i32, i32* %5
%17 = call i32 @mkdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %16)
store i32 %17, i32* %2
%18 = load i32, i32* %2
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %0
%21 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %191
22: ; preds = %0
%23 = load i32, i32* %5
%24 = call i32 @mkdir(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %23)
store i32 %24, i32* %2
%25 = load i32, i32* %2
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %191
29: ; preds = %22
%30 = load i32, i32* %5
%31 = call i32 @creat(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %30)
store i32 %31, i32* %2
%32 = load i32, i32* %2
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %29
%35 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %191
36: ; preds = %29
%37 = load i32, i32* %2
%38 = call i32 @close(i32 %37)
store i32 %38, i32* %2
%39 = load i32, i32* %2
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %36
%42 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %1
br label %191
43: ; preds = %36
%44 = load i32, i32* %5
%45 = call i32 @creat(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0), i32 %44)
store i32 %45, i32* %2
%46 = load i32, i32* %2
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %43
%49 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0))
store i32 1, i32* %1
br label %191
50: ; preds = %43
%51 = load i32, i32* %2
%52 = call i32 @close(i32 %51)
store i32 %52, i32* %2
%53 = load i32, i32* %2
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %50
%56 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0))
store i32 1, i32* %1
br label %191
57: ; preds = %50
%58 = load i32, i32* %5
%59 = call i32 @mkdir(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.10, i64 0, i64 0), i32 %58)
store i32 %59, i32* %2
%60 = load i32, i32* %2
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %57
%63 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.11, i64 0, i64 0))
store i32 1, i32* %1
br label %191
64: ; preds = %57
%65 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.12, i64 0, i64 0))
%66 = load i32, i32* @O_RDONLY
%67 = load i32, i32* @O_DIRECTORY
%68 = or i32 %66, %67
%69 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %68)
store i32 %69, i32* %3
%70 = load i32, i32* %3
%71 = icmp slt i32 %70, 0
br i1 %71, label %72, label %74
72: ; preds = %64
%73 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.13, i64 0, i64 0))
store i32 1, i32* %1
br label %191
74: ; preds = %64
br label %75
75: ; preds = %82, %74
%76 = load i32, i32* %3
%77 = call i32 @do_getdents(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.14, i64 0, i64 0), i32 %76, i32 512)
store i32 %77, i32* %4
%78 = load i32, i32* %4
%79 = icmp slt i32 %78, 0
br i1 %79, label %80, label %81
80: ; preds = %75
store i32 1, i32* %1
br label %191
81: ; preds = %75
br label %82
82: ; preds = %81
%83 = load i32, i32* %4
%84 = icmp sgt i32 %83, 0
br i1 %84, label %75, label %85
85: ; preds = %82
%86 = load i32, i32* %3
%87 = call i32 @close(i32 %86)
store i32 %87, i32* %2
%88 = load i32, i32* %2
%89 = icmp ne i32 %88, 0
br i1 %89, label %90, label %92
90: ; preds = %85
%91 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %1
br label %191
92: ; preds = %85
%93 = load i32, i32* @O_RDONLY
%94 = load i32, i32* @O_DIRECTORY
%95 = or i32 %93, %94
%96 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %95)
store i32 %96, i32* %3
%97 = load i32, i32* %3
%98 = icmp slt i32 %97, 0
br i1 %98, label %99, label %101
99: ; preds = %92
%100 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.15, i64 0, i64 0))
store i32 1, i32* %1
br label %191
101: ; preds = %92
br label %102
102: ; preds = %109, %101
%103 = load i32, i32* %3
%104 = call i32 @do_getdents64(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.16, i64 0, i64 0), i32 %103, i32 512)
store i32 %104, i32* %4
%105 = load i32, i32* %4
%106 = icmp slt i32 %105, 0
br i1 %106, label %107, label %108
107: ; preds = %102
store i32 1, i32* %1
br label %191
108: ; preds = %102
br label %109
109: ; preds = %108
%110 = load i32, i32* %4
%111 = icmp sgt i32 %110, 0
br i1 %111, label %102, label %112
112: ; preds = %109
%113 = load i32, i32* %3
%114 = call i32 @close(i32 %113)
store i32 %114, i32* %2
%115 = load i32, i32* %2
%116 = icmp ne i32 %115, 0
br i1 %116, label %117, label %119
117: ; preds = %112
%118 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0))
store i32 1, i32* %1
br label %191
119: ; preds = %112
%120 = load i32, i32* @O_RDONLY
%121 = load i32, i32* @O_DIRECTORY
%122 = or i32 %120, %121
%123 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %122)
store i32 %123, i32* %3
%124 = load i32, i32* %3
%125 = call i32 @do_getdents64(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.17, i64 0, i64 0), i32 %124, i32 64)
store i32 %125, i32* %4
%126 = load i32, i32* %4
%127 = icmp slt i32 %126, 0
br i1 %127, label %128, label %129
128: ; preds = %119
store i32 1, i32* %1
br label %191
129: ; preds = %119
%130 = load i32, i32* @stdout
%131 = call i32 @fflush(i32 %130)
%132 = call i64 (...) @fork()
store i64 %132, i64* %6
%133 = load i64, i64* %6
%134 = icmp slt i64 %133, 0
br i1 %134, label %135, label %137
135: ; preds = %129
%136 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.18, i64 0, i64 0))
store i32 1, i32* %1
br label %191
137: ; preds = %129
%138 = load i64, i64* %6
%139 = icmp ne i64 %138, 0
%140 = zext i1 %139 to i64
%141 = select i1 %139, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.19, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.20, i64 0, i64 0)
store i8* %141, i8** %7
%142 = load i8*, i8** %7
%143 = load i32, i32* %3
%144 = call i32 @do_getdents64(i8* %142, i32 %143, i32 64)
store i32 %144, i32* %4
%145 = load i32, i32* %4
%146 = icmp slt i32 %145, 0
br i1 %146, label %147, label %148
147: ; preds = %137
store i32 1, i32* %1
br label %191
148: ; preds = %137
%149 = load i32, i32* %4
%150 = icmp eq i32 %149, 0
br i1 %150, label %151, label %155
151: ; preds = %148
%152 = load i32, i32* @stderr
%153 = load i8*, i8** %7
%154 = call i32 (i32, i8*, ...) @fprintf(i32 %152, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.21, i64 0, i64 0), i8* %153)
store i32 1, i32* %1
br label %191
155: ; preds = %148
%156 = load i32, i32* %3
%157 = call i32 @close(i32 %156)
store i32 %157, i32* %2
%158 = load i32, i32* %2
%159 = icmp ne i32 %158, 0
br i1 %159, label %160, label %162
160: ; preds = %155
%161 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.22, i64 0, i64 0))
store i32 1, i32* %1
br label %191
162: ; preds = %155
%163 = load i64, i64* %6
%164 = icmp ne i64 %163, 0
br i1 %164, label %165, label %190
165: ; preds = %162
%166 = load i64, i64* %6
%167 = call i32 @waitpid(i64 %166, i32* %8, i32 0)
store i32 %167, i32* %2
%168 = load i32, i32* %2
%169 = icmp slt i32 %168, 0
br i1 %169, label %170, label %172
170: ; preds = %165
%171 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.23, i64 0, i64 0))
store i32 1, i32* %1
br label %191
172: ; preds = %165
%173 = load i32, i32* %8
%174 = call i32 @WIFEXITED(i32 %173)
%175 = icmp ne i32 %174, 0
br i1 %175, label %176, label %180
176: ; preds = %172
%177 = load i32, i32* %8
%178 = call i64 @WEXITSTATUS(i32 %177)
%179 = icmp ne i64 %178, 0
br i1 %179, label %180, label %184
180: ; preds = %176, %172
%181 = load i32, i32* @stderr
%182 = load i32, i32* %8
%183 = call i32 (i32, i8*, ...) @fprintf(i32 %181, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.24, i64 0, i64 0), i32 %182)
store i32 1, i32* %1
br label %191
184: ; preds = %176
%185 = call i32 @remove(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%186 = call i32 @remove(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0))
%187 = call i32 @remove(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.10, i64 0, i64 0))
%188 = call i32 @remove(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%189 = call i32 @remove(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %190
190: ; preds = %184, %162
store i32 0, i32* %1
br label %191
191: ; preds = %190, %180, %170, %160, %151, %147, %135, %128, %117, %107, %99, %90, %80, %72, %62, %55, %48, %41, %34, %27, %20
%192 = load i32, i32* %1
ret i32 %192
}

LLVM IR (-O2):
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IROTH = global i32 0
@S_IXOTH = global i32 0
@.str = [5 x i8] c"root\00"
@.str.1 = [8 x i8] c"mkdir 1\00"
@.str.2 = [13 x i8] c"root/testdir\00"
@.str.3 = [8 x i8] c"mkdir 2\00"
@.str.4 = [19 x i8] c"root/testdir/file1\00"
@.str.5 = [8 x i8] c"creat 1\00"
@.str.6 = [8 x i8] c"close 1\00"
@.str.7 = [19 x i8] c"root/testdir/file2\00"
@.str.8 = [8 x i8] c"creat 2\00"
@.str.9 = [8 x i8] c"close 2\00"
@.str.10 = [18 x i8] c"root/testdir/dir3\00"
@.str.11 = [8 x i8] c"mkdir 3\00"
@O_RDONLY = global i32 0
@O_DIRECTORY = global i32 0
@.str.13 = [7 x i8] c"open 1\00"
@.str.14 = [11 x i8] c"getdents32\00"
@.str.15 = [7 x i8] c"open 2\00"
@.str.16 = [11 x i8] c"getdents64\00"
@.str.17 = [23 x i8] c"getdents64 before fork\00"
@stdout = global i32 0
@.str.18 = [5 x i8] c"fork\00"
@.str.19 = [18 x i8] c"parent getdents64\00"
@.str.20 = [17 x i8] c"child getdents64\00"
@stderr = global i32 0
@.str.21 = [27 x i8] c"%s: handle empty too soon\0A\00"
@.str.22 = [17 x i8] c"close after fork\00"
@.str.23 = [8 x i8] c"waitpid\00"
@.str.24 = [17 x i8] c"waitpid: got %d\0A\00"
@str = [19 x i8] c"getdents: setup ok\00"
define i32 @main() {
%1 = alloca i32
%2 = load i32, i32* @S_IRWXU
%3 = load i32, i32* @S_IRWXG
%4 = or i32 %3, %2
%5 = load i32, i32* @S_IROTH
%6 = or i32 %4, %5
%7 = load i32, i32* @S_IXOTH
%8 = or i32 %6, %7
%9 = tail call i32 @mkdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = icmp eq i32 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %0
%12 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %135
13: ; preds = %0
%14 = tail call i32 @mkdir(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %8)
%15 = icmp eq i32 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %135
18: ; preds = %13
%19 = tail call i32 @creat(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %8)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
br label %135
23: ; preds = %18
%24 = tail call i32 @close(i32 %19)
%25 = icmp eq i32 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
br label %135
28: ; preds = %23
%29 = tail call i32 @creat(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0), i32 %8)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0))
br label %135
33: ; preds = %28
%34 = tail call i32 @close(i32 %29)
%35 = icmp eq i32 %34, 0
br i1 %35, label %38, label %36
36: ; preds = %33
%37 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0))
br label %135
38: ; preds = %33
%39 = tail call i32 @mkdir(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.10, i64 0, i64 0), i32 %8)
%40 = icmp eq i32 %39, 0
br i1 %40, label %43, label %41
41: ; preds = %38
%42 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.11, i64 0, i64 0))
br label %135
43: ; preds = %38
%44 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%45 = load i32, i32* @O_RDONLY
%46 = load i32, i32* @O_DIRECTORY
%47 = or i32 %46, %45
%48 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %47)
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %43
%51 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.13, i64 0, i64 0))
br label %135
52: ; preds = %43, %55
%53 = tail call i32 @do_getdents(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.14, i64 0, i64 0), i32 %48, i32 512)
%54 = icmp slt i32 %53, 0
br i1 %54, label %135, label %55
55: ; preds = %52
%56 = icmp eq i32 %53, 0
br i1 %56, label %57, label %52
57: ; preds = %55
%58 = tail call i32 @close(i32 %48)
%59 = icmp eq i32 %58, 0
br i1 %59, label %62, label %60
60: ; preds = %57
%61 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
br label %135
62: ; preds = %57
%63 = load i32, i32* @O_RDONLY
%64 = load i32, i32* @O_DIRECTORY
%65 = or i32 %64, %63
%66 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %65)
%67 = icmp slt i32 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %62
%69 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.15, i64 0, i64 0))
br label %135
70: ; preds = %62, %73
%71 = tail call i32 @do_getdents64(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.16, i64 0, i64 0), i32 %66, i32 512)
%72 = icmp slt i32 %71, 0
br i1 %72, label %135, label %73
73: ; preds = %70
%74 = icmp eq i32 %71, 0
br i1 %74, label %75, label %70
75: ; preds = %73
%76 = tail call i32 @close(i32 %66)
%77 = icmp eq i32 %76, 0
br i1 %77, label %80, label %78
78: ; preds = %75
%79 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0))
br label %135
80: ; preds = %75
%81 = load i32, i32* @O_RDONLY
%82 = load i32, i32* @O_DIRECTORY
%83 = or i32 %82, %81
%84 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %83)
%85 = tail call i32 @do_getdents64(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.17, i64 0, i64 0), i32 %84, i32 64)
%86 = icmp slt i32 %85, 0
br i1 %86, label %135, label %87
87: ; preds = %80
%88 = load i32, i32* @stdout
%89 = tail call i32 @fflush(i32 %88)
%90 = tail call i64 (...) @fork()
%91 = icmp slt i64 %90, 0
br i1 %91, label %92, label %94
92: ; preds = %87
%93 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.18, i64 0, i64 0))
br label %135
94: ; preds = %87
%95 = icmp eq i64 %90, 0
%96 = select i1 %95, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.20, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.19, i64 0, i64 0)
%97 = tail call i32 @do_getdents64(i8* %96, i32 %84, i32 64)
%98 = icmp slt i32 %97, 0
br i1 %98, label %135, label %99
99: ; preds = %94
%100 = icmp eq i32 %97, 0
br i1 %100, label %101, label %104
101: ; preds = %99
%102 = load i32, i32* @stderr
%103 = tail call i32 (i32, i8*, ...) @fprintf(i32 %102, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.21, i64 0, i64 0), i8* %96)
br label %135
104: ; preds = %99
%105 = tail call i32 @close(i32 %84)
%106 = icmp eq i32 %105, 0
br i1 %106, label %109, label %107
107: ; preds = %104
%108 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.22, i64 0, i64 0))
br label %135
109: ; preds = %104
br i1 %95, label %135, label %110
110: ; preds = %109
%111 = bitcast i32* %1 to i8*
%112 = call i32 @waitpid(i64 %90, i32* %1, i32 0)
%113 = icmp slt i32 %112, 0
br i1 %113, label %114, label %116
114: ; preds = %110
%115 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.23, i64 0, i64 0))
br label %128
116: ; preds = %110
%117 = load i32, i32* %1
%118 = call i32 @WIFEXITED(i32 %117)
%119 = icmp eq i32 %118, 0
br i1 %119, label %124, label %120
120: ; preds = %116
%121 = load i32, i32* %1
%122 = call i64 @WEXITSTATUS(i32 %121)
%123 = icmp eq i64 %122, 0
br i1 %123, label %129, label %124
124: ; preds = %120, %116
%125 = load i32, i32* @stderr
%126 = load i32, i32* %1
%127 = call i32 (i32, i8*, ...) @fprintf(i32 %125, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.24, i64 0, i64 0), i32 %126)
br label %128
128: ; preds = %124, %114
br label %135
129: ; preds = %120
%130 = call i32 @remove(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%131 = call i32 @remove(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0))
%132 = call i32 @remove(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.10, i64 0, i64 0))
%133 = call i32 @remove(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%134 = call i32 @remove(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %135
135: ; preds = %52, %70, %109, %129, %128, %92, %94, %107, %101, %80, %78, %68, %60, %50, %41, %36, %31, %26, %21, %16, %11
%136 = phi i32 [ 1, %11 ], [ 1, %16 ], [ 1, %21 ], [ 1, %26 ], [ 1, %31 ], [ 1, %36 ], [ 1, %41 ], [ 1, %50 ], [ 1, %60 ], [ 1, %68 ], [ 1, %78 ], [ 1, %80 ], [ 1, %92 ], [ 1, %101 ], [ 1, %107 ], [ 1, %94 ], [ 1, %128 ], [ 0, %129 ], [ 0, %109 ], [ 1, %70 ], [ 1, %52 ]
ret i32 %136
}