#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*/ EXIT_FAILURE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, char *,
                           char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int read_all(int fd, void *buf, int sz) {
  char *p;
  int n, n_read;
  p = buf;
  n_read = 0;
  do {
    do {
      if (!(n = read(fd, p, sz - n_read), n >= 0)) {
        fprintf(stderr, "%s,%d \"%s\": %s\n",
                "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                "outputs/2022-01-22/02-19-57/repos/cventus/otivm/refs/heads/"
                "master/src/ok/posix/run_test.c",
                147, "n = read(fd, p, sz - n_read), n >= 0", strerror(errno));
        exit(EXIT_FAILURE);
      }
    } while (0);
    n_read += n;
    if (n > 0)
      p += n;
  } while (n_read < sz && n > 0);
  return n_read;
}


x86 assembly (clang, -O0):
<read_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
jmp    1166 <read_all+0x26>
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
sub    -0x28(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11d2 <read_all+0x92>
mov    0x2e54(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%r9
lea    0xe54(%rip),%rsi
lea    0xe5d(%rip),%rdx
mov    $0x93,%ecx
lea    0xee3(%rip),%r8
call   1040 <fprintf@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
jmp    11d7 <read_all+0x97>
mov    -0x24(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    11fb <read_all+0xbb>
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    1200 <read_all+0xc0>
mov    -0x28(%rbp),%ecx
xor    %eax,%eax
cmp    -0x14(%rbp),%ecx
mov    %al,-0x2d(%rbp)
jge    121b <read_all+0xdb>
cmpl   $0x0,-0x24(%rbp)
setg   %al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
test   $0x1,%al
jne    1161 <read_all+0x21>
mov    -0x28(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_all>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%r15d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14d,%edx
sub    %ebp,%edx
mov    %r15d,%edi
mov    %rbx,%rsi
call   1030 <read@plt>
test   %eax,%eax
js     1191 <read_all+0x51>
add    %eax,%ebp
test   %eax,%eax
je     1184 <read_all+0x44>
mov    %eax,%eax
add    %rax,%rbx
cmp    %r14d,%ebp
jl     1160 <read_all+0x20>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e48(%rip),%rax
mov    (%rax),%ebx
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe51(%rip),%rsi
lea    0xe5a(%rip),%rdx
lea    0xee5(%rip),%r8
mov    %ebx,%edi
mov    $0x93,%ecx
mov    %rax,%r9
call   1040 <fprintf@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [16 x i8] c"%s,%d \22%s\22: %s\0A\00"
@.str.1 = [146 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/cventus/otivm/refs/heads/master/src/ok/posix/run_test.c\00"
@.str.2 = [37 x i8] c"n = read(fd, p, sz - n_read), n >= 0\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @read_all(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = load i8*, i8** %5
store i8* %10, i8** %7
store i32 0, i32* %9
br label %11
11: ; preds = %48, %3
br label %12
12: ; preds = %11
%13 = load i32, i32* %4
%14 = load i8*, i8** %7
%15 = load i32, i32* %6
%16 = load i32, i32* %9
%17 = sub nsw i32 %15, %16
%18 = call i32 @read(i32 %13, i8* %14, i32 %17)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp sge i32 %19, 0
br i1 %20, label %28, label %21
21: ; preds = %12
%22 = load i32, i32* @stderr
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([146 x i8], [146 x i8]* @.str.1, i64 0, i64 0), i32 147, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %24)
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %12
br label %29
29: ; preds = %28
%30 = load i32, i32* %8
%31 = load i32, i32* %9
%32 = add nsw i32 %31, %30
store i32 %32, i32* %9
%33 = load i32, i32* %8
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %29
%36 = load i32, i32* %8
%37 = load i8*, i8** %7
%38 = sext i32 %36 to i64
%39 = getelementptr inbounds i8, i8* %37, i64 %38
store i8* %39, i8** %7
br label %40
40: ; preds = %35, %29
br label %41
41: ; preds = %40
%42 = load i32, i32* %9
%43 = load i32, i32* %6
%44 = icmp slt i32 %42, %43
br i1 %44, label %45, label %48
45: ; preds = %41
%46 = load i32, i32* %8
%47 = icmp sgt i32 %46, 0
br label %48
48: ; preds = %45, %41
%49 = phi i1 [ false, %41 ], [ %47, %45 ]
br i1 %49, label %11, label %50
50: ; preds = %48
%51 = load i32, i32* %9
ret i32 %51
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [16 x i8] c"%s,%d \22%s\22: %s\0A\00"
@.str.1 = [146 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/cventus/otivm/refs/heads/master/src/ok/posix/run_test.c\00"
@.str.2 = [37 x i8] c"n = read(fd, p, sz - n_read), n >= 0\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @read_all(i32 %0, i8* nocapture %1, i32 %2) {
br label %4
4: ; preds = %17, %3
%5 = phi i8* [ %1, %3 ], [ %21, %17 ]
%6 = phi i32 [ 0, %3 ], [ %18, %17 ]
%7 = sub nsw i32 %2, %6
%8 = tail call i32 @read(i32 %0, i8* %5, i32 %7)
%9 = icmp sgt i32 %8, -1
br i1 %9, label %17, label %10
10: ; preds = %4
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([146 x i8], [146 x i8]* @.str.1, i64 0, i64 0), i32 147, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %4
%18 = add nsw i32 %8, %6
%19 = icmp ne i32 %8, 0
%20 = zext i32 %8 to i64
%21 = getelementptr i8, i8* %5, i64 %20
%22 = icmp slt i32 %18, %2
%23 = and i1 %19, %22
br i1 %23, label %4, label %24
24: ; preds = %17
ret i32 %18
}