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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *, ...);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
int strlen(char *);
void stub1(char *);
int /*<<< orphan*/ write_footer(int /*<<< orphan*/ *);
int /*<<< orphan*/ write_header(int /*<<< orphan*/ *);
void dir_walk(char *dir, void (*function_name)(char *)) {
  char name[1024];
  struct dirent *dp;
  DIR *dfd;
  if ((dfd = opendir(dir)) == NULL) {
    fprintf(stderr, "dirwalk: can't open %s\n", dir);
    return;
  }
  FILE *fp;
  char file_index[1024];
  char file_link[1024];
  snprintf(file_index, 1024, "%s/%s", dir, "index.html");
  fp = fopen(file_index, "w");
  if (fp == NULL) {
    fprintf(stderr, "Error opening index.html\n");
    exit(1);
  }
  write_header(fp);
  while ((dp = readdir(dfd)) != NULL) {
    if (dp->d_name[0] == '.' || strcmp(dp->d_name, "index.html") == 0) {
      continue;
    }
    memset(file_link, 0, 1024);
    snprintf(file_link, 1024,
             "\t\t\t\t\t\t<div class=\"col-md-4 col-sm-6\"><div "
             "class=\"thumbnail\"><div class=\"caption\"><a href=\"%s\">%s%s\n",
             dp->d_name, dp->d_name, "</a></div></div></div>");
    fputs(file_link, fp);
    if (strlen(dir) + strlen(dp->d_name) + 2 > sizeof(name)) {
      fprintf(stderr, "dirwalk: name %s %s too long\n", dir, dp->d_name);
    } else {
      snprintf(name, 1024, "%s/%s", dir, dp->d_name);
      (*function_name)(name);
    }
  }
  write_footer(fp);
  fclose(fp);
  closedir(dfd);
}


x86 assembly (clang, -O0):
<dir_walk>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x420(%rbp)
cmp    $0x0,%rax
jne    122d <dir_walk+0x4d>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xddf(%rip),%rsi
mov    $0x0,%al
call   10c0 <fprintf@plt>
jmp    141b <dir_walk+0x23b>
lea    -0x830(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x400,%esi
lea    0xdd4(%rip),%rdx
lea    0xdd3(%rip),%r8
mov    $0x0,%al
call   1070 <snprintf@plt>
lea    -0x830(%rbp),%rdi
lea    0xdc9(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    129b <dir_walk+0xbb>
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
lea    0xda1(%rip),%rsi
mov    $0x0,%al
call   10c0 <fprintf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0x428(%rbp),%rdi
call   1100 <write_header@plt>
mov    -0x420(%rbp),%rdi
call   10d0 <readdir@plt>
mov    %rax,-0x418(%rbp)
cmp    $0x0,%rax
je     13f7 <dir_walk+0x217>
mov    -0x418(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
je     12fa <dir_walk+0x11a>
mov    -0x418(%rbp),%rax
mov    (%rax),%rdi
lea    0xd33(%rip),%rsi
call   10b0 <strcmp@plt>
cmp    $0x0,%rax
jne    12ff <dir_walk+0x11f>
jmp    12a7 <dir_walk+0xc7>
lea    -0xc30(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1090 <memset@plt>
lea    -0xc30(%rbp),%rdi
mov    -0x418(%rbp),%rax
mov    (%rax),%rcx
mov    -0x418(%rbp),%rax
mov    (%rax),%r8
mov    $0x400,%esi
lea    0xd0c(%rip),%rdx
lea    0xd69(%rip),%r9
mov    $0x0,%al
call   1070 <snprintf@plt>
lea    -0xc30(%rbp),%rdi
mov    -0x428(%rbp),%rsi
call   1080 <fputs@plt>
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0xc34(%rbp)
mov    -0x418(%rbp),%rax
mov    (%rax),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0xc34(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
cltq
cmp    $0x400,%rax
jbe    13bd <dir_walk+0x1dd>
mov    0x2c46(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    -0x418(%rbp),%rax
mov    (%rax),%rcx
lea    0xd0f(%rip),%rsi
mov    $0x0,%al
call   10c0 <fprintf@plt>
jmp    13f2 <dir_walk+0x212>
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    -0x418(%rbp),%rax
mov    (%rax),%r8
mov    $0x400,%esi
lea    0xc3a(%rip),%rdx
mov    $0x0,%al
call   1070 <snprintf@plt>
mov    -0x10(%rbp),%rax
lea    -0x410(%rbp),%rdi
call   *%rax
jmp    12a7 <dir_walk+0xc7>
mov    -0x428(%rbp),%rdi
call   1030 <write_footer@plt>
mov    -0x428(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x420(%rbp),%rdi
call   10a0 <closedir@plt>
add    $0xc40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dir_walk>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xc18,%rsp
mov    %rsi,0x8(%rsp)
mov    %rdi,%r14
call   1050 <opendir@plt>
test   %rax,%rax
je     138c <dir_walk+0x1ac>
mov    %rax,%r12
lea    0xe07(%rip),%rdx
lea    0xe06(%rip),%r8
lea    0x410(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x400,%esi
mov    %r14,%rcx
xor    %eax,%eax
call   1070 <snprintf@plt>
lea    0xdf0(%rip),%rsi
mov    %rbp,%rdi
call   10e0 <fopen@plt>
test   %rax,%rax
je     13b7 <dir_walk+0x1d7>
mov    %rax,%rbp
mov    %rax,%rdi
call   1100 <write_header@plt>
mov    %r12,%rdi
call   10d0 <readdir@plt>
test   %rax,%rax
je     1362 <dir_walk+0x182>
mov    %rax,%r15
lea    0x10(%rsp),%rbx
jmp    12c4 <dir_walk+0xe4>
mov    (%r15),%r8
mov    %rbx,%r15
mov    %rbp,%rbx
lea    0x810(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x400,%esi
lea    0xd88(%rip),%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1070 <snprintf@plt>
mov    %rbp,%rdi
mov    %rbx,%rbp
mov    %r15,%rbx
call   *0x8(%rsp)
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   10d0 <readdir@plt>
mov    %rax,%r15
test   %rax,%rax
je     1362 <dir_walk+0x182>
mov    (%r15),%rdi
cmpb   $0x2e,(%rdi)
je     12b0 <dir_walk+0xd0>
lea    0xd4b(%rip),%rsi
call   10b0 <strcmp@plt>
test   %rax,%rax
je     12b0 <dir_walk+0xd0>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1090 <memset@plt>
mov    (%r15),%rcx
mov    %rbx,%rdi
mov    $0x400,%esi
lea    0xd47(%rip),%rdx
mov    %rcx,%r8
lea    0xda1(%rip),%r9
xor    %eax,%eax
call   1070 <snprintf@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1080 <fputs@plt>
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %eax,%r13d
mov    (%r15),%rdi
call   1060 <strlen@plt>
add    %r13d,%eax
add    $0xfffffc01,%eax
cmp    $0xfffffbfe,%eax
ja     1270 <dir_walk+0x90>
mov    0x2c99(%rip),%rax
mov    (%rax),%edi
mov    (%r15),%rcx
lea    0xd6d(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   10c0 <fprintf@plt>
jmp    12b0 <dir_walk+0xd0>
mov    %rbp,%rdi
call   1030 <write_footer@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    %r12,%rdi
call   10a0 <closedir@plt>
add    $0xc18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c4d(%rip),%rax
mov    (%rax),%edi
lea    0xc64(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
add    $0xc18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10c0 <fprintf@plt>
mov    0x2c22(%rip),%rax
mov    (%rax),%edi
lea    0xc64(%rip),%rsi
xor    %eax,%eax
call   10c0 <fprintf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@stderr = global i32 0
@.str = [24 x i8] c"dirwalk: can't open %s\0A\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@.str.2 = [11 x i8] c"index.html\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [26 x i8] c"Error opening index.html\0A\00"
@.str.5 = [100 x i8] c"\09\09\09\09\09\09<div class=\22col-md-4 col-sm-6\22><div class=\22thumbnail\22><div class=\22caption\22><a href=\22%s\22>%s%s\0A\00"
@.str.6 = [23 x i8] c"</a></div></div></div>\00"
@.str.7 = [30 x i8] c"dirwalk: name %s %s too long\0A\00"
define void @dir_walk(i8* %0, void (i8*)* %1) {
%3 = alloca i8*
%4 = alloca void (i8*)*
%5 = alloca [1024 x i8]
%6 = alloca %struct.dirent*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca [1024 x i8]
%10 = alloca [1024 x i8]
store i8* %0, i8** %3
store void (i8*)* %1, void (i8*)** %4
%11 = load i8*, i8** %3
%12 = call i32* @opendir(i8* %11)
store i32* %12, i32** %7
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %3
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %16)
br label %100
18: ; preds = %2
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%20 = load i8*, i8** %3
%21 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %19, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %20, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %23, i32** %8
%24 = load i32*, i32** %8
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %30
26: ; preds = %18
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %18
%31 = load i32*, i32** %8
%32 = call i32 @write_header(i32* %31)
br label %33
33: ; preds = %92, %51, %30
%34 = load i32*, i32** %7
%35 = call %struct.dirent* @readdir(i32* %34)
store %struct.dirent* %35, %struct.dirent** %6
%36 = icmp ne %struct.dirent* %35, null
br i1 %36, label %37, label %93
37: ; preds = %33
%38 = load %struct.dirent*, %struct.dirent** %6
%39 = getelementptr inbounds %struct.dirent, %struct.dirent* %38, i32 0, i32 0
%40 = load i8*, i8** %39
%41 = getelementptr inbounds i8, i8* %40, i64 0
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp eq i32 %43, 46
br i1 %44, label %51, label %45
45: ; preds = %37
%46 = load %struct.dirent*, %struct.dirent** %6
%47 = getelementptr inbounds %struct.dirent, %struct.dirent* %46, i32 0, i32 0
%48 = load i8*, i8** %47
%49 = call i64 @strcmp(i8* %48, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %45, %37
br label %33
52: ; preds = %45
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%54 = call i32 @memset(i8* %53, i32 0, i32 1024)
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%56 = load %struct.dirent*, %struct.dirent** %6
%57 = getelementptr inbounds %struct.dirent, %struct.dirent* %56, i32 0, i32 0
%58 = load i8*, i8** %57
%59 = load %struct.dirent*, %struct.dirent** %6
%60 = getelementptr inbounds %struct.dirent, %struct.dirent* %59, i32 0, i32 0
%61 = load i8*, i8** %60
%62 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %55, i32 1024, i8* getelementptr inbounds ([100 x i8], [100 x i8]* @.str.5, i64 0, i64 0), i8* %58, i8* %61, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
%63 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%64 = load i32*, i32** %8
%65 = call i32 @fputs(i8* %63, i32* %64)
%66 = load i8*, i8** %3
%67 = call i32 @strlen(i8* %66)
%68 = load %struct.dirent*, %struct.dirent** %6
%69 = getelementptr inbounds %struct.dirent, %struct.dirent* %68, i32 0, i32 0
%70 = load i8*, i8** %69
%71 = call i32 @strlen(i8* %70)
%72 = add nsw i32 %67, %71
%73 = add nsw i32 %72, 2
%74 = sext i32 %73 to i64
%75 = icmp ugt i64 %74, 1024
br i1 %75, label %76, label %83
76: ; preds = %52
%77 = load i32, i32* @stderr
%78 = load i8*, i8** %3
%79 = load %struct.dirent*, %struct.dirent** %6
%80 = getelementptr inbounds %struct.dirent, %struct.dirent* %79, i32 0, i32 0
%81 = load i8*, i8** %80
%82 = call i32 (i32, i8*, ...) @fprintf(i32 %77, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.7, i64 0, i64 0), i8* %78, i8* %81)
br label %92
83: ; preds = %52
%84 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%85 = load i8*, i8** %3
%86 = load %struct.dirent*, %struct.dirent** %6
%87 = getelementptr inbounds %struct.dirent, %struct.dirent* %86, i32 0, i32 0
%88 = load i8*, i8** %87
%89 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %84, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %85, i8* %88)
%90 = load void (i8*)*, void (i8*)** %4
%91 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
br label %92
92: ; preds = %83, %76
br label %33
93: ; preds = %33
%94 = load i32*, i32** %8
%95 = call i32 @write_footer(i32* %94)
%96 = load i32*, i32** %8
%97 = call i32 @fclose(i32* %96)
%98 = load i32*, i32** %7
%99 = call i32 @closedir(i32* %98)
br label %100
100: ; preds = %93, %14
ret void
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@stderr = global i32 0
@.str = [24 x i8] c"dirwalk: can't open %s\0A\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@.str.2 = [11 x i8] c"index.html\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [26 x i8] c"Error opening index.html\0A\00"
@.str.5 = [100 x i8] c"\09\09\09\09\09\09<div class=\22col-md-4 col-sm-6\22><div class=\22thumbnail\22><div class=\22caption\22><a href=\22%s\22>%s%s\0A\00"
@.str.6 = [23 x i8] c"</a></div></div></div>\00"
@.str.7 = [30 x i8] c"dirwalk: name %s %s too long\0A\00"
define void @dir_walk(i8* %0, void (i8*)* nocapture %1) {
%3 = alloca [1024 x i8]
%4 = alloca [1024 x i8]
%5 = alloca [1024 x i8]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = tail call i32* @opendir(i8* %0)
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %60
12: ; preds = %2
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%15 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %13, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%16 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %22
18: ; preds = %12
%19 = load i32, i32* @stderr
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %12
%23 = call i32 @write_header(i32* %16)
%24 = call %struct.dirent* @readdir(i32* %7)
%25 = icmp eq %struct.dirent* %24, null
br i1 %25, label %56, label %26
26: ; preds = %22, %35
%27 = phi %struct.dirent* [ %36, %35 ], [ %24, %22 ]
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i64 0, i32 0
%29 = load i8*, i8** %28
%30 = load i8, i8* %29
%31 = icmp eq i8 %30, 46
br i1 %31, label %35, label %32
32: ; preds = %26
%33 = call i64 @strcmp(i8* %29, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %49, %53, %26, %32
%36 = call %struct.dirent* @readdir(i32* %7)
%37 = icmp eq %struct.dirent* %36, null
br i1 %37, label %56, label %26
38: ; preds = %32
%39 = call i32 @memset(i8* %14, i32 0, i32 1024)
%40 = load i8*, i8** %28
%41 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %14, i32 1024, i8* getelementptr inbounds ([100 x i8], [100 x i8]* @.str.5, i64 0, i64 0), i8* %40, i8* %40, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
%42 = call i32 @fputs(i8* %14, i32* %16)
%43 = call i32 @strlen(i8* %0)
%44 = load i8*, i8** %28
%45 = call i32 @strlen(i8* %44)
%46 = add i32 %43, -1023
%47 = add i32 %46, %45
%48 = icmp ult i32 %47, -1025
br i1 %48, label %49, label %53
49: ; preds = %38
%50 = load i32, i32* @stderr
%51 = load i8*, i8** %28
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.7, i64 0, i64 0), i8* %0, i8* %51)
br label %35
53: ; preds = %38
%54 = load i8*, i8** %28
%55 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %6, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %54)
br label %35
56: ; preds = %35, %22
%57 = call i32 @write_footer(i32* %16)
%58 = call i32 @fclose(i32* %16)
%59 = call i32 @closedir(i32* %7)
br label %60
60: ; preds = %56, %9
ret void
}