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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
scalar_t__ S_ISREG(int /*<<< orphan*/);
scalar_t__ cj_encrypt(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, int /*<<< orphan*/);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int encrypt(char *key, char *folder) {
  int fn_len = 4092;
  char fn[fn_len];
  struct stat stat_buf;
  int fd;
  DIR *dirp;
  struct dirent *dentry;
  dirp = opendir(folder);
  if (dirp == NULL) {
    perror("try to open a directory\n");
    return -1;
  }
  while ((dentry = readdir(dirp)) != NULL) {
    snprintf(fn, fn_len - 1, "%s%s", folder, dentry->d_name);
    fn[fn_len - 1] = '\0';
    printf("%s\n", fn);
    if (stat(fn, &stat_buf) == 0 && S_ISREG(stat_buf.st_mode)) {
      fd = open(fn, O_RDWR);
      if (fd < 0) {
        printf("warning: can't open in file: %s?\n", fn);
      } else if (cj_encrypt(key, fd)) {
        fprintf(stderr, "something when doing encryption: %s\n", fn);
        return -1;
      }
      close(fd);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<encrypt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0xffc,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1224 <encrypt+0x74>
lea    0xdf4(%rip),%rdi
call   10d0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    133c <encrypt+0x18c>
jmp    1229 <encrypt+0x79>
mov    -0x48(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x50(%rbp)
cmp    $0x0,%rax
je     132e <encrypt+0x17e>
mov    -0x60(%rbp),%rdi
mov    -0x1c(%rbp),%esi
sub    $0x1,%esi
mov    -0x18(%rbp),%rcx
mov    -0x50(%rbp),%rax
mov    (%rax),%r8d
lea    0xdbd(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x60(%rbp),%rsi
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,(%rsi,%rax,1)
lea    0xde9(%rip),%rdi
call   1040 <printf@plt>
mov    -0x60(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jne    1329 <encrypt+0x179>
mov    -0x38(%rbp),%edi
call   1090 <S_ISREG@plt>
cmp    $0x0,%rax
je     1329 <encrypt+0x179>
mov    -0x60(%rbp),%rdi
mov    0x2d37(%rip),%rax
mov    (%rax),%esi
call   10c0 <open@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    12da <encrypt+0x12a>
mov    -0x60(%rbp),%rsi
lea    0xd4e(%rip),%rdi
call   1040 <printf@plt>
jmp    1321 <encrypt+0x171>
mov    -0x10(%rbp),%rdi
mov    -0x3c(%rbp),%esi
call   10b0 <cj_encrypt@plt>
cmp    $0x0,%rax
je     131c <encrypt+0x16c>
mov    -0x60(%rbp),%rdx
mov    0x2cdd(%rip),%rax
mov    (%rax),%edi
lea    0xd3c(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    133c <encrypt+0x18c>
jmp    1321 <encrypt+0x171>
mov    -0x3c(%rbp),%edi
call   1060 <close@plt>
jmp    1229 <encrypt+0x79>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encrypt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %rsi,%r13
mov    %rdi,0x8(%rsp)
mov    %rsi,%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     12d1 <encrypt+0x121>
mov    %rax,%r15
mov    %rax,%rdi
call   10a0 <readdir@plt>
xor    %ecx,%ecx
test   %rax,%rax
je     12e2 <encrypt+0x132>
lea    0xe22(%rip),%r12
lea    0x10(%rsp),%r14
lea    0xe5e(%rip),%rbx
mov    %rsp,%rbp
jmp    1236 <encrypt+0x86>
lea    0xe0f(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %r12d,%edi
call   1060 <close@plt>
mov    %rbp,%r12
mov    %rsp,%rbp
mov    %r15,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     12cd <encrypt+0x11d>
mov    (%rax),%r8d
mov    %r14,%rdi
mov    $0xffb,%esi
mov    %r12,%rdx
mov    %r13,%rcx
call   1050 <snprintf@plt>
movb   $0x0,0x100b(%rsp)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %r14,%rdi
mov    %rbp,%rsi
call   1080 <stat@plt>
test   %rax,%rax
jne    1225 <encrypt+0x75>
mov    (%rsp),%edi
call   1090 <S_ISREG@plt>
test   %rax,%rax
je     1225 <encrypt+0x75>
mov    %r12,%rbp
mov    0x2d62(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   10c0 <open@plt>
mov    %eax,%r12d
test   %eax,%eax
js     1208 <encrypt+0x58>
mov    0x8(%rsp),%rdi
mov    %r12d,%esi
call   10b0 <cj_encrypt@plt>
test   %rax,%rax
je     1217 <encrypt+0x67>
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
lea    0xd7f(%rip),%rsi
lea    0x10(%rsp),%rdx
call   1070 <fprintf@plt>
jmp    12dd <encrypt+0x12d>
xor    %ecx,%ecx
jmp    12e2 <encrypt+0x132>
lea    0xd28(%rip),%rdi
call   10d0 <perror@plt>
mov    $0xffffffff,%ecx
mov    %ecx,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
%struct.dirent = type { i32 }
@.str = [25 x i8] c"try to open a directory\0A\00"
@.str.1 = [5 x i8] c"%s%s\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@O_RDWR = global i32 0
@.str.3 = [34 x i8] c"warning: can't open in file: %s?\0A\00"
@stderr = global i32 0
@.str.4 = [37 x i8] c"something when doing encryption: %s\0A\00"
define i32 @encrypt(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca %struct.stat
%10 = alloca i32
%11 = alloca i32*
%12 = alloca %struct.dirent*
%13 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 4092, i32* %6
%14 = load i32, i32* %6
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %7
%17 = alloca i8, i64 %15
store i64 %15, i64* %8
%18 = load i8*, i8** %5
%19 = call i32* @opendir(i8* %18)
store i32* %19, i32** %11
%20 = load i32*, i32** %11
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %24
22: ; preds = %2
%23 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
store i32 1, i32* %13
br label %70
24: ; preds = %2
br label %25
25: ; preds = %68, %24
%26 = load i32*, i32** %11
%27 = call %struct.dirent* @readdir(i32* %26)
store %struct.dirent* %27, %struct.dirent** %12
%28 = icmp ne %struct.dirent* %27, null
br i1 %28, label %29, label %69
29: ; preds = %25
%30 = load i32, i32* %6
%31 = sub nsw i32 %30, 1
%32 = load i8*, i8** %5
%33 = load %struct.dirent*, %struct.dirent** %12
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @snprintf(i8* %17, i32 %31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %32, i32 %35)
%37 = load i32, i32* %6
%38 = sub nsw i32 %37, 1
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %17, i64 %39
store i8 0, i8* %40
%41 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %17)
%42 = call i64 @stat(i8* %17, %struct.stat* %9)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %68
44: ; preds = %29
%45 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%46 = load i32, i32* %45
%47 = call i64 @S_ISREG(i32 %46)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %68
49: ; preds = %44
%50 = load i32, i32* @O_RDWR
%51 = call i32 @open(i8* %17, i32 %50)
store i32 %51, i32* %10
%52 = load i32, i32* %10
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %49
%55 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %17)
br label %65
56: ; preds = %49
%57 = load i8*, i8** %4
%58 = load i32, i32* %10
%59 = call i64 @cj_encrypt(i8* %57, i32 %58)
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %64
61: ; preds = %56
%62 = load i32, i32* @stderr
%63 = call i32 @fprintf(i32 %62, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i8* %17)
store i32 -1, i32* %3
store i32 1, i32* %13
br label %70
64: ; preds = %56
br label %65
65: ; preds = %64, %54
%66 = load i32, i32* %10
%67 = call i32 @close(i32 %66)
br label %68
68: ; preds = %65, %44, %29
br label %25
69: ; preds = %25
store i32 0, i32* %3
store i32 1, i32* %13
br label %70
70: ; preds = %69, %61, %22
%71 = load i8*, i8** %7
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i32 }
@.str = [25 x i8] c"try to open a directory\0A\00"
@.str.1 = [5 x i8] c"%s%s\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@O_RDWR = global i32 0
@.str.3 = [34 x i8] c"warning: can't open in file: %s?\0A\00"
@stderr = global i32 0
@.str.4 = [37 x i8] c"something when doing encryption: %s\0A\00"
define i32 @encrypt(i8* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = alloca [4092 x i8]
%5 = getelementptr inbounds [4092 x i8], [4092 x i8]* %4, i64 0, i64 0
%6 = bitcast %struct.stat* %3 to i8*
%7 = tail call i32* @opendir(i8* %1)
%8 = icmp eq i32* %7, null
br i1 %8, label %15, label %9
9: ; preds = %2
%10 = call %struct.dirent* @readdir(i32* %7)
%11 = icmp eq %struct.dirent* %10, null
br i1 %11, label %46, label %12
12: ; preds = %9
%13 = getelementptr inbounds [4092 x i8], [4092 x i8]* %4, i64 0, i64 4091
%14 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
br label %17
15: ; preds = %2
%16 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %46
17: ; preds = %12, %43
%18 = phi %struct.dirent* [ %10, %12 ], [ %44, %43 ]
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i64 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @snprintf(i8* %5, i32 4091, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %1, i32 %20)
store i8 0, i8* %13
%22 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %5)
%23 = call i64 @stat(i8* %5, %struct.stat* %3)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %43
25: ; preds = %17
%26 = load i32, i32* %14
%27 = call i64 @S_ISREG(i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %43, label %29
29: ; preds = %25
%30 = load i32, i32* @O_RDWR
%31 = call i32 @open(i8* %5, i32 %30)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %5)
br label %41
35: ; preds = %29
%36 = call i64 @cj_encrypt(i8* %0, i32 %31)
%37 = icmp eq i64 %36, 0
br i1 %37, label %41, label %38
38: ; preds = %35
%39 = load i32, i32* @stderr
%40 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i8* %5)
br label %46
41: ; preds = %35, %33
%42 = call i32 @close(i32 %31)
br label %43
43: ; preds = %41, %25, %17
%44 = call %struct.dirent* @readdir(i32* %7)
%45 = icmp eq %struct.dirent* %44, null
br i1 %45, label %46, label %17
46: ; preds = %43, %9, %38, %15
%47 = phi i32 [ -1, %15 ], [ -1, %38 ], [ 0, %9 ], [ 0, %43 ]
ret i32 %47
}