#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 */
scalar_t__ ESPIPE;
int /*<<< orphan*/ SEEK_CUR;
scalar_t__ errno;
int lseek(int, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int safe_read(int, void *, size_t);
int skip_blocks(int fd, void *buf, unsigned int blks, size_t size) {
  unsigned int blk;
  int ret = 0;
  for (blk = 0; blk < blks; blk++) {
    ret = lseek(fd, size, SEEK_CUR);
    if (ret == -1)
      break;
  }
  if (blk == 0 && ret == -1 && errno == ESPIPE) {
    for (blk = 0; blk < blks; blk++) {
      ret = safe_read(fd, buf, size);
      if (ret != (int)size)
        break;
    }
  }
  if (ret == -1) {
    perror("seek/skip");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<skip_blocks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    119a <skip_blocks+0x6a>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    0x2e7a(%rip),%rax
mov    (%rax),%edx
call   1030 <lseek@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    1187 <skip_blocks+0x57>
jmp    119a <skip_blocks+0x6a>
jmp    118c <skip_blocks+0x5c>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1154 <skip_blocks+0x24>
cmpl   $0x0,-0x24(%rbp)
jne    121a <skip_blocks+0xea>
cmpl   $0xffffffff,-0x28(%rbp)
jne    121a <skip_blocks+0xea>
mov    0x2e1b(%rip),%rax
mov    (%rax),%rax
mov    0x2e21(%rip),%rcx
cmp    (%rcx),%rax
jne    121a <skip_blocks+0xea>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    1215 <skip_blocks+0xe5>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <safe_read@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    -0x20(%rbp),%rcx
cmp    %ecx,%eax
je     1202 <skip_blocks+0xd2>
jmp    1215 <skip_blocks+0xe5>
jmp    1207 <skip_blocks+0xd7>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11cf <skip_blocks+0x9f>
jmp    121a <skip_blocks+0xea>
cmpl   $0xffffffff,-0x28(%rbp)
jne    123c <skip_blocks+0x10c>
lea    0xdd5(%rip),%rdi
call   1050 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1243 <skip_blocks+0x113>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<skip_blocks>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
je     117c <skip_blocks+0x4c>
mov    %rcx,%rbx
mov    %edx,%r12d
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
mov    0x2e94(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x0(%r13),%edx
mov    %r15d,%edi
mov    %rbx,%rsi
call   1030 <lseek@plt>
cmp    $0xffffffff,%eax
je     1180 <skip_blocks+0x50>
add    $0x1,%ebp
cmp    %ebp,%r12d
jne    1160 <skip_blocks+0x30>
xor    %eax,%eax
jmp    11c5 <skip_blocks+0x95>
test   %ebp,%ebp
jne    11d4 <skip_blocks+0xa4>
mov    0x2e45(%rip),%rax
mov    (%rax),%rax
mov    0x2e4b(%rip),%rcx
cmp    (%rcx),%rax
jne    11d4 <skip_blocks+0xa4>
mov    $0x1,%ebp
nop
mov    %r15d,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <safe_read@plt>
mov    %eax,%ecx
cmp    %ebx,%eax
jne    11be <skip_blocks+0x8e>
lea    0x1(%rbp),%eax
cmp    %r12d,%ebp
mov    %eax,%ebp
jb     11a0 <skip_blocks+0x70>
xor    %eax,%eax
cmp    $0xffffffff,%ecx
je     11d4 <skip_blocks+0xa4>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe25(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%eax
jmp    11c5 <skip_blocks+0x95>

LLVM IR (-O0):
@SEEK_CUR = global i32 0
@errno = global i64 0
@ESPIPE = global i64 0
@.str = [10 x i8] c"seek/skip\00"
define i32 @skip_blocks(i32 %0, i8* %1, i32 %2, i64 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i64 %3, i64* %9
store i32 0, i32* %11
store i32 0, i32* %10
br label %12
12: ; preds = %25, %4
%13 = load i32, i32* %10
%14 = load i32, i32* %8
%15 = icmp ult i32 %13, %14
br i1 %15, label %16, label %28
16: ; preds = %12
%17 = load i32, i32* %6
%18 = load i64, i64* %9
%19 = load i32, i32* @SEEK_CUR
%20 = call i32 @lseek(i32 %17, i64 %18, i32 %19)
store i32 %20, i32* %11
%21 = load i32, i32* %11
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %24
23: ; preds = %16
br label %28
24: ; preds = %16
br label %25
25: ; preds = %24
%26 = load i32, i32* %10
%27 = add i32 %26, 1
store i32 %27, i32* %10
br label %12
28: ; preds = %23, %12
%29 = load i32, i32* %10
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %58
31: ; preds = %28
%32 = load i32, i32* %11
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %58
34: ; preds = %31
%35 = load i64, i64* @errno
%36 = load i64, i64* @ESPIPE
%37 = icmp eq i64 %35, %36
br i1 %37, label %38, label %58
38: ; preds = %34
store i32 0, i32* %10
br label %39
39: ; preds = %54, %38
%40 = load i32, i32* %10
%41 = load i32, i32* %8
%42 = icmp ult i32 %40, %41
br i1 %42, label %43, label %57
43: ; preds = %39
%44 = load i32, i32* %6
%45 = load i8*, i8** %7
%46 = load i64, i64* %9
%47 = call i32 @safe_read(i32 %44, i8* %45, i64 %46)
store i32 %47, i32* %11
%48 = load i32, i32* %11
%49 = load i64, i64* %9
%50 = trunc i64 %49 to i32
%51 = icmp ne i32 %48, %50
br i1 %51, label %52, label %53
52: ; preds = %43
br label %57
53: ; preds = %43
br label %54
54: ; preds = %53
%55 = load i32, i32* %10
%56 = add i32 %55, 1
store i32 %56, i32* %10
br label %39
57: ; preds = %52, %39
br label %58
58: ; preds = %57, %34, %31, %28
%59 = load i32, i32* %11
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %63
61: ; preds = %58
%62 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %5
br label %64
63: ; preds = %58
store i32 0, i32* %5
br label %64
64: ; preds = %63, %61
%65 = load i32, i32* %5
ret i32 %65
}

LLVM IR (-O2):
@SEEK_CUR = global i32 0
@errno = global i64 0
@ESPIPE = global i64 0
@.str = [10 x i8] c"seek/skip\00"
define i32 @skip_blocks(i32 %0, i8* %1, i32 %2, i64 %3) {
%5 = icmp eq i32 %2, 0
br i1 %5, label %33, label %6
6: ; preds = %4, %11
%7 = phi i32 [ %12, %11 ], [ 0, %4 ]
%8 = load i32, i32* @SEEK_CUR
%9 = tail call i32 @lseek(i32 %0, i64 %3, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %14, label %11
11: ; preds = %6
%12 = add nuw i32 %7, 1
%13 = icmp eq i32 %12, %2
br i1 %13, label %33, label %6
14: ; preds = %6
%15 = icmp eq i32 %7, 0
br i1 %15, label %16, label %31
16: ; preds = %14
%17 = load i64, i64* @errno
%18 = load i64, i64* @ESPIPE
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %31
20: ; preds = %16
%21 = trunc i64 %3 to i32
br label %22
22: ; preds = %22, %20
%23 = phi i32 [ 0, %20 ], [ %26, %22 ]
%24 = tail call i32 @safe_read(i32 %0, i8* %1, i64 %3)
%25 = icmp eq i32 %24, %21
%26 = add nuw i32 %23, 1
%27 = icmp ult i32 %26, %2
%28 = select i1 %25, i1 %27, i1 false
br i1 %28, label %22, label %29
29: ; preds = %22
%30 = icmp eq i32 %24, -1
br i1 %30, label %31, label %33
31: ; preds = %14, %16, %29
%32 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %33
33: ; preds = %11, %4, %29, %31
%34 = phi i32 [ 1, %31 ], [ 0, %29 ], [ 0, %4 ], [ 0, %11 ]
ret i32 %34
}