#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 int32_t;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int read(int, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ swap4(char *, int);
int read_text(int fd, char *text, int size, int swap) {
  int n;
  int32_t rl1, rl2;
  n = read(fd, &rl1, 4);
  if (n == 0)
    return -1;
  if (swap)
    swap4((char *)&rl1, 1);
  n = read(fd, text, rl1);
  n = read(fd, &rl2, 4);
  if (swap)
    swap4((char *)&rl2, 1);
  if (rl1 != rl2) {
    fprintf(stderr, "Error read text block\n");
    return -2;
  }
  if (size != rl2)
    fprintf(stderr, "read_text: Expected %d, got %d elements\n", size, rl2);
  return 0;
}


x86 assembly (clang, -O0):
<read_text>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1171 <read_text+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <read_text+0xfa>
cmpl   $0x0,-0x18(%rbp)
je     1189 <read_text+0x59>
lea    -0x20(%rbp),%rdi
mov    $0x1,%esi
call   1050 <swap4@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%edx
mov    $0x0,%al
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     11cb <read_text+0x9b>
lea    -0x24(%rbp),%rdi
mov    $0x1,%esi
call   1050 <swap4@plt>
mov    -0x20(%rbp),%eax
cmp    -0x24(%rbp),%eax
je     11fa <read_text+0xca>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
lea    0xe19(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    122a <read_text+0xfa>
mov    -0x14(%rbp),%eax
cmp    -0x24(%rbp),%eax
je     1223 <read_text+0xf3>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
mov    -0x24(%rbp),%ecx
lea    0xdfb(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_text>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%ebx
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%ebp
mov    %rsp,%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1030 <read@plt>
test   %eax,%eax
je     1199 <read_text+0x69>
test   %ebx,%ebx
je     11a0 <read_text+0x70>
mov    %rsp,%rdi
mov    $0x1,%esi
call   1050 <swap4@plt>
mov    (%rsp),%edx
mov    %ebp,%edi
mov    %r15,%rsi
xor    %eax,%eax
call   1030 <read@plt>
lea    0x4(%rsp),%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1030 <read@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
call   1050 <swap4@plt>
jmp    11c2 <read_text+0x92>
mov    $0xffffffff,%ebx
jmp    120c <read_text+0xdc>
mov    (%rsp),%edx
mov    %ebp,%edi
mov    %r15,%rsi
xor    %eax,%eax
call   1030 <read@plt>
lea    0x4(%rsp),%rsi
mov    %ebp,%edi
mov    $0x4,%edx
xor    %eax,%eax
call   1030 <read@plt>
mov    (%rsp),%ecx
cmp    0x4(%rsp),%ecx
jne    11f0 <read_text+0xc0>
xor    %ebx,%ebx
cmp    %r14d,%ecx
je     120c <read_text+0xdc>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
lea    0xe35(%rip),%rsi
xor    %ebx,%ebx
mov    %r14d,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    120c <read_text+0xdc>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
lea    0xe00(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xfffffffe,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [23 x i8] c"Error read text block\0A\00"
@.str.1 = [41 x i8] c"read_text: Expected %d, got %d elements\0A\00"
define i32 @read_text(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%13 = load i32, i32* %6
%14 = call i32 (i32, ...) @read(i32 %13, i32* %11, i32 4)
store i32 %14, i32* %10
%15 = load i32, i32* %10
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %4
store i32 -1, i32* %5
br label %53
18: ; preds = %4
%19 = load i32, i32* %9
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = bitcast i32* %11 to i8*
%23 = call i32 @swap4(i8* %22, i32 1)
br label %24
24: ; preds = %21, %18
%25 = load i32, i32* %6
%26 = load i8*, i8** %7
%27 = load i32, i32* %11
%28 = call i32 (i32, ...) @read(i32 %25, i8* %26, i32 %27)
store i32 %28, i32* %10
%29 = load i32, i32* %6
%30 = call i32 (i32, ...) @read(i32 %29, i32* %12, i32 4)
store i32 %30, i32* %10
%31 = load i32, i32* %9
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %24
%34 = bitcast i32* %12 to i8*
%35 = call i32 @swap4(i8* %34, i32 1)
br label %36
36: ; preds = %33, %24
%37 = load i32, i32* %11
%38 = load i32, i32* %12
%39 = icmp ne i32 %37, %38
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = load i32, i32* @stderr
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 -2, i32* %5
br label %53
43: ; preds = %36
%44 = load i32, i32* %8
%45 = load i32, i32* %12
%46 = icmp ne i32 %44, %45
br i1 %46, label %47, label %52
47: ; preds = %43
%48 = load i32, i32* @stderr
%49 = load i32, i32* %8
%50 = load i32, i32* %12
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %49, i32 %50)
br label %52
52: ; preds = %47, %43
store i32 0, i32* %5
br label %53
53: ; preds = %52, %40, %17
%54 = load i32, i32* %5
ret i32 %54
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [23 x i8] c"Error read text block\0A\00"
@.str.1 = [41 x i8] c"read_text: Expected %d, got %d elements\0A\00"
define i32 @read_text(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = bitcast i32* %5 to i8*
%8 = bitcast i32* %6 to i8*
%9 = call i32 (i32, ...) @read(i32 %0, i32* %5, i32 4)
%10 = icmp eq i32 %9, 0
br i1 %10, label %35, label %11
11: ; preds = %4
%12 = icmp eq i32 %3, 0
br i1 %12, label %19, label %13
13: ; preds = %11
%14 = call i32 @swap4(i8* %7, i32 1)
%15 = load i32, i32* %5
%16 = call i32 (i32, ...) @read(i32 %0, i8* %1, i32 %15)
%17 = call i32 (i32, ...) @read(i32 %0, i32* %6, i32 4)
%18 = call i32 @swap4(i8* %8, i32 1)
br label %23
19: ; preds = %11
%20 = load i32, i32* %5
%21 = call i32 (i32, ...) @read(i32 %0, i8* %1, i32 %20)
%22 = call i32 (i32, ...) @read(i32 %0, i32* %6, i32 4)
br label %23
23: ; preds = %19, %13
%24 = load i32, i32* %5
%25 = load i32, i32* %6
%26 = icmp eq i32 %24, %25
br i1 %26, label %30, label %27
27: ; preds = %23
%28 = load i32, i32* @stderr
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %35
30: ; preds = %23
%31 = icmp eq i32 %24, %2
br i1 %31, label %35, label %32
32: ; preds = %30
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %2, i32 %24)
br label %35
35: ; preds = %30, %32, %4, %27
%36 = phi i32 [ -2, %27 ], [ -1, %4 ], [ 0, %32 ], [ 0, %30 ]
ret i32 %36
}