#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*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *, char *);
int read(int, unsigned char *, int);
char *strerror(int /*<<< orphan*/);
int read_data(int sock, unsigned char *buf, int len) {
  int l;
  int to_read = len;
  do {
    if ((l = read(sock, buf, to_read)) < 0) {
      printf("Error in read: %s\n", strerror(errno));
      exit(1);
    }
    to_read -= len;
  } while (to_read > 0);
  return len;
}


x86 assembly (clang, -O0):
<read_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    119a <read_data+0x5a>
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %rax,%rsi
lea    0xe75(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x14(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     1158 <read_data+0x18>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_data>:
push   %rbx
mov    %edx,%ebx
call   1040 <read@plt>
test   %eax,%eax
js     1150 <read_data+0x10>
mov    %ebx,%eax
pop    %rbx
ret
mov    0x2e89(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe9b(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"Error in read: %s\0A\00"
@errno = global i32 0
define i32 @read_data(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i32, i32* %6
store i32 %9, i32* %8
br label %10
10: ; preds = %25, %3
%11 = load i32, i32* %4
%12 = load i8*, i8** %5
%13 = load i32, i32* %8
%14 = call i32 @read(i32 %11, i8* %12, i32 %13)
store i32 %14, i32* %7
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %10
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %10
%22 = load i32, i32* %6
%23 = load i32, i32* %8
%24 = sub nsw i32 %23, %22
store i32 %24, i32* %8
br label %25
25: ; preds = %21
%26 = load i32, i32* %8
%27 = icmp sgt i32 %26, 0
br i1 %27, label %10, label %28
28: ; preds = %25
%29 = load i32, i32* %6
ret i32 %29
}

LLVM IR (-O2):
@.str = [19 x i8] c"Error in read: %s\0A\00"
@errno = global i32 0
define i32 @read_data(i32 %0, i8* nocapture %1, i32 returned %2) {
%4 = tail call i32 @read(i32 %0, i8* %1, i32 %2)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %11
6: ; preds = %3
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %3
ret i32 %2
}