#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);
size_t strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
char *substring(char *s, size_t start, size_t count) {
  if (start + count > strlen(s)) {
    return NULL;
  }
  char *ret = malloc((sizeof(char) * count) + 1);
  strncpy(ret, s + start, count);
  return ret;
}


x86 assembly (clang, -O0):
<substring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
cmp    %rcx,%rax
jbe    1176 <substring+0x46>
movq   $0x0,-0x8(%rbp)
jmp    11aa <substring+0x7a>
mov    -0x20(%rbp),%rax
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<substring>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
lea    (%rdx,%rsi,1),%r12
call   1040 <strlen@plt>
cmp    %rax,%r12
jbe    1154 <substring+0x24>
xor    %r12d,%r12d
jmp    1171 <substring+0x41>
lea    0x1(%r14),%edi
call   1050 <malloc@plt>
mov    %rax,%r12
add    %r15,%rbx
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <strncpy@plt>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @substring(i8* %0, i64 %1, i64 %2) {
%4 = add i64 %2, %1
%5 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%6 = icmp ugt i64 %4, %5
br i1 %6, label %13, label %7
7: ; preds = %3
%8 = trunc i64 %2 to i32
%9 = add i32 %8, 1
%10 = tail call i8* @malloc(i32 %9)
%11 = getelementptr inbounds i8, i8* %0, i64 %1
%12 = tail call i32 @strncpy(i8* %10, i8* %11, i64 %2)
br label %13
13: ; preds = %3, %7
%14 = phi i8* [ %10, %7 ], [ null, %3 ]
ret i8* %14
}