#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 *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ swap4(char *, int);
int read_int4(int fd, int *istore, 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, istore, rl1);
  if (swap)
    swap4((char *)istore, size);
  n = read(fd, &rl2, 4);
  if (swap)
    swap4((char *)&rl2, 1);
  if (rl1 != rl2) {
    fprintf(stderr, "Error read int block\n");
    return -2;
  }
  if (4 * size != rl2)
    fprintf(stderr, "read_int4: Expected %d, got %d elements\n", 4 * size, rl2);
  return 0;
}


x86 assembly (clang, -O0):
<read_int4>:
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
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    116f <read_int4+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1240 <read_int4+0x110>
cmpl   $0x0,-0x18(%rbp)
je     1187 <read_int4+0x57>
lea    -0x20(%rbp),%rdi
mov    $0x1,%esi
call   1050 <swap4@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     11af <read_int4+0x7f>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <swap4@plt>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0x4,%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     11db <read_int4+0xab>
lea    -0x24(%rbp),%rdi
mov    $0x1,%esi
call   1050 <swap4@plt>
mov    -0x20(%rbp),%eax
cmp    -0x24(%rbp),%eax
je     120a <read_int4+0xda>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
lea    0xe09(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1240 <read_int4+0x110>
mov    -0x14(%rbp),%eax
shl    $0x2,%eax
cmp    -0x24(%rbp),%eax
je     1239 <read_int4+0x109>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
shl    $0x2,%edx
mov    -0x24(%rbp),%ecx
lea    0xde4(%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_int4>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%ebx
mov    %edx,%r14d
mov    %rsi,%r12
mov    %edi,%ebp
lea    0x8(%rsp),%rsi
mov    $0x4,%edx
call   1030 <read@plt>
test   %eax,%eax
je     11e6 <read_int4+0xb6>
test   %ebx,%ebx
je     11ed <read_int4+0xbd>
lea    0x8(%rsp),%rdi
mov    $0x1,%esi
call   1050 <swap4@plt>
mov    0x8(%rsp),%edx
mov    %ebp,%edi
mov    %r12,%rsi
call   1030 <read@plt>
mov    %r12,%rdi
mov    %r14d,%esi
call   1050 <swap4@plt>
lea    0xc(%rsp),%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1030 <read@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
call   1050 <swap4@plt>
mov    0x8(%rsp),%r15d
cmp    0xc(%rsp),%r15d
jne    1217 <read_int4+0xe7>
shl    $0x2,%r14d
xor    %ebx,%ebx
cmp    %r15d,%r14d
je     1233 <read_int4+0x103>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
lea    0xe41(%rip),%rsi
xor    %ebx,%ebx
mov    %r14d,%edx
mov    %r15d,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    1233 <read_int4+0x103>
mov    $0xffffffff,%ebx
jmp    1233 <read_int4+0x103>
mov    0x8(%rsp),%r15d
mov    %ebp,%edi
mov    %r12,%rsi
mov    %r15d,%edx
call   1030 <read@plt>
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   1030 <read@plt>
cmp    0xc(%rsp),%r15d
je     11ba <read_int4+0x8a>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
lea    0xdd9(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xfffffffe,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [22 x i8] c"Error read int block\0A\00"
@.str.1 = [41 x i8] c"read_int4: Expected %d, got %d elements\0A\00"
define i32 @read_int4(i32 %0, i32* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %6
store i32* %1, i32** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%13 = load i32, i32* %6
%14 = call 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 %63
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 i32*, i32** %7
%27 = load i32, i32* %11
%28 = call i32 @read(i32 %25, i32* %26, i32 %27)
store i32 %28, i32* %10
%29 = load i32, i32* %9
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %24
%32 = load i32*, i32** %7
%33 = bitcast i32* %32 to i8*
%34 = load i32, i32* %8
%35 = call i32 @swap4(i8* %33, i32 %34)
br label %36
36: ; preds = %31, %24
%37 = load i32, i32* %6
%38 = call i32 @read(i32 %37, i32* %12, i32 4)
store i32 %38, i32* %10
%39 = load i32, i32* %9
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = bitcast i32* %12 to i8*
%43 = call i32 @swap4(i8* %42, i32 1)
br label %44
44: ; preds = %41, %36
%45 = load i32, i32* %11
%46 = load i32, i32* %12
%47 = icmp ne i32 %45, %46
br i1 %47, label %48, label %51
48: ; preds = %44
%49 = load i32, i32* @stderr
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 -2, i32* %5
br label %63
51: ; preds = %44
%52 = load i32, i32* %8
%53 = mul nsw i32 4, %52
%54 = load i32, i32* %12
%55 = icmp ne i32 %53, %54
br i1 %55, label %56, label %62
56: ; preds = %51
%57 = load i32, i32* @stderr
%58 = load i32, i32* %8
%59 = mul nsw i32 4, %58
%60 = load i32, i32* %12
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %59, i32 %60)
br label %62
62: ; preds = %56, %51
store i32 0, i32* %5
br label %63
63: ; preds = %62, %48, %17
%64 = load i32, i32* %5
ret i32 %64
}

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