#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ EINVAL;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_CUR;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ fwrite(char const *, int, scalar_t__, int /*<<< orphan*/ *);
int lseek(int, long, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
scalar_t__ read(int, char *, scalar_t__);
scalar_t__ strcmp(char *, char const *);
scalar_t__ strlen(char const *);
int main(void) {
  FILE *f;
  const char fname[] = "sk1test.dat";
  const char tsttxt[] =
      "A random line of text, used to test correct read, write and seek.\n";
  char buf[sizeof tsttxt] = "";
  int fd;
  f = fopen(fname, "wb");
  if (f == NULL || fwrite(tsttxt, 1, strlen(tsttxt), f) != strlen(tsttxt) ||
      fclose(f) != 0) {
    printf("fail\n");
    exit(1);
  }
  fd = open(fname, O_RDONLY);
  if (fd < 0 || lseek(fd, -1L, SEEK_CUR) != -1 || errno != EINVAL ||
      read(fd, buf, strlen(tsttxt)) != strlen(tsttxt) ||
      strcmp(buf, tsttxt) != 0) {
    printf("fail\n");
    exit(1);
  }
  printf("pass\n");
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe27(%rip),%rax
mov    %rax,-0x1c(%rbp)
mov    0xe25(%rip),%eax
mov    %eax,-0x14(%rbp)
lea    -0x60(%rbp),%rdi
lea    0xe2f(%rip),%rsi
mov    $0x43,%edx
call   10a0 <memcpy@plt>
lea    -0xb0(%rbp),%rdi
xor    %esi,%esi
mov    $0x43,%edx
call   1070 <memset@plt>
lea    -0x1c(%rbp),%rdi
lea    0xdf3(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     128f <main+0xcf>
lea    -0x60(%rbp),%rax
mov    %rax,-0xc8(%rbp)
lea    -0x60(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xc8(%rbp),%rdi
mov    %rax,%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   10e0 <fwrite@plt>
mov    %rax,-0xc0(%rbp)
lea    -0x60(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0xc0(%rbp),%rax
cmp    %rcx,%rax
jne    128f <main+0xcf>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
cmp    $0x0,%rax
je     12a5 <main+0xe5>
lea    0xd79(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    -0x1c(%rbp),%rdi
mov    0x2d30(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0xb4(%rbp)
cmpl   $0x0,-0xb4(%rbp)
jl     137d <main+0x1bd>
mov    -0xb4(%rbp),%edi
mov    0x2d11(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffffffffffff,%rsi
call   1060 <lseek@plt>
cmp    $0xffffffff,%eax
jne    137d <main+0x1bd>
mov    0x2cdb(%rip),%rax
mov    (%rax),%rax
mov    0x2cc9(%rip),%rcx
cmp    (%rcx),%rax
jne    137d <main+0x1bd>
mov    -0xb4(%rbp),%eax
mov    %eax,-0xdc(%rbp)
lea    -0xb0(%rbp),%rax
mov    %rax,-0xd8(%rbp)
lea    -0x60(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xdc(%rbp),%edi
mov    -0xd8(%rbp),%rsi
mov    %rax,%rdx
call   1080 <read@plt>
mov    %rax,-0xd0(%rbp)
lea    -0x60(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0xd0(%rbp),%rax
cmp    %rcx,%rax
jne    137d <main+0x1bd>
lea    -0xb0(%rbp),%rdi
lea    -0x60(%rbp),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     1393 <main+0x1d3>
lea    0xc8b(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xc7b(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0xb0,%rsp
movabs $0x2e74736574316b73,%rax
73 74 2e
mov    %rax,(%rsp)
movl   $0x746164,0x8(%rsp)
movaps 0xe87(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movaps 0xe6b(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0xe4f(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe33(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0xa2e6b,0x4f(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movl   $0x0,0x9f(%rsp)
lea    0xde3(%rip),%rsi
mov    %rsp,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1322 <main+0x182>
mov    %rax,%rbx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
mov    %rbx,%rcx
call   10c0 <fwrite@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1050 <strlen@plt>
cmp    %rax,%r15
jne    1322 <main+0x182>
mov    %rbx,%rdi
call   1040 <fclose@plt>
test   %rax,%rax
jne    1322 <main+0x182>
mov    0x2d57(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1322 <main+0x182>
mov    %eax,%ebx
mov    0x2d44(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    $0xffffffffffffffff,%rsi
call   1060 <lseek@plt>
cmp    $0xffffffff,%eax
jne    1322 <main+0x182>
mov    0x2d10(%rip),%rax
mov    (%rax),%rax
mov    0x2cfe(%rip),%rcx
cmp    (%rcx),%rax
jne    1322 <main+0x182>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x60(%rsp),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <read@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1050 <strlen@plt>
cmp    %rax,%rbx
jne    1322 <main+0x182>
lea    0x60(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
jne    1322 <main+0x182>
lea    0xcf9(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
lea    0xceb(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@__const.main.fname = [12 x i8] c"sk1test.dat\00"
@__const.main.tsttxt = [67 x i8] c"A random line of text, used to test correct read, write and seek.\0A\00"
@.str = [3 x i8] c"wb\00"
@.str.1 = [6 x i8] c"fail\0A\00"
@O_RDONLY = global i32 0
@SEEK_CUR = global i32 0
@errno = global i64 0
@EINVAL = global i64 0
@.str.2 = [6 x i8] c"pass\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [12 x i8]
%4 = alloca [67 x i8]
%5 = alloca [67 x i8]
%6 = alloca i32
store i32 0, i32* %1
%7 = bitcast [12 x i8]* %3 to i8*
%8 = bitcast [67 x i8]* %4 to i8*
%9 = bitcast [67 x i8]* %5 to i8*
%10 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 0
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %2
%12 = load i32*, i32** %2
%13 = icmp eq i32* %12, null
br i1 %13, label %27, label %14
14: ; preds = %0
%15 = getelementptr inbounds [67 x i8], [67 x i8]* %4, i64 0, i64 0
%16 = getelementptr inbounds [67 x i8], [67 x i8]* %4, i64 0, i64 0
%17 = call i64 @strlen(i8* %16)
%18 = load i32*, i32** %2
%19 = call i64 @fwrite(i8* %15, i32 1, i64 %17, i32* %18)
%20 = getelementptr inbounds [67 x i8], [67 x i8]* %4, i64 0, i64 0
%21 = call i64 @strlen(i8* %20)
%22 = icmp ne i64 %19, %21
br i1 %22, label %27, label %23
23: ; preds = %14
%24 = load i32*, i32** %2
%25 = call i64 @fclose(i32* %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %23, %14, %0
%28 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %23
%31 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 0
%32 = load i32, i32* @O_RDONLY
%33 = call i32 @open(i8* %31, i32 %32)
store i32 %33, i32* %6
%34 = load i32, i32* %6
%35 = icmp slt i32 %34, 0
br i1 %35, label %59, label %36
36: ; preds = %30
%37 = load i32, i32* %6
%38 = load i32, i32* @SEEK_CUR
%39 = call i32 @lseek(i32 %37, i64 -1, i32 %38)
%40 = icmp ne i32 %39, -1
br i1 %40, label %59, label %41
41: ; preds = %36
%42 = load i64, i64* @errno
%43 = load i64, i64* @EINVAL
%44 = icmp ne i64 %42, %43
br i1 %44, label %59, label %45
45: ; preds = %41
%46 = load i32, i32* %6
%47 = getelementptr inbounds [67 x i8], [67 x i8]* %5, i64 0, i64 0
%48 = getelementptr inbounds [67 x i8], [67 x i8]* %4, i64 0, i64 0
%49 = call i64 @strlen(i8* %48)
%50 = call i64 @read(i32 %46, i8* %47, i64 %49)
%51 = getelementptr inbounds [67 x i8], [67 x i8]* %4, i64 0, i64 0
%52 = call i64 @strlen(i8* %51)
%53 = icmp ne i64 %50, %52
br i1 %53, label %59, label %54
54: ; preds = %45
%55 = getelementptr inbounds [67 x i8], [67 x i8]* %5, i64 0, i64 0
%56 = getelementptr inbounds [67 x i8], [67 x i8]* %4, i64 0, i64 0
%57 = call i64 @strcmp(i8* %55, i8* %56)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %54, %45, %41, %36, %30
%60 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%61 = call i32 @exit(i32 1)
unreachable
62: ; preds = %54
%63 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%64 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@__const.main.fname = [12 x i8] c"sk1test.dat\00"
@__const.main.tsttxt = [67 x i8] c"A random line of text, used to test correct read, write and seek.\0A\00"
@.str = [3 x i8] c"wb\00"
@O_RDONLY = global i32 0
@SEEK_CUR = global i32 0
@errno = global i64 0
@EINVAL = global i64 0
@str = [5 x i8] c"pass\00"
@str.4 = [5 x i8] c"fail\00"
define i32 @main() {
%1 = alloca [12 x i8]
%2 = alloca [67 x i8]
%3 = alloca [67 x i8]
%4 = getelementptr inbounds [12 x i8], [12 x i8]* %1, i64 0, i64 0
%5 = getelementptr inbounds [67 x i8], [67 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [67 x i8], [67 x i8]* %3, i64 0, i64 0
%7 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %17, label %9
9: ; preds = %0
%10 = call i64 @strlen(i8* %5)
%11 = call i64 @fwrite(i8* %5, i32 1, i64 %10, i32* %7)
%12 = call i64 @strlen(i8* %5)
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = tail call i64 @fclose(i32* %7)
%16 = icmp eq i64 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %14, %9, %0
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.4, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %14
%21 = load i32, i32* @O_RDONLY
%22 = call i32 @open(i8* %4, i32 %21)
%23 = icmp slt i32 %22, 0
br i1 %23, label %40, label %24
24: ; preds = %20
%25 = load i32, i32* @SEEK_CUR
%26 = tail call i32 @lseek(i32 %22, i64 -1, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %40
28: ; preds = %24
%29 = load i64, i64* @errno
%30 = load i64, i64* @EINVAL
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %40
32: ; preds = %28
%33 = call i64 @strlen(i8* %5)
%34 = call i64 @read(i32 %22, i8* %6, i64 %33)
%35 = call i64 @strlen(i8* %5)
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %40
37: ; preds = %32
%38 = call i64 @strcmp(i8* %6, i8* %5)
%39 = icmp eq i64 %38, 0
br i1 %39, label %43, label %40
40: ; preds = %37, %32, %28, %24, %20
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.4, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %37
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
%45 = call i32 @exit(i32 0)
unreachable
}