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

/* Variables and functions */
int find_key(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, int);
int list_getnextindex(const char *name) {
  int i, index;
  char s[1024];
  for (i = 0; i < 2048; i++) {
    snprintf(s, sizeof(s), "%s%d", name, i);
    index = find_key(s);
    if (index == -1)
      return i;
  };
  return -1;
}


x86 assembly (clang, -O0):
<list_getnextindex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x800,-0x14(%rbp)
jge    119a <list_getnextindex+0x7a>
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x14(%rbp),%r8d
mov    $0x400,%esi
lea    0xea2(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x420(%rbp),%rdi
call   1040 <find_key@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1187 <list_getnextindex+0x67>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11a1 <list_getnextindex+0x81>
jmp    118c <list_getnextindex+0x6c>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1136 <list_getnextindex+0x16>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<list_getnextindex>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%r14
xor    %ebx,%ebx
lea    0xec6(%rip),%r15
mov    %rsp,%r12
nopl   (%rax)
mov    %r12,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
mov    %r14,%rcx
mov    %ebx,%r8d
call   1030 <snprintf@plt>
mov    %r12,%rdi
call   1040 <find_key@plt>
cmp    $0xffffffff,%eax
je     1173 <list_getnextindex+0x53>
add    $0x1,%ebx
cmp    $0x800,%ebx
jne    1140 <list_getnextindex+0x20>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%s%d\00"
define i32 @list_getnextindex(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1024 x i8]
store i8* %0, i8** %3
store i32 0, i32* %4
br label %7
7: ; preds = %22, %1
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 2048
br i1 %9, label %10, label %25
10: ; preds = %7
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%12 = load i8*, i8** %3
%13 = load i32, i32* %4
%14 = call i32 @snprintf(i8* %11, i32 1024, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %12, i32 %13)
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%16 = call i32 @find_key(i8* %15)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %10
%20 = load i32, i32* %4
store i32 %20, i32* %2
br label %26
21: ; preds = %10
br label %22
22: ; preds = %21
%23 = load i32, i32* %4
%24 = add nsw i32 %23, 1
store i32 %24, i32* %4
br label %7
25: ; preds = %7
store i32 -1, i32* %2
br label %26
26: ; preds = %25, %19
%27 = load i32, i32* %2
ret i32 %27
}

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