#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;
scalar_t__ 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, 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;
    if (ioctl(fd, MTIOCTOP, &mtop) != 0)
      return 1;
  }
  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,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%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    1206 <read_tape+0xd6>
movl   $0x1,-0x4(%rbp)
jmp    1206 <read_tape+0xd6>
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     11ff <read_tape+0xcf>
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>
cmp    $0x0,%rax
je     11fa <read_tape+0xca>
movl   $0x1,-0x4(%rbp)
jmp    1206 <read_tape+0xd6>
jmp    11ff <read_tape+0xcf>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_tape>:
push   %rbp
push   %rbx
sub    $0x10018,%rsp
mov    %edi,%ebx
call   1030 <tell_tape@plt>
mov    %eax,%ebp
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xffff,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
je     119a <read_tape+0x6a>
mov    %ebx,%edi
call   1030 <tell_tape@plt>
cmp    %eax,%ebp
je     1196 <read_tape+0x66>
mov    0x2e86(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %ebp,0x8(%rsp)
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
jne    11b3 <read_tape+0x83>
xor    %eax,%eax
jmp    11b3 <read_tape+0x83>
mov    0x2e2f(%rip),%rax
mov    (%rax),%rcx
mov    0x2e1d(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
setne  %al
add    $0x10018,%rsp
pop    %rbx
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 [65535 x i8]
store i32 %0, i32* %3
%8 = load i32, i32* %3
%9 = call i32 @tell_tape(i32 %8)
store i32 %9, i32* %5
%10 = load i32, i32* %3
%11 = getelementptr inbounds [65535 x i8], [65535 x i8]* %7, i64 0, i64 0
%12 = call i32 @read(i32 %10, i8* %11, i32 65535)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %21
15: ; preds = %1
%16 = load i64, i64* @errno
%17 = load i64, i64* @ENOSPC
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %20
19: ; preds = %15
store i32 0, i32* %2
br label %38
20: ; preds = %15
store i32 1, i32* %2
br label %38
21: ; preds = %1
%22 = load i32, i32* %5
%23 = load i32, i32* %3
%24 = call i32 @tell_tape(i32 %23)
%25 = icmp ne i32 %22, %24
br i1 %25, label %26, label %37
26: ; preds = %21
%27 = load i32, i32* @MTSEEK
%28 = getelementptr inbounds %struct.mtop, %struct.mtop* %4, i32 0, i32 1
store i32 %27, i32* %28
%29 = load i32, i32* %5
%30 = getelementptr inbounds %struct.mtop, %struct.mtop* %4, i32 0, i32 0
store i32 %29, i32* %30
%31 = load i32, i32* %3
%32 = load i32, i32* @MTIOCTOP
%33 = call i64 @ioctl(i32 %31, i32 %32, %struct.mtop* %4)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %26
store i32 1, i32* %2
br label %38
36: ; preds = %26
br label %37
37: ; preds = %36, %21
store i32 0, i32* %2
br label %38
38: ; preds = %37, %35, %20, %19
%39 = load i32, i32* %2
ret i32 %39
}

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 %25
14: ; preds = %1
%15 = tail call i32 @tell_tape(i32 %0)
%16 = icmp eq i32 %6, %15
br i1 %16, label %24, 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 i64 @ioctl(i32 %0, i32 %21, %struct.mtop* %2)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %17, %14
br label %25
25: ; preds = %17, %9, %24
%26 = phi i32 [ 0, %24 ], [ %13, %9 ], [ 1, %17 ]
ret i32 %26
}