#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*/ curfilename;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
char *get_tvtime_file(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, int);
int animation_get_num_frames(const char *filename_base) {
  int numframes = 0;
  for (;;) {
    char curfilename[1024];
    char *fullfilename;
    snprintf(curfilename, sizeof(curfilename), "%s_%04d.png", filename_base,
             numframes);
    fullfilename = get_tvtime_file(curfilename);
    if (!fullfilename) {
      return numframes;
    }
    free(fullfilename);
    numframes++;
  }
}


x86 assembly (clang, -O0):
<animation_get_num_frames>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    -0xc(%rbp),%r8d
mov    $0x400,%esi
lea    0xe9f(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x410(%rbp),%rdi
call   1050 <get_tvtime_file@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
jne    1193 <animation_get_num_frames+0x63>
mov    -0xc(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret
mov    -0x418(%rbp),%rdi
call   1030 <free@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1146 <animation_get_num_frames+0x16>

x86 assembly (clang, -O2):
<animation_get_num_frames>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%r15
lea    0xeb9(%rip),%rdx
mov    %rsp,%rbx
xor    %ebp,%ebp
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r15,%rcx
xor    %r8d,%r8d
call   1040 <snprintf@plt>
mov    %rbx,%rdi
call   1050 <get_tvtime_file@plt>
test   %rax,%rax
je     11ae <animation_get_num_frames+0x7e>
xor    %ebp,%ebp
lea    0xe8b(%rip),%r14
mov    %rsp,%rbx
nopl   0x0(%rax,%rax,1)
mov    %rax,%rdi
call   1030 <free@plt>
add    $0x1,%ebp
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r14,%rdx
mov    %r15,%rcx
mov    %ebp,%r8d
call   1040 <snprintf@plt>
mov    %rbx,%rdi
call   1050 <get_tvtime_file@plt>
test   %rax,%rax
jne    1180 <animation_get_num_frames+0x50>
mov    %ebp,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"%s_%04d.png\00"
define i32 @animation_get_num_frames(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca i8*
store i8* %0, i8** %2
store i32 0, i32* %3
br label %6
6: ; preds = %17, %1
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%8 = load i8*, i8** %2
%9 = load i32, i32* %3
%10 = call i32 @snprintf(i8* %7, i32 1024, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %8, i32 %9)
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%12 = call i8* @get_tvtime_file(i8* %11)
store i8* %12, i8** %5
%13 = load i8*, i8** %5
%14 = icmp ne i8* %13, null
br i1 %14, label %17, label %15
15: ; preds = %6
%16 = load i32, i32* %3
ret i32 %16
17: ; preds = %6
%18 = load i8*, i8** %5
%19 = call i32 @free(i8* %18)
%20 = load i32, i32* %3
%21 = add nsw i32 %20, 1
store i32 %21, i32* %3
br label %6
}

LLVM IR (-O2):
@.str = [12 x i8] c"%s_%04d.png\00"
define i32 @animation_get_num_frames(i8* %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 1024, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %0, i32 0)
%5 = call i8* @get_tvtime_file(i8* %3)
%6 = icmp eq i8* %5, null
br i1 %6, label %15, label %7
7: ; preds = %1, %7
%8 = phi i8* [ %13, %7 ], [ %5, %1 ]
%9 = phi i32 [ %11, %7 ], [ 0, %1 ]
%10 = call i32 @free(i8* %8)
%11 = add nuw nsw i32 %9, 1
%12 = call i32 @snprintf(i8* %3, i32 1024, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %11)
%13 = call i8* @get_tvtime_file(i8* %3)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %7
15: ; preds = %7, %1
%16 = phi i32 [ 0, %1 ], [ %11, %7 ]
ret i32 %16
}