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

/* Variables and functions */
scalar_t__ asprintf(char **, char *, char const *, char *);
int /*<<< orphan*/ die(char *, ...);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
unsigned int strtoul(char *, char **, int);
int dirent(DIR *parent, const char *parent_src, const char *parent_dst,
           char **ent_src, char **ent_dst, unsigned *ent_num, int is_top) {
  char *endp;
  struct dirent *ent;
again:
  errno = 0;
  ent = readdir(parent);
  if (ent == NULL) {
    if (errno)
      die("Error reading directory %s", parent_src);
    else
      return 0;
  }
  *ent_num = strtoul(ent->d_name, &endp, 10);
  if (*endp != 0) {
    if (!strcmp(".", ent->d_name) || !strcmp("..", ent->d_name))
      goto again;
    if (is_top) {
      if (strcmp("index.lev1", ent->d_name))
        fprintf(stderr, "Warning: ignoring %s/%s\n", parent_src, ent->d_name);
      goto again;
    } else {
      die("Found unexpected file %s/%s", parent_src, ent->d_name);
    }
  }
  if (asprintf(ent_src, "%s/%s", parent_src, ent->d_name) < 0)
    die("malloc failure");
  if (asprintf(ent_dst, "%s/%s", parent_dst, ent->d_name) < 0)
    die("malloc failure");
  return 1;
}


x86 assembly (clang, -O0):
<dirent>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0x10(%rbp),%eax
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
mov    0x2e4e(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
call   1050 <readdir@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    11e2 <dirent+0x82>
mov    0x2e28(%rip),%rax
cmpq   $0x0,(%rax)
je     11d1 <dirent+0x71>
mov    -0x18(%rbp),%rsi
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1070 <die@plt>
jmp    11dd <dirent+0x7d>
movl   $0x0,-0x4(%rbp)
jmp    1325 <dirent+0x1c5>
jmp    11e2 <dirent+0x82>
mov    -0x48(%rbp),%rax
mov    (%rax),%rdi
lea    -0x40(%rbp),%rsi
mov    $0xa,%edx
call   1080 <strtoul@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x40(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12b8 <dirent+0x158>
mov    -0x48(%rbp),%rax
mov    (%rax),%rsi
lea    0xdff(%rip),%rdi
call   1030 <strcmp@plt>
cmp    $0x0,%rax
je     1249 <dirent+0xe9>
mov    -0x48(%rbp),%rax
mov    (%rax),%rsi
lea    0xde1(%rip),%rdi
call   1030 <strcmp@plt>
cmp    $0x0,%rax
jne    124e <dirent+0xee>
jmp    1183 <dirent+0x23>
cmpl   $0x0,0x10(%rbp)
je     129a <dirent+0x13a>
mov    -0x48(%rbp),%rax
mov    (%rax),%rsi
lea    0xdb8(%rip),%rdi
call   1030 <strcmp@plt>
cmp    $0x0,%rax
je     1295 <dirent+0x135>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
mov    -0x48(%rbp),%rax
mov    (%rax),%rcx
lea    0xd99(%rip),%rsi
call   1040 <fprintf@plt>
jmp    1183 <dirent+0x23>
mov    -0x18(%rbp),%rsi
mov    -0x48(%rbp),%rax
mov    (%rax),%rdx
lea    0xd96(%rip),%rdi
mov    $0x0,%al
call   1070 <die@plt>
jmp    12b8 <dirent+0x158>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x48(%rbp),%rax
mov    (%rax),%rcx
lea    0xd8a(%rip),%rsi
call   1060 <asprintf@plt>
cmp    $0x0,%rax
jge    12eb <dirent+0x18b>
lea    0xd7a(%rip),%rdi
mov    $0x0,%al
call   1070 <die@plt>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x48(%rbp),%rax
mov    (%rax),%rcx
lea    0xd57(%rip),%rsi
call   1060 <asprintf@plt>
cmp    $0x0,%rax
jge    131e <dirent+0x1be>
lea    0xd47(%rip),%rdi
mov    $0x0,%al
call   1070 <die@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirent>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r9,%rbp
mov    %r8,0x10(%rsp)
mov    %rcx,0x8(%rsp)
mov    %rdx,0x18(%rsp)
mov    %rsi,%r13
mov    %rdi,%r14
mov    0x2e4b(%rip),%r15
lea    0x20(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movq   $0x0,(%r15)
mov    %r14,%rdi
call   1050 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11d2 <dirent+0x72>
cmpq   $0x0,(%r15)
je     1259 <dirent+0xf9>
lea    0xe38(%rip),%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1070 <die@plt>
mov    (%rbx),%rdi
mov    %r12,%rsi
mov    $0xa,%edx
call   1080 <strtoul@plt>
mov    %eax,0x0(%rbp)
mov    0x20(%rsp),%rax
cmpb   $0x0,(%rax)
je     126e <dirent+0x10e>
mov    (%rbx),%rsi
lea    0xe23(%rip),%rdi
call   1030 <strcmp@plt>
test   %rax,%rax
je     11a0 <dirent+0x40>
mov    (%rbx),%rsi
lea    0xe0e(%rip),%rdi
call   1030 <strcmp@plt>
test   %rax,%rax
je     11a0 <dirent+0x40>
mov    (%rbx),%rdx
cmpl   $0x0,0x60(%rsp)
je     125d <dirent+0xfd>
lea    0xdf6(%rip),%rdi
mov    %rdx,%rsi
call   1030 <strcmp@plt>
test   %rax,%rax
je     11a0 <dirent+0x40>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rcx
lea    0xddd(%rip),%rsi
mov    %r13,%rdx
call   1040 <fprintf@plt>
jmp    11a0 <dirent+0x40>
xor    %ebx,%ebx
jmp    12c9 <dirent+0x169>
lea    0xdde(%rip),%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1070 <die@plt>
mov    (%rbx),%rcx
lea    0xde0(%rip),%rsi
mov    0x8(%rsp),%rdi
mov    %r13,%rdx
call   1060 <asprintf@plt>
test   %rax,%rax
jns    1298 <dirent+0x138>
lea    0xdcd(%rip),%rdi
xor    %eax,%eax
call   1070 <die@plt>
mov    (%rbx),%rcx
lea    0xdb6(%rip),%rsi
mov    0x10(%rsp),%rdi
mov    0x18(%rsp),%rdx
call   1060 <asprintf@plt>
mov    $0x1,%ebx
test   %rax,%rax
jns    12c9 <dirent+0x169>
lea    0xd9c(%rip),%rdi
xor    %eax,%eax
call   1070 <die@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@errno = global i64 0
@.str = [27 x i8] c"Error reading directory %s\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [11 x i8] c"index.lev1\00"
@stderr = global i32 0
@.str.4 = [25 x i8] c"Warning: ignoring %s/%s\0A\00"
@.str.5 = [28 x i8] c"Found unexpected file %s/%s\00"
@.str.6 = [6 x i8] c"%s/%s\00"
@.str.7 = [15 x i8] c"malloc failure\00"
define i32 @dirent(i32* %0, i8* %1, i8* %2, i8** %3, i8** %4, i32* %5, i32 %6) {
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8**
%13 = alloca i8**
%14 = alloca i32*
%15 = alloca i32
%16 = alloca i8*
%17 = alloca %struct.dirent*
store i32* %0, i32** %9
store i8* %1, i8** %10
store i8* %2, i8** %11
store i8** %3, i8*** %12
store i8** %4, i8*** %13
store i32* %5, i32** %14
store i32 %6, i32* %15
br label %18
18: ; preds = %70, %53, %7
store i64 0, i64* @errno
%19 = load i32*, i32** %9
%20 = call %struct.dirent* @readdir(i32* %19)
store %struct.dirent* %20, %struct.dirent** %17
%21 = load %struct.dirent*, %struct.dirent** %17
%22 = icmp eq %struct.dirent* %21, null
br i1 %22, label %23, label %31
23: ; preds = %18
%24 = load i64, i64* @errno
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = load i8*, i8** %10
%28 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %27)
br label %30
29: ; preds = %23
store i32 0, i32* %8
br label %99
30: ; preds = %26
br label %31
31: ; preds = %30, %18
%32 = load %struct.dirent*, %struct.dirent** %17
%33 = getelementptr inbounds %struct.dirent, %struct.dirent* %32, i32 0, i32 0
%34 = load i8*, i8** %33
%35 = call i32 @strtoul(i8* %34, i8** %16, i32 10)
%36 = load i32*, i32** %14
store i32 %35, i32* %36
%37 = load i8*, i8** %16
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %78
41: ; preds = %31
%42 = load %struct.dirent*, %struct.dirent** %17
%43 = getelementptr inbounds %struct.dirent, %struct.dirent* %42, i32 0, i32 0
%44 = load i8*, i8** %43
%45 = call i64 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %53
47: ; preds = %41
%48 = load %struct.dirent*, %struct.dirent** %17
%49 = getelementptr inbounds %struct.dirent, %struct.dirent* %48, i32 0, i32 0
%50 = load i8*, i8** %49
%51 = call i64 @strcmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %50)
%52 = icmp ne i64 %51, 0
br i1 %52, label %54, label %53
53: ; preds = %47, %41
br label %18
54: ; preds = %47
%55 = load i32, i32* %15
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %71
57: ; preds = %54
%58 = load %struct.dirent*, %struct.dirent** %17
%59 = getelementptr inbounds %struct.dirent, %struct.dirent* %58, i32 0, i32 0
%60 = load i8*, i8** %59
%61 = call i64 @strcmp(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %60)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %70
63: ; preds = %57
%64 = load i32, i32* @stderr
%65 = load i8*, i8** %10
%66 = load %struct.dirent*, %struct.dirent** %17
%67 = getelementptr inbounds %struct.dirent, %struct.dirent* %66, i32 0, i32 0
%68 = load i8*, i8** %67
%69 = call i32 @fprintf(i32 %64, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %65, i8* %68)
br label %70
70: ; preds = %63, %57
br label %18
71: ; preds = %54
%72 = load i8*, i8** %10
%73 = load %struct.dirent*, %struct.dirent** %17
%74 = getelementptr inbounds %struct.dirent, %struct.dirent* %73, i32 0, i32 0
%75 = load i8*, i8** %74
%76 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %72, i8* %75)
br label %77
77: ; preds = %71
br label %78
78: ; preds = %77, %31
%79 = load i8**, i8*** %12
%80 = load i8*, i8** %10
%81 = load %struct.dirent*, %struct.dirent** %17
%82 = getelementptr inbounds %struct.dirent, %struct.dirent* %81, i32 0, i32 0
%83 = load i8*, i8** %82
%84 = call i64 @asprintf(i8** %79, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i8* %80, i8* %83)
%85 = icmp slt i64 %84, 0
br i1 %85, label %86, label %88
86: ; preds = %78
%87 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0))
br label %88
88: ; preds = %86, %78
%89 = load i8**, i8*** %13
%90 = load i8*, i8** %11
%91 = load %struct.dirent*, %struct.dirent** %17
%92 = getelementptr inbounds %struct.dirent, %struct.dirent* %91, i32 0, i32 0
%93 = load i8*, i8** %92
%94 = call i64 @asprintf(i8** %89, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i8* %90, i8* %93)
%95 = icmp slt i64 %94, 0
br i1 %95, label %96, label %98
96: ; preds = %88
%97 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0))
br label %98
98: ; preds = %96, %88
store i32 1, i32* %8
br label %99
99: ; preds = %98, %29
%100 = load i32, i32* %8
ret i32 %100
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@errno = global i64 0
@.str = [27 x i8] c"Error reading directory %s\00"
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [11 x i8] c"index.lev1\00"
@stderr = global i32 0
@.str.4 = [25 x i8] c"Warning: ignoring %s/%s\0A\00"
@.str.5 = [28 x i8] c"Found unexpected file %s/%s\00"
@.str.6 = [6 x i8] c"%s/%s\00"
@.str.7 = [15 x i8] c"malloc failure\00"
define i32 @dirent(i32* %0, i8* %1, i8* %2, i8** %3, i8** %4, i32* nocapture writeonly %5, i32 %6) {
%8 = alloca i8*
%9 = bitcast i8** %8 to i8*
%10 = icmp eq i32 %6, 0
br label %11
11: ; preds = %39, %7
store i64 0, i64* @errno
%12 = tail call %struct.dirent* @readdir(i32* %0)
%13 = icmp eq %struct.dirent* %12, null
br i1 %13, label %14, label %19
14: ; preds = %11
%15 = load i64, i64* @errno
%16 = icmp eq i64 %15, 0
br i1 %16, label %58, label %17
17: ; preds = %14
%18 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %19
19: ; preds = %17, %11
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %12, i64 0, i32 0
%21 = load i8*, i8** %20
%22 = call i32 @strtoul(i8* %21, i8** %8, i32 10)
store i32 %22, i32* %5
%23 = load i8*, i8** %8
%24 = load i8, i8* %23
%25 = icmp eq i8 %24, 0
br i1 %25, label %46, label %26
26: ; preds = %19
%27 = load i8*, i8** %20
%28 = tail call i64 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %39, label %30
30: ; preds = %26
%31 = load i8*, i8** %20
%32 = tail call i64 @strcmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %39, label %34
34: ; preds = %30
%35 = load i8*, i8** %20
br i1 %10, label %44, label %36
36: ; preds = %34
%37 = tail call i64 @strcmp(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %35)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %36, %40, %26, %30
br label %11
40: ; preds = %36
%41 = load i32, i32* @stderr
%42 = load i8*, i8** %20
%43 = tail call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %1, i8* %42)
br label %39
44: ; preds = %34
%45 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %1, i8* %35)
br label %46
46: ; preds = %19, %44
%47 = load i8*, i8** %20
%48 = tail call i64 @asprintf(i8** %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i8* %1, i8* %47)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0))
br label %52
52: ; preds = %50, %46
%53 = load i8*, i8** %20
%54 = tail call i64 @asprintf(i8** %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i8* %2, i8* %53)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %52
%57 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0))
br label %58
58: ; preds = %14, %52, %56
%59 = phi i32 [ 1, %56 ], [ 1, %52 ], [ 0, %14 ]
ret i32 %59
}