#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 /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(void *, void *, int);
int min(int, int);
int read4k(int /*<<< orphan*/ *, void *);
int readnb_1(FILE *fp, void *buff_n, unsigned int n) {
  int valid = 0, read = 0;
  int unreadbuf = n, readbuf = 0;
  void *buf4k_p = (void *)malloc(128);
  if (!fp)
    return 0;
  if (!buff_n)
    return 0;
  if (!n)
    return 0;
  do {
    read = read4k(fp, buf4k_p);
    if (unreadbuf >= 128) {
      memcpy(buff_n + readbuf, buf4k_p, read);
      readbuf = readbuf + read;
      unreadbuf = unreadbuf - read;
      if (read == 128)
        continue;
      else
        break;
    } else {
      valid = min(unreadbuf, read);
      memcpy(buff_n + readbuf, buf4k_p, valid);
      readbuf = readbuf + valid;
      unreadbuf = 0;
      break;
    }
  } while (1);
  *(char *)(buff_n + readbuf) = ('\0');
  return readbuf;
}


x86 assembly (clang, -O0):
<readnb_1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    $0x80,%edi
call   1050 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1193 <readnb_1+0x53>
movl   $0x0,-0x4(%rbp)
jmp    127d <readnb_1+0x13d>
cmpq   $0x0,-0x18(%rbp)
jne    11aa <readnb_1+0x6a>
movl   $0x0,-0x4(%rbp)
jmp    127d <readnb_1+0x13d>
cmpl   $0x0,-0x1c(%rbp)
jne    11c0 <readnb_1+0x80>
movl   $0x0,-0x4(%rbp)
jmp    127d <readnb_1+0x13d>
jmp    11c5 <readnb_1+0x85>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1060 <read4k@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x80,-0x28(%rbp)
jl     1222 <readnb_1+0xe2>
mov    -0x18(%rbp),%rdi
movslq -0x2c(%rbp),%rax
add    %rax,%rdi
mov    -0x38(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x2c(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%eax
sub    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x80,-0x24(%rbp)
jne    121d <readnb_1+0xdd>
jmp    125c <readnb_1+0x11c>
jmp    126b <readnb_1+0x12b>
mov    -0x28(%rbp),%edi
mov    -0x24(%rbp),%esi
call   1030 <min@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
movslq -0x2c(%rbp),%rax
add    %rax,%rdi
mov    -0x38(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x2c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x28(%rbp)
jmp    126b <readnb_1+0x12b>
mov    $0x1,%al
test   $0x1,%al
jne    11c5 <readnb_1+0x85>
jmp    126b <readnb_1+0x12b>
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readnb_1>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r13d
mov    %rsi,%r14
mov    %rdi,%r12
mov    $0x80,%edi
call   1050 <malloc@plt>
xor    %ebx,%ebx
test   %r12,%r12
je     11da <readnb_1+0x9a>
test   %r14,%r14
je     11da <readnb_1+0x9a>
test   %r13d,%r13d
je     11da <readnb_1+0x9a>
mov    %rax,%r15
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r12,%rdi
mov    %r15,%rsi
call   1060 <read4k@plt>
mov    %eax,%ebp
movslq %ebx,%rbx
cmp    $0x80,%r13d
jl     11b6 <readnb_1+0x76>
lea    (%r14,%rbx,1),%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1040 <memcpy@plt>
add    %ebp,%ebx
sub    %ebp,%r13d
cmp    $0x80,%ebp
je     1180 <readnb_1+0x40>
jmp    11d2 <readnb_1+0x92>
mov    %r13d,%edi
mov    %ebp,%esi
call   1030 <min@plt>
mov    %eax,%ebp
lea    (%r14,%rbx,1),%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1040 <memcpy@plt>
add    %ebp,%ebx
movslq %ebx,%rax
movb   $0x0,(%r14,%rax,1)
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @readnb_1(i32* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i32* %0, i32** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
%13 = load i32, i32* %7
store i32 %13, i32* %10
store i32 0, i32* %11
%14 = call i64 @malloc(i32 128)
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %12
%16 = load i32*, i32** %5
%17 = icmp ne i32* %16, null
br i1 %17, label %19, label %18
18: ; preds = %3
store i32 0, i32* %4
br label %73
19: ; preds = %3
%20 = load i8*, i8** %6
%21 = icmp ne i8* %20, null
br i1 %21, label %23, label %22
22: ; preds = %19
store i32 0, i32* %4
br label %73
23: ; preds = %19
%24 = load i32, i32* %7
%25 = icmp ne i32 %24, 0
br i1 %25, label %27, label %26
26: ; preds = %23
store i32 0, i32* %4
br label %73
27: ; preds = %23
br label %28
28: ; preds = %66, %27
%29 = load i32*, i32** %5
%30 = load i8*, i8** %12
%31 = call i32 @read4k(i32* %29, i8* %30)
store i32 %31, i32* %9
%32 = load i32, i32* %10
%33 = icmp sge i32 %32, 128
br i1 %33, label %34, label %52
34: ; preds = %28
%35 = load i8*, i8** %6
%36 = load i32, i32* %11
%37 = sext i32 %36 to i64
%38 = getelementptr i8, i8* %35, i64 %37
%39 = load i8*, i8** %12
%40 = load i32, i32* %9
%41 = call i32 @memcpy(i8* %38, i8* %39, i32 %40)
%42 = load i32, i32* %11
%43 = load i32, i32* %9
%44 = add nsw i32 %42, %43
store i32 %44, i32* %11
%45 = load i32, i32* %10
%46 = load i32, i32* %9
%47 = sub nsw i32 %45, %46
store i32 %47, i32* %10
%48 = load i32, i32* %9
%49 = icmp eq i32 %48, 128
br i1 %49, label %50, label %51
50: ; preds = %34
br label %66
51: ; preds = %34
br label %67
52: ; preds = %28
%53 = load i32, i32* %10
%54 = load i32, i32* %9
%55 = call i32 @min(i32 %53, i32 %54)
store i32 %55, i32* %8
%56 = load i8*, i8** %6
%57 = load i32, i32* %11
%58 = sext i32 %57 to i64
%59 = getelementptr i8, i8* %56, i64 %58
%60 = load i8*, i8** %12
%61 = load i32, i32* %8
%62 = call i32 @memcpy(i8* %59, i8* %60, i32 %61)
%63 = load i32, i32* %11
%64 = load i32, i32* %8
%65 = add nsw i32 %63, %64
store i32 %65, i32* %11
store i32 0, i32* %10
br label %67
66: ; preds = %50
br i1 true, label %28, label %67
67: ; preds = %66, %52, %51
%68 = load i8*, i8** %6
%69 = load i32, i32* %11
%70 = sext i32 %69 to i64
%71 = getelementptr i8, i8* %68, i64 %70
store i8 0, i8* %71
%72 = load i32, i32* %11
store i32 %72, i32* %4
br label %73
73: ; preds = %67, %26, %22, %18
%74 = load i32, i32* %4
ret i32 %74
}

LLVM IR (-O2):
define i32 @readnb_1(i32* %0, i8* %1, i32 %2) {
%4 = tail call i64 @malloc(i32 128)
%5 = inttoptr i64 %4 to i8*
%6 = icmp eq i32* %0, null
%7 = icmp eq i8* %1, null
%8 = or i1 %6, %7
%9 = icmp eq i32 %2, 0
%10 = or i1 %8, %9
br i1 %10, label %33, label %11
11: ; preds = %3, %16
%12 = phi i32 [ %21, %16 ], [ %2, %3 ]
%13 = phi i32 [ %20, %16 ], [ 0, %3 ]
%14 = tail call i32 @read4k(i32* %0, i8* %5)
%15 = icmp sgt i32 %12, 127
br i1 %15, label %16, label %23
16: ; preds = %11
%17 = sext i32 %13 to i64
%18 = getelementptr i8, i8* %1, i64 %17
%19 = tail call i32 @memcpy(i8* %18, i8* %5, i32 %14)
%20 = add nsw i32 %14, %13
%21 = sub nsw i32 %12, %14
%22 = icmp eq i32 %14, 128
br i1 %22, label %11, label %29
23: ; preds = %11
%24 = tail call i32 @min(i32 %12, i32 %14)
%25 = sext i32 %13 to i64
%26 = getelementptr i8, i8* %1, i64 %25
%27 = tail call i32 @memcpy(i8* %26, i8* %5, i32 %24)
%28 = add nsw i32 %24, %13
br label %29
29: ; preds = %16, %23
%30 = phi i32 [ %28, %23 ], [ %20, %16 ]
%31 = sext i32 %30 to i64
%32 = getelementptr i8, i8* %1, i64 %31
store i8 0, i8* %32
br label %33
33: ; preds = %3, %29
%34 = phi i32 [ %30, %29 ], [ 0, %3 ]
ret i32 %34
}