#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 */
char *malloc(int);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *substring(char string[], int index) {
  int len = strlen(string);
  if (index > len)
    return string;
  char *newstring = malloc(sizeof(char) * len);
  if (!newstring)
    return string;
  strncpy(newstring, string + index, len - index);
  strcat(newstring, "\0");
  return newstring;
}


x86 assembly (clang, -O0):
<substring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jle    1174 <substring+0x34>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d1 <substring+0x91>
movslq -0x18(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    119f <substring+0x5f>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d1 <substring+0x91>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%edx
sub    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rdi
lea    0xe3c(%rip),%rsi
call   1060 <strcat@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<substring>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%rbx
call   1040 <strlen@plt>
mov    %eax,%ebp
sub    %r14d,%ebp
jl     118d <substring+0x4d>
mov    %eax,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     118d <substring+0x4d>
mov    %rax,%r15
movslq %r14d,%rax
add    %rax,%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
lea    0xe7e(%rip),%rsi
mov    %r15,%rdi
call   1060 <strcat@plt>
mov    %r15,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] zeroinitializer
define i8* @substring(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
%9 = call i32 @strlen(i8* %8)
store i32 %9, i32* %6
%10 = load i32, i32* %5
%11 = load i32, i32* %6
%12 = icmp sgt i32 %10, %11
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = load i8*, i8** %4
store i8* %14, i8** %3
br label %38
15: ; preds = %2
%16 = load i32, i32* %6
%17 = sext i32 %16 to i64
%18 = mul i64 1, %17
%19 = trunc i64 %18 to i32
%20 = call i8* @malloc(i32 %19)
store i8* %20, i8** %7
%21 = load i8*, i8** %7
%22 = icmp ne i8* %21, null
br i1 %22, label %25, label %23
23: ; preds = %15
%24 = load i8*, i8** %4
store i8* %24, i8** %3
br label %38
25: ; preds = %15
%26 = load i8*, i8** %7
%27 = load i8*, i8** %4
%28 = load i32, i32* %5
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = load i32, i32* %6
%32 = load i32, i32* %5
%33 = sub nsw i32 %31, %32
%34 = call i32 @strncpy(i8* %26, i8* %30, i32 %33)
%35 = load i8*, i8** %7
%36 = call i32 @strcat(i8* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%37 = load i8*, i8** %7
store i8* %37, i8** %3
br label %38
38: ; preds = %25, %23, %13
%39 = load i8*, i8** %3
ret i8* %39
}

LLVM IR (-O2):
@.str = [2 x i8] zeroinitializer
define i8* @substring(i8* %0, i32 %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = icmp slt i32 %3, %1
br i1 %4, label %14, label %5
5: ; preds = %2
%6 = tail call i8* @malloc(i32 %3)
%7 = icmp eq i8* %6, null
br i1 %7, label %14, label %8
8: ; preds = %5
%9 = sext i32 %1 to i64
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = sub nsw i32 %3, %1
%12 = tail call i32 @strncpy(i8* %6, i8* %10, i32 %11)
%13 = tail call i32 @strcat(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %8, %5, %2
%15 = phi i8* [ %0, %2 ], [ %6, %8 ], [ %0, %5 ]
ret i8* %15
}