#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*/ fatal(char const *, char *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strlen(char const *);
char *astrmid(const char *src, size_t offset, size_t count) {
  static const char fnName[] __attribute__((unused)) = "astrmid()";
  char *midString = NULL;
  size_t actualCount;
  if (offset >= strlen(src)) {
    return NULL;
  } else {
    if ((count == 0) || (offset + count > strlen(src))) {
      actualCount = strlen(src) - offset;
    } else {
      actualCount = count;
    }
    if ((midString = (char *)malloc((actualCount + 1) * sizeof(char))) ==
        NULL) {
      fatal(fnName, "Out of memory!");
    } else {
      memcpy(midString, &src[offset], actualCount);
      midString[actualCount] = '\0';
    }
  }
  return midString;
}


x86 assembly (clang, -O0):
<astrmid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
cmp    %rcx,%rax
jb     118a <astrmid+0x4a>
movq   $0x0,-0x8(%rbp)
jmp    123d <astrmid+0xfd>
cmpq   $0x0,-0x20(%rbp)
je     11ba <astrmid+0x7a>
mov    -0x18(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
cmp    %rcx,%rax
jbe    11d0 <astrmid+0x90>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
sub    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    11d8 <astrmid+0x98>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
shl    $0x0,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    120f <astrmid+0xcf>
lea    0xe02(%rip),%rdi
lea    0xe05(%rip),%rsi
call   1060 <fatal@plt>
jmp    1230 <astrmid+0xf0>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1235 <astrmid+0xf5>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<astrmid>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%r14
call   1030 <strlen@plt>
mov    %rax,%rcx
sub    %r15,%rcx
jbe    119c <astrmid+0x5c>
lea    (%r12,%r15,1),%rdx
cmp    %rax,%rdx
mov    %r12,%rbx
cmova  %rcx,%rbx
test   %r12,%r12
cmove  %rcx,%rbx
lea    0x1(%rbx),%rdi
call   1050 <malloc@plt>
mov    %rax,%r12
test   %rax,%rax
je     11a1 <astrmid+0x61>
add    %r15,%r14
mov    %r12,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
movb   $0x0,(%r12,%rbx,1)
jmp    11b4 <astrmid+0x74>
xor    %r12d,%r12d
jmp    11b4 <astrmid+0x74>
lea    0xe58(%rip),%rdi
lea    0xe5b(%rip),%rsi
call   1060 <fatal@plt>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@astrmid.fnName = internal constant [10 x i8] c"astrmid()\00"
@.str = [15 x i8] c"Out of memory!\00"
define i8* @astrmid(i8* %0, i64 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i8* null, i8** %8
%10 = load i64, i64* %6
%11 = load i8*, i8** %5
%12 = call i64 @strlen(i8* %11)
%13 = icmp uge i64 %10, %12
br i1 %13, label %14, label %15
14: ; preds = %3
store i8* null, i8** %4
br label %54
15: ; preds = %3
%16 = load i64, i64* %7
%17 = icmp eq i64 %16, 0
br i1 %17, label %25, label %18
18: ; preds = %15
%19 = load i64, i64* %6
%20 = load i64, i64* %7
%21 = add i64 %19, %20
%22 = load i8*, i8** %5
%23 = call i64 @strlen(i8* %22)
%24 = icmp ugt i64 %21, %23
br i1 %24, label %25, label %30
25: ; preds = %18, %15
%26 = load i8*, i8** %5
%27 = call i64 @strlen(i8* %26)
%28 = load i64, i64* %6
%29 = sub i64 %27, %28
store i64 %29, i64* %9
br label %32
30: ; preds = %18
%31 = load i64, i64* %7
store i64 %31, i64* %9
br label %32
32: ; preds = %30, %25
%33 = load i64, i64* %9
%34 = add i64 %33, 1
%35 = mul i64 %34, 1
%36 = call i64 @malloc(i64 %35)
%37 = inttoptr i64 %36 to i8*
store i8* %37, i8** %8
%38 = icmp eq i8* %37, null
br i1 %38, label %39, label %41
39: ; preds = %32
%40 = call i32 @fatal(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @astrmid.fnName, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %51
41: ; preds = %32
%42 = load i8*, i8** %8
%43 = load i8*, i8** %5
%44 = load i64, i64* %6
%45 = getelementptr inbounds i8, i8* %43, i64 %44
%46 = load i64, i64* %9
%47 = call i32 @memcpy(i8* %42, i8* %45, i64 %46)
%48 = load i8*, i8** %8
%49 = load i64, i64* %9
%50 = getelementptr inbounds i8, i8* %48, i64 %49
store i8 0, i8* %50
br label %51
51: ; preds = %41, %39
br label %52
52: ; preds = %51
%53 = load i8*, i8** %8
store i8* %53, i8** %4
br label %54
54: ; preds = %52, %14
%55 = load i8*, i8** %4
ret i8* %55
}

LLVM IR (-O2):
@astrmid.fnName = internal constant [10 x i8] c"astrmid()\00"
@.str = [15 x i8] c"Out of memory!\00"
define i8* @astrmid(i8* %0, i64 %1, i64 %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = icmp ugt i64 %4, %1
br i1 %5, label %6, label %23
6: ; preds = %3
%7 = icmp eq i64 %2, 0
%8 = add i64 %2, %1
%9 = icmp ugt i64 %8, %4
%10 = select i1 %7, i1 true, i1 %9
%11 = sub i64 %4, %1
%12 = select i1 %10, i64 %11, i64 %2
%13 = add i64 %12, 1
%14 = tail call i64 @malloc(i64 %13)
%15 = inttoptr i64 %14 to i8*
%16 = icmp eq i64 %14, 0
br i1 %16, label %17, label %19
17: ; preds = %6
%18 = tail call i32 @fatal(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @astrmid.fnName, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %23
19: ; preds = %6
%20 = getelementptr inbounds i8, i8* %0, i64 %1
%21 = tail call i32 @memcpy(i8* %15, i8* %20, i64 %12)
%22 = getelementptr inbounds i8, i8* %15, i64 %12
store i8 0, i8* %22
br label %23
23: ; preds = %19, %17, %3
%24 = phi i8* [ null, %3 ], [ %15, %17 ], [ %15, %19 ]
ret i8* %24
}