#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 int /*<<< orphan*/ uint8_t;
typedef int ssize_t;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ EWOULDBLOCK;
size_t SSIZE_MAX;
scalar_t__ errno;
int read(int, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ ssh_set_error(char *);
ssize_t ssh_net_read(int sock, void *data, size_t len) {
  ssize_t len_left;
  uint8_t *p;
  if (len > SSIZE_MAX) {
    ssh_set_error("read too large");
    errno = 0;
    return -1;
  }
  p = data;
  len_left = len;
  while (len_left > 0) {
    ssize_t ret = read(sock, p, len_left);
    if (ret < 0) {
      if (errno == EINTR)
        continue;
      if (errno == EWOULDBLOCK || errno == EAGAIN)
        return len - len_left;
      ssh_set_error("read error");
      return -1;
    }
    if (ret == 0) {
      ssh_set_error("connection closed");
      errno = 0;
      return -1;
    }
    p += ret;
    len_left -= ret;
  }
  return len;
}


x86 assembly (clang, -O0):
<ssh_net_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2eaa(%rip),%rcx
cmp    (%rcx),%rax
jbe    116d <ssh_net_read+0x4d>
lea    0xeb2(%rip),%rdi
call   1040 <ssh_set_error@plt>
mov    0x2e7e(%rip),%rax
movq   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    127c <ssh_net_read+0x15c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    1275 <ssh_net_read+0x155>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1220 <ssh_net_read+0x100>
mov    0x2e2f(%rip),%rax
mov    (%rax),%rax
mov    0x2e15(%rip),%rcx
cmp    (%rcx),%rax
jne    11c1 <ssh_net_read+0xa1>
jmp    117c <ssh_net_read+0x5c>
mov    0x2e10(%rip),%rax
mov    (%rax),%rax
mov    0x2dfe(%rip),%rcx
cmp    (%rcx),%rax
je     11f5 <ssh_net_read+0xd5>
mov    0x2df6(%rip),%rax
mov    (%rax),%rax
mov    0x2dcc(%rip),%rcx
cmp    (%rcx),%rax
jne    1208 <ssh_net_read+0xe8>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x4(%rbp)
jmp    127c <ssh_net_read+0x15c>
lea    0xe00(%rip),%rdi
call   1040 <ssh_set_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127c <ssh_net_read+0x15c>
cmpl   $0x0,-0x2c(%rbp)
jne    1250 <ssh_net_read+0x130>
lea    0xde9(%rip),%rdi
call   1040 <ssh_set_error@plt>
mov    0x2d9b(%rip),%rax
movq   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    127c <ssh_net_read+0x15c>
mov    -0x2c(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
jmp    117c <ssh_net_read+0x5c>
mov    -0x18(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ssh_net_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2eb6(%rip),%rax
cmp    %rdx,(%rax)
jae    115c <ssh_net_read+0x3c>
lea    0xec2(%rip),%rdi
call   1040 <ssh_set_error@plt>
mov    0x2e8e(%rip),%rax
movq   $0x0,(%rax)
mov    $0xffffffff,%r14d
jmp    120a <ssh_net_read+0xea>
mov    %rdx,%r14
test   %r14d,%r14d
jle    120a <ssh_net_read+0xea>
mov    %rsi,%rbx
mov    %edi,%r15d
mov    0x2e63(%rip),%r12
mov    0x2e4c(%rip),%r13
mov    %r14d,%ebp
jmp    119e <ssh_net_read+0x7e>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%r12),%rax
cmp    0x0(%r13),%rax
jne    11db <ssh_net_read+0xbb>
test   %ebp,%ebp
jle    120a <ssh_net_read+0xea>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <read@plt>
test   %eax,%eax
js     1190 <ssh_net_read+0x70>
je     11bf <ssh_net_read+0x9f>
mov    %eax,%ecx
lea    (%rbx,%rcx,4),%rbx
sub    %eax,%ebp
test   %ebp,%ebp
jg     119e <ssh_net_read+0x7e>
jmp    120a <ssh_net_read+0xea>
lea    0xe54(%rip),%rdi
call   1040 <ssh_set_error@plt>
movq   $0x0,(%r12)
mov    $0xffffffff,%r14d
jmp    120a <ssh_net_read+0xea>
mov    0x2dee(%rip),%rcx
cmp    (%rcx),%rax
je     1207 <ssh_net_read+0xe7>
mov    0x2dca(%rip),%rcx
cmp    (%rcx),%rax
je     1207 <ssh_net_read+0xe7>
lea    0xe15(%rip),%rdi
call   1040 <ssh_set_error@plt>
mov    $0xffffffff,%r14d
jmp    120a <ssh_net_read+0xea>
sub    %ebp,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SSIZE_MAX = global i64 0
@.str = [15 x i8] c"read too large\00"
@errno = global i64 0
@EINTR = global i64 0
@EWOULDBLOCK = global i64 0
@EAGAIN = global i64 0
@.str.1 = [11 x i8] c"read error\00"
@.str.2 = [18 x i8] c"connection closed\00"
define i32 @ssh_net_read(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i64, i64* %7
%12 = load i64, i64* @SSIZE_MAX
%13 = icmp ugt i64 %11, %12
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = call i32 @ssh_set_error(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i64 0, i64* @errno
store i32 -1, i32* %4
br label %68
16: ; preds = %3
%17 = load i8*, i8** %6
%18 = bitcast i8* %17 to i32*
store i32* %18, i32** %9
%19 = load i64, i64* %7
%20 = trunc i64 %19 to i32
store i32 %20, i32* %8
br label %21
21: ; preds = %57, %35, %16
%22 = load i32, i32* %8
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %65
24: ; preds = %21
%25 = load i32, i32* %5
%26 = load i32*, i32** %9
%27 = load i32, i32* %8
%28 = call i32 @read(i32 %25, i32* %26, i32 %27)
store i32 %28, i32* %10
%29 = load i32, i32* %10
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %52
31: ; preds = %24
%32 = load i64, i64* @errno
%33 = load i64, i64* @EINTR
%34 = icmp eq i64 %32, %33
br i1 %34, label %35, label %36
35: ; preds = %31
br label %21
36: ; preds = %31
%37 = load i64, i64* @errno
%38 = load i64, i64* @EWOULDBLOCK
%39 = icmp eq i64 %37, %38
br i1 %39, label %44, label %40
40: ; preds = %36
%41 = load i64, i64* @errno
%42 = load i64, i64* @EAGAIN
%43 = icmp eq i64 %41, %42
br i1 %43, label %44, label %50
44: ; preds = %40, %36
%45 = load i64, i64* %7
%46 = load i32, i32* %8
%47 = sext i32 %46 to i64
%48 = sub i64 %45, %47
%49 = trunc i64 %48 to i32
store i32 %49, i32* %4
br label %68
50: ; preds = %40
%51 = call i32 @ssh_set_error(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %68
52: ; preds = %24
%53 = load i32, i32* %10
%54 = icmp eq i32 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %52
%56 = call i32 @ssh_set_error(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
store i64 0, i64* @errno
store i32 -1, i32* %4
br label %68
57: ; preds = %52
%58 = load i32, i32* %10
%59 = load i32*, i32** %9
%60 = sext i32 %58 to i64
%61 = getelementptr inbounds i32, i32* %59, i64 %60
store i32* %61, i32** %9
%62 = load i32, i32* %10
%63 = load i32, i32* %8
%64 = sub nsw i32 %63, %62
store i32 %64, i32* %8
br label %21
65: ; preds = %21
%66 = load i64, i64* %7
%67 = trunc i64 %66 to i32
store i32 %67, i32* %4
br label %68
68: ; preds = %65, %55, %50, %44, %14
%69 = load i32, i32* %4
ret i32 %69
}

LLVM IR (-O2):
@SSIZE_MAX = global i64 0
@.str = [15 x i8] c"read too large\00"
@errno = global i64 0
@EINTR = global i64 0
@EWOULDBLOCK = global i64 0
@EAGAIN = global i64 0
@.str.1 = [11 x i8] c"read error\00"
@.str.2 = [18 x i8] c"connection closed\00"
define i32 @ssh_net_read(i32 %0, i8* nocapture %1, i64 %2) {
%4 = load i64, i64* @SSIZE_MAX
%5 = icmp ult i64 %4, %2
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @ssh_set_error(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i64 0, i64* @errno
br label %44
8: ; preds = %3
%9 = trunc i64 %2 to i32
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %44
11: ; preds = %8
%12 = bitcast i8* %1 to i32*
br label %13
13: ; preds = %11, %40
%14 = phi i32* [ %42, %40 ], [ %12, %11 ]
%15 = phi i32 [ %41, %40 ], [ %9, %11 ]
%16 = tail call i32 @read(i32 %0, i32* %14, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %32
18: ; preds = %13
%19 = load i64, i64* @errno
%20 = load i64, i64* @EINTR
%21 = icmp eq i64 %19, %20
br i1 %21, label %40, label %22
22: ; preds = %18
%23 = load i64, i64* @EWOULDBLOCK
%24 = icmp eq i64 %19, %23
%25 = load i64, i64* @EAGAIN
%26 = icmp eq i64 %19, %25
%27 = select i1 %24, i1 true, i1 %26
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = sub i32 %9, %15
br label %44
30: ; preds = %22
%31 = tail call i32 @ssh_set_error(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %44
32: ; preds = %13
%33 = icmp eq i32 %16, 0
br i1 %33, label %34, label %36
34: ; preds = %32
%35 = tail call i32 @ssh_set_error(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
store i64 0, i64* @errno
br label %44
36: ; preds = %32
%37 = zext i32 %16 to i64
%38 = getelementptr inbounds i32, i32* %14, i64 %37
%39 = sub nsw i32 %15, %16
br label %40
40: ; preds = %18, %36
%41 = phi i32 [ %39, %36 ], [ %15, %18 ]
%42 = phi i32* [ %38, %36 ], [ %14, %18 ]
%43 = icmp sgt i32 %41, 0
br i1 %43, label %13, label %44
44: ; preds = %40, %8, %34, %30, %28, %6
%45 = phi i32 [ -1, %6 ], [ -1, %34 ], [ -1, %30 ], [ %29, %28 ], [ %9, %8 ], [ %9, %40 ]
ret i32 %45
}