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

/* Variables and functions */
int EBADF;
scalar_t__ closedir(int /*<<< orphan*/ *);
int dirfd(int /*<<< orphan*/ *);
int errno;
int /*<<< orphan*/ *opendir(char *);
int parse_fd(int /*<<< orphan*/);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ strcmp(int /*<<< orphan*/, char *);
void stub1(void *, int);
int fdwalk(void (*func)(void *, int), void *opaque) {
  DIR *dir = opendir("/proc/self/fd");
  if (!dir) {
    return -1;
  }
  int opendirfd = dirfd(dir);
  int parse_errors = 0;
  struct dirent *ent;
  while (errno = 0, (ent = readdir(dir)) != NULL) {
    if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
      continue;
    }
    int fd = parse_fd(ent->d_name);
    if (fd < 0) {
      parse_errors = 1;
      continue;
    }
    if (fd != opendirfd) {
      (*func)(opaque, fd);
    }
  }
  int saved_errno = errno;
  if (closedir(dir) < 0) {
    if (!saved_errno) {
      return -1;
    }
  }
  if (saved_errno) {
    errno = saved_errno;
    return -1;
  } else if (parse_errors) {
    errno = EBADF;
    return -1;
  } else {
    return 0;
  }
}


x86 assembly (clang, -O0):
<fdwalk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    0xe89(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1197 <fdwalk+0x37>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d8 <fdwalk+0x178>
mov    -0x20(%rbp),%rdi
call   1070 <dirfd@plt>
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    0x2e2f(%rip),%rax
movl   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     124d <fdwalk+0xed>
mov    -0x30(%rbp),%rax
mov    (%rax),%edi
lea    0xe34(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     1206 <fdwalk+0xa6>
mov    -0x30(%rbp),%rax
mov    (%rax),%edi
lea    0xe17(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    120b <fdwalk+0xab>
jmp    11aa <fdwalk+0x4a>
mov    -0x30(%rbp),%rax
mov    (%rax),%edi
call   1050 <parse_fd@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    122f <fdwalk+0xcf>
movl   $0x1,-0x28(%rbp)
jmp    11aa <fdwalk+0x4a>
mov    -0x34(%rbp),%eax
cmp    -0x24(%rbp),%eax
je     1248 <fdwalk+0xe8>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   *%rax
jmp    11aa <fdwalk+0x4a>
mov    0x2d8c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <closedir@plt>
cmp    $0x0,%rax
jge    1287 <fdwalk+0x127>
cmpl   $0x0,-0x38(%rbp)
jne    1282 <fdwalk+0x122>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d8 <fdwalk+0x178>
jmp    1287 <fdwalk+0x127>
cmpl   $0x0,-0x38(%rbp)
je     12a9 <fdwalk+0x149>
mov    -0x38(%rbp),%ecx
mov    0x2d45(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12d8 <fdwalk+0x178>
cmpl   $0x0,-0x28(%rbp)
je     12d1 <fdwalk+0x171>
mov    0x2d1e(%rip),%rax
mov    (%rax),%ecx
mov    0x2d1d(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12d8 <fdwalk+0x178>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fdwalk>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r12
mov    %rdi,%rbp
lea    0xe85(%rip),%rdi
call   1030 <opendir@plt>
mov    $0xffffffff,%r13d
test   %rax,%rax
je     1212 <fdwalk+0xb2>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <dirfd@plt>
mov    %eax,0x4(%rsp)
mov    0x2e3b(%rip),%r15
movl   $0x0,(%r15)
mov    %rbx,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     11dd <fdwalk+0x7d>
mov    %rax,%r14
mov    %rbp,0x8(%rsp)
mov    %r12,0x10(%rsp)
movl   $0x0,(%rsp)
lea    0xe3b(%rip),%rbp
lea    0xe33(%rip),%r12
jmp    1247 <fdwalk+0xe7>
movl   $0x0,(%rsp)
mov    (%r15),%ebp
mov    %rbx,%rdi
call   1040 <closedir@plt>
test   %rax,%rax
jns    11f8 <fdwalk+0x98>
test   %ebp,%ebp
je     1212 <fdwalk+0xb2>
test   %ebp,%ebp
jne    120f <fdwalk+0xaf>
cmpl   $0x0,(%rsp)
je     1286 <fdwalk+0x126>
mov    0x2dcb(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,(%r15)
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
movl   $0x1,(%rsp)
nopl   0x0(%rax,%rax,1)
movl   $0x0,(%r15)
mov    %rbx,%rdi
call   1080 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     11e4 <fdwalk+0x84>
mov    (%r14),%edi
mov    %rbp,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     1230 <fdwalk+0xd0>
mov    (%r14),%edi
mov    %r12,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     1230 <fdwalk+0xd0>
mov    (%r14),%edi
call   1050 <parse_fd@plt>
test   %eax,%eax
js     1224 <fdwalk+0xc4>
cmp    0x4(%rsp),%eax
je     1230 <fdwalk+0xd0>
mov    0x10(%rsp),%rdi
mov    %eax,%esi
call   *0x8(%rsp)
jmp    1230 <fdwalk+0xd0>
xor    %r13d,%r13d
jmp    1212 <fdwalk+0xb2>

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [14 x i8] c"/proc/self/fd\00"
@errno = global i32 0
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@EBADF = global i32 0
define i32 @fdwalk(void (i8*, i32)* %0, i8* %1) {
%3 = alloca i32
%4 = alloca void (i8*, i32)*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.dirent*
%10 = alloca i32
%11 = alloca i32
store void (i8*, i32)* %0, void (i8*, i32)** %4
store i8* %1, i8** %5
%12 = call i32* @opendir(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %6
%13 = load i32*, i32** %6
%14 = icmp ne i32* %13, null
br i1 %14, label %16, label %15
15: ; preds = %2
store i32 -1, i32* %3
br label %74
16: ; preds = %2
%17 = load i32*, i32** %6
%18 = call i32 @dirfd(i32* %17)
store i32 %18, i32* %7
store i32 0, i32* %8
br label %19
19: ; preds = %52, %43, %35, %16
store i32 0, i32* @errno
%20 = load i32*, i32** %6
%21 = call %struct.dirent* @readdir(i32* %20)
store %struct.dirent* %21, %struct.dirent** %9
%22 = icmp ne %struct.dirent* %21, null
br i1 %22, label %23, label %53
23: ; preds = %19
%24 = load %struct.dirent*, %struct.dirent** %9
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %24, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i64 @strcmp(i32 %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%28 = icmp eq i64 %27, 0
br i1 %28, label %35, label %29
29: ; preds = %23
%30 = load %struct.dirent*, %struct.dirent** %9
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i64 @strcmp(i32 %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %29, %23
br label %19
36: ; preds = %29
%37 = load %struct.dirent*, %struct.dirent** %9
%38 = getelementptr inbounds %struct.dirent, %struct.dirent* %37, i32 0, i32 0
%39 = load i32, i32* %38
%40 = call i32 @parse_fd(i32 %39)
store i32 %40, i32* %10
%41 = load i32, i32* %10
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %36
store i32 1, i32* %8
br label %19
44: ; preds = %36
%45 = load i32, i32* %10
%46 = load i32, i32* %7
%47 = icmp ne i32 %45, %46
br i1 %47, label %48, label %52
48: ; preds = %44
%49 = load void (i8*, i32)*, void (i8*, i32)** %4
%50 = load i8*, i8** %5
%51 = load i32, i32* %10
br label %52
52: ; preds = %48, %44
br label %19
53: ; preds = %19
%54 = load i32, i32* @errno
store i32 %54, i32* %11
%55 = load i32*, i32** %6
%56 = call i64 @closedir(i32* %55)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %63
58: ; preds = %53
%59 = load i32, i32* %11
%60 = icmp ne i32 %59, 0
br i1 %60, label %62, label %61
61: ; preds = %58
store i32 -1, i32* %3
br label %74
62: ; preds = %58
br label %63
63: ; preds = %62, %53
%64 = load i32, i32* %11
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %63
%67 = load i32, i32* %11
store i32 %67, i32* @errno
store i32 -1, i32* %3
br label %74
68: ; preds = %63
%69 = load i32, i32* %8
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %73
71: ; preds = %68
%72 = load i32, i32* @EBADF
store i32 %72, i32* @errno
store i32 -1, i32* %3
br label %74
73: ; preds = %68
store i32 0, i32* %3
br label %74
74: ; preds = %73, %71, %66, %61, %15
%75 = load i32, i32* %3
ret i32 %75
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [14 x i8] c"/proc/self/fd\00"
@errno = global i32 0
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@EBADF = global i32 0
define i32 @fdwalk(void (i8*, i32)* nocapture %0, i8* %1) {
%3 = tail call i32* @opendir(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %50, label %5
5: ; preds = %2
%6 = tail call i32 @dirfd(i32* %3)
store i32 0, i32* @errno
%7 = tail call %struct.dirent* @readdir(i32* %3)
%8 = icmp eq %struct.dirent* %7, null
br i1 %8, label %36, label %9
9: ; preds = %5, %32
%10 = phi %struct.dirent* [ %34, %32 ], [ %7, %5 ]
%11 = phi i32 [ %33, %32 ], [ 0, %5 ]
br label %12
12: ; preds = %9, %22
%13 = phi %struct.dirent* [ %10, %9 ], [ %23, %22 ]
%14 = getelementptr inbounds %struct.dirent, %struct.dirent* %13, i64 0, i32 0
%15 = load i32, i32* %14
%16 = tail call i64 @strcmp(i32 %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %12
%19 = load i32, i32* %14
%20 = tail call i64 @strcmp(i32 %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %18, %12
store i32 0, i32* @errno
%23 = tail call %struct.dirent* @readdir(i32* %3)
%24 = icmp eq %struct.dirent* %23, null
br i1 %24, label %36, label %12
25: ; preds = %18
%26 = load i32, i32* %14
%27 = tail call i32 @parse_fd(i32 %26)
%28 = icmp slt i32 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %25
%30 = icmp eq i32 %27, %6
br i1 %30, label %32, label %31
31: ; preds = %29
tail call void %0(i8* %1, i32 %27)
br label %32
32: ; preds = %29, %31, %25
%33 = phi i32 [ 1, %25 ], [ %11, %31 ], [ %11, %29 ]
store i32 0, i32* @errno
%34 = tail call %struct.dirent* @readdir(i32* %3)
%35 = icmp eq %struct.dirent* %34, null
br i1 %35, label %36, label %9
36: ; preds = %32, %22, %5
%37 = phi i32 [ 0, %5 ], [ %11, %22 ], [ %33, %32 ]
%38 = load i32, i32* @errno
%39 = tail call i64 @closedir(i32* %3)
%40 = icmp sgt i64 %39, -1
%41 = icmp ne i32 %38, 0
%42 = select i1 %40, i1 true, i1 %41
br i1 %42, label %43, label %50
43: ; preds = %36
br i1 %41, label %48, label %44
44: ; preds = %43
%45 = icmp eq i32 %37, 0
br i1 %45, label %50, label %46
46: ; preds = %44
%47 = load i32, i32* @EBADF
br label %48
48: ; preds = %43, %46
%49 = phi i32 [ %47, %46 ], [ %38, %43 ]
store i32 %49, i32* @errno
br label %50
50: ; preds = %48, %36, %44, %2
%51 = phi i32 [ -1, %2 ], [ -1, %36 ], [ 0, %44 ], [ -1, %48 ]
ret i32 %51
}