#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 /*<<< orphan*/ assert(int);
char *malloc(int);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *get_substring(const char *str, int beg, int n) {
  assert(beg - 1 + n < strlen(str));
  char *ret = malloc(n + 1);
  strncpy(ret, (str + beg), n);
  *(ret + n) = 0;
  return ret;
}


x86 assembly (clang, -O0):
<get_substring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
cmp    %ecx,%eax
setl   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
movslq -0xc(%rbp),%rax
add    %rax,%rsi
mov    -0x10(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_substring>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %edx,%r12d
mov    %esi,%r14d
mov    %rdi,%r15
lea    (%r14,%r12,1),%ebx
add    $0xffffffff,%ebx
call   1040 <strlen@plt>
xor    %edi,%edi
cmp    %eax,%ebx
setl   %dil
call   1050 <assert@plt>
lea    0x1(%r12),%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
movslq %r14d,%rsi
add    %r15,%rsi
mov    %rax,%rdi
mov    %r12d,%edx
call   1030 <strncpy@plt>
movslq %r12d,%rax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @get_substring(i8* %0, i32 %1, i32 %2) {
%4 = add nsw i32 %1, -1
%5 = add nsw i32 %4, %2
%6 = tail call i32 @strlen(i8* %0)
%7 = icmp slt i32 %5, %6
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = add nsw i32 %2, 1
%11 = tail call i8* @malloc(i32 %10)
%12 = sext i32 %1 to i64
%13 = getelementptr inbounds i8, i8* %0, i64 %12
%14 = tail call i32 @strncpy(i8* %11, i8* %13, i32 %2)
%15 = sext i32 %2 to i64
%16 = getelementptr inbounds i8, i8* %11, i64 %15
store i8 0, i8* %16
ret i8* %11
}