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

/* Variables and functions */
int EOF;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ unlink(char *);
int main(int argc, char **argv) {
  DIR *dh;
  DIR *dh_tester;
  char fn_buffer[1024];
  struct dirent *file;
  char *input_dir;
  FILE *fp;
  char fc_buffer[512];
  int eof_check;
  if (argc < 2)
    return 1;
  input_dir = argv[1];
  dh = opendir(input_dir);
  if (dh == NULL)
    return 1;
  strcpy(fc_buffer, "");
  while (file = readdir(dh)) {
    if (file->d_name[0] == '.')
      continue;
    strcpy(fn_buffer, input_dir);
    strcat(fn_buffer, file->d_name);
    dh_tester = opendir(fn_buffer);
    if (dh_tester != NULL) {
      closedir(dh_tester);
      continue;
    }
    fp = fopen(fn_buffer, "r");
    eof_check = fscanf(fp, "%s", fc_buffer);
    fclose(fp);
    if (eof_check == EOF)
      strcpy(fc_buffer, "");
    unlink(fn_buffer);
    break;
  }
  closedir(dh);
  if (strlen(fc_buffer) == 0) {
    printf("{}\n");
  } else if (fc_buffer[0] == 'S') {
    printf("{\"scale\":\"%s\"}\n", fc_buffer);
  } else {
    printf("{\"scans\":\"%s\"}\n", fc_buffer);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x650,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11df <main+0x2f>
movl   $0x1,-0x4(%rbp)
jmp    13a6 <main+0x1f6>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x430(%rbp)
mov    -0x430(%rbp),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1215 <main+0x65>
movl   $0x1,-0x4(%rbp)
jmp    13a6 <main+0x1f6>
lea    -0x640(%rbp),%rdi
lea    0xdf5(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x428(%rbp)
cmp    $0x0,%rax
je     1329 <main+0x179>
mov    -0x428(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    125d <main+0xad>
jmp    1228 <main+0x78>
lea    -0x420(%rbp),%rdi
mov    -0x430(%rbp),%rsi
call   1040 <strcpy@plt>
lea    -0x420(%rbp),%rdi
mov    -0x428(%rbp),%rax
mov    (%rax),%rsi
call   10d0 <strcat@plt>
lea    -0x420(%rbp),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12af <main+0xff>
mov    -0x20(%rbp),%rdi
call   10a0 <closedir@plt>
jmp    1228 <main+0x78>
lea    -0x420(%rbp),%rdi
lea    0xd43(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x438(%rbp)
mov    -0x438(%rbp),%rdi
lea    -0x640(%rbp),%rdx
lea    0xd24(%rip),%rsi
call   1090 <fscanf@plt>
mov    %eax,-0x644(%rbp)
mov    -0x438(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x644(%rbp),%eax
mov    0x2ce6(%rip),%rcx
cmp    (%rcx),%eax
jne    131d <main+0x16d>
lea    -0x640(%rbp),%rdi
lea    0xd00(%rip),%rsi
call   1040 <strcpy@plt>
lea    -0x420(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x18(%rbp),%rdi
call   10a0 <closedir@plt>
lea    -0x640(%rbp),%rdi
call   1070 <strlen@plt>
cmp    $0x0,%rax
jne    135b <main+0x1ab>
lea    0xcb6(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    139f <main+0x1ef>
movsbl -0x640(%rbp),%eax
cmp    $0x53,%eax
jne    1385 <main+0x1d5>
lea    -0x640(%rbp),%rsi
lea    0xc90(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    139a <main+0x1ea>
lea    -0x640(%rbp),%rsi
lea    0xc86(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    139f <main+0x1ef>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x650,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x600,%rsp
mov    $0x1,%ebx
cmp    $0x2,%edi
jl     1302 <main+0x152>
mov    0x8(%rsi),%r14
mov    %r14,%rdi
call   1070 <opendir@plt>
test   %rax,%rax
je     1302 <main+0x152>
mov    %rax,%r15
lea    0xe28(%rip),%rsi
mov    %rsp,%rdi
call   1040 <strcpy@plt>
mov    %r15,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     12c3 <main+0x113>
mov    %rax,%rbx
lea    0x200(%rsp),%r12
jmp    1234 <main+0x84>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15,%rdi
call   10b0 <readdir@plt>
mov    %rax,%rbx
test   %rax,%rax
je     12c3 <main+0x113>
mov    (%rbx),%rax
cmpb   $0x2e,(%rax)
je     1220 <main+0x70>
mov    %r12,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    (%rbx),%rsi
mov    %r12,%rdi
call   10d0 <strcat@plt>
mov    %r12,%rdi
call   1070 <opendir@plt>
test   %rax,%rax
je     1269 <main+0xb9>
mov    %rax,%rdi
call   10a0 <closedir@plt>
jmp    1220 <main+0x70>
lea    0xd90(%rip),%rsi
lea    0x200(%rsp),%rdi
call   10c0 <fopen@plt>
mov    %rax,%rbx
lea    0xd7b(%rip),%rsi
mov    %rsp,%rdx
mov    %rax,%rdi
call   1090 <fscanf@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    0x2d45(%rip),%rax
cmp    (%rax),%ebp
jne    12b6 <main+0x106>
lea    0xd66(%rip),%rsi
mov    %rsp,%rdi
call   1040 <strcpy@plt>
lea    0x200(%rsp),%rdi
call   1030 <unlink@plt>
mov    %r15,%rdi
call   10a0 <closedir@plt>
mov    (%rsp),%al
cmp    $0x53,%al
je     12e6 <main+0x136>
test   %al,%al
jne    12ef <main+0x13f>
lea    0xd48(%rip),%rdi
call   1050 <puts@plt>
xor    %ebx,%ebx
jmp    1302 <main+0x152>
lea    0xd18(%rip),%rdi
jmp    12f6 <main+0x146>
lea    0xd1f(%rip),%rdi
xor    %ebx,%ebx
mov    %rsp,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %ebx,%eax
add    $0x600,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [1 x i8] zeroinitializer
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%s\00"
@EOF = global i32 0
@.str.3 = [4 x i8] c"{}\0A\00"
@.str.4 = [16 x i8] c"{\22scale\22:\22%s\22}\0A\00"
@.str.5 = [16 x i8] c"{\22scans\22:\22%s\22}\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca [1024 x i8]
%9 = alloca %struct.dirent*
%10 = alloca i8*
%11 = alloca i32*
%12 = alloca [512 x i8]
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 2
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 1, i32* %3
br label %96
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
store i8* %20, i8** %10
%21 = load i8*, i8** %10
%22 = call i32* @opendir(i8* %21)
store i32* %22, i32** %6
%23 = load i32*, i32** %6
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %26
25: ; preds = %17
store i32 1, i32* %3
br label %96
26: ; preds = %17
%27 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%28 = call i32 @strcpy(i8* %27, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %29
29: ; preds = %55, %41, %26
%30 = load i32*, i32** %6
%31 = call %struct.dirent* @readdir(i32* %30)
store %struct.dirent* %31, %struct.dirent** %9
%32 = icmp ne %struct.dirent* %31, null
br i1 %32, label %33, label %75
33: ; preds = %29
%34 = load %struct.dirent*, %struct.dirent** %9
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i32 0, i32 0
%36 = load i8*, i8** %35
%37 = getelementptr inbounds i8, i8* %36, i64 0
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 46
br i1 %40, label %41, label %42
41: ; preds = %33
br label %29
42: ; preds = %33
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%44 = load i8*, i8** %10
%45 = call i32 @strcpy(i8* %43, i8* %44)
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%47 = load %struct.dirent*, %struct.dirent** %9
%48 = getelementptr inbounds %struct.dirent, %struct.dirent* %47, i32 0, i32 0
%49 = load i8*, i8** %48
%50 = call i32 @strcat(i8* %46, i8* %49)
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%52 = call i32* @opendir(i8* %51)
store i32* %52, i32** %7
%53 = load i32*, i32** %7
%54 = icmp ne i32* %53, null
br i1 %54, label %55, label %58
55: ; preds = %42
%56 = load i32*, i32** %7
%57 = call i32 @closedir(i32* %56)
br label %29
58: ; preds = %42
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%60 = call i32* @fopen(i8* %59, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %60, i32** %11
%61 = load i32*, i32** %11
%62 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%63 = call i32 @fscanf(i32* %61, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %62)
store i32 %63, i32* %13
%64 = load i32*, i32** %11
%65 = call i32 @fclose(i32* %64)
%66 = load i32, i32* %13
%67 = load i32, i32* @EOF
%68 = icmp eq i32 %66, %67
br i1 %68, label %69, label %72
69: ; preds = %58
%70 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%71 = call i32 @strcpy(i8* %70, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %72
72: ; preds = %69, %58
%73 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%74 = call i32 @unlink(i8* %73)
br label %75
75: ; preds = %72, %29
%76 = load i32*, i32** %6
%77 = call i32 @closedir(i32* %76)
%78 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%79 = call i64 @strlen(i8* %78)
%80 = icmp eq i64 %79, 0
br i1 %80, label %81, label %83
81: ; preds = %75
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
br label %95
83: ; preds = %75
%84 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%85 = load i8, i8* %84
%86 = sext i8 %85 to i32
%87 = icmp eq i32 %86, 83
br i1 %87, label %88, label %91
88: ; preds = %83
%89 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %89)
br label %94
91: ; preds = %83
%92 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%93 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i8* %92)
br label %94
94: ; preds = %91, %88
br label %95
95: ; preds = %94, %81
store i32 0, i32* %3
br label %96
96: ; preds = %95, %25, %16
%97 = load i32, i32* %3
ret i32 %97
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [1 x i8] zeroinitializer
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%s\00"
@EOF = global i32 0
@.str.4 = [16 x i8] c"{\22scale\22:\22%s\22}\0A\00"
@.str.5 = [16 x i8] c"{\22scans\22:\22%s\22}\0A\00"
@str = [3 x i8] c"{}\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = alloca [512 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%7 = icmp slt i32 %0, 2
br i1 %7, label %53, label %8
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32* @opendir(i8* %10)
%12 = icmp eq i32* %11, null
br i1 %12, label %53, label %13
13: ; preds = %8
%14 = call i32 @strcpy(i8* %6, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%15 = call %struct.dirent* @readdir(i32* %11)
%16 = icmp eq %struct.dirent* %15, null
br i1 %16, label %44, label %17
17: ; preds = %13, %31
%18 = phi %struct.dirent* [ %32, %31 ], [ %15, %13 ]
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i64 0, i32 0
%20 = load i8*, i8** %19
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 46
br i1 %22, label %31, label %23
23: ; preds = %17
%24 = call i32 @strcpy(i8* %5, i8* %10)
%25 = load i8*, i8** %19
%26 = call i32 @strcat(i8* %5, i8* %25)
%27 = call i32* @opendir(i8* %5)
%28 = icmp eq i32* %27, null
br i1 %28, label %34, label %29
29: ; preds = %23
%30 = call i32 @closedir(i32* %27)
br label %31
31: ; preds = %29, %17
%32 = call %struct.dirent* @readdir(i32* %11)
%33 = icmp eq %struct.dirent* %32, null
br i1 %33, label %44, label %17
34: ; preds = %23
%35 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%36 = call i32 @fscanf(i32* %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %6)
%37 = call i32 @fclose(i32* %35)
%38 = load i32, i32* @EOF
%39 = icmp eq i32 %36, %38
br i1 %39, label %40, label %42
40: ; preds = %34
%41 = call i32 @strcpy(i8* %6, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %42
42: ; preds = %40, %34
%43 = call i32 @unlink(i8* %5)
br label %44
44: ; preds = %31, %13, %42
%45 = call i32 @closedir(i32* %11)
%46 = load i8, i8* %6
switch i8 %46, label %51 [
i8 0, label %47
i8 83, label %49
]
47: ; preds = %44
%48 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @str, i64 0, i64 0))
br label %53
49: ; preds = %44
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %6)
br label %53
51: ; preds = %44
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i8* %6)
br label %53
53: ; preds = %47, %51, %49, %8, %2
%54 = phi i32 [ 1, %2 ], [ 1, %8 ], [ 0, %49 ], [ 0, %51 ], [ 0, %47 ]
ret i32 %54
}