#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 lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, size_t, int, ...);
int check_offsets(int fds[], size_t count, off_t expected_offset) {
  int failed = 0;
  for (int index = 0; index < count; ++index) {
    off_t actual_offset;
    bool valid = true;
    if ((actual_offset = lseek(fds[index], 0, SEEK_CUR)) == -1) {
      return -1;
    }
    valid = (actual_offset == expected_offset);
    if (!valid) {
      failed++;
    }
    printf("+ fd %d: offset=%lld [%s]\n", index, (long long)actual_offset,
           (valid ? "VALID" : "INVALID!"));
  }
  printf("Total: %ld, failed: %d\n\n", count, failed);
  return failed;
}


x86 assembly (clang, -O0):
<check_offsets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movslq -0x24(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11eb <check_offsets+0xcb>
movl   $0x1,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    0x2e80(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %eax,-0x28(%rbp)
cmp    $0xffffffff,%eax
jne    1189 <check_offsets+0x69>
movl   $0xffffffff,-0x4(%rbp)
jmp    1206 <check_offsets+0xe6>
mov    -0x28(%rbp),%eax
cmp    -0x1c(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    11ad <check_offsets+0x8d>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
movslq -0x24(%rbp),%rsi
movslq -0x28(%rbp),%rax
mov    %eax,%edx
mov    -0x2c(%rbp),%edi
lea    0xe60(%rip),%rcx
lea    0xe53(%rip),%rax
cmp    $0x0,%edi
cmovne %rax,%rcx
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1141 <check_offsets+0x21>
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%edx
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_offsets>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
test   %rsi,%rsi
je     11a0 <check_offsets+0x80>
mov    %edx,%r14d
mov    %rdi,%r13
lea    0xec0(%rip),%r12
xor    %ebx,%ebx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x0(%r13,%rbx,4),%edi
mov    0x2e8c(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
je     11b7 <check_offsets+0x97>
xor    %edx,%edx
cmp    %r14d,%eax
setne  %dl
lea    0xea8(%rip),%rcx
lea    0xe9b(%rip),%rsi
cmove  %rsi,%rcx
add    %edx,%ebp
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1150 <check_offsets+0x30>
jmp    11a2 <check_offsets+0x82>
xor    %ebp,%ebp
lea    0xe81(%rip),%rdi
mov    %r15,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1030 <printf@plt>
jmp    11bc <check_offsets+0x9c>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_CUR = global i32 0
@.str = [27 x i8] c"+ fd %d: offset=%lld [%s]\0A\00"
@.str.1 = [6 x i8] c"VALID\00"
@.str.2 = [9 x i8] c"INVALID!\00"
@.str.3 = [25 x i8] c"Total: %ld, failed: %d\0A\0A\00"
define i32 @check_offsets(i32* %0, i64 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32* %0, i32** %5
store i64 %1, i64* %6
store i32 %2, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
br label %12
12: ; preds = %48, %3
%13 = load i32, i32* %9
%14 = sext i32 %13 to i64
%15 = load i64, i64* %6
%16 = icmp ult i64 %14, %15
br i1 %16, label %17, label %51
17: ; preds = %12
store i32 1, i32* %11
%18 = load i32*, i32** %5
%19 = load i32, i32* %9
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i32, i32* %18, i64 %20
%22 = load i32, i32* %21
%23 = load i32, i32* @SEEK_CUR
%24 = call i32 @lseek(i32 %22, i32 0, i32 %23)
store i32 %24, i32* %10
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %27
26: ; preds = %17
store i32 -1, i32* %4
br label %56
27: ; preds = %17
%28 = load i32, i32* %10
%29 = load i32, i32* %7
%30 = icmp eq i32 %28, %29
%31 = zext i1 %30 to i32
store i32 %31, i32* %11
%32 = load i32, i32* %11
%33 = icmp ne i32 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %27
%35 = load i32, i32* %8
%36 = add nsw i32 %35, 1
store i32 %36, i32* %8
br label %37
37: ; preds = %34, %27
%38 = load i32, i32* %9
%39 = sext i32 %38 to i64
%40 = load i32, i32* %10
%41 = sext i32 %40 to i64
%42 = trunc i64 %41 to i32
%43 = load i32, i32* %11
%44 = icmp ne i32 %43, 0
%45 = zext i1 %44 to i64
%46 = select i1 %44, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0)
%47 = call i32 (i8*, i64, i32, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i64 %39, i32 %42, i8* %46)
br label %48
48: ; preds = %37
%49 = load i32, i32* %9
%50 = add nsw i32 %49, 1
store i32 %50, i32* %9
br label %12
51: ; preds = %12
%52 = load i64, i64* %6
%53 = load i32, i32* %8
%54 = call i32 (i8*, i64, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i64 %52, i32 %53)
%55 = load i32, i32* %8
store i32 %55, i32* %4
br label %56
56: ; preds = %51, %26
%57 = load i32, i32* %4
ret i32 %57
}

LLVM IR (-O2):
@SEEK_CUR = global i32 0
@.str = [27 x i8] c"+ fd %d: offset=%lld [%s]\0A\00"
@.str.1 = [6 x i8] c"VALID\00"
@.str.2 = [9 x i8] c"INVALID!\00"
@.str.3 = [25 x i8] c"Total: %ld, failed: %d\0A\0A\00"
define i32 @check_offsets(i32* nocapture %0, i64 %1, i32 %2) {
%4 = icmp eq i64 %1, 0
br i1 %4, label %22, label %5
5: ; preds = %3, %13
%6 = phi i64 [ %20, %13 ], [ 0, %3 ]
%7 = phi i32 [ %18, %13 ], [ 0, %3 ]
%8 = getelementptr inbounds i32, i32* %0, i64 %6
%9 = load i32, i32* %8
%10 = load i32, i32* @SEEK_CUR
%11 = tail call i32 @lseek(i32 %9, i32 0, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %25, label %13
13: ; preds = %5
%14 = icmp eq i32 %11, %2
%15 = select i1 %14, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0)
%16 = xor i1 %14, true
%17 = zext i1 %16 to i32
%18 = add nuw nsw i32 %7, %17
%19 = tail call i32 (i8*, i64, i32, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i64 %6, i32 %11, i8* %15)
%20 = add nuw i64 %6, 1
%21 = icmp eq i64 %20, %1
br i1 %21, label %22, label %5
22: ; preds = %13, %3
%23 = phi i32 [ 0, %3 ], [ %18, %13 ]
%24 = tail call i32 (i8*, i64, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i64 %1, i32 %23)
br label %25
25: ; preds = %5, %22
%26 = phi i32 [ %23, %22 ], [ -1, %5 ]
ret i32 %26
}