#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 */
int /*<<< orphan*/ free(char *);
size_t getline(char **, size_t *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
size_t read_buffer(FILE *file, char *buf, size_t len) {
  char *line = NULL;
  size_t size = 0;
  ssize_t read;
  read = getline(&line, &size, file);
  memcpy(buf, line, len);
  size -= read;
  free(line);
  return read < len ? read : len;
}


x86 assembly (clang, -O0):
<read_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdx
lea    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1050 <getline@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x30(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11ad <read_buffer+0x7d>
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    11b5 <read_buffer+0x85>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_buffer>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%rdx
movq   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
call   1050 <getline@plt>
mov    %rax,%r15
mov    (%rsp),%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
sub    %r15,0x8(%rsp)
mov    (%rsp),%rdi
call   1030 <free@plt>
cmp    %rbx,%r15
cmovb  %r15,%rbx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @read_buffer(i32* %0, i8* %1, i64 %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
store i32* %0, i32** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i8* null, i8** %7
store i64 0, i64* %8
%10 = load i32*, i32** %4
%11 = call i64 @getline(i8** %7, i64* %8, i32* %10)
store i64 %11, i64* %9
%12 = load i8*, i8** %5
%13 = load i8*, i8** %7
%14 = load i64, i64* %6
%15 = call i32 @memcpy(i8* %12, i8* %13, i64 %14)
%16 = load i64, i64* %9
%17 = load i64, i64* %8
%18 = sub i64 %17, %16
store i64 %18, i64* %8
%19 = load i8*, i8** %7
%20 = call i32 @free(i8* %19)
%21 = load i64, i64* %9
%22 = load i64, i64* %6
%23 = icmp ult i64 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %3
%25 = load i64, i64* %9
br label %28
26: ; preds = %3
%27 = load i64, i64* %6
br label %28
28: ; preds = %26, %24
%29 = phi i64 [ %25, %24 ], [ %27, %26 ]
ret i64 %29
}

LLVM IR (-O2):
define i64 @read_buffer(i32* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = bitcast i8** %4 to i8*
store i8* null, i8** %4
%7 = bitcast i64* %5 to i8*
store i64 0, i64* %5
%8 = call i64 @getline(i8** %4, i64* %5, i32* %0)
%9 = load i8*, i8** %4
%10 = call i32 @memcpy(i8* %1, i8* %9, i64 %2)
%11 = load i64, i64* %5
%12 = sub i64 %11, %8
store i64 %12, i64* %5
%13 = load i8*, i8** %4
%14 = call i32 @free(i8* %13)
%15 = icmp ult i64 %8, %2
%16 = select i1 %15, i64 %8, i64 %2
ret i64 %16
}