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

/* Variables and functions */
int DT_DIR;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
char *malloc(int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
scalar_t__ strcmp(char *, char *);
int strlen(char *);
int listaskin(char *nomedir, char *sottodir[]) {
  int i;
  int j = 0;
  DIR *dir;
  struct dirent *dp;
  if ((dir = opendir(nomedir)) == NULL) {
    perror("Cannot open skin dir");
    exit(1);
  }
  for (i = 0; (dp = readdir(dir)) != NULL && i < 64; i++) {
    if ((dp->d_type & DT_DIR) && strcmp(dp->d_name, ".") != 0 &&
        strcmp(dp->d_name, "..") != 0) {
      sottodir[j] = malloc(sizeof(char) * (strlen(dp->d_name) + 1));
      snprintf(sottodir[j], strlen(dp->d_name) + 1, "%s", dp->d_name);
      j++;
    }
  }
  closedir(dir);
  if (i == 64)
    printf("listaskin: troncata a %d per evitare buffer overflow.\n", 64);
  return j;
}


x86 assembly (clang, -O0):
<listaskin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11e4 <listaskin+0x44>
lea    0xe2b(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x20(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,%rcx
mov    %rcx,-0x28(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x29(%rbp)
je     1214 <listaskin+0x74>
cmpl   $0x40,-0x14(%rbp)
setl   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1224 <listaskin+0x84>
jmp    12fa <listaskin+0x15a>
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    0x2daf(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12e7 <listaskin+0x147>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdcb(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     12e7 <listaskin+0x147>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdac(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     12e7 <listaskin+0x147>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,%rdx
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%esi
add    $0x1,%esi
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rcx
lea    0xd3f(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    12ec <listaskin+0x14c>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11eb <listaskin+0x4b>
mov    -0x20(%rbp),%rdi
call   1070 <closedir@plt>
cmpl   $0x40,-0x14(%rbp)
jne    131e <listaskin+0x17e>
lea    0xd07(%rip),%rdi
mov    $0x40,%esi
call   1050 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listaskin>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,(%rsp)
call   1030 <opendir@plt>
test   %rax,%rax
je     12ca <listaskin+0x12a>
mov    %rax,%r15
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     12ad <listaskin+0x10d>
mov    %rax,%rbp
xor    %r12d,%r12d
mov    0x2e02(%rip),%r13
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x0(%r13),%eax
test   %eax,0x0(%rbp)
je     126b <listaskin+0xcb>
mov    0x8(%rbp),%rdi
lea    0xe12(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     126b <listaskin+0xcb>
mov    0x8(%rbp),%rdi
lea    0xdfc(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     126b <listaskin+0xcb>
mov    0x8(%rbp),%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%edi
call   10a0 <malloc@plt>
mov    %r13,%rbx
mov    %rax,%r13
movslq %r14d,%r14
mov    (%rsp),%rax
mov    %r13,(%rax,%r14,8)
mov    0x8(%rbp),%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%esi
mov    0x8(%rbp),%rcx
mov    %r13,%rdi
mov    %rbx,%r13
lea    0xdb6(%rip),%rdx
call   1060 <snprintf@plt>
add    $0x1,%r14d
lea    0x1(%r12),%ebx
mov    %r15,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     128d <listaskin+0xed>
mov    %rax,%rbp
cmp    $0x3f,%r12d
mov    %ebx,%r12d
jb     11f0 <listaskin+0x50>
mov    %r15,%rdi
call   1070 <closedir@plt>
cmp    $0x40,%ebx
jne    12b8 <listaskin+0x118>
lea    0xd7a(%rip),%rdi
mov    $0x40,%esi
call   1050 <printf@plt>
jmp    12b8 <listaskin+0x118>
mov    %r15,%rdi
call   1070 <closedir@plt>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd2f(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i32, i8* }
@.str = [21 x i8] c"Cannot open skin dir\00"
@DT_DIR = global i32 0
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [55 x i8] c"listaskin: troncata a %d per evitare buffer overflow.\0A\00"
define i32 @listaskin(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca %struct.dirent*
store i8* %0, i8** %3
store i8** %1, i8*** %4
store i32 0, i32* %6
%9 = load i8*, i8** %3
%10 = call i32* @opendir(i8* %9)
store i32* %10, i32** %7
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
store i32 0, i32* %5
br label %16
16: ; preds = %75, %15
%17 = load i32*, i32** %7
%18 = call %struct.dirent* @readdir(i32* %17)
store %struct.dirent* %18, %struct.dirent** %8
%19 = icmp ne %struct.dirent* %18, null
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i32, i32* %5
%22 = icmp slt i32 %21, 64
br label %23
23: ; preds = %20, %16
%24 = phi i1 [ false, %16 ], [ %22, %20 ]
br i1 %24, label %25, label %78
25: ; preds = %23
%26 = load %struct.dirent*, %struct.dirent** %8
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i32 0, i32 0
%28 = load i32, i32* %27
%29 = load i32, i32* @DT_DIR
%30 = and i32 %28, %29
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %74
32: ; preds = %25
%33 = load %struct.dirent*, %struct.dirent** %8
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 1
%35 = load i8*, i8** %34
%36 = call i64 @strcmp(i8* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %74
38: ; preds = %32
%39 = load %struct.dirent*, %struct.dirent** %8
%40 = getelementptr inbounds %struct.dirent, %struct.dirent* %39, i32 0, i32 1
%41 = load i8*, i8** %40
%42 = call i64 @strcmp(i8* %41, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %74
44: ; preds = %38
%45 = load %struct.dirent*, %struct.dirent** %8
%46 = getelementptr inbounds %struct.dirent, %struct.dirent* %45, i32 0, i32 1
%47 = load i8*, i8** %46
%48 = call i32 @strlen(i8* %47)
%49 = add nsw i32 %48, 1
%50 = sext i32 %49 to i64
%51 = mul i64 1, %50
%52 = trunc i64 %51 to i32
%53 = call i8* @malloc(i32 %52)
%54 = load i8**, i8*** %4
%55 = load i32, i32* %6
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8*, i8** %54, i64 %56
store i8* %53, i8** %57
%58 = load i8**, i8*** %4
%59 = load i32, i32* %6
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8*, i8** %58, i64 %60
%62 = load i8*, i8** %61
%63 = load %struct.dirent*, %struct.dirent** %8
%64 = getelementptr inbounds %struct.dirent, %struct.dirent* %63, i32 0, i32 1
%65 = load i8*, i8** %64
%66 = call i32 @strlen(i8* %65)
%67 = add nsw i32 %66, 1
%68 = load %struct.dirent*, %struct.dirent** %8
%69 = getelementptr inbounds %struct.dirent, %struct.dirent* %68, i32 0, i32 1
%70 = load i8*, i8** %69
%71 = call i32 @snprintf(i8* %62, i32 %67, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %70)
%72 = load i32, i32* %6
%73 = add nsw i32 %72, 1
store i32 %73, i32* %6
br label %74
74: ; preds = %44, %38, %32, %25
br label %75
75: ; preds = %74
%76 = load i32, i32* %5
%77 = add nsw i32 %76, 1
store i32 %77, i32* %5
br label %16
78: ; preds = %23
%79 = load i32*, i32** %7
%80 = call i32 @closedir(i32* %79)
%81 = load i32, i32* %5
%82 = icmp eq i32 %81, 64
br i1 %82, label %83, label %85
83: ; preds = %78
%84 = call i32 @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.4, i64 0, i64 0), i32 64)
br label %85
85: ; preds = %83, %78
%86 = load i32, i32* %6
ret i32 %86
}

LLVM IR (-O2):
%struct.dirent = type { i32, i8* }
@.str = [21 x i8] c"Cannot open skin dir\00"
@DT_DIR = global i32 0
@.str.1 = [2 x i8] c".\00"
@.str.2 = [3 x i8] c"..\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [55 x i8] c"listaskin: troncata a %d per evitare buffer overflow.\0A\00"
define i32 @listaskin(i8* nocapture %0, i8** nocapture writeonly %1) {
%3 = tail call i32* @opendir(i8* %0)
%4 = icmp eq i32* %3, null
br i1 %4, label %10, label %5
5: ; preds = %2
%6 = tail call %struct.dirent* @readdir(i32* %3)
%7 = icmp eq %struct.dirent* %6, null
br i1 %7, label %8, label %13
8: ; preds = %5
%9 = tail call i32 @closedir(i32* %3)
br label %56
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %5, %44
%14 = phi %struct.dirent* [ %47, %44 ], [ %6, %5 ]
%15 = phi i32 [ %46, %44 ], [ 0, %5 ]
%16 = phi i32 [ %45, %44 ], [ 0, %5 ]
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%18 = load i32, i32* %17
%19 = load i32, i32* @DT_DIR
%20 = and i32 %19, %18
%21 = icmp eq i32 %20, 0
br i1 %21, label %44, label %22
22: ; preds = %13
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 1
%24 = load i8*, i8** %23
%25 = tail call i64 @strcmp(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
br i1 %26, label %44, label %27
27: ; preds = %22
%28 = load i8*, i8** %23
%29 = tail call i64 @strcmp(i8* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
br i1 %30, label %44, label %31
31: ; preds = %27
%32 = load i8*, i8** %23
%33 = tail call i32 @strlen(i8* %32)
%34 = add nsw i32 %33, 1
%35 = tail call i8* @malloc(i32 %34)
%36 = sext i32 %16 to i64
%37 = getelementptr inbounds i8*, i8** %1, i64 %36
store i8* %35, i8** %37
%38 = load i8*, i8** %23
%39 = tail call i32 @strlen(i8* %38)
%40 = add nsw i32 %39, 1
%41 = load i8*, i8** %23
%42 = tail call i32 @snprintf(i8* %35, i32 %40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %41)
%43 = add nsw i32 %16, 1
br label %44
44: ; preds = %13, %22, %27, %31
%45 = phi i32 [ %43, %31 ], [ %16, %27 ], [ %16, %22 ], [ %16, %13 ]
%46 = add nuw nsw i32 %15, 1
%47 = tail call %struct.dirent* @readdir(i32* %3)
%48 = icmp ne %struct.dirent* %47, null
%49 = icmp ult i32 %15, 63
%50 = select i1 %48, i1 %49, i1 false
br i1 %50, label %13, label %51
51: ; preds = %44
%52 = icmp eq i32 %46, 64
%53 = tail call i32 @closedir(i32* %3)
br i1 %52, label %54, label %56
54: ; preds = %51
%55 = tail call i32 @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.4, i64 0, i64 0), i32 64)
br label %56
56: ; preds = %8, %54, %51
%57 = phi i32 [ 0, %8 ], [ %45, %54 ], [ %45, %51 ]
ret i32 %57
}