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

/* Variables and functions */
int /*<<< orphan*/ SEEK_CUR;
int check_offsets(int *, size_t, int);
int errno;
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int, size_t const, size_t const);
size_t const read(int, char *, size_t const);
int read_and_check(int fds[], size_t count, int selected, const size_t size) {
  char data[size];
  off_t new_offset;
  if (read(fds[selected], data, size) != size) {
    return errno;
  }
  if ((new_offset = lseek(fds[selected], 0, SEEK_CUR)) == -1) {
    return errno;
  }
  printf("read(fds[%d], data, %ld) = %ld\n", selected, size, size);
  return check_offsets(fds, count, new_offset);
}


x86 assembly (clang, -O0):
<read_and_check>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    -0x28(%rbp),%rdx
call   1050 <read@plt>
cmp    -0x28(%rbp),%rax
je     11b0 <read_and_check+0x70>
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    1220 <read_and_check+0xe0>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0xffffffff,%eax
jne    11ef <read_and_check+0xaf>
mov    0x2dfa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    1220 <read_and_check+0xe0>
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    0xdff(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1060 <check_offsets@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_and_check>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %edx,%r12d
mov    %rsi,%r13
mov    %rdi,%r15
mov    %rsp,%rsi
lea    0xf(%rcx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rsi
mov    %rsi,%rsp
movslq %edx,%rbx
mov    (%rdi,%rbx,4),%edi
mov    %rcx,%rdx
call   1050 <read@plt>
cmp    %r14,%rax
jne    11d0 <read_and_check+0x90>
mov    %r13,-0x30(%rbp)
mov    (%r15,%rbx,4),%edi
mov    0x2e5b(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
je     11d0 <read_and_check+0x90>
mov    %eax,%r13d
lea    0xe5b(%rip),%rdi
mov    %r12d,%esi
mov    %r14,%rdx
mov    %r14,%rcx
call   1030 <printf@plt>
mov    %r15,%rdi
mov    -0x30(%rbp),%rsi
mov    %r13d,%edx
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <check_offsets@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@SEEK_CUR = global i32 0
@.str = [32 x i8] c"read(fds[%d], data, %ld) = %ld\0A\00"
define i32 @read_and_check(i32* %0, i64 %1, i32 %2, i64 %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
store i32* %0, i32** %6
store i64 %1, i64* %7
store i32 %2, i32* %8
store i64 %3, i64* %9
%14 = load i64, i64* %9
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %10
%16 = alloca i8, i64 %14
store i64 %14, i64* %11
%17 = load i32*, i32** %6
%18 = load i32, i32* %8
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i32, i32* %17, i64 %19
%21 = load i32, i32* %20
%22 = load i64, i64* %9
%23 = call i64 @read(i32 %21, i8* %16, i64 %22)
%24 = load i64, i64* %9
%25 = icmp ne i64 %23, %24
br i1 %25, label %26, label %28
26: ; preds = %4
%27 = load i32, i32* @errno
store i32 %27, i32* %5
store i32 1, i32* %13
br label %48
28: ; preds = %4
%29 = load i32*, i32** %6
%30 = load i32, i32* %8
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i32, i32* %29, i64 %31
%33 = load i32, i32* %32
%34 = load i32, i32* @SEEK_CUR
%35 = call i32 @lseek(i32 %33, i32 0, i32 %34)
store i32 %35, i32* %12
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %28
%38 = load i32, i32* @errno
store i32 %38, i32* %5
store i32 1, i32* %13
br label %48
39: ; preds = %28
%40 = load i32, i32* %8
%41 = load i64, i64* %9
%42 = load i64, i64* %9
%43 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %40, i64 %41, i64 %42)
%44 = load i32*, i32** %6
%45 = load i64, i64* %7
%46 = load i32, i32* %12
%47 = call i32 @check_offsets(i32* %44, i64 %45, i32 %46)
store i32 %47, i32* %5
store i32 1, i32* %13
br label %48
48: ; preds = %39, %37, %26
%49 = load i8*, i8** %10
%50 = load i32, i32* %5
ret i32 %50
}

LLVM IR (-O2):
@errno = global i32 0
@SEEK_CUR = global i32 0
@.str = [32 x i8] c"read(fds[%d], data, %ld) = %ld\0A\00"
define i32 @read_and_check(i32* %0, i64 %1, i32 %2, i64 %3) {
%5 = alloca i8, i64 %3
%6 = sext i32 %2 to i64
%7 = getelementptr inbounds i32, i32* %0, i64 %6
%8 = load i32, i32* %7
%9 = call i64 @read(i32 %8, i8* %5, i64 %3)
%10 = icmp eq i64 %9, %3
br i1 %10, label %13, label %11
11: ; preds = %4
%12 = load i32, i32* @errno
br label %23
13: ; preds = %4
%14 = load i32, i32* %7
%15 = load i32, i32* @SEEK_CUR
%16 = tail call i32 @lseek(i32 %14, i32 0, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = load i32, i32* @errno
br label %23
20: ; preds = %13
%21 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %2, i64 %3, i64 %3)
%22 = tail call i32 @check_offsets(i32* %0, i64 %1, i32 %16)
br label %23
23: ; preds = %20, %18, %11
%24 = phi i32 [ %12, %11 ], [ %19, %18 ], [ %22, %20 ]
ret i32 %24
}