#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_2(FILE *fp, void *buff_n, unsigned int n) {
  void *buf4k_p = (void *)malloc(128);
  int times = n / 128;
  int remdr = n % 128;
  int i = 0;
  int widx = 0, read = 0;
  int eof = 0;
  for (i = 0; i < times; ++i) {
    read = read4k(fp, buf4k_p);
    memcpy(buff_n + widx, buf4k_p, read);
    widx += read;
    if (read < 128) {
      eof = 1;
      break;
    }
  }
  if (!eof) {
    read = read4k(fp, buf4k_p);
    memcpy(buff_n + widx, buf4k_p, min(read, remdr));
    widx += min(read, remdr);
  }
  *(char *)(buff_n + widx) = ('\0');
  return widx;
}


x86 assembly (clang, -O0):
<readnb_2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    $0x80,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
shr    $0x7,%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%eax
and    $0x7f,%eax
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    11fe <readnb_2+0xbe>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <read4k@plt>
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x30(%rbp),%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x34(%rbp),%eax
add    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
cmpl   $0x80,-0x34(%rbp)
jge    11eb <readnb_2+0xab>
movl   $0x1,-0x38(%rbp)
jmp    11fe <readnb_2+0xbe>
jmp    11f0 <readnb_2+0xb0>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1196 <readnb_2+0x56>
cmpl   $0x0,-0x38(%rbp)
jne    125a <readnb_2+0x11a>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <read4k@plt>
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x30(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x34(%rbp),%edi
mov    -0x28(%rbp),%esi
call   1030 <min@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
call   1040 <memcpy@plt>
mov    -0x34(%rbp),%edi
mov    -0x28(%rbp),%esi
call   1030 <min@plt>
add    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readnb_2>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r15
mov    %rdi,%r12
mov    $0x80,%edi
call   1050 <malloc@plt>
mov    %rax,%r13
xor    %ebp,%ebp
mov    %ebx,0x4(%rsp)
cmp    $0x80,%ebx
jb     11ae <readnb_2+0x6e>
mov    0x4(%rsp),%r14d
shr    $0x7,%r14d
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %r13,%rsi
call   1060 <read4k@plt>
mov    %eax,%ebx
movslq %ebp,%rbp
lea    (%r15,%rbp,1),%rdi
mov    %r13,%rsi
mov    %eax,%edx
call   1040 <memcpy@plt>
add    %ebx,%ebp
cmp    $0x80,%ebx
jl     11ec <readnb_2+0xac>
add    $0xffffffff,%r14d
jne    1180 <readnb_2+0x40>
mov    0x4(%rsp),%ebx
and    $0x7f,%ebx
mov    %r12,%rdi
mov    %r13,%rsi
call   1060 <read4k@plt>
mov    %eax,%r14d
movslq %ebp,%rbp
lea    (%r15,%rbp,1),%r12
mov    %eax,%edi
mov    %ebx,%esi
call   1030 <min@plt>
mov    %r12,%rdi
mov    %r13,%rsi
mov    %eax,%edx
call   1040 <memcpy@plt>
mov    %r14d,%edi
mov    %ebx,%esi
call   1030 <min@plt>
add    %eax,%ebp
movslq %ebp,%rax
movb   $0x0,(%r15,%rax,1)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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