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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
char *strerror(int);
int strlen(char *);
void stub1(char *);
void dirwalk(char *dir, void (*fsize)(char *name)) {
  char name[1024];
  struct dirent *dp;
  DIR *dfd;
  if ((dfd = opendir(dir)) == NULL) {
    fprintf(stderr, "Err.:(%d) - %s: '%s'\n", errno, strerror(errno), dir);
    return;
  }
  while ((dp = readdir(dfd)) != NULL) {
    if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0)
      continue;
    if (strlen(dir) + strlen(dp->d_name) + 2 > sizeof(name))
      fprintf(stderr, "nome %s/%s troppo lungo\n", dir, dp->d_name);
    else {
      sprintf(name, "%s/%s", dir, dp->d_name);
      (*fsize)(name);
    }
  }
  closedir(dfd);
}


x86 assembly (clang, -O0):
<dirwalk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x420(%rbp)
cmp    $0x0,%rax
jne    11ff <dirwalk+0x7f>
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x428(%rbp)
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x424(%rbp)
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x428(%rbp),%edi
mov    -0x424(%rbp),%edx
mov    %rax,%rcx
mov    -0x8(%rbp),%r8
lea    0xe0d(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1308 <dirwalk+0x188>
jmp    1204 <dirwalk+0x84>
mov    -0x420(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x418(%rbp)
cmp    $0x0,%rax
je     12fc <dirwalk+0x17c>
mov    -0x418(%rbp),%rax
mov    (%rax),%rdi
lea    0xde5(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     1261 <dirwalk+0xe1>
mov    -0x418(%rbp),%rax
mov    (%rax),%rdi
lea    0xdc4(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1266 <dirwalk+0xe6>
jmp    1204 <dirwalk+0x84>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x42c(%rbp)
mov    -0x418(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x42c(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
cltq
cmp    $0x400,%rax
jbe    12c9 <dirwalk+0x149>
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    -0x418(%rbp),%rax
mov    (%rax),%rcx
lea    0xd5c(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    12f7 <dirwalk+0x177>
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x418(%rbp),%rax
mov    (%rax),%rcx
lea    0xd4d(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x10(%rbp),%rax
lea    -0x410(%rbp),%rdi
call   *%rax
jmp    1204 <dirwalk+0x84>
mov    -0x420(%rbp),%rdi
call   1050 <closedir@plt>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirwalk>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rsi,0x8(%rsp)
mov    %rdi,%r15
call   1030 <opendir@plt>
test   %rax,%rax
je     127f <dirwalk+0xff>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1265 <dirwalk+0xe5>
mov    %rax,%rbp
lea    0xe52(%rip),%r12
lea    0xe4a(%rip),%r13
jmp    1200 <dirwalk+0x80>
mov    0x0(%rbp),%rcx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
lea    0xe51(%rip),%rsi
mov    %r15,%rdx
call   1090 <sprintf@plt>
mov    %rbp,%rdi
call   *0x8(%rsp)
mov    %rbx,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1265 <dirwalk+0xe5>
mov    0x0(%rbp),%rdi
mov    %r12,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11f0 <dirwalk+0x70>
mov    0x0(%rbp),%rdi
mov    %r13,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11f0 <dirwalk+0x70>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%r14d
mov    0x0(%rbp),%rdi
call   1040 <strlen@plt>
add    %r14d,%eax
add    $0xfffffc01,%eax
cmp    $0xfffffbfe,%eax
ja     11ce <dirwalk+0x4e>
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rcx
lea    0xdc0(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    11f0 <dirwalk+0x70>
mov    %rbx,%rdi
call   1050 <closedir@plt>
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d5a(%rip),%rax
mov    (%rax),%ebx
mov    0x2d49(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   10a0 <strerror@plt>
lea    0xd61(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
mov    %rax,%rcx
mov    %r15,%r8
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <fprintf@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@stderr = global i32 0
@.str = [22 x i8] c"Err.:(%d) - %s: '%s'\0A\00"
@errno = global i32 0
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [25 x i8] c"nome %s/%s troppo lungo\0A\00"
@.str.4 = [6 x i8] c"%s/%s\00"
define void @dirwalk(i8* %0, void (i8*)* %1) {
%3 = alloca i8*
%4 = alloca void (i8*)*
%5 = alloca [1024 x i8]
%6 = alloca %struct.dirent*
%7 = alloca i32*
store i8* %0, i8** %3
store void (i8*)* %1, void (i8*)** %4
%8 = load i8*, i8** %3
%9 = call i32* @opendir(i8* %8)
store i32* %9, i32** %7
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %18
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = load i8*, i8** %3
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %13, i8* %15, i8* %16)
br label %67
18: ; preds = %2
br label %19
19: ; preds = %63, %35, %18
%20 = load i32*, i32** %7
%21 = call %struct.dirent* @readdir(i32* %20)
store %struct.dirent* %21, %struct.dirent** %6
%22 = icmp ne %struct.dirent* %21, null
br i1 %22, label %23, label %64
23: ; preds = %19
%24 = load %struct.dirent*, %struct.dirent** %6
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %24, i32 0, i32 0
%26 = load i8*, i8** %25
%27 = call i64 @strcmp(i8* %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** %6
%31 = getelementptr inbounds %struct.dirent, %struct.dirent* %30, i32 0, i32 0
%32 = load i8*, i8** %31
%33 = call i64 @strcmp(i8* %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 i8*, i8** %3
%38 = call i32 @strlen(i8* %37)
%39 = load %struct.dirent*, %struct.dirent** %6
%40 = getelementptr inbounds %struct.dirent, %struct.dirent* %39, i32 0, i32 0
%41 = load i8*, i8** %40
%42 = call i32 @strlen(i8* %41)
%43 = add nsw i32 %38, %42
%44 = add nsw i32 %43, 2
%45 = sext i32 %44 to i64
%46 = icmp ugt i64 %45, 1024
br i1 %46, label %47, label %54
47: ; preds = %36
%48 = load i32, i32* @stderr
%49 = load i8*, i8** %3
%50 = load %struct.dirent*, %struct.dirent** %6
%51 = getelementptr inbounds %struct.dirent, %struct.dirent* %50, i32 0, i32 0
%52 = load i8*, i8** %51
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %49, i8* %52)
br label %63
54: ; preds = %36
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%56 = load i8*, i8** %3
%57 = load %struct.dirent*, %struct.dirent** %6
%58 = getelementptr inbounds %struct.dirent, %struct.dirent* %57, i32 0, i32 0
%59 = load i8*, i8** %58
%60 = call i32 @sprintf(i8* %55, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %56, i8* %59)
%61 = load void (i8*)*, void (i8*)** %4
%62 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
br label %63
63: ; preds = %54, %47
br label %19
64: ; preds = %19
%65 = load i32*, i32** %7
%66 = call i32 @closedir(i32* %65)
br label %67
67: ; preds = %64, %11
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@stderr = global i32 0
@.str = [22 x i8] c"Err.:(%d) - %s: '%s'\0A\00"
@errno = global i32 0
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [25 x i8] c"nome %s/%s troppo lungo\0A\00"
@.str.4 = [6 x i8] c"%s/%s\00"
define void @dirwalk(i8* %0, void (i8*)* nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @opendir(i8* %0)
%6 = icmp eq i32* %5, null
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = call %struct.dirent* @readdir(i32* %5)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %42, label %15
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %12, i8* %13, i8* %0)
br label %44
15: ; preds = %7, %25
%16 = phi %struct.dirent* [ %26, %25 ], [ %8, %7 ]
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i64 0, i32 0
%18 = load i8*, i8** %17
%19 = call i64 @strcmp(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %15
%22 = load i8*, i8** %17
%23 = call i64 @strcmp(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %35, %39, %15, %21
%26 = call %struct.dirent* @readdir(i32* %5)
%27 = icmp eq %struct.dirent* %26, null
br i1 %27, label %42, label %15
28: ; preds = %21
%29 = call i32 @strlen(i8* %0)
%30 = load i8*, i8** %17
%31 = call i32 @strlen(i8* %30)
%32 = add i32 %29, -1023
%33 = add i32 %32, %31
%34 = icmp ult i32 %33, -1025
br i1 %34, label %35, label %39
35: ; preds = %28
%36 = load i32, i32* @stderr
%37 = load i8*, i8** %17
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %37)
br label %25
39: ; preds = %28
%40 = load i8*, i8** %17
%41 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* %40)
br label %25
42: ; preds = %25, %7
%43 = call i32 @closedir(i32* %5)
br label %44
44: ; preds = %42, %10
ret void
}