#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__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, char *);
scalar_t__ read(int, char *, size_t);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
ssize_t Readn(int sockfd, void *buf, size_t nbytes) {
  size_t nleft;
  ssize_t nread;
  char *ptr;
  ptr = buf;
  nleft = nbytes;
  while (nleft > 0) {
    if ((nread = read(sockfd, ptr, nleft)) < 0) {
      if (errno == EINTR)
        nread = 0;
      else {
        fprintf(stderr, "%s:%d: Readn(): %s\n",
                "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                "outputs/2022-01-22/02-19-57/repos/Ricky-Gong/LegoCar/refs/"
                "heads/master/CarServer/self/nano_spec/wrap.c",
                44, strerror(errno));
        exit(1);
      }
    } else if (nread == 0)
      break;
    nleft -= nread;
    ptr += nread;
  }
  return (nbytes - nleft);
}


x86 assembly (clang, -O0):
<Readn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    1230 <Readn+0xf0>
mov    -0x4(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <read@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jge    11fb <Readn+0xbb>
mov    0x2e45(%rip),%rax
mov    (%rax),%rax
mov    0x2e33(%rip),%rcx
cmp    (%rcx),%rax
jne    11b3 <Readn+0x73>
movq   $0x0,-0x28(%rbp)
jmp    11f6 <Readn+0xb6>
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%r8
lea    0xe25(%rip),%rsi
lea    0xe32(%rip),%rdx
mov    $0x2c,%ecx
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
jmp    1210 <Readn+0xd0>
cmpq   $0x0,-0x28(%rbp)
jne    120b <Readn+0xcb>
jmp    1230 <Readn+0xf0>
jmp    1210 <Readn+0xd0>
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1163 <Readn+0x23>
mov    -0x18(%rbp),%rax
sub    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Readn>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
test   %rdx,%rdx
je     119a <Readn+0x5a>
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2e79(%rip),%r12
mov    0x2e6a(%rip),%r13
mov    %r14,%r15
jmp    117a <Readn+0x3a>
nopl   0x0(%rax,%rax,1)
je     119d <Readn+0x5d>
add    %rax,%rbx
sub    %rax,%r15
je     119a <Readn+0x5a>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1030 <read@plt>
test   %rax,%rax
jns    1170 <Readn+0x30>
mov    (%r12),%rdi
cmp    0x0(%r13),%rdi
jne    11b2 <Readn+0x72>
xor    %eax,%eax
jmp    1172 <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    0x2e27(%rip),%rax
mov    (%rax),%ebx
call   1060 <strerror@plt>
lea    0xe39(%rip),%rsi
lea    0xe46(%rip),%rdx
mov    %ebx,%edi
mov    $0x2c,%ecx
mov    %rax,%r8
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [20 x i8] c"%s:%d: Readn(): %s\0A\00"
@.str.1 = [159 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/Ricky-Gong/LegoCar/refs/heads/master/CarServer/self/nano_spec/wrap.c\00"
define i64 @Readn(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%10 = load i8*, i8** %5
store i8* %10, i8** %9
%11 = load i64, i64* %6
store i64 %11, i64* %7
br label %12
12: ; preds = %38, %3
%13 = load i64, i64* %7
%14 = icmp ugt i64 %13, 0
br i1 %14, label %15, label %45
15: ; preds = %12
%16 = load i32, i32* %4
%17 = load i8*, i8** %9
%18 = load i64, i64* %7
%19 = call i64 @read(i32 %16, i8* %17, i64 %18)
store i64 %19, i64* %8
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %33
21: ; preds = %15
%22 = load i64, i64* @errno
%23 = load i64, i64* @EINTR
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %21
store i64 0, i64* %8
br label %32
26: ; preds = %21
%27 = load i32, i32* @stderr
%28 = load i64, i64* @errno
%29 = call i8* @strerror(i64 %28)
%30 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([159 x i8], [159 x i8]* @.str.1, i64 0, i64 0), i32 44, i8* %29)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %25
br label %38
33: ; preds = %15
%34 = load i64, i64* %8
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %33
br label %45
37: ; preds = %33
br label %38
38: ; preds = %37, %32
%39 = load i64, i64* %8
%40 = load i64, i64* %7
%41 = sub i64 %40, %39
store i64 %41, i64* %7
%42 = load i64, i64* %8
%43 = load i8*, i8** %9
%44 = getelementptr inbounds i8, i8* %43, i64 %42
store i8* %44, i8** %9
br label %12
45: ; preds = %36, %12
%46 = load i64, i64* %6
%47 = load i64, i64* %7
%48 = sub i64 %46, %47
ret i64 %48
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [20 x i8] c"%s:%d: Readn(): %s\0A\00"
@.str.1 = [159 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/Ricky-Gong/LegoCar/refs/heads/master/CarServer/self/nano_spec/wrap.c\00"
define i64 @Readn(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %26, label %5
5: ; preds = %3, %21
%6 = phi i8* [ %24, %21 ], [ %1, %3 ]
%7 = phi i64 [ %23, %21 ], [ %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 %19
10: ; preds = %5
%11 = load i64, i64* @errno
%12 = load i64, i64* @EINTR
%13 = icmp eq i64 %11, %12
br i1 %13, label %21, label %14
14: ; preds = %10
%15 = load i32, i32* @stderr
%16 = tail call i8* @strerror(i64 %11)
%17 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([159 x i8], [159 x i8]* @.str.1, i64 0, i64 0), i32 44, i8* %16)
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %5
%20 = icmp eq i64 %8, 0
br i1 %20, label %26, label %21
21: ; preds = %10, %19
%22 = phi i64 [ %8, %19 ], [ 0, %10 ]
%23 = sub i64 %7, %22
%24 = getelementptr inbounds i8, i8* %6, i64 %22
%25 = icmp eq i64 %23, 0
br i1 %25, label %26, label %5
26: ; preds = %21, %19, %3
%27 = phi i64 [ 0, %3 ], [ %7, %19 ], [ 0, %21 ]
%28 = sub i64 %2, %27
ret i64 %28
}