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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
scalar_t__ fclose(int /*<<< orphan*/ *);
scalar_t__ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ ftruncate(int /*<<< orphan*/, int);
int fwrite(char const *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perr(char *);
int /*<<< orphan*/ printf(char *);
int strlen(char const *);
scalar_t__ strncmp(char *, char const *, int);
int main(void) {
  FILE *f;
  const char fname[] = "sk1test.dat";
  const char tsttxt1[] = "This is the first and only line of this file.\n";
  const char tsttxt2[] = "Now there is a second line.\n";
  char buf[sizeof(tsttxt1) + sizeof(tsttxt2) - 1] = "";
  f = fopen(fname, "w+");
  if (f == NULL || fwrite(tsttxt1, 1, strlen(tsttxt1), f) != strlen(tsttxt1))
    perr("open or fwrite");
  if (fflush(f) != 0)
    perr("fflush");
  if (ftruncate(fileno(f), strlen(tsttxt1) - 20) != 0)
    perr("ftruncate");
  if (fclose(f) != 0)
    perr("fclose");
  f = fopen(fname, "r");
  if (f == NULL || fread(buf, 1, sizeof(buf), f) != strlen(tsttxt1) - 20 ||
      strncmp(buf, tsttxt1, strlen(tsttxt1) - 20) != 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    $0xf0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe07(%rip),%rax
mov    %rax,-0x1c(%rbp)
mov    0xe05(%rip),%eax
mov    %eax,-0x14(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xe3f(%rip),%rsi
mov    $0x2f,%edx
call   10b0 <memcpy@plt>
mov    0xe5e(%rip),%rax
mov    %rax,-0x70(%rbp)
mov    0xe5b(%rip),%rax
mov    %rax,-0x68(%rbp)
mov    0xe58(%rip),%rax
mov    %rax,-0x60(%rbp)
mov    0xe56(%rip),%eax
mov    %eax,-0x58(%rbp)
mov    0xe51(%rip),%al
mov    %al,-0x54(%rbp)
lea    -0xc0(%rbp),%rdi
xor    %esi,%esi
mov    $0x4b,%edx
call   10a0 <memset@plt>
lea    -0x1c(%rbp),%rdi
lea    0xda0(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     12ca <main+0xea>
lea    -0x50(%rbp),%rax
mov    %rax,-0xd0(%rbp)
lea    -0x50(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0xd0(%rbp),%rdi
mov    %eax,%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1100 <fwrite@plt>
mov    %eax,-0xc4(%rbp)
lea    -0x50(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0xc4(%rbp),%eax
cmp    %ecx,%eax
je     12d6 <main+0xf6>
lea    0xd3e(%rip),%rdi
call   1090 <perr@plt>
mov    -0x10(%rbp),%rdi
call   10d0 <fflush@plt>
cmp    $0x0,%rax
je     12f5 <main+0x115>
lea    0xd2e(%rip),%rdi
call   1090 <perr@plt>
mov    -0x10(%rbp),%rdi
call   10c0 <fileno@plt>
mov    %eax,-0xd4(%rbp)
lea    -0x50(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0xd4(%rbp),%edi
mov    %eax,%esi
sub    $0x14,%esi
call   1080 <ftruncate@plt>
cmp    $0x0,%rax
je     1333 <main+0x153>
lea    0xcf7(%rip),%rdi
call   1090 <perr@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
cmp    $0x0,%rax
je     1352 <main+0x172>
lea    0xce2(%rip),%rdi
call   1090 <perr@plt>
lea    -0x1c(%rbp),%rdi
lea    0xcd9(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1404 <main+0x224>
lea    -0xc0(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4b,%edx
call   1040 <fread@plt>
mov    %eax,-0xd8(%rbp)
lea    -0x50(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0xd8(%rbp),%eax
sub    $0x14,%ecx
cmp    %ecx,%eax
jne    1404 <main+0x224>
lea    -0xc0(%rbp),%rax
mov    %rax,-0xe8(%rbp)
lea    -0x50(%rbp),%rax
mov    %rax,-0xe0(%rbp)
lea    -0x50(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0xe8(%rbp),%rdi
mov    -0xe0(%rbp),%rsi
mov    %eax,%edx
sub    $0x14,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1404 <main+0x224>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
cmp    $0x0,%rax
je     141a <main+0x23a>
lea    0xc2d(%rip),%rdi
call   1070 <printf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
lea    0xc1d(%rip),%rdi
call   1070 <printf@plt>
xor    %edi,%edi
call   10f0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x90,%rsp
movabs $0x2e74736574316b73,%rax
73 74 2e
mov    %rax,(%rsp)
movl   $0x746164,0x8(%rsp)
movups 0xe87(%rip),%xmm0
movups %xmm0,0x2f(%rsp)
movaps 0xe6c(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe50(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x7b(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
lea    0xde4(%rip),%rsi
mov    %rsp,%rdi
call   10c0 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1265 <main+0xa5>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1070 <strlen@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %rbx,%rcx
call   10e0 <fwrite@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1070 <strlen@plt>
cmp    %eax,%ebp
je     1271 <main+0xb1>
lea    0xda3(%rip),%rdi
call   1090 <perr@plt>
mov    %rbx,%rdi
call   10b0 <fflush@plt>
test   %rax,%rax
je     128a <main+0xca>
lea    0xd99(%rip),%rdi
call   1090 <perr@plt>
mov    %rbx,%rdi
call   10a0 <fileno@plt>
mov    %eax,%ebp
lea    0x10(%rsp),%rdi
call   1070 <strlen@plt>
lea    -0x14(%rax),%esi
mov    %ebp,%edi
call   1080 <ftruncate@plt>
test   %rax,%rax
je     12b9 <main+0xf9>
lea    0xd71(%rip),%rdi
call   1090 <perr@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
test   %rax,%rax
je     12d2 <main+0x112>
lea    0xd62(%rip),%rdi
call   1090 <perr@plt>
lea    0xd5d(%rip),%rsi
mov    %rsp,%rdi
call   10c0 <fopen@plt>
test   %rax,%rax
je     1355 <main+0x195>
mov    %rax,%rbx
lea    0x40(%rsp),%rdi
mov    $0x1,%esi
mov    $0x4b,%edx
mov    %rax,%rcx
call   1050 <fread@plt>
mov    %eax,%ebp
lea    0x10(%rsp),%rdi
call   1070 <strlen@plt>
add    $0xffffffec,%eax
cmp    %eax,%ebp
jne    1355 <main+0x195>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   1070 <strlen@plt>
lea    -0x14(%rax),%edx
lea    0x40(%rsp),%rdi
mov    %rbp,%rsi
call   1030 <strncmp@plt>
test   %rax,%rax
jne    1355 <main+0x195>
mov    %rbx,%rdi
call   1060 <fclose@plt>
test   %rax,%rax
jne    1355 <main+0x195>
lea    0xcef(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
lea    0xce1(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@__const.main.fname = [12 x i8] c"sk1test.dat\00"
@__const.main.tsttxt1 = [47 x i8] c"This is the first and only line of this file.\0A\00"
@__const.main.tsttxt2 = [29 x i8] c"Now there is a second line.\0A\00"
@.str = [3 x i8] c"w+\00"
@.str.1 = [15 x i8] c"open or fwrite\00"
@.str.2 = [7 x i8] c"fflush\00"
@.str.3 = [10 x i8] c"ftruncate\00"
@.str.4 = [7 x i8] c"fclose\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [6 x i8] c"fail\0A\00"
@.str.7 = [6 x i8] c"pass\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [12 x i8]
%4 = alloca [47 x i8]
%5 = alloca [29 x i8]
%6 = alloca [75 x i8]
store i32 0, i32* %1
%7 = bitcast [12 x i8]* %3 to i8*
%8 = bitcast [47 x i8]* %4 to i8*
%9 = bitcast [29 x i8]* %5 to i8*
%10 = bitcast [75 x i8]* %6 to i8*
%11 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 0
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %2
%13 = load i32*, i32** %2
%14 = icmp eq i32* %13, null
br i1 %14, label %24, label %15
15: ; preds = %0
%16 = getelementptr inbounds [47 x i8], [47 x i8]* %4, i64 0, i64 0
%17 = getelementptr inbounds [47 x i8], [47 x i8]* %4, i64 0, i64 0
%18 = call i32 @strlen(i8* %17)
%19 = load i32*, i32** %2
%20 = call i32 @fwrite(i8* %16, i32 1, i32 %18, i32* %19)
%21 = getelementptr inbounds [47 x i8], [47 x i8]* %4, i64 0, i64 0
%22 = call i32 @strlen(i8* %21)
%23 = icmp ne i32 %20, %22
br i1 %23, label %24, label %26
24: ; preds = %15, %0
%25 = call i32 @perr(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %15
%27 = load i32*, i32** %2
%28 = call i64 @fflush(i32* %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 @perr(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %30, %26
%33 = load i32*, i32** %2
%34 = call i32 @fileno(i32* %33)
%35 = getelementptr inbounds [47 x i8], [47 x i8]* %4, i64 0, i64 0
%36 = call i32 @strlen(i8* %35)
%37 = sub nsw i32 %36, 20
%38 = call i64 @ftruncate(i32 %34, i32 %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %32
%41 = call i32 @perr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
br label %42
42: ; preds = %40, %32
%43 = load i32*, i32** %2
%44 = call i64 @fclose(i32* %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %42
%47 = call i32 @perr(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %48
48: ; preds = %46, %42
%49 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 0
%50 = call i32* @fopen(i8* %49, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %50, i32** %2
%51 = load i32*, i32** %2
%52 = icmp eq i32* %51, null
br i1 %52, label %73, label %53
53: ; preds = %48
%54 = getelementptr inbounds [75 x i8], [75 x i8]* %6, i64 0, i64 0
%55 = load i32*, i32** %2
%56 = call i32 @fread(i8* %54, i32 1, i32 75, i32* %55)
%57 = getelementptr inbounds [47 x i8], [47 x i8]* %4, i64 0, i64 0
%58 = call i32 @strlen(i8* %57)
%59 = sub nsw i32 %58, 20
%60 = icmp ne i32 %56, %59
br i1 %60, label %73, label %61
61: ; preds = %53
%62 = getelementptr inbounds [75 x i8], [75 x i8]* %6, i64 0, i64 0
%63 = getelementptr inbounds [47 x i8], [47 x i8]* %4, i64 0, i64 0
%64 = getelementptr inbounds [47 x i8], [47 x i8]* %4, i64 0, i64 0
%65 = call i32 @strlen(i8* %64)
%66 = sub nsw i32 %65, 20
%67 = call i64 @strncmp(i8* %62, i8* %63, i32 %66)
%68 = icmp ne i64 %67, 0
br i1 %68, label %73, label %69
69: ; preds = %61
%70 = load i32*, i32** %2
%71 = call i64 @fclose(i32* %70)
%72 = icmp ne i64 %71, 0
br i1 %72, label %73, label %76
73: ; preds = %69, %61, %53, %48
%74 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%75 = call i32 @exit(i32 1)
unreachable
76: ; preds = %69
%77 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
%78 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@__const.main.fname = [12 x i8] c"sk1test.dat\00"
@__const.main.tsttxt1 = [47 x i8] c"This is the first and only line of this file.\0A\00"
@.str = [3 x i8] c"w+\00"
@.str.1 = [15 x i8] c"open or fwrite\00"
@.str.2 = [7 x i8] c"fflush\00"
@.str.3 = [10 x i8] c"ftruncate\00"
@.str.4 = [7 x i8] c"fclose\00"
@.str.5 = [2 x i8] c"r\00"
@str = [5 x i8] c"pass\00"
@str.8 = [5 x i8] c"fail\00"
define i32 @main() {
%1 = alloca [12 x i8]
%2 = alloca [47 x i8]
%3 = alloca [75 x i8]
%4 = getelementptr inbounds [12 x i8], [12 x i8]* %1, i64 0, i64 0
%5 = getelementptr inbounds [47 x i8], [47 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [75 x i8], [75 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 %14, label %9
9: ; preds = %0
%10 = call i32 @strlen(i8* %5)
%11 = call i32 @fwrite(i8* %5, i32 1, i32 %10, i32* %7)
%12 = call i32 @strlen(i8* %5)
%13 = icmp eq i32 %11, %12
br i1 %13, label %16, label %14
14: ; preds = %9, %0
%15 = call i32 @perr(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %9
%17 = call i64 @fflush(i32* %7)
%18 = icmp eq i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = call i32 @perr(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %16
%22 = call i32 @fileno(i32* %7)
%23 = call i32 @strlen(i8* %5)
%24 = add nsw i32 %23, -20
%25 = call i64 @ftruncate(i32 %22, i32 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %21
%28 = call i32 @perr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
br label %29
29: ; preds = %27, %21
%30 = call i64 @fclose(i32* %7)
%31 = icmp eq i64 %30, 0
br i1 %31, label %34, label %32
32: ; preds = %29
%33 = call i32 @perr(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %34
34: ; preds = %32, %29
%35 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%36 = icmp eq i32* %35, null
br i1 %36, label %50, label %37
37: ; preds = %34
%38 = call i32 @fread(i8* %6, i32 1, i32 75, i32* %35)
%39 = call i32 @strlen(i8* %5)
%40 = add nsw i32 %39, -20
%41 = icmp eq i32 %38, %40
br i1 %41, label %42, label %50
42: ; preds = %37
%43 = call i32 @strlen(i8* %5)
%44 = add nsw i32 %43, -20
%45 = call i64 @strncmp(i8* %6, i8* %5, i32 %44)
%46 = icmp eq i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %42
%48 = call i64 @fclose(i32* %35)
%49 = icmp eq i64 %48, 0
br i1 %49, label %53, label %50
50: ; preds = %47, %42, %37, %34
%51 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.8, i64 0, i64 0))
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %47
%54 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
%55 = call i32 @exit(i32 0)
unreachable
}