#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 int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ log_sys(char *);
char *realloc(char *, int);
int tread(int, char *, int, int);
ssize_t readmore(int sockfd, char **bpp, int off, int *bszp) {
  ssize_t nr;
  char *bp = *bpp;
  int bsz = *bszp;
  if (off >= bsz) {
    bsz += 8192;
    if ((bp = realloc(*bpp, bsz)) == NULL)
      log_sys("readmore: can't allocate bigger read buffer");
    *bszp = bsz;
    *bpp = bp;
  }
  if ((nr = tread(sockfd, &bp[off], bsz - off, 1)) > 0)
    return (off + nr);
  else
    return (-1);
}


x86 assembly (clang, -O0):
<readmore>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x34(%rbp),%eax
jl     11ae <readmore+0x7e>
mov    -0x34(%rbp),%eax
add    $0x2000,%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x34(%rbp),%esi
call   1040 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    119a <readmore+0x6a>
lea    0xe6b(%rip),%rdi
call   1030 <log_sys@plt>
mov    -0x34(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x30(%rbp),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
mov    -0x34(%rbp),%edx
sub    -0x14(%rbp),%edx
mov    $0x1,%ecx
call   1050 <tread@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jle    11e6 <readmore+0xb6>
mov    -0x14(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ed <readmore+0xbd>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readmore>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %edi,%r14d
mov    (%rsi),%r15
mov    (%rcx),%ebp
cmp    %edx,%ebp
jg     117b <readmore+0x4b>
mov    %rcx,%r12
mov    %rsi,%r13
add    $0x2000,%ebp
mov    %r15,%rdi
mov    %ebp,%esi
call   1040 <realloc@plt>
mov    %rax,%r15
test   %rax,%rax
jne    1173 <readmore+0x43>
lea    0xe92(%rip),%rdi
call   1030 <log_sys@plt>
mov    %ebp,(%r12)
mov    %r15,0x0(%r13)
movslq %ebx,%rsi
add    %r15,%rsi
sub    %ebx,%ebp
mov    %r14d,%edi
mov    %ebp,%edx
mov    $0x1,%ecx
call   1050 <tread@plt>
add    %eax,%ebx
test   %eax,%eax
mov    $0xffffffff,%eax
cmovg  %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [44 x i8] c"readmore: can't allocate bigger read buffer\00"
define i32 @readmore(i32 %0, i8** %1, i32 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
store i32 %0, i32* %6
store i8** %1, i8*** %7
store i32 %2, i32* %8
store i32* %3, i32** %9
%13 = load i8**, i8*** %7
%14 = load i8*, i8** %13
store i8* %14, i8** %11
%15 = load i32*, i32** %9
%16 = load i32, i32* %15
store i32 %16, i32* %12
%17 = load i32, i32* %8
%18 = load i32, i32* %12
%19 = icmp sge i32 %17, %18
br i1 %19, label %20, label %35
20: ; preds = %4
%21 = load i32, i32* %12
%22 = add nsw i32 %21, 8192
store i32 %22, i32* %12
%23 = load i8**, i8*** %7
%24 = load i8*, i8** %23
%25 = load i32, i32* %12
%26 = call i8* @realloc(i8* %24, i32 %25)
store i8* %26, i8** %11
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %30
28: ; preds = %20
%29 = call i32 @log_sys(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
br label %30
30: ; preds = %28, %20
%31 = load i32, i32* %12
%32 = load i32*, i32** %9
store i32 %31, i32* %32
%33 = load i8*, i8** %11
%34 = load i8**, i8*** %7
store i8* %33, i8** %34
br label %35
35: ; preds = %30, %4
%36 = load i32, i32* %6
%37 = load i8*, i8** %11
%38 = load i32, i32* %8
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
%41 = load i32, i32* %12
%42 = load i32, i32* %8
%43 = sub nsw i32 %41, %42
%44 = call i32 @tread(i32 %36, i8* %40, i32 %43, i32 1)
store i32 %44, i32* %10
%45 = icmp sgt i32 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %35
%47 = load i32, i32* %8
%48 = load i32, i32* %10
%49 = add nsw i32 %47, %48
store i32 %49, i32* %5
br label %51
50: ; preds = %35
store i32 -1, i32* %5
br label %51
51: ; preds = %50, %46
%52 = load i32, i32* %5
ret i32 %52
}

LLVM IR (-O2):
@.str = [44 x i8] c"readmore: can't allocate bigger read buffer\00"
define i32 @readmore(i32 %0, i8** nocapture %1, i32 %2, i32* nocapture %3) {
%5 = load i8*, i8** %1
%6 = load i32, i32* %3
%7 = icmp sgt i32 %6, %2
br i1 %7, label %15, label %8
8: ; preds = %4
%9 = add nsw i32 %6, 8192
%10 = tail call i8* @realloc(i8* %5, i32 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = tail call i32 @log_sys(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %8
store i32 %9, i32* %3
store i8* %10, i8** %1
br label %15
15: ; preds = %14, %4
%16 = phi i8* [ %10, %14 ], [ %5, %4 ]
%17 = phi i32 [ %9, %14 ], [ %6, %4 ]
%18 = sext i32 %2 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = sub nsw i32 %17, %2
%21 = tail call i32 @tread(i32 %0, i8* %19, i32 %20, i32 1)
%22 = icmp sgt i32 %21, 0
%23 = add nsw i32 %21, %2
%24 = select i1 %22, i32 %23, i32 -1
ret i32 %24
}