#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 */
struct mtop {
  int mt_count;
  int /*<<< orphan*/ mt_op;
};
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
scalar_t__ ENOSPC;
int /*<<< orphan*/ MTIOCTOP;
int /*<<< orphan*/ MTSEEK;
scalar_t__ errno;
int ioctl(int, int /*<<< orphan*/, struct mtop *);
int read(int, char *, int);
int tell_tape(int);
int read_tape(int fd) {
  struct mtop mtop;
  int block_number, rc, n_read;
  char buffer[65535];
  block_number = tell_tape(fd);
  n_read = read(fd, buffer, sizeof(buffer));
  if (n_read == -1) {
    if (errno == ENOSPC)
      return 0;
    else
      return 1;
  }
  if (block_number != tell_tape(fd)) {
    mtop.mt_op = MTSEEK;
    mtop.mt_count = block_number;
    rc = ioctl(fd, MTIOCTOP, &mtop);
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_tape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10030,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <tell_tape@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10020(%rbp),%rsi
mov    $0xffff,%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    119c <read_tape+0x6c>
mov    0x2e5f(%rip),%rax
mov    (%rax),%rax
mov    0x2e4d(%rip),%rcx
cmp    (%rcx),%rax
jne    1190 <read_tape+0x60>
movl   $0x0,-0x4(%rbp)
jmp    11ee <read_tape+0xbe>
movl   $0x1,-0x4(%rbp)
jmp    11ee <read_tape+0xbe>
mov    -0x14(%rbp),%eax
mov    %eax,-0x10024(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <tell_tape@plt>
mov    %eax,%ecx
mov    -0x10024(%rbp),%eax
cmp    %ecx,%eax
je     11e7 <read_tape+0xb7>
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_tape>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10010,%rsp
mov    %edi,%ebx
call   1030 <tell_tape@plt>
mov    %eax,%r14d
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xffff,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
je     1192 <read_tape+0x62>
mov    %ebx,%edi
call   1030 <tell_tape@plt>
xor    %ebp,%ebp
cmp    %eax,%r14d
je     11ac <read_tape+0x7c>
mov    0x2e80(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %r14d,0x8(%rsp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
jmp    11ac <read_tape+0x7c>
mov    0x2e37(%rip),%rax
mov    (%rax),%rax
mov    0x2e25(%rip),%rcx
xor    %ebp,%ebp
cmp    (%rcx),%rax
setne  %bpl
mov    %ebp,%eax
add    $0x10010,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.mtop = type { i32, i32 }
@errno = global i64 0
@ENOSPC = global i64 0
@MTSEEK = global i32 0
@MTIOCTOP = global i32 0
define i32 @read_tape(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.mtop
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [65535 x i8]
store i32 %0, i32* %3
%9 = load i32, i32* %3
%10 = call i32 @tell_tape(i32 %9)
store i32 %10, i32* %5
%11 = load i32, i32* %3
%12 = getelementptr inbounds [65535 x i8], [65535 x i8]* %8, i64 0, i64 0
%13 = call i32 @read(i32 %11, i8* %12, i32 65535)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %22
16: ; preds = %1
%17 = load i64, i64* @errno
%18 = load i64, i64* @ENOSPC
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %21
20: ; preds = %16
store i32 0, i32* %2
br label %36
21: ; preds = %16
store i32 1, i32* %2
br label %36
22: ; preds = %1
%23 = load i32, i32* %5
%24 = load i32, i32* %3
%25 = call i32 @tell_tape(i32 %24)
%26 = icmp ne i32 %23, %25
br i1 %26, label %27, label %35
27: ; preds = %22
%28 = load i32, i32* @MTSEEK
%29 = getelementptr inbounds %struct.mtop, %struct.mtop* %4, i32 0, i32 1
store i32 %28, i32* %29
%30 = load i32, i32* %5
%31 = getelementptr inbounds %struct.mtop, %struct.mtop* %4, i32 0, i32 0
store i32 %30, i32* %31
%32 = load i32, i32* %3
%33 = load i32, i32* @MTIOCTOP
%34 = call i32 @ioctl(i32 %32, i32 %33, %struct.mtop* %4)
store i32 %34, i32* %6
br label %35
35: ; preds = %27, %22
store i32 0, i32* %2
br label %36
36: ; preds = %35, %21, %20
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
%struct.mtop = type { i32, i32 }
@errno = global i64 0
@ENOSPC = global i64 0
@MTSEEK = global i32 0
@MTIOCTOP = global i32 0
define i32 @read_tape(i32 %0) {
%2 = alloca %struct.mtop
%3 = alloca [65535 x i8]
%4 = bitcast %struct.mtop* %2 to i8*
%5 = getelementptr inbounds [65535 x i8], [65535 x i8]* %3, i64 0, i64 0
%6 = tail call i32 @tell_tape(i32 %0)
%7 = call i32 @read(i32 %0, i8* %5, i32 65535)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %14
9: ; preds = %1
%10 = load i64, i64* @errno
%11 = load i64, i64* @ENOSPC
%12 = icmp ne i64 %10, %11
%13 = zext i1 %12 to i32
br label %23
14: ; preds = %1
%15 = tail call i32 @tell_tape(i32 %0)
%16 = icmp eq i32 %6, %15
br i1 %16, label %23, label %17
17: ; preds = %14
%18 = load i32, i32* @MTSEEK
%19 = getelementptr inbounds %struct.mtop, %struct.mtop* %2, i64 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.mtop, %struct.mtop* %2, i64 0, i32 0
store i32 %6, i32* %20
%21 = load i32, i32* @MTIOCTOP
%22 = call i32 @ioctl(i32 %0, i32 %21, %struct.mtop* %2)
br label %23
23: ; preds = %14, %17, %9
%24 = phi i32 [ %13, %9 ], [ 0, %17 ], [ 0, %14 ]
ret i32 %24
}