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

/* Variables and functions */
int /*<<< orphan*/ MTIOCTOP;
int /*<<< orphan*/ MTREW;
int /*<<< orphan*/ MTWEOF;
int ioctl(int, int /*<<< orphan*/, struct mtop *);
int init_tape(int fd) {
  struct mtop mtop;
  int rc;
  mtop.mt_op = MTWEOF;
  mtop.mt_count = 2;
  rc = ioctl(fd, MTIOCTOP, &mtop);
  if (rc)
    return rc;
  mtop.mt_op = MTREW;
  return ioctl(fd, MTIOCTOP, &mtop);
}


x86 assembly (clang, -O0):
<init_tape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2ebe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
movl   $0x2,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2eb0(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     115b <init_tape+0x4b>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    117f <init_tape+0x6f>
mov    0x2e6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_tape>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2ec3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
movl   $0x2,(%rsp)
mov    0x2eb7(%rip),%r14
mov    (%r14),%esi
mov    %rsp,%rdx
call   1030 <ioctl@plt>
test   %eax,%eax
jne    115a <init_tape+0x4a>
mov    0x2e89(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    (%r14),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.mtop = type { i32, i32 }
@MTWEOF = global i32 0
@MTIOCTOP = global i32 0
@MTREW = global i32 0
define i32 @init_tape(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.mtop
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* @MTWEOF
%7 = getelementptr inbounds %struct.mtop, %struct.mtop* %4, i32 0, i32 1
store i32 %6, i32* %7
%8 = getelementptr inbounds %struct.mtop, %struct.mtop* %4, i32 0, i32 0
store i32 2, i32* %8
%9 = load i32, i32* %3
%10 = load i32, i32* @MTIOCTOP
%11 = call i32 @ioctl(i32 %9, i32 %10, %struct.mtop* %4)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = load i32, i32* %5
store i32 %15, i32* %2
br label %22
16: ; preds = %1
%17 = load i32, i32* @MTREW
%18 = getelementptr inbounds %struct.mtop, %struct.mtop* %4, i32 0, i32 1
store i32 %17, i32* %18
%19 = load i32, i32* %3
%20 = load i32, i32* @MTIOCTOP
%21 = call i32 @ioctl(i32 %19, i32 %20, %struct.mtop* %4)
store i32 %21, i32* %2
br label %22
22: ; preds = %16, %14
%23 = load i32, i32* %2
ret i32 %23
}

LLVM IR (-O2):
%struct.mtop = type { i32, i32 }
@MTWEOF = global i32 0
@MTIOCTOP = global i32 0
@MTREW = global i32 0
define i32 @init_tape(i32 %0) {
%2 = alloca %struct.mtop
%3 = bitcast %struct.mtop* %2 to i8*
%4 = load i32, i32* @MTWEOF
%5 = getelementptr inbounds %struct.mtop, %struct.mtop* %2, i64 0, i32 1
store i32 %4, i32* %5
%6 = getelementptr inbounds %struct.mtop, %struct.mtop* %2, i64 0, i32 0
store i32 2, i32* %6
%7 = load i32, i32* @MTIOCTOP
%8 = call i32 @ioctl(i32 %0, i32 %7, %struct.mtop* %2)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %1
%11 = load i32, i32* @MTREW
store i32 %11, i32* %5
%12 = load i32, i32* @MTIOCTOP
%13 = call i32 @ioctl(i32 %0, i32 %12, %struct.mtop* %2)
br label %14
14: ; preds = %1, %10
%15 = phi i32 [ %13, %10 ], [ %8, %1 ]
ret i32 %15
}