#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 */

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int read(int, void *, int);
int /*<<< orphan*/ stderr;
void read_node(unsigned char *buffer, int sock, int how_many) {
  int pointer = 0;
  int maximum = how_many;
  int bytes_read;
  while (pointer < how_many) {
    bytes_read = read(sock, (void *)(buffer + pointer), maximum);
    if (bytes_read == -1) {
      perror("Error reading the socket.");
      exit(1);
    } else if (bytes_read == 0) {
      fprintf(stderr, "End of file.\n");
      exit(1);
    }
    pointer += bytes_read;
    maximum -= bytes_read;
  }
}


x86 assembly (clang, -O0):
<read_node>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11eb <read_node+0xab>
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11a4 <read_node+0x64>
lea    0xe6b(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    11cd <read_node+0x8d>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
lea    0xe5c(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
jmp    11d2 <read_node+0x92>
mov    -0x1c(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
jmp    115f <read_node+0x1f>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_node>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %edx,%edx
jle    1182 <read_node+0x42>
mov    %edx,%r12d
mov    %esi,%r14d
mov    %rdi,%r15
xor    %ebp,%ebp
mov    %edx,%ebx
nopl   0x0(%rax)
movslq %ebp,%rsi
add    %r15,%rsi
mov    %r14d,%edi
mov    %ebx,%edx
call   1030 <read@plt>
test   %eax,%eax
je     118b <read_node+0x4b>
cmp    $0xffffffff,%eax
je     11aa <read_node+0x6a>
add    %eax,%ebp
sub    %eax,%ebx
cmp    %r12d,%ebp
jl     1160 <read_node+0x20>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
lea    0xe7f(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
lea    0xe4f(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [26 x i8] c"Error reading the socket.\00"
@stderr = global i32 0
@.str.1 = [14 x i8] c"End of file.\0A\00"
define void @read_node(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 0, i32* %7
%10 = load i32, i32* %6
store i32 %10, i32* %8
br label %11
11: ; preds = %36, %3
%12 = load i32, i32* %7
%13 = load i32, i32* %6
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %43
15: ; preds = %11
%16 = load i32, i32* %5
%17 = load i8*, i8** %4
%18 = load i32, i32* %7
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i32, i32* %8
%22 = call i32 @read(i32 %16, i8* %20, i32 %21)
store i32 %22, i32* %9
%23 = load i32, i32* %9
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %15
%26 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %15
%29 = load i32, i32* %9
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %28
%32 = load i32, i32* @stderr
%33 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %28
br label %36
36: ; preds = %35
%37 = load i32, i32* %9
%38 = load i32, i32* %7
%39 = add nsw i32 %38, %37
store i32 %39, i32* %7
%40 = load i32, i32* %9
%41 = load i32, i32* %8
%42 = sub nsw i32 %41, %40
store i32 %42, i32* %8
br label %11
43: ; preds = %11
ret void
}

LLVM IR (-O2):
@.str = [26 x i8] c"Error reading the socket.\00"
@stderr = global i32 0
@.str.1 = [14 x i8] c"End of file.\0A\00"
define void @read_node(i8* nocapture %0, i32 %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %22
5: ; preds = %3, %18
%6 = phi i32 [ %19, %18 ], [ 0, %3 ]
%7 = phi i32 [ %20, %18 ], [ %2, %3 ]
%8 = sext i32 %6 to i64
%9 = getelementptr inbounds i8, i8* %0, i64 %8
%10 = tail call i32 @read(i32 %1, i8* %9, i32 %7)
switch i32 %10, label %18 [
i32 -1, label %11
i32 0, label %14
]
11: ; preds = %5
%12 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %5
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %5
%19 = add nsw i32 %10, %6
%20 = sub nsw i32 %7, %10
%21 = icmp slt i32 %19, %2
br i1 %21, label %5, label %22
22: ; preds = %18, %3
ret void
}