#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*/ sprintf(char *, char *, int);
char *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
int n_files(char *root_name, char msym) {
  char *p, tmp_name[200];
  int count = 0, flag = 1;
  FILE *in;
  strcpy(tmp_name, root_name);
  p = strchr(tmp_name, msym);
  if (p == NULL)
    return -1;
  while (flag == 1) {
    *p = '\000';
    sprintf(p, "%d", count);
    in = fopen(tmp_name, "r");
    flag = (in != NULL);
    if (flag == 1) {
      count++;
      fclose(in);
    }
  }
  return count;
}


x86 assembly (clang, -O0):
<n_files>:
push   %rbp
mov    %rsp,%rbp
sub    $0x100,%rsp
mov    %sil,%al
mov    %rdi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
movl   $0x0,-0xf4(%rbp)
movl   $0x1,-0xf8(%rbp)
lea    -0xf0(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0xf0(%rbp),%rdi
movsbl -0x11(%rbp),%esi
call   1050 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b4 <n_files+0x64>
movl   $0xffffffff,-0x4(%rbp)
jmp    1249 <n_files+0xf9>
jmp    11b9 <n_files+0x69>
cmpl   $0x1,-0xf8(%rbp)
jne    1240 <n_files+0xf0>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
mov    -0xf4(%rbp),%edx
lea    0xe22(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0xf0(%rbp),%rdi
lea    0xe12(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x100(%rbp)
cmpq   $0x0,-0x100(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0xf8(%rbp)
cmpl   $0x1,-0xf8(%rbp)
jne    123b <n_files+0xeb>
mov    -0xf4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xf4(%rbp)
mov    -0x100(%rbp),%rdi
call   1040 <fclose@plt>
jmp    11b9 <n_files+0x69>
mov    -0xf4(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x100,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<n_files>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0xd0,%rsp
mov    %esi,%ebx
mov    %rdi,%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1030 <strcpy@plt>
mov    %rbp,%rdi
mov    %ebx,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11ed <n_files+0x9d>
mov    %rax,%rbx
movb   $0x0,(%rax)
lea    0xe75(%rip),%rsi
xor    %ebp,%ebp
mov    %rax,%rdi
xor    %edx,%edx
call   1070 <sprintf@plt>
lea    0xe65(%rip),%rsi
mov    %rsp,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11f2 <n_files+0xa2>
xor    %ebp,%ebp
lea    0xe4c(%rip),%r14
lea    0xe48(%rip),%r15
mov    %rsp,%r12
xchg   %ax,%ax
add    $0x1,%ebp
mov    %rax,%rdi
call   1040 <fclose@plt>
movb   $0x0,(%rbx)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1070 <sprintf@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   1060 <fopen@plt>
test   %rax,%rax
jne    11c0 <n_files+0x70>
jmp    11f2 <n_files+0xa2>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0xd0,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @n_files(i8* %0, i8 signext %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i8*
%7 = alloca [200 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
store i8* %0, i8** %4
store i8 %1, i8* %5
store i32 0, i32* %8
store i32 1, i32* %9
%11 = getelementptr inbounds [200 x i8], [200 x i8]* %7, i64 0, i64 0
%12 = load i8*, i8** %4
%13 = call i32 @strcpy(i8* %11, i8* %12)
%14 = getelementptr inbounds [200 x i8], [200 x i8]* %7, i64 0, i64 0
%15 = load i8, i8* %5
%16 = call i8* @strchr(i8* %14, i8 signext %15)
store i8* %16, i8** %6
%17 = load i8*, i8** %6
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %20
19: ; preds = %2
store i32 -1, i32* %3
br label %44
20: ; preds = %2
br label %21
21: ; preds = %41, %20
%22 = load i32, i32* %9
%23 = icmp eq i32 %22, 1
br i1 %23, label %24, label %42
24: ; preds = %21
%25 = load i8*, i8** %6
store i8 0, i8* %25
%26 = load i8*, i8** %6
%27 = load i32, i32* %8
%28 = call i32 @sprintf(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %27)
%29 = getelementptr inbounds [200 x i8], [200 x i8]* %7, i64 0, i64 0
%30 = call i32* @fopen(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %30, i32** %10
%31 = load i32*, i32** %10
%32 = icmp ne i32* %31, null
%33 = zext i1 %32 to i32
store i32 %33, i32* %9
%34 = load i32, i32* %9
%35 = icmp eq i32 %34, 1
br i1 %35, label %36, label %41
36: ; preds = %24
%37 = load i32, i32* %8
%38 = add nsw i32 %37, 1
store i32 %38, i32* %8
%39 = load i32*, i32** %10
%40 = call i32 @fclose(i32* %39)
br label %41
41: ; preds = %36, %24
br label %21
42: ; preds = %21
%43 = load i32, i32* %8
store i32 %43, i32* %3
br label %44
44: ; preds = %42, %19
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @n_files(i8* %0, i8 signext %1) {
%3 = alloca [200 x i8]
%4 = getelementptr inbounds [200 x i8], [200 x i8]* %3, i64 0, i64 0
%5 = call i32 @strcpy(i8* %4, i8* %0)
%6 = call i8* @strchr(i8* %4, i8 signext %1)
%7 = icmp eq i8* %6, null
br i1 %7, label %20, label %8
8: ; preds = %2
store i8 0, i8* %6
%9 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 0)
%10 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %20, label %12
12: ; preds = %8, %12
%13 = phi i32* [ %18, %12 ], [ %10, %8 ]
%14 = phi i32 [ %15, %12 ], [ 0, %8 ]
%15 = add nuw nsw i32 %14, 1
%16 = call i32 @fclose(i32* %13)
store i8 0, i8* %6
%17 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %15)
%18 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %12
20: ; preds = %12, %8, %2
%21 = phi i32 [ -1, %2 ], [ 0, %8 ], [ %15, %12 ]
ret i32 %21
}