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

/* Variables and functions */
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ perror(char *);
scalar_t__ write(int /*<<< orphan*/, void *, scalar_t__);
void read_mem_segment(void *addr, ssize_t len) {
  static const ssize_t chunk_size = 1024;
  ssize_t chunk, written;
  ssize_t n = 0;
  char *addr_n = (char *)addr;
  while (n < len) {
    if (len - n >= chunk_size) {
      chunk = chunk_size;
    } else {
      chunk = len - n;
    }
    written = write(STDOUT_FILENO, (void *)addr_n, chunk);
    if (written < 0) {
      perror("read_mem_segment write");
    }
    n += written;
    addr_n += written;
  }
}


x86 assembly (clang, -O0):
<read_mem_segment>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jge    11c9 <read_mem_segment+0xa9>
mov    -0x10(%rbp),%rax
sub    -0x28(%rbp),%rax
cmp    $0x400,%rax
jl     116f <read_mem_segment+0x4f>
movq   $0x400,-0x18(%rbp)
jmp    117b <read_mem_segment+0x5b>
mov    -0x10(%rbp),%rax
sub    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11ac <read_mem_segment+0x8c>
lea    0xe61(%rip),%rdi
call   1040 <perror@plt>
mov    -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1140 <read_mem_segment+0x20>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_mem_segment>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rsi,%rsi
jle    118d <read_mem_segment+0x6d>
mov    %rsi,%r14
mov    %rdi,%r12
xor    %ebx,%ebx
mov    $0x400,%ebp
mov    0x2eac(%rip),%r15
jmp    115b <read_mem_segment+0x3b>
cs nopw 0x0(%rax,%rax,1)
add    %r13,%rbx
add    %r13,%r12
cmp    %r14,%rbx
jge    118d <read_mem_segment+0x6d>
mov    %r14,%rdx
sub    %rbx,%rdx
cmp    $0x400,%rdx
cmovge %rbp,%rdx
mov    (%r15),%edi
mov    %r12,%rsi
call   1030 <write@plt>
mov    %rax,%r13
test   %rax,%rax
jns    1150 <read_mem_segment+0x30>
lea    0xe7a(%rip),%rdi
call   1040 <perror@plt>
jmp    1150 <read_mem_segment+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@read_mem_segment.chunk_size = internal constant i64 1024
@STDOUT_FILENO = global i32 0
@.str = [23 x i8] c"read_mem_segment write\00"
define void @read_mem_segment(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
store i64 0, i64* %7
%9 = load i8*, i8** %3
store i8* %9, i8** %8
br label %10
10: ; preds = %33, %2
%11 = load i64, i64* %7
%12 = load i64, i64* %4
%13 = icmp slt i64 %11, %12
br i1 %13, label %14, label %40
14: ; preds = %10
%15 = load i64, i64* %4
%16 = load i64, i64* %7
%17 = sub nsw i64 %15, %16
%18 = icmp sge i64 %17, 1024
br i1 %18, label %19, label %20
19: ; preds = %14
store i64 1024, i64* %5
br label %24
20: ; preds = %14
%21 = load i64, i64* %4
%22 = load i64, i64* %7
%23 = sub nsw i64 %21, %22
store i64 %23, i64* %5
br label %24
24: ; preds = %20, %19
%25 = load i32, i32* @STDOUT_FILENO
%26 = load i8*, i8** %8
%27 = load i64, i64* %5
%28 = call i64 @write(i32 %25, i8* %26, i64 %27)
store i64 %28, i64* %6
%29 = load i64, i64* %6
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %24
%32 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %33
33: ; preds = %31, %24
%34 = load i64, i64* %6
%35 = load i64, i64* %7
%36 = add nsw i64 %35, %34
store i64 %36, i64* %7
%37 = load i64, i64* %6
%38 = load i8*, i8** %8
%39 = getelementptr inbounds i8, i8* %38, i64 %37
store i8* %39, i8** %8
br label %10
40: ; preds = %10
ret void
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@.str = [23 x i8] c"read_mem_segment write\00"
define void @read_mem_segment(i8* nocapture %0, i64 %1) {
%3 = icmp sgt i64 %1, 0
br i1 %3, label %4, label %19
4: ; preds = %2, %15
%5 = phi i8* [ %17, %15 ], [ %0, %2 ]
%6 = phi i64 [ %16, %15 ], [ 0, %2 ]
%7 = sub nsw i64 %1, %6
%8 = icmp slt i64 %7, 1024
%9 = select i1 %8, i64 %7, i64 1024
%10 = load i32, i32* @STDOUT_FILENO
%11 = tail call i64 @write(i32 %10, i8* %5, i64 %9)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %4
%14 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %4
%16 = add nsw i64 %11, %6
%17 = getelementptr inbounds i8, i8* %5, i64 %11
%18 = icmp slt i64 %16, %1
br i1 %18, label %4, label %19
19: ; preds = %15, %2
ret void
}