#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 */
scalar_t__ EAGAIN;
scalar_t__ errno;
scalar_t__ read(int const, char *, size_t);
ssize_t gaspi_sn_readn(const int sockfd, const void *data_ptr, const size_t n) {
  ssize_t ndone;
  size_t left;
  char *ptr;
  ptr = (char *)data_ptr;
  left = n;
  while (left > 0) {
    if ((ndone = read(sockfd, ptr, left)) < 0) {
      if (ndone < 0 && errno == EAGAIN)
        ndone = 0;
      else
        return (-1);
    } else if (0 == ndone)
      break;
    left -= ndone;
    ptr += ndone;
  }
  return (n - left);
}


x86 assembly (clang, -O0):
<gaspi_sn_readn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jbe    11d6 <gaspi_sn_readn+0xc6>
mov    -0xc(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1030 <read@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jge    11a0 <gaspi_sn_readn+0x90>
cmpq   $0x0,-0x28(%rbp)
jge    118e <gaspi_sn_readn+0x7e>
mov    0x2e72(%rip),%rax
mov    (%rax),%rax
mov    0x2e58(%rip),%rcx
cmp    (%rcx),%rax
jne    118e <gaspi_sn_readn+0x7e>
movq   $0x0,-0x28(%rbp)
jmp    119b <gaspi_sn_readn+0x8b>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11e2 <gaspi_sn_readn+0xd2>
jmp    11b6 <gaspi_sn_readn+0xa6>
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    11b1 <gaspi_sn_readn+0xa1>
jmp    11d6 <gaspi_sn_readn+0xc6>
jmp    11b6 <gaspi_sn_readn+0xa6>
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1133 <gaspi_sn_readn+0x23>
mov    -0x20(%rbp),%rax
sub    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gaspi_sn_readn>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
test   %rdx,%rdx
je     116a <gaspi_sn_readn+0x5a>
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2eb1(%rip),%r12
mov    0x2e9a(%rip),%r13
mov    %r14,%r15
jmp    114a <gaspi_sn_readn+0x3a>
nopl   0x0(%rax,%rax,1)
je     116d <gaspi_sn_readn+0x5d>
add    %rax,%rbx
sub    %rax,%r15
je     116a <gaspi_sn_readn+0x5a>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1030 <read@plt>
test   %rax,%rax
jns    1140 <gaspi_sn_readn+0x30>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
jne    1182 <gaspi_sn_readn+0x72>
xor    %eax,%eax
jmp    1142 <gaspi_sn_readn+0x32>
xor    %r15d,%r15d
sub    %r15,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffffffffffff,%r14
jmp    1170 <gaspi_sn_readn+0x60>

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
define i64 @gaspi_sn_readn(i32 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i8*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i8*, i8** %6
store i8* %11, i8** %10
%12 = load i64, i64* %7
store i64 %12, i64* %9
br label %13
13: ; preds = %37, %3
%14 = load i64, i64* %9
%15 = icmp ugt i64 %14, 0
br i1 %15, label %16, label %44
16: ; preds = %13
%17 = load i32, i32* %5
%18 = load i8*, i8** %10
%19 = load i64, i64* %9
%20 = call i64 @read(i32 %17, i8* %18, i64 %19)
store i64 %20, i64* %8
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %32
22: ; preds = %16
%23 = load i64, i64* %8
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %22
%26 = load i64, i64* @errno
%27 = load i64, i64* @EAGAIN
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %25
store i64 0, i64* %8
br label %31
30: ; preds = %25, %22
store i64 -1, i64* %4
br label %48
31: ; preds = %29
br label %37
32: ; preds = %16
%33 = load i64, i64* %8
%34 = icmp eq i64 0, %33
br i1 %34, label %35, label %36
35: ; preds = %32
br label %44
36: ; preds = %32
br label %37
37: ; preds = %36, %31
%38 = load i64, i64* %8
%39 = load i64, i64* %9
%40 = sub i64 %39, %38
store i64 %40, i64* %9
%41 = load i64, i64* %8
%42 = load i8*, i8** %10
%43 = getelementptr inbounds i8, i8* %42, i64 %41
store i8* %43, i8** %10
br label %13
44: ; preds = %35, %13
%45 = load i64, i64* %7
%46 = load i64, i64* %9
%47 = sub i64 %45, %46
store i64 %47, i64* %4
br label %48
48: ; preds = %44, %30
%49 = load i64, i64* %4
ret i64 %49
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
define i64 @gaspi_sn_readn(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %21, label %5
5: ; preds = %3, %16
%6 = phi i8* [ %19, %16 ], [ %1, %3 ]
%7 = phi i64 [ %18, %16 ], [ %2, %3 ]
%8 = tail call i64 @read(i32 %0, i8* %6, i64 %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %5
%11 = load i64, i64* @errno
%12 = load i64, i64* @EAGAIN
%13 = icmp eq i64 %11, %12
br i1 %13, label %16, label %24
14: ; preds = %5
%15 = icmp eq i64 %8, 0
br i1 %15, label %21, label %16
16: ; preds = %10, %14
%17 = phi i64 [ %8, %14 ], [ 0, %10 ]
%18 = sub i64 %7, %17
%19 = getelementptr inbounds i8, i8* %6, i64 %17
%20 = icmp eq i64 %18, 0
br i1 %20, label %21, label %5
21: ; preds = %16, %14, %3
%22 = phi i64 [ 0, %3 ], [ %7, %14 ], [ 0, %16 ]
%23 = sub i64 %2, %22
br label %24
24: ; preds = %10, %21
%25 = phi i64 [ %23, %21 ], [ -1, %10 ]
ret i64 %25
}