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


x86 assembly (clang, -O0):
<astrleft>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1180 <astrleft+0x30>
movq   $0x0,-0x8(%rbp)
jmp    1254 <astrleft+0x104>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
cmp    %rcx,%rax
jb     11b3 <astrleft+0x63>
mov    -0x10(%rbp),%rdi
call   1040 <astrcpy@plt>
mov    %rax,-0x8(%rbp)
jmp    1254 <astrleft+0x104>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
shl    $0x0,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11ea <astrleft+0x9a>
lea    0xe27(%rip),%rdi
lea    0xe2b(%rip),%rsi
call   1070 <fatal@plt>
jmp    1242 <astrleft+0xf2>
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     121d <astrleft+0xcd>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
jmp    1225 <astrleft+0xd5>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1247 <astrleft+0xf7>
jmp    124c <astrleft+0xfc>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<astrleft>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rsi,%rsi
je     11a5 <astrleft+0x55>
mov    %rsi,%rbx
mov    %rdi,%r15
call   1030 <strlen@plt>
cmp    %rbx,%rax
jbe    11aa <astrleft+0x5a>
lea    0x1(%rbx),%rdi
call   1060 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
je     11bd <astrleft+0x6d>
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %rax,%r12
cmp    %rbx,%rax
cmova  %rbx,%r12
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1050 <memcpy@plt>
movb   $0x0,(%r14,%r12,1)
jmp    11d0 <astrleft+0x80>
xor    %r14d,%r14d
jmp    11d0 <astrleft+0x80>
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1040 <astrcpy@plt>
lea    0xe3c(%rip),%rdi
lea    0xe40(%rip),%rsi
call   1070 <fatal@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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