#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 */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ exit(int);
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ fread(char *, int, scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ *freopen(char const *, char *, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
scalar_t__ ftell(int /*<<< orphan*/ *);
scalar_t__ fwrite(char const *, int, scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ *stderr;
scalar_t__ strcmp(char *, char const *);
scalar_t__ strlen(char const *);
int main(void) {
  FILE *old_stderr;
  FILE *f;
  const char fname[] = "sk1test.dat";
  const char tsttxt[] =
      "A random line of text, used to test correct freopen etc.\n";
  char buf[sizeof tsttxt] = "";
  old_stderr = freopen(fname, "w+", stderr);
  if (old_stderr == NULL ||
      fwrite(tsttxt, 1, strlen(tsttxt), stderr) != strlen(tsttxt) ||
      fclose(stderr) != 0) {
    printf("fail\n");
    exit(1);
  }
  f = fopen(fname, "rb");
  if (f == NULL || fseek(f, 0L, SEEK_END) != 0 || ftell(f) != strlen(tsttxt)) {
    printf("fail\n");
    exit(1);
  }
  rewind(f);
  if (fread(buf, 1, strlen(tsttxt), f) != strlen(tsttxt) ||
      strcmp(buf, tsttxt) != 0 || fclose(f) != 0) {
    printf("fail\n");
    exit(1);
  }
  printf("pass\n");
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe07(%rip),%rax
mov    %rax,-0x24(%rbp)
mov    0xe05(%rip),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x60(%rbp),%rdi
lea    0xe0f(%rip),%rsi
mov    $0x3a,%edx
call   10c0 <memcpy@plt>
lea    -0xa0(%rbp),%rdi
xor    %esi,%esi
mov    $0x3a,%edx
call   1080 <memset@plt>
lea    -0x24(%rbp),%rdi
mov    0x2d9f(%rip),%rax
mov    (%rax),%rdx
lea    0xdc9(%rip),%rsi
call   1090 <freopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     12c5 <main+0xe5>
lea    -0x60(%rbp),%rax
mov    %rax,-0xb0(%rbp)
lea    -0x60(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0xb0(%rbp),%rdi
mov    %rax,%rdx
mov    0x2d5c(%rip),%rax
mov    (%rax),%rcx
mov    $0x1,%esi
call   1100 <fwrite@plt>
mov    %rax,-0xa8(%rbp)
lea    -0x60(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0xa8(%rbp),%rax
cmp    %rcx,%rax
jne    12c5 <main+0xe5>
mov    0x2d25(%rip),%rax
mov    (%rax),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     12db <main+0xfb>
lea    0xd43(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
lea    -0x24(%rbp),%rdi
lea    0xd2f(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1346 <main+0x166>
mov    -0x18(%rbp),%rdi
mov    0x2ce3(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10d0 <fseek@plt>
cmp    $0x0,%rax
jne    1346 <main+0x166>
mov    -0x18(%rbp),%rdi
call   10b0 <ftell@plt>
mov    %rax,-0xb8(%rbp)
lea    -0x60(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0xb8(%rbp),%rax
cmp    %rcx,%rax
je     135c <main+0x17c>
lea    0xcc2(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1070 <rewind@plt>
lea    -0xa0(%rbp),%rax
mov    %rax,-0xc8(%rbp)
lea    -0x60(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0xc8(%rbp),%rdi
mov    %rax,%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0xc0(%rbp)
lea    -0x60(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0xc0(%rbp),%rax
cmp    %rcx,%rax
jne    13e4 <main+0x204>
lea    -0xa0(%rbp),%rdi
lea    -0x60(%rbp),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
jne    13e4 <main+0x204>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     13fa <main+0x21a>
lea    0xc24(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
lea    0xc17(%rip),%rdi
call   1060 <printf@plt>
xor    %edi,%edi
call   10f0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x90,%rsp
movabs $0x2e74736574316b73,%rax
73 74 2e
mov    %rax,(%rsp)
movl   $0x746164,0x8(%rsp)
movups 0xe61(%rip),%xmm0
movups %xmm0,0x3a(%rsp)
movaps 0xe4b(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0xe2f(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe13(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x7a(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
mov    0x2da8(%rip),%r15
mov    (%r15),%rdx
lea    0xdd2(%rip),%rsi
mov    %rsp,%rdi
call   1080 <freopen@plt>
test   %rax,%rax
je     134f <main+0x18f>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    (%r15),%rcx
mov    %r14,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
call   10e0 <fwrite@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1060 <strlen@plt>
cmp    %rax,%rbx
jne    134f <main+0x18f>
mov    (%r15),%rdi
call   1050 <fclose@plt>
test   %rax,%rax
jne    134f <main+0x18f>
lea    0xd78(%rip),%rsi
mov    %rsp,%rdi
call   10c0 <fopen@plt>
test   %rax,%rax
je     134f <main+0x18f>
mov    %rax,%rbx
mov    0x2d36(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10b0 <fseek@plt>
test   %rax,%rax
jne    134f <main+0x18f>
mov    %rbx,%rdi
call   10a0 <ftell@plt>
mov    %rax,%r14
lea    0x10(%rsp),%rdi
call   1060 <strlen@plt>
cmp    %rax,%r14
jne    134f <main+0x18f>
mov    %rbx,%rdi
call   1070 <rewind@plt>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1060 <strlen@plt>
lea    0x50(%rsp),%rdi
mov    $0x1,%esi
mov    %rax,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1060 <strlen@plt>
cmp    %rax,%r15
jne    134f <main+0x18f>
lea    0x50(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
jne    134f <main+0x18f>
mov    %rbx,%rdi
call   1050 <fclose@plt>
test   %rax,%rax
jne    134f <main+0x18f>
lea    0xccf(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
lea    0xcc1(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

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

LLVM IR (-O2):
@__const.main.fname = [12 x i8] c"sk1test.dat\00"
@__const.main.tsttxt = [58 x i8] c"A random line of text, used to test correct freopen etc.\0A\00"
@.str = [3 x i8] c"w+\00"
@stderr = global i32* null
@.str.2 = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@str = [5 x i8] c"pass\00"
@str.6 = [5 x i8] c"fail\00"
define i32 @main() {
%1 = alloca [12 x i8]
%2 = alloca [58 x i8]
%3 = alloca [58 x i8]
%4 = getelementptr inbounds [12 x i8], [12 x i8]* %1, i64 0, i64 0
%5 = getelementptr inbounds [58 x i8], [58 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [58 x i8], [58 x i8]* %3, i64 0, i64 0
%7 = load i32*, i32** @stderr
%8 = call i32* @freopen(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %7)
%9 = icmp eq i32* %8, null
br i1 %9, label %20, label %10
10: ; preds = %0
%11 = call i64 @strlen(i8* %5)
%12 = load i32*, i32** @stderr
%13 = call i64 @fwrite(i8* %5, i32 1, i64 %11, i32* %12)
%14 = call i64 @strlen(i8* %5)
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %20
16: ; preds = %10
%17 = load i32*, i32** @stderr
%18 = call i64 @fclose(i32* %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %16, %10, %0
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.6, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %16
%24 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %34, label %26
26: ; preds = %23
%27 = load i32, i32* @SEEK_END
%28 = call i64 @fseek(i32* %24, i64 0, i32 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %26
%31 = call i64 @ftell(i32* %24)
%32 = call i64 @strlen(i8* %5)
%33 = icmp eq i64 %31, %32
br i1 %33, label %37, label %34
34: ; preds = %30, %26, %23
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.6, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %30
%38 = call i32 @rewind(i32* %24)
%39 = call i64 @strlen(i8* %5)
%40 = call i64 @fread(i8* %6, i32 1, i64 %39, i32* %24)
%41 = call i64 @strlen(i8* %5)
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %49
43: ; preds = %37
%44 = call i64 @strcmp(i8* %6, i8* %5)
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %43
%47 = call i64 @fclose(i32* %24)
%48 = icmp eq i64 %47, 0
br i1 %48, label %52, label %49
49: ; preds = %46, %43, %37
%50 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.6, i64 0, i64 0))
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %46
%53 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
%54 = call i32 @exit(i32 0)
unreachable
}