#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 */
unsigned int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, unsigned int);
int substr(char *instr, unsigned int start, unsigned int length, char *retstr) {
  unsigned int i;
  char *startstr;
  if ((start + length) > strlen(instr))
    return (-1);
  for (i = 0; i < start; i++)
    startstr = instr++;
  strncpy(retstr, instr, length);
  retstr[length] = '\0';
  return (0);
}


x86 assembly (clang, -O0):
<substr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
cmp    %ecx,%eax
jbe    1161 <substr+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b7 <substr+0x97>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    1195 <substr+0x75>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1168 <substr+0x48>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<substr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %edx,%r15d
mov    %esi,%r13d
mov    %rdi,%rbx
lea    (%r15,%r13,1),%ebp
call   1040 <strlen@plt>
mov    $0xffffffff,%r12d
cmp    %eax,%ebp
ja     117b <substr+0x5b>
lea    -0x1(%r13),%eax
add    $0x1,%rax
xor    %r12d,%r12d
cmp    $0x1,%r13d
mov    $0x0,%ecx
cmovae %rax,%rcx
add    %rcx,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1030 <strncpy@plt>
mov    %r15d,%eax
movb   $0x0,(%r14,%rax,1)
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @substr(i8* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%12 = load i32, i32* %7
%13 = load i32, i32* %8
%14 = add i32 %12, %13
%15 = load i8*, i8** %6
%16 = call i32 @strlen(i8* %15)
%17 = icmp ugt i32 %14, %16
br i1 %17, label %18, label %19
18: ; preds = %4
store i32 -1, i32* %5
br label %39
19: ; preds = %4
store i32 0, i32* %10
br label %20
20: ; preds = %27, %19
%21 = load i32, i32* %10
%22 = load i32, i32* %7
%23 = icmp ult i32 %21, %22
br i1 %23, label %24, label %30
24: ; preds = %20
%25 = load i8*, i8** %6
%26 = getelementptr inbounds i8, i8* %25, i32 1
store i8* %26, i8** %6
store i8* %25, i8** %11
br label %27
27: ; preds = %24
%28 = load i32, i32* %10
%29 = add i32 %28, 1
store i32 %29, i32* %10
br label %20
30: ; preds = %20
%31 = load i8*, i8** %9
%32 = load i8*, i8** %6
%33 = load i32, i32* %8
%34 = call i32 @strncpy(i8* %31, i8* %32, i32 %33)
%35 = load i8*, i8** %9
%36 = load i32, i32* %8
%37 = zext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
store i8 0, i8* %38
store i32 0, i32* %5
br label %39
39: ; preds = %30, %18
%40 = load i32, i32* %5
ret i32 %40
}

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