#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 */
scalar_t__ malloc(unsigned int);
char *strdup(char *);
int /*<<< orphan*/ strncpy(char *, char *, unsigned int);
char *copy_substring(char *str, unsigned int start_offset,
                     unsigned int end_offset) {
  if (end_offset > start_offset) {
    unsigned int substring_len = end_offset - start_offset;
    char *substr = (char *)malloc((substring_len + 1) * sizeof(char));
    strncpy(substr, str + start_offset, substring_len);
    substr[substring_len] = '\0';
    return substr;
  } else
    return strdup("");
}


x86 assembly (clang, -O0):
<copy_substring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jbe    119c <copy_substring+0x6c>
mov    -0x18(%rbp),%eax
sub    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,%eax
shl    $0x0,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%eax
add    %rax,%rsi
mov    -0x1c(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x1c(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ac <copy_substring+0x7c>
lea    0xe5d(%rip),%rdi
call   1050 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_substring>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
sub    %esi,%ebx
jbe    1172 <copy_substring+0x42>
mov    %esi,%ebp
mov    %rdi,%r14
lea    0x1(%rbx),%edi
call   1040 <malloc@plt>
mov    %rax,%r15
mov    %ebp,%eax
add    %rax,%r14
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
movb   $0x0,(%r15,%rbx,1)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe87(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <strdup@plt>

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

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define i8* @copy_substring(i8* %0, i32 %1, i32 %2) {
%4 = icmp ugt i32 %2, %1
br i1 %4, label %5, label %15
5: ; preds = %3
%6 = sub i32 %2, %1
%7 = add i32 %6, 1
%8 = tail call i64 @malloc(i32 %7)
%9 = inttoptr i64 %8 to i8*
%10 = zext i32 %1 to i64
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = tail call i32 @strncpy(i8* %9, i8* %11, i32 %6)
%13 = zext i32 %6 to i64
%14 = getelementptr inbounds i8, i8* %9, i64 %13
store i8 0, i8* %14
br label %17
15: ; preds = %3
%16 = tail call dereferenceable_or_null(1) i8* @strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %5
%18 = phi i8* [ %9, %5 ], [ %16, %15 ]
ret i8* %18
}