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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, char *, int);
int /*<<< orphan*/ stderr;
int check_num_files(char *prefix) {
  FILE *fil;
  fil = fopen(prefix, "r");
  if (fil != NULL) {
    fclose(fil);
    return 1;
  } else {
    int nfils = 0;
    while (nfils >= 0) {
      char fname[256];
      sprintf(fname, "%s.%d", prefix, nfils);
      fil = fopen(fname, "r");
      if (fil != NULL) {
        fclose(fil);
        nfils++;
      } else {
        if (nfils == 0) {
          fprintf(stderr, "CUTE: can't find file %s or %s.x\n", prefix, prefix);
          return -1;
        } else if (nfils == 1) {
          fprintf(stderr, "CUTE: only file %s found. Weird.\n", fname);
          return -1;
        } else {
          return nfils;
        }
      }
    }
  }
  fprintf(stderr, "CUTE: this shouldn't have happened \n");
  return -1;
}


x86 assembly (clang, -O0):
<check_num_files>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea6(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1183 <check_num_files+0x43>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    1283 <check_num_files+0x143>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     1260 <check_num_files+0x120>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
lea    0xe59(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x120(%rbp),%rdi
lea    0xe44(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11e7 <check_num_files+0xa7>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    125b <check_num_files+0x11b>
cmpl   $0x0,-0x1c(%rbp)
jne    121c <check_num_files+0xdc>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xdff(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1283 <check_num_files+0x143>
cmpl   $0x1,-0x1c(%rbp)
jne    1250 <check_num_files+0x110>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
lea    -0x120(%rbp),%rdx
lea    0xded(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1283 <check_num_files+0x143>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1283 <check_num_files+0x143>
jmp    118a <check_num_files+0x4a>
jmp    1265 <check_num_files+0x125>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
lea    0xdd7(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_num_files>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x100,%rsp
mov    %rdi,%r15
lea    0xea7(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1175 <check_num_files+0x35>
mov    %rax,%rdi
call   1030 <fclose@plt>
mov    $0x1,%ebp
jmp    122e <check_num_files+0xee>
lea    0xe86(%rip),%rsi
mov    %rsp,%rbx
xor    %ebp,%ebp
mov    %rbx,%rdi
mov    %r15,%rdx
xor    %ecx,%ecx
call   1060 <sprintf@plt>
lea    0xe6b(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     120c <check_num_files+0xcc>
lea    0xe59(%rip),%r14
mov    %rsp,%rbx
lea    0xe4d(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rax,%rdi
call   1030 <fclose@plt>
add    $0x1,%ebp
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
mov    %ebp,%ecx
call   1060 <sprintf@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1050 <fopen@plt>
test   %rax,%rax
jne    11c0 <check_num_files+0x80>
cmp    $0x1,%ebp
jne    122e <check_num_files+0xee>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
lea    0xe2a(%rip),%rsi
mov    %rsp,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    1229 <check_num_files+0xe9>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
lea    0xdec(%rip),%rsi
mov    %r15,%rdx
mov    %r15,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"%s.%d\00"
@stderr = global i32 0
@.str.2 = [34 x i8] c"CUTE: can't find file %s or %s.x\0A\00"
@.str.3 = [34 x i8] c"CUTE: only file %s found. Weird.\0A\00"
@.str.4 = [37 x i8] c"CUTE: this shouldn't have happened \0A\00"
define i32 @check_num_files(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca [256 x i8]
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %4
%9 = load i32*, i32** %4
%10 = icmp ne i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32*, i32** %4
%13 = call i32 @fclose(i32* %12)
store i32 1, i32* %2
br label %54
14: ; preds = %1
store i32 0, i32* %5
br label %15
15: ; preds = %49, %14
%16 = load i32, i32* %5
%17 = icmp sge i32 %16, 0
br i1 %17, label %18, label %50
18: ; preds = %15
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%20 = load i8*, i8** %3
%21 = load i32, i32* %5
%22 = call i32 @sprintf(i8* %19, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %20, i32 %21)
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%24 = call i32* @fopen(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %24, i32** %4
%25 = load i32*, i32** %4
%26 = icmp ne i32* %25, null
br i1 %26, label %27, label %32
27: ; preds = %18
%28 = load i32*, i32** %4
%29 = call i32 @fclose(i32* %28)
%30 = load i32, i32* %5
%31 = add nsw i32 %30, 1
store i32 %31, i32* %5
br label %49
32: ; preds = %18
%33 = load i32, i32* %5
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %32
%36 = load i32, i32* @stderr
%37 = load i8*, i8** %3
%38 = load i8*, i8** %3
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %37, i8* %38)
store i32 -1, i32* %2
br label %54
40: ; preds = %32
%41 = load i32, i32* %5
%42 = icmp eq i32 %41, 1
br i1 %42, label %43, label %47
43: ; preds = %40
%44 = load i32, i32* @stderr
%45 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %45)
store i32 -1, i32* %2
br label %54
47: ; preds = %40
%48 = load i32, i32* %5
store i32 %48, i32* %2
br label %54
49: ; preds = %27
br label %15
50: ; preds = %15
br label %51
51: ; preds = %50
%52 = load i32, i32* @stderr
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %2
br label %54
54: ; preds = %51, %47, %43, %35, %11
%55 = load i32, i32* %2
ret i32 %55
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"%s.%d\00"
@stderr = global i32 0
@.str.2 = [34 x i8] c"CUTE: can't find file %s or %s.x\0A\00"
@.str.3 = [34 x i8] c"CUTE: only file %s found. Weird.\0A\00"
define i32 @check_num_files(i8* %0) {
%2 = alloca [256 x i8]
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %10
5: ; preds = %1
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%7 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 0)
%8 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %14, label %20
10: ; preds = %1
%11 = tail call i32 @fclose(i32* %3)
br label %30
12: ; preds = %20
%13 = icmp eq i32 %22, 0
br i1 %13, label %17, label %28
14: ; preds = %5
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %0)
br label %28
17: ; preds = %12
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i8* %6)
br label %28
20: ; preds = %5, %20
%21 = phi i32* [ %26, %20 ], [ %8, %5 ]
%22 = phi i32 [ %24, %20 ], [ 0, %5 ]
%23 = tail call i32 @fclose(i32* %21)
%24 = add nuw nsw i32 %22, 1
%25 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %24)
%26 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%27 = icmp eq i32* %26, null
br i1 %27, label %12, label %20
28: ; preds = %12, %14, %17
%29 = phi i32 [ %24, %12 ], [ -1, %17 ], [ -1, %14 ]
br label %30
30: ; preds = %28, %10
%31 = phi i32 [ 1, %10 ], [ %29, %28 ]
ret i32 %31
}