#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 */
typedef size_t ssize_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ realloc(char *, size_t);
int _bufFallBack(FILE *file, char *buf, size_t n, char *fallback,
                 size_t *fallbackSize) {
  size_t fallbackCopy = 0;
  ssize_t recvSize;
  if (*fallbackSize) {
    fallbackCopy = ((*fallbackSize < n) ? *fallbackSize : n);
    memcpy(buf, fallback, fallbackCopy);
  }
  if (fallbackCopy < n) {
    recvSize = fread(buf + fallbackCopy, 1, n - fallbackCopy, file);
    if (recvSize < 0) {
      printf("_bufFallBack: error\n");
      return -1;
    } else if (recvSize < (n - fallbackCopy)) {
      printf("_bufFallBack: less recv than expected\n");
      size_t total_written = recvSize + fallbackCopy;
      if (*fallbackSize < total_written) {
        realloc(fallback, total_written);
        *fallbackSize = total_written;
      }
      memcpy(fallback, buf, total_written);
      return total_written;
    }
  }
  free(fallback);
  *fallbackSize = 0;
  return n;
}


x86 assembly (clang, -O0):
<_bufFallBack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x30(%rbp),%rax
cmpq   $0x0,(%rax)
je     11c4 <_bufFallBack+0x74>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
cmp    -0x20(%rbp),%rax
jae    11a3 <_bufFallBack+0x53>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    11ab <_bufFallBack+0x5b>
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x38(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1294 <_bufFallBack+0x144>
mov    -0x18(%rbp),%rdi
add    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rdx
sub    -0x38(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jae    1217 <_bufFallBack+0xc7>
lea    0xdfa(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12af <_bufFallBack+0x15f>
mov    -0x40(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    -0x38(%rbp),%rcx
cmp    %rcx,%rax
jae    128a <_bufFallBack+0x13a>
lea    0xde2(%rip),%rdi
call   1050 <printf@plt>
mov    -0x40(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
cmp    -0x48(%rbp),%rax
jae    126d <_bufFallBack+0x11d>
mov    -0x28(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   1070 <realloc@plt>
mov    -0x48(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x48(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    12af <_bufFallBack+0x15f>
jmp    128f <_bufFallBack+0x13f>
jmp    1294 <_bufFallBack+0x144>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_bufFallBack>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r12
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbp
mov    %rdi,%r13
mov    (%r8),%rbx
test   %rbx,%rbx
je     118e <_bufFallBack+0x3e>
cmp    %r15,%rbx
cmovae %r15,%rbx
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1060 <memcpy@plt>
cmp    %r15,%rbx
jb     1195 <_bufFallBack+0x45>
jmp    11f3 <_bufFallBack+0xa3>
xor    %ebx,%ebx
cmp    %r15,%rbx
jae    11f3 <_bufFallBack+0xa3>
lea    (%rbx,%rbp,1),%rdi
mov    %rbp,(%rsp)
mov    %r13,%rcx
mov    %r15,%r13
sub    %rbx,%r13
mov    $0x1,%esi
mov    %r13,%rdx
call   1050 <fread@plt>
cmp    %r13,%rax
mov    (%rsp),%r13
jae    11f3 <_bufFallBack+0xa3>
mov    %rax,%rbp
lea    0xe3a(%rip),%rdi
call   1040 <puts@plt>
add    %rbx,%rbp
cmp    %rbp,(%r12)
jae    11e3 <_bufFallBack+0x93>
mov    %r14,%rdi
mov    %rbp,%rsi
call   1070 <realloc@plt>
mov    %rbp,(%r12)
mov    %r14,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1060 <memcpy@plt>
jmp    1206 <_bufFallBack+0xb6>
mov    %r14,%rdi
call   1030 <free@plt>
movq   $0x0,(%r12)
mov    %r15,%rbp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [21 x i8] c"_bufFallBack: error\0A\00"
@.str.1 = [39 x i8] c"_bufFallBack: less recv than expected\0A\00"
define i32 @_bufFallBack(i32* %0, i8* %1, i64 %2, i8* %3, i64* %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i64*
%12 = alloca i64
%13 = alloca i64
%14 = alloca i64
store i32* %0, i32** %7
store i8* %1, i8** %8
store i64 %2, i64* %9
store i8* %3, i8** %10
store i64* %4, i64** %11
store i64 0, i64* %12
%15 = load i64*, i64** %11
%16 = load i64, i64* %15
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %34
18: ; preds = %5
%19 = load i64*, i64** %11
%20 = load i64, i64* %19
%21 = load i64, i64* %9
%22 = icmp ult i64 %20, %21
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = load i64*, i64** %11
%25 = load i64, i64* %24
br label %28
26: ; preds = %18
%27 = load i64, i64* %9
br label %28
28: ; preds = %26, %23
%29 = phi i64 [ %25, %23 ], [ %27, %26 ]
store i64 %29, i64* %12
%30 = load i8*, i8** %8
%31 = load i8*, i8** %10
%32 = load i64, i64* %12
%33 = call i32 @memcpy(i8* %30, i8* %31, i64 %32)
br label %34
34: ; preds = %28, %5
%35 = load i64, i64* %12
%36 = load i64, i64* %9
%37 = icmp ult i64 %35, %36
br i1 %37, label %38, label %81
38: ; preds = %34
%39 = load i8*, i8** %8
%40 = load i64, i64* %12
%41 = getelementptr inbounds i8, i8* %39, i64 %40
%42 = load i64, i64* %9
%43 = load i64, i64* %12
%44 = sub i64 %42, %43
%45 = load i32*, i32** %7
%46 = call i64 @fread(i8* %41, i32 1, i64 %44, i32* %45)
store i64 %46, i64* %13
%47 = load i64, i64* %13
%48 = icmp ult i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %38
%50 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %6
br label %87
51: ; preds = %38
%52 = load i64, i64* %13
%53 = load i64, i64* %9
%54 = load i64, i64* %12
%55 = sub i64 %53, %54
%56 = icmp ult i64 %52, %55
br i1 %56, label %57, label %79
57: ; preds = %51
%58 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
%59 = load i64, i64* %13
%60 = load i64, i64* %12
%61 = add i64 %59, %60
store i64 %61, i64* %14
%62 = load i64*, i64** %11
%63 = load i64, i64* %62
%64 = load i64, i64* %14
%65 = icmp ult i64 %63, %64
br i1 %65, label %66, label %72
66: ; preds = %57
%67 = load i8*, i8** %10
%68 = load i64, i64* %14
%69 = call i32 @realloc(i8* %67, i64 %68)
%70 = load i64, i64* %14
%71 = load i64*, i64** %11
store i64 %70, i64* %71
br label %72
72: ; preds = %66, %57
%73 = load i8*, i8** %10
%74 = load i8*, i8** %8
%75 = load i64, i64* %14
%76 = call i32 @memcpy(i8* %73, i8* %74, i64 %75)
%77 = load i64, i64* %14
%78 = trunc i64 %77 to i32
store i32 %78, i32* %6
br label %87
79: ; preds = %51
br label %80
80: ; preds = %79
br label %81
81: ; preds = %80, %34
%82 = load i8*, i8** %10
%83 = call i32 @free(i8* %82)
%84 = load i64*, i64** %11
store i64 0, i64* %84
%85 = load i64, i64* %9
%86 = trunc i64 %85 to i32
store i32 %86, i32* %6
br label %87
87: ; preds = %81, %72, %49
%88 = load i32, i32* %6
ret i32 %88
}

LLVM IR (-O2):
@str = [38 x i8] c"_bufFallBack: less recv than expected\00"
define i32 @_bufFallBack(i32* nocapture %0, i8* %1, i64 %2, i8* %3, i64* nocapture %4) {
%6 = load i64, i64* %4
%7 = icmp eq i64 %6, 0
br i1 %7, label %12, label %8
8: ; preds = %5
%9 = icmp ult i64 %6, %2
%10 = select i1 %9, i64 %6, i64 %2
%11 = tail call i32 @memcpy(i8* %1, i8* %3, i64 %10)
br label %12
12: ; preds = %8, %5
%13 = phi i64 [ %10, %8 ], [ 0, %5 ]
%14 = icmp ult i64 %13, %2
br i1 %14, label %15, label %29
15: ; preds = %12
%16 = getelementptr inbounds i8, i8* %1, i64 %13
%17 = sub i64 %2, %13
%18 = tail call i64 @fread(i8* %16, i32 1, i64 %17, i32* %0)
%19 = icmp ult i64 %18, %17
br i1 %19, label %20, label %29
20: ; preds = %15
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
%22 = add i64 %18, %13
%23 = load i64, i64* %4
%24 = icmp ult i64 %23, %22
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = tail call i32 @realloc(i8* %3, i64 %22)
store i64 %22, i64* %4
br label %27
27: ; preds = %25, %20
%28 = tail call i32 @memcpy(i8* %3, i8* %1, i64 %22)
br label %31
29: ; preds = %15, %12
%30 = tail call i32 @free(i8* %3)
store i64 0, i64* %4
br label %31
31: ; preds = %29, %27
%32 = phi i64 [ %22, %27 ], [ %2, %29 ]
%33 = trunc i64 %32 to i32
ret i32 %33
}