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

/* Variables and functions */
int /*<<< orphan*/ S_ISDIR(int);
int /*<<< orphan*/ chdir(char *);
int chmod(char *, int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int stat(char *, struct stat *);
int strtol(char *, int /*<<< orphan*/, int);
int main(int argc, char *argv[]) {
  struct stat atributos;
  struct dirent *direntp;
  DIR *dirp;
  int stat_result, chmod_result, permisos_antiguos, permisos_nuevos;
  if (argc != 3) {
    printf("Modo de uso: %s <pathname> <permisos>\n", argv[0]);
    exit(1);
  }
  dirp = opendir(argv[1]);
  if (dirp == NULL) {
    printf("Error: No se puede abrir el directorio: %s\n", argv[1]);
    exit(2);
  }
  printf("<nombre>\t<ant>\t<new>\n");
  while ((direntp = readdir(dirp)) != NULL) {
    chdir(argv[1]);
    stat_result = stat(direntp->d_name, &atributos);
    if (stat_result < 0) {
      printf("Error al intentar acceder a los atributos de %s\n",
             direntp->d_name);
    } else {
      if (!S_ISDIR(atributos.st_mode)) {
        permisos_nuevos = strtol(argv[2], 0, 8) & 0777;
        permisos_antiguos = atributos.st_mode & 0777;
        chmod_result = chmod(direntp->d_name, permisos_nuevos);
        if (chmod_result < 0) {
          printf("%s : \t%d \t%o\n", direntp->d_name, chmod_result,
                 permisos_antiguos);
          exit(-1);
        } else {
          printf("%s : \t%o \t%o\n", direntp->d_name, permisos_antiguos,
                 permisos_nuevos);
        }
      };
    }
  }
  closedir(dirp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11df <main+0x3f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    121b <main+0x7b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x2,%edi
call   10c0 <exit@plt>
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1327 <main+0x187>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <chdir@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    -0x18(%rbp),%rsi
call   1080 <stat@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1284 <main+0xe4>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1322 <main+0x182>
mov    -0x18(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    131d <main+0x17d>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1090 <strtol@plt>
and    $0x1ff,%eax
mov    %eax,-0x38(%rbp)
mov    -0x18(%rbp),%eax
and    $0x1ff,%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x38(%rbp),%esi
call   10b0 <chmod@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    12fd <main+0x15d>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
mov    -0x30(%rbp),%edx
mov    -0x34(%rbp),%ecx
lea    0xdae(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
mov    -0x34(%rbp),%edx
mov    -0x38(%rbp),%ecx
lea    0xd97(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    131d <main+0x17d>
jmp    1322 <main+0x182>
jmp    1229 <main+0x89>
mov    -0x28(%rbp),%rdi
call   1070 <closedir@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    12df <main+0x12f>
mov    0x8(%rbx),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     12fa <main+0x14a>
mov    %rax,%r14
lea    0xebd(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     12a9 <main+0xf9>
mov    %rax,%rbp
mov    %rsp,%r15
jmp    1232 <main+0x82>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x0(%rbp),%rsi
lea    0xe38(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
call   10b0 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     12a9 <main+0xf9>
mov    0x8(%rbx),%rdi
call   1050 <chdir@plt>
mov    0x0(%rbp),%rdi
mov    %r15,%rsi
call   1090 <stat@plt>
test   %eax,%eax
js     1210 <main+0x60>
mov    (%rsp),%r13d
mov    %r13d,%edi
call   1070 <S_ISDIR@plt>
test   %eax,%eax
jne    1222 <main+0x72>
mov    0x10(%rbx),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   10a0 <strtol@plt>
mov    %eax,%r12d
and    $0x1ff,%r12d
and    $0x1ff,%r13d
mov    0x0(%rbp),%rdi
mov    %r12d,%esi
call   10c0 <chmod@plt>
mov    0x0(%rbp),%rsi
test   %eax,%eax
js     12c2 <main+0x112>
lea    0xdfb(%rip),%rdi
mov    %r13d,%edx
mov    %r12d,%ecx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1222 <main+0x72>
mov    %r14,%rdi
call   1080 <closedir@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdbb(%rip),%rdi
mov    %eax,%edx
mov    %r13d,%ecx
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    (%rbx),%rsi
lea    0xd17(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x8(%rbx),%rsi
lea    0xd22(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x2,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [39 x i8] c"Modo de uso: %s <pathname> <permisos>\0A\00"
@.str.1 = [44 x i8] c"Error: No se puede abrir el directorio: %s\0A\00"
@.str.2 = [22 x i8] c"<nombre>\09<ant>\09<new>\0A\00"
@.str.3 = [49 x i8] c"Error al intentar acceder a los atributos de %s\0A\00"
@.str.4 = [14 x i8] c"%s : \09%d \09%o\0A\00"
@.str.5 = [14 x i8] c"%s : \09%o \09%o\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.stat
%7 = alloca %struct.dirent*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 3
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32* @opendir(i8* %24)
store i32* %25, i32** %8
%26 = load i32*, i32** %8
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %34
28: ; preds = %21
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i8* %31)
%33 = call i32 @exit(i32 2)
unreachable
34: ; preds = %21
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %36
36: ; preds = %94, %34
%37 = load i32*, i32** %8
%38 = call %struct.dirent* @readdir(i32* %37)
store %struct.dirent* %38, %struct.dirent** %7
%39 = icmp ne %struct.dirent* %38, null
br i1 %39, label %40, label %95
40: ; preds = %36
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 1
%43 = load i8*, i8** %42
%44 = call i32 @chdir(i8* %43)
%45 = load %struct.dirent*, %struct.dirent** %7
%46 = getelementptr inbounds %struct.dirent, %struct.dirent* %45, i32 0, i32 0
%47 = load i8*, i8** %46
%48 = call i32 @stat(i8* %47, %struct.stat* %6)
store i32 %48, i32* %9
%49 = load i32, i32* %9
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %40
%52 = load %struct.dirent*, %struct.dirent** %7
%53 = getelementptr inbounds %struct.dirent, %struct.dirent* %52, i32 0, i32 0
%54 = load i8*, i8** %53
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), i8* %54)
br label %94
56: ; preds = %40
%57 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%58 = load i32, i32* %57
%59 = call i32 @S_ISDIR(i32 %58)
%60 = icmp ne i32 %59, 0
br i1 %60, label %93, label %61
61: ; preds = %56
%62 = load i8**, i8*** %5
%63 = getelementptr inbounds i8*, i8** %62, i64 2
%64 = load i8*, i8** %63
%65 = call i32 @strtol(i8* %64, i32 0, i32 8)
%66 = and i32 %65, 511
store i32 %66, i32* %12
%67 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%68 = load i32, i32* %67
%69 = and i32 %68, 511
store i32 %69, i32* %11
%70 = load %struct.dirent*, %struct.dirent** %7
%71 = getelementptr inbounds %struct.dirent, %struct.dirent* %70, i32 0, i32 0
%72 = load i8*, i8** %71
%73 = load i32, i32* %12
%74 = call i32 @chmod(i8* %72, i32 %73)
store i32 %74, i32* %10
%75 = load i32, i32* %10
%76 = icmp slt i32 %75, 0
br i1 %76, label %77, label %85
77: ; preds = %61
%78 = load %struct.dirent*, %struct.dirent** %7
%79 = getelementptr inbounds %struct.dirent, %struct.dirent* %78, i32 0, i32 0
%80 = load i8*, i8** %79
%81 = load i32, i32* %10
%82 = load i32, i32* %11
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* %80, i32 %81, i32 %82)
%84 = call i32 @exit(i32 -1)
unreachable
85: ; preds = %61
%86 = load %struct.dirent*, %struct.dirent** %7
%87 = getelementptr inbounds %struct.dirent, %struct.dirent* %86, i32 0, i32 0
%88 = load i8*, i8** %87
%89 = load i32, i32* %11
%90 = load i32, i32* %12
%91 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* %88, i32 %89, i32 %90)
br label %92
92: ; preds = %85
br label %93
93: ; preds = %92, %56
br label %94
94: ; preds = %93, %51
br label %36
95: ; preds = %36
%96 = load i32*, i32** %8
%97 = call i32 @closedir(i32* %96)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [39 x i8] c"Modo de uso: %s <pathname> <permisos>\0A\00"
@.str.1 = [44 x i8] c"Error: No se puede abrir el directorio: %s\0A\00"
@.str.3 = [49 x i8] c"Error al intentar acceder a los atributos de %s\0A\00"
@.str.4 = [14 x i8] c"%s : \09%d \09%o\0A\00"
@.str.5 = [14 x i8] c"%s : \09%o \09%o\0A\00"
@str = [21 x i8] c"<nombre>\09<ant>\09<new>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp eq i32 %0, 3
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32* @opendir(i8* %12)
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %19
15: ; preds = %10
%16 = load i8*, i8** %11
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = tail call i32 @exit(i32 2)
unreachable
19: ; preds = %10
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%21 = tail call %struct.dirent* @readdir(i32* %13)
%22 = icmp eq %struct.dirent* %21, null
br i1 %22, label %58, label %23
23: ; preds = %19
%24 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%25 = getelementptr inbounds i8*, i8** %1, i64 2
br label %26
26: ; preds = %23, %55
%27 = phi %struct.dirent* [ %21, %23 ], [ %56, %55 ]
%28 = load i8*, i8** %11
%29 = tail call i32 @chdir(i8* %28)
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i64 0, i32 0
%31 = load i8*, i8** %30
%32 = call i32 @stat(i8* %31, %struct.stat* %3)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %26
%35 = load i8*, i8** %30
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), i8* %35)
br label %55
37: ; preds = %26
%38 = load i32, i32* %24
%39 = tail call i32 @S_ISDIR(i32 %38)
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %55
41: ; preds = %37
%42 = load i8*, i8** %25
%43 = tail call i32 @strtol(i8* %42, i32 0, i32 8)
%44 = and i32 %43, 511
%45 = and i32 %38, 511
%46 = load i8*, i8** %30
%47 = tail call i32 @chmod(i8* %46, i32 %44)
%48 = icmp slt i32 %47, 0
%49 = load i8*, i8** %30
br i1 %48, label %50, label %53
50: ; preds = %41
%51 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* %49, i32 %47, i32 %45)
%52 = tail call i32 @exit(i32 -1)
unreachable
53: ; preds = %41
%54 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* %49, i32 %45, i32 %44)
br label %55
55: ; preds = %37, %53, %34
%56 = tail call %struct.dirent* @readdir(i32* %13)
%57 = icmp eq %struct.dirent* %56, null
br i1 %57, label %58, label %26
58: ; preds = %55, %19
%59 = tail call i32 @closedir(i32* %13)
ret i32 0
}