#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 *);
char charAt(char *src, int index) {
  char *p = src;
  int i = 0;
  if (index < 0 || index > strlen(src))
    return 0;
  while (i < index)
    i++;
  return p[i];
}


x86 assembly (clang, -O0):
<charAt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     1154 <charAt+0x44>
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
cmp    %ecx,%eax
jle    115d <charAt+0x4d>
movb   $0x0,-0x1(%rbp)
jmp    118a <charAt+0x7a>
jmp    1162 <charAt+0x52>
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    117c <charAt+0x6c>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1162 <charAt+0x52>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x1(%rbp)
movsbl -0x1(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<charAt>:
push   %rbp
push   %rbx
push   %rax
test   %esi,%esi
js     1125 <charAt+0x15>
mov    %esi,%ebp
mov    %rdi,%rbx
call   1030 <strlen@plt>
cmp    %ebp,%eax
jge    1129 <charAt+0x19>
xor    %eax,%eax
jmp    112e <charAt+0x1e>
mov    %ebp,%eax
mov    (%rbx,%rax,1),%al
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
define signext i8 @charAt(i8* %0, i32 %1) {
%3 = icmp slt i32 %1, 0
br i1 %3, label %11, label %4
4: ; preds = %2
%5 = tail call i32 @strlen(i8* %0)
%6 = icmp slt i32 %5, %1
br i1 %6, label %11, label %7
7: ; preds = %4
%8 = zext i32 %1 to i64
%9 = getelementptr inbounds i8, i8* %0, i64 %8
%10 = load i8, i8* %9
br label %11
11: ; preds = %2, %4, %7
%12 = phi i8 [ %10, %7 ], [ 0, %4 ], [ 0, %2 ]
ret i8 %12
}