#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(char str[], int ch) {
  int i, index = -1;
  for (i = 0; i < strlen(str); ++i) {
    if (str[i] == ch) {
      index = i;
      i = strlen(str);
    }
  }
  return index;
}


x86 assembly (clang, -O0):
<index>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
cmp    %ecx,%eax
jge    1183 <index+0x73>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    -0xc(%rbp),%eax
jne    1170 <index+0x60>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x10(%rbp)
jmp    1175 <index+0x65>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    112d <index+0x1d>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<index>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r15d
mov    %rdi,%r12
call   1030 <strlen@plt>
test   %eax,%eax
jle    116f <index+0x5f>
xor    %ebx,%ebx
mov    $0xffffffff,%r14d
jmp    115e <index+0x4e>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
mov    %ebx,%r14d
add    $0x1,%ebp
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %ebp,%ebx
cmp    %eax,%ebp
jge    1175 <index+0x65>
movslq %ebx,%rax
movsbl (%r12,%rax,1),%eax
cmp    %r15d,%eax
je     1140 <index+0x30>
mov    %ebx,%ebp
jmp    114d <index+0x3d>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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