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

/* Variables and functions */
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *opendir(char const *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, int, int);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char *);
scalar_t__ stat(char *, struct stat *);
void opcion1(const char *ruta) {
  DIR *directorio;
  struct dirent *entrada;
  struct stat attr;
  char nombre_fichero[256];
  if ((directorio = opendir(ruta)) == NULL) {
    perror("Directorio");
    exit(1);
  }
  while ((entrada = readdir(directorio)) != NULL) {
    sprintf(nombre_fichero, "%s/%s", ruta, entrada->d_name);
    if ((stat(nombre_fichero, &attr)) < 0) {
      perror("Error en stat");
    }
    if (S_ISREG(attr.st_mode)) {
      int peso = attr.st_size;
      printf(" %s   tamaño: %d Bytes %d KB \n", entrada->d_name, peso,
             peso / 1024);
    }
  }
  closedir(directorio);
}


x86 assembly (clang, -O0):
<opcion1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11cc <opcion1+0x3c>
lea    0xe43(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    11d1 <opcion1+0x41>
mov    -0x10(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1281 <opcion1+0xf1>
lea    -0x120(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
lea    0xe0a(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x120(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jge    122c <opcion1+0x9c>
lea    0xdea(%rip),%rdi
call   1090 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1070 <S_ISREG@plt>
cmp    $0x0,%rax
je     127c <opcion1+0xec>
mov    -0x20(%rbp),%eax
mov    %eax,-0x124(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    -0x124(%rbp),%eax
mov    %eax,-0x128(%rbp)
mov    -0x124(%rbp),%eax
mov    $0x400,%ecx
cltd
idiv   %ecx
mov    -0x128(%rbp),%edx
mov    %eax,%ecx
lea    0xda8(%rip),%rdi
call   1040 <printf@plt>
jmp    11d1 <opcion1+0x41>
mov    -0x10(%rbp),%rdi
call   1050 <closedir@plt>
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<opcion1>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %rdi,%r14
call   1030 <opendir@plt>
test   %rax,%rax
je     126c <opcion1+0xdc>
mov    %rax,%r12
mov    %rax,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1252 <opcion1+0xc2>
mov    %rax,%rbp
lea    0xe3b(%rip),%r15
lea    0x10(%rsp),%rbx
lea    0x8(%rsp),%r13
jmp    11f0 <opcion1+0x60>
nopl   0x0(%rax)
mov    %r12,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1252 <opcion1+0xc2>
mov    0x0(%rbp),%rcx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   10a0 <sprintf@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1060 <stat@plt>
test   %rax,%rax
js     1244 <opcion1+0xb4>
mov    0xc(%rsp),%edi
call   1070 <S_ISREG@plt>
test   %rax,%rax
je     11e0 <opcion1+0x50>
mov    0x8(%rsp),%edx
mov    0x0(%rbp),%rsi
lea    0x3ff(%rdx),%ecx
test   %edx,%edx
cmovns %edx,%ecx
sar    $0xa,%ecx
lea    0xde2(%rip),%rdi
call   1040 <printf@plt>
jmp    11e0 <opcion1+0x50>
lea    0xdc6(%rip),%rdi
call   1090 <perror@plt>
jmp    1212 <opcion1+0x82>
mov    %r12,%rdi
call   1050 <closedir@plt>
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd8d(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32, i32 }
@.str = [11 x i8] c"Directorio\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@.str.2 = [14 x i8] c"Error en stat\00"
@.str.3 = [32 x i8] c" %s tama\C3\B1o: %d Bytes %d KB \0A\00"
define void @opcion1(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca %struct.dirent*
%5 = alloca %struct.stat
%6 = alloca [256 x i8]
%7 = alloca i32
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = call i32* @opendir(i8* %8)
store i32* %9, i32** %3
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %1
br label %15
15: ; preds = %46, %14
%16 = load i32*, i32** %3
%17 = call %struct.dirent* @readdir(i32* %16)
store %struct.dirent* %17, %struct.dirent** %4
%18 = icmp ne %struct.dirent* %17, null
br i1 %18, label %19, label %47
19: ; preds = %15
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%21 = load i8*, i8** %2
%22 = load %struct.dirent*, %struct.dirent** %4
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %22, i32 0, i32 0
%24 = load i8*, i8** %23
%25 = call i32 @sprintf(i8* %20, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %21, i8* %24)
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%27 = call i64 @stat(i8* %26, %struct.stat* %5)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %19
%30 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %31
31: ; preds = %29, %19
%32 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 1
%33 = load i32, i32* %32
%34 = call i64 @S_ISREG(i32 %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %46
36: ; preds = %31
%37 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%38 = load i32, i32* %37
store i32 %38, i32* %7
%39 = load %struct.dirent*, %struct.dirent** %4
%40 = getelementptr inbounds %struct.dirent, %struct.dirent* %39, i32 0, i32 0
%41 = load i8*, i8** %40
%42 = load i32, i32* %7
%43 = load i32, i32* %7
%44 = sdiv i32 %43, 1024
%45 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %41, i32 %42, i32 %44)
br label %46
46: ; preds = %36, %31
br label %15
47: ; preds = %15
%48 = load i32*, i32** %3
%49 = call i32 @closedir(i32* %48)
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
%struct.dirent = type { i8* }
@.str = [11 x i8] c"Directorio\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@.str.2 = [14 x i8] c"Error en stat\00"
@.str.3 = [32 x i8] c" %s tama\C3\B1o: %d Bytes %d KB \0A\00"
define void @opcion1(i8* %0) {
%2 = alloca %struct.stat
%3 = alloca [256 x i8]
%4 = bitcast %struct.stat* %2 to i8*
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = tail call i32* @opendir(i8* %0)
%7 = icmp eq i32* %6, null
br i1 %7, label %14, label %8
8: ; preds = %1
%9 = call %struct.dirent* @readdir(i32* %6)
%10 = icmp eq %struct.dirent* %9, null
br i1 %10, label %38, label %11
11: ; preds = %8
%12 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%13 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
br label %17
14: ; preds = %1
%15 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %11, %35
%18 = phi %struct.dirent* [ %9, %11 ], [ %36, %35 ]
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i64 0, i32 0
%20 = load i8*, i8** %19
%21 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %20)
%22 = call i64 @stat(i8* %5, %struct.stat* %2)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %17
%25 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %24, %17
%27 = load i32, i32* %12
%28 = call i64 @S_ISREG(i32 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %35, label %30
30: ; preds = %26
%31 = load i32, i32* %13
%32 = load i8*, i8** %19
%33 = sdiv i32 %31, 1024
%34 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %32, i32 %31, i32 %33)
br label %35
35: ; preds = %30, %26
%36 = call %struct.dirent* @readdir(i32* %6)
%37 = icmp eq %struct.dirent* %36, null
br i1 %37, label %38, label %17
38: ; preds = %35, %8
%39 = call i32 @closedir(i32* %6)
ret void
}