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

/* Variables and functions */
int strlen(char *);
int index_for_char(char c, char *string) {
  int index = -1;
  int string_amount = strlen(string);
  int i;
  for (i = 0; i < string_amount; i++) {
    if (c == string[i]) {
      index = i;
      break;
    }
  }
  return index;
}


x86 assembly (clang, -O0):
<index_for_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dil,%al
mov    %al,-0x1(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    117e <index_for_char+0x6e>
movsbl -0x1(%rbp),%eax
mov    -0x10(%rbp),%rcx
movslq -0x1c(%rbp),%rdx
movsbl (%rcx,%rdx,1),%ecx
cmp    %ecx,%eax
jne    116b <index_for_char+0x5b>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    117e <index_for_char+0x6e>
jmp    1170 <index_for_char+0x60>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    113c <index_for_char+0x2c>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<index_for_char>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
test   %ecx,%ecx
jle    1143 <index_for_char+0x33>
mov    %ecx,%edx
xor    %ecx,%ecx
nop
cmp    %bpl,(%rbx,%rcx,1)
je     1141 <index_for_char+0x31>
add    $0x1,%rcx
cmp    %rcx,%rdx
jne    1130 <index_for_char+0x20>
jmp    1143 <index_for_char+0x33>
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @index_for_char(i8 signext %0, i8* %1) {
%3 = alloca i8
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8 %0, i8* %3
store i8* %1, i8** %4
store i32 -1, i32* %5
%8 = load i8*, i8** %4
%9 = call i32 @strlen(i8* %8)
store i32 %9, i32* %6
store i32 0, i32* %7
br label %10
10: ; preds = %27, %2
%11 = load i32, i32* %7
%12 = load i32, i32* %6
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %30
14: ; preds = %10
%15 = load i8, i8* %3
%16 = sext i8 %15 to i32
%17 = load i8*, i8** %4
%18 = load i32, i32* %7
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %16, %22
br i1 %23, label %24, label %26
24: ; preds = %14
%25 = load i32, i32* %7
store i32 %25, i32* %5
br label %30
26: ; preds = %14
br label %27
27: ; preds = %26
%28 = load i32, i32* %7
%29 = add nsw i32 %28, 1
store i32 %29, i32* %7
br label %10
30: ; preds = %24, %10
%31 = load i32, i32* %5
ret i32 %31
}

LLVM IR (-O2):
define i32 @index_for_char(i8 signext %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = icmp sgt i32 %3, 0
br i1 %4, label %5, label %17
5: ; preds = %2
%6 = zext i32 %3 to i64
br label %7
7: ; preds = %5, %12
%8 = phi i64 [ 0, %5 ], [ %13, %12 ]
%9 = getelementptr inbounds i8, i8* %1, i64 %8
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, %0
br i1 %11, label %15, label %12
12: ; preds = %7
%13 = add nuw nsw i64 %8, 1
%14 = icmp eq i64 %13, %6
br i1 %14, label %17, label %7
15: ; preds = %7
%16 = trunc i64 %8 to i32
br label %17
17: ; preds = %12, %15, %2
%18 = phi i32 [ -1, %2 ], [ %16, %15 ], [ -1, %12 ]
ret i32 %18
}