#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*/ strncpy(char *, char const *, size_t);
char *xmalloc(int);
size_t xmax(int, int /*<<< orphan*/);
size_t xmin(size_t, int);
size_t xstrlen(char const *);
char *xstrsub(const char *src, int begin, int len) {
  if (src == NULL)
    return NULL;
  char *ret;
  size_t s_full = xstrlen(src);
  int l = len == -1 ? (int)s_full : len;
  int ind;
  ret = xmalloc((xmin(s_full, l) + 1) * sizeof(char));
  ind = begin < 0 ? xmax((int)s_full + begin, 0) : xmin(s_full, begin);
  strncpy(ret, src + ind, xmin(s_full, l));
  ret[xmin(s_full, l)] = '\0';
  return ret;
}


x86 assembly (clang, -O0):
<xstrsub>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    117a <xstrsub+0x2a>
movq   $0x0,-0x8(%rbp)
jmp    125c <xstrsub+0x10c>
mov    -0x10(%rbp),%rdi
call   1070 <xstrlen@plt>
mov    %rax,-0x28(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    119d <xstrsub+0x4d>
mov    -0x28(%rbp),%rax
mov    %eax,-0x34(%rbp)
jmp    11a3 <xstrsub+0x53>
mov    -0x18(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1050 <xmin@plt>
add    $0x1,%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1040 <xmalloc@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11eb <xstrsub+0x9b>
mov    -0x28(%rbp),%rax
mov    %eax,%edi
add    -0x14(%rbp),%edi
xor    %esi,%esi
call   1060 <xmax@plt>
mov    %rax,-0x40(%rbp)
jmp    11fb <xstrsub+0xab>
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <xmin@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x30(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x50(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1050 <xmin@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    %rax,%rdx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1050 <xmin@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xstrsub>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rdi,%rdi
je     119b <xstrsub+0x4b>
mov    %edx,%ebp
mov    %esi,%r12d
mov    %rdi,%rbx
call   1070 <xstrlen@plt>
mov    %rax,%r14
cmp    $0xffffffff,%ebp
cmove  %r14d,%ebp
mov    %rax,%rdi
mov    %ebp,%esi
call   1050 <xmin@plt>
lea    0x1(%rax),%edi
call   1040 <xmalloc@plt>
mov    %rax,%r15
test   %r12d,%r12d
js     11a0 <xstrsub+0x50>
mov    %r14,%rdi
mov    %r12d,%esi
call   1050 <xmin@plt>
jmp    11ad <xstrsub+0x5d>
xor    %r15d,%r15d
jmp    11d9 <xstrsub+0x89>
add    %r14d,%r12d
mov    %r12d,%edi
xor    %esi,%esi
call   1060 <xmax@plt>
cltq
add    %rax,%rbx
mov    %r14,%rdi
mov    %ebp,%esi
call   1050 <xmin@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1030 <strncpy@plt>
mov    %r14,%rdi
mov    %ebp,%esi
call   1050 <xmin@plt>
movb   $0x0,(%r15,%rax,1)
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @xstrsub(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%12 = load i8*, i8** %5
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %15
14: ; preds = %3
store i8* null, i8** %4
br label %64
15: ; preds = %3
%16 = load i8*, i8** %5
%17 = call i64 @xstrlen(i8* %16)
store i64 %17, i64* %9
%18 = load i32, i32* %7
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load i64, i64* %9
%22 = trunc i64 %21 to i32
br label %25
23: ; preds = %15
%24 = load i32, i32* %7
br label %25
25: ; preds = %23, %20
%26 = phi i32 [ %22, %20 ], [ %24, %23 ]
store i32 %26, i32* %10
%27 = load i64, i64* %9
%28 = load i32, i32* %10
%29 = call i64 @xmin(i64 %27, i32 %28)
%30 = add i64 %29, 1
%31 = mul i64 %30, 1
%32 = trunc i64 %31 to i32
%33 = call i8* @xmalloc(i32 %32)
store i8* %33, i8** %8
%34 = load i32, i32* %6
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %42
36: ; preds = %25
%37 = load i64, i64* %9
%38 = trunc i64 %37 to i32
%39 = load i32, i32* %6
%40 = add nsw i32 %38, %39
%41 = call i64 @xmax(i32 %40, i32 0)
br label %46
42: ; preds = %25
%43 = load i64, i64* %9
%44 = load i32, i32* %6
%45 = call i64 @xmin(i64 %43, i32 %44)
br label %46
46: ; preds = %42, %36
%47 = phi i64 [ %41, %36 ], [ %45, %42 ]
%48 = trunc i64 %47 to i32
store i32 %48, i32* %11
%49 = load i8*, i8** %8
%50 = load i8*, i8** %5
%51 = load i32, i32* %11
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %50, i64 %52
%54 = load i64, i64* %9
%55 = load i32, i32* %10
%56 = call i64 @xmin(i64 %54, i32 %55)
%57 = call i32 @strncpy(i8* %49, i8* %53, i64 %56)
%58 = load i8*, i8** %8
%59 = load i64, i64* %9
%60 = load i32, i32* %10
%61 = call i64 @xmin(i64 %59, i32 %60)
%62 = getelementptr inbounds i8, i8* %58, i64 %61
store i8 0, i8* %62
%63 = load i8*, i8** %8
store i8* %63, i8** %4
br label %64
64: ; preds = %46, %14
%65 = load i8*, i8** %4
ret i8* %65
}

LLVM IR (-O2):
define i8* @xstrsub(i8* %0, i32 %1, i32 %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %29, label %5
5: ; preds = %3
%6 = tail call i64 @xstrlen(i8* %0)
%7 = icmp eq i32 %2, -1
%8 = trunc i64 %6 to i32
%9 = select i1 %7, i32 %8, i32 %2
%10 = tail call i64 @xmin(i64 %6, i32 %9)
%11 = trunc i64 %10 to i32
%12 = add i32 %11, 1
%13 = tail call i8* @xmalloc(i32 %12)
%14 = icmp slt i32 %1, 0
br i1 %14, label %15, label %18
15: ; preds = %5
%16 = add nsw i32 %8, %1
%17 = tail call i64 @xmax(i32 %16, i32 0)
br label %20
18: ; preds = %5
%19 = tail call i64 @xmin(i64 %6, i32 %1)
br label %20
20: ; preds = %18, %15
%21 = phi i64 [ %17, %15 ], [ %19, %18 ]
%22 = shl i64 %21, 32
%23 = ashr exact i64 %22, 32
%24 = getelementptr inbounds i8, i8* %0, i64 %23
%25 = tail call i64 @xmin(i64 %6, i32 %9)
%26 = tail call i32 @strncpy(i8* %13, i8* %24, i64 %25)
%27 = tail call i64 @xmin(i64 %6, i32 %9)
%28 = getelementptr inbounds i8, i8* %13, i64 %27
store i8 0, i8* %28
br label %29
29: ; preds = %3, %20
%30 = phi i8* [ %13, %20 ], [ null, %3 ]
ret i8* %30
}