#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 */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int readx(int s, void *_data, int len) {
  char *data = _data;
  int r;
  while (len > 0) {
    r = read(s, data, len);
    if (r == 0) {
      fprintf(stderr, "error: eof during socket read\n");
      return -1;
    }
    if (r < 0) {
      if (errno == EINTR)
        continue;
      fprintf(stderr, "error: %s during socket read\n", strerror(errno));
      return -1;
    }
    data += r;
    len -= r;
  }
  return 0;
}


x86 assembly (clang, -O0):
<readx>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1218 <readx+0xe8>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    1193 <readx+0x63>
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
lea    0xe80(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121f <readx+0xef>
cmpl   $0x0,-0x24(%rbp)
jge    11f7 <readx+0xc7>
mov    0x2e34(%rip),%rax
mov    (%rax),%rax
mov    0x2e22(%rip),%rcx
cmp    (%rcx),%rax
jne    11bc <readx+0x8c>
jmp    114a <readx+0x1a>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e09(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xe3b(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121f <readx+0xef>
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    114a <readx+0x1a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readx>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %r14d,%r14d
test   %edx,%edx
jle    11ca <readx+0x9a>
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r15d
mov    0x2e83(%rip),%r12
mov    0x2e74(%rip),%r13
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <read@plt>
test   %eax,%eax
je     118a <readx+0x5a>
jns    117f <readx+0x4f>
mov    (%r12),%rdi
cmp    0x0(%r13),%rdi
je     1160 <readx+0x30>
jmp    11a3 <readx+0x73>
mov    %eax,%ecx
add    %rcx,%rbx
sub    %eax,%ebp
jg     1160 <readx+0x30>
jmp    11ca <readx+0x9a>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
lea    0xe66(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    11c4 <readx+0x94>
mov    0x2e36(%rip),%rax
mov    (%rax),%ebx
call   1050 <strerror@plt>
lea    0xe67(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [31 x i8] c"error: eof during socket read\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [30 x i8] c"error: %s during socket read\0A\00"
define i32 @readx(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %6
store i8* %10, i8** %8
br label %11
11: ; preds = %37, %31, %3
%12 = load i32, i32* %7
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %45
14: ; preds = %11
%15 = load i32, i32* %5
%16 = load i8*, i8** %8
%17 = load i32, i32* %7
%18 = call i32 @read(i32 %15, i8* %16, i32 %17)
store i32 %18, i32* %9
%19 = load i32, i32* %9
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %14
%22 = load i32, i32* @stderr
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %46
24: ; preds = %14
%25 = load i32, i32* %9
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %37
27: ; preds = %24
%28 = load i64, i64* @errno
%29 = load i64, i64* @EINTR
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %32
31: ; preds = %27
br label %11
32: ; preds = %27
%33 = load i32, i32* @stderr
%34 = load i64, i64* @errno
%35 = call i8* @strerror(i64 %34)
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %35)
store i32 -1, i32* %4
br label %46
37: ; preds = %24
%38 = load i32, i32* %9
%39 = load i8*, i8** %8
%40 = sext i32 %38 to i64
%41 = getelementptr inbounds i8, i8* %39, i64 %40
store i8* %41, i8** %8
%42 = load i32, i32* %9
%43 = load i32, i32* %7
%44 = sub nsw i32 %43, %42
store i32 %44, i32* %7
br label %11
45: ; preds = %11
store i32 0, i32* %4
br label %46
46: ; preds = %45, %32, %21
%47 = load i32, i32* %4
ret i32 %47
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [31 x i8] c"error: eof during socket read\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [30 x i8] c"error: %s during socket read\0A\00"
define i32 @readx(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %29
5: ; preds = %3, %24
%6 = phi i8* [ %26, %24 ], [ %1, %3 ]
%7 = phi i32 [ %27, %24 ], [ %2, %3 ]
br label %8
8: ; preds = %5, %16
%9 = tail call i32 @read(i32 %0, i8* %6, i32 %7)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = load i32, i32* @stderr
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %29
14: ; preds = %8
%15 = icmp slt i32 %9, 0
br i1 %15, label %16, label %24
16: ; preds = %14
%17 = load i64, i64* @errno
%18 = load i64, i64* @EINTR
%19 = icmp eq i64 %17, %18
br i1 %19, label %8, label %20
20: ; preds = %16
%21 = load i32, i32* @stderr
%22 = tail call i8* @strerror(i64 %17)
%23 = tail call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %22)
br label %29
24: ; preds = %14
%25 = zext i32 %9 to i64
%26 = getelementptr inbounds i8, i8* %6, i64 %25
%27 = sub nsw i32 %7, %9
%28 = icmp sgt i32 %27, 0
br i1 %28, label %5, label %29
29: ; preds = %24, %3, %20, %11
%30 = phi i32 [ -1, %11 ], [ -1, %20 ], [ 0, %3 ], [ 0, %24 ]
ret i32 %30
}