#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 stat {
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ MTREW;
int /*<<< orphan*/ SEEK_SET;
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ errx(int, char *);
scalar_t__ fstat(int, struct stat *);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ writeop(int, int /*<<< orphan*/);
void rewind_tape(int fd) {
  struct stat sp;
  if (fstat(fd, &sp))
    errx(12, "fstat in rewind");
  if (S_ISREG(sp.st_mode)) {
    if (lseek(fd, 0, SEEK_SET) == -1)
      errx(13, "lseek");
  } else
    writeop(fd, MTREW);
}


x86 assembly (clang, -O0):
<rewind_tape>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0x0,%rax
je     1182 <rewind_tape+0x32>
mov    $0xc,%edi
lea    0xe83(%rip),%rsi
call   1030 <errx@plt>
mov    -0x8(%rbp),%edi
call   1050 <S_ISREG@plt>
cmp    $0x0,%rax
je     11c6 <rewind_tape+0x76>
mov    -0x4(%rbp),%edi
mov    0x2e42(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
jne    11c1 <rewind_tape+0x71>
mov    $0xd,%edi
lea    0xe54(%rip),%rsi
call   1030 <errx@plt>
jmp    11d7 <rewind_tape+0x87>
mov    -0x4(%rbp),%edi
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
call   1060 <writeop@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rewind_tape>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
lea    0x8(%rsp),%rsi
call   1070 <fstat@plt>
test   %rax,%rax
je     1177 <rewind_tape+0x27>
lea    0xe93(%rip),%rsi
mov    $0xc,%edi
call   1030 <errx@plt>
mov    0x8(%rsp),%edi
call   1050 <S_ISREG@plt>
test   %rax,%rax
je     11a2 <rewind_tape+0x52>
mov    0x2e54(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
je     11b7 <rewind_tape+0x67>
add    $0x10,%rsp
pop    %rbx
ret
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
add    $0x10,%rsp
pop    %rbx
jmp    1060 <writeop@plt>
lea    0xe52(%rip),%rsi
mov    $0xd,%edi
add    $0x10,%rsp
pop    %rbx
jmp    1030 <errx@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [16 x i8] c"fstat in rewind\00"
@SEEK_SET = global i32 0
@.str.1 = [6 x i8] c"lseek\00"
@MTREW = global i32 0
define void @rewind_tape(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.stat
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = call i64 @fstat(i32 %4, %struct.stat* %3)
%6 = icmp ne i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = call i32 @errx(i32 12, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %1
%10 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%11 = load i32, i32* %10
%12 = call i64 @S_ISREG(i32 %11)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %22
14: ; preds = %9
%15 = load i32, i32* %2
%16 = load i32, i32* @SEEK_SET
%17 = call i32 @lseek(i32 %15, i32 0, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = call i32 @errx(i32 13, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %14
br label %26
22: ; preds = %9
%23 = load i32, i32* %2
%24 = load i32, i32* @MTREW
%25 = call i32 @writeop(i32 %23, i32 %24)
br label %26
26: ; preds = %22, %21
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [16 x i8] c"fstat in rewind\00"
@SEEK_SET = global i32 0
@.str.1 = [6 x i8] c"lseek\00"
@MTREW = global i32 0
define void @rewind_tape(i32 %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @fstat(i32 %0, %struct.stat* %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %1
%7 = tail call i32 @errx(i32 12, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %1
%9 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%10 = load i32, i32* %9
%11 = tail call i64 @S_ISREG(i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %19, label %13
13: ; preds = %8
%14 = load i32, i32* @SEEK_SET
%15 = tail call i32 @lseek(i32 %0, i32 0, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %22
17: ; preds = %13
%18 = tail call i32 @errx(i32 13, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %22
19: ; preds = %8
%20 = load i32, i32* @MTREW
%21 = tail call i32 @writeop(i32 %0, i32 %20)
br label %22
22: ; preds = %13, %17, %19
ret void
}