#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*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int fwrite(char const *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int strlen(char const *);
scalar_t__ strncmp(char *, char const *, int);
scalar_t__ truncate(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) ||
      fclose(f) != 0) {
    printf("fail\n");
    exit(1);
  }
  if (truncate(fname, strlen(tsttxt1) - 10) != 0) {
    perror("truncate");
    exit(1);
  }
  f = fopen(fname, "r");
  if (f == NULL || fread(buf, 1, sizeof(buf), f) != strlen(tsttxt1) - 10 ||
      strncmp(buf, tsttxt1, strlen(tsttxt1) - 10) != 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    0xe27(%rip),%rax
mov    %rax,-0x1c(%rbp)
mov    0xe25(%rip),%eax
mov    %eax,-0x14(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xe3f(%rip),%rsi
mov    $0x2f,%edx
call   1090 <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   1080 <memset@plt>
lea    -0x1c(%rbp),%rdi
lea    0xdc0(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     12bd <main+0xfd>
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   10e0 <fwrite@plt>
mov    %eax,-0xc4(%rbp)
lea    -0x50(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0xc4(%rbp),%eax
cmp    %ecx,%eax
jne    12bd <main+0xfd>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
cmp    $0x0,%rax
je     12d3 <main+0x113>
lea    0xd4b(%rip),%rdi
call   1070 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    -0x1c(%rbp),%rax
mov    %rax,-0xd8(%rbp)
lea    -0x50(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0xd8(%rbp),%rdi
mov    %eax,%esi
sub    $0xa,%esi
call   10a0 <truncate@plt>
cmp    $0x0,%rax
je     1318 <main+0x158>
lea    0xd0c(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    -0x1c(%rbp),%rdi
lea    0xcfb(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     13ca <main+0x20a>
lea    -0xc0(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4b,%edx
call   1040 <fread@plt>
mov    %eax,-0xdc(%rbp)
lea    -0x50(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0xdc(%rbp),%eax
sub    $0xa,%ecx
cmp    %ecx,%eax
jne    13ca <main+0x20a>
lea    -0xc0(%rbp),%rax
mov    %rax,-0xf0(%rbp)
lea    -0x50(%rbp),%rax
mov    %rax,-0xe8(%rbp)
lea    -0x50(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0xf0(%rbp),%rdi
mov    -0xe8(%rbp),%rsi
mov    %eax,%edx
sub    $0xa,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    13ca <main+0x20a>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
cmp    $0x0,%rax
je     13e0 <main+0x220>
lea    0xc3e(%rip),%rdi
call   1070 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xc39(%rip),%rdi
call   1070 <printf@plt>
xor    %edi,%edi
call   10d0 <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    0xe04(%rip),%rsi
mov    %rsp,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     12ff <main+0x15f>
mov    %rax,%rbx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1070 <strlen@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %rbx,%rcx
call   10c0 <fwrite@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1070 <strlen@plt>
cmp    %eax,%ebp
jne    12ff <main+0x15f>
mov    %rbx,%rdi
call   1060 <fclose@plt>
test   %rax,%rax
jne    12ff <main+0x15f>
lea    0x10(%rsp),%rdi
call   1070 <strlen@plt>
lea    -0xa(%rax),%esi
mov    %rsp,%rdi
call   1080 <truncate@plt>
test   %rax,%rax
jne    1315 <main+0x175>
lea    0xd95(%rip),%rsi
mov    %rsp,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     12ff <main+0x15f>
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    $0xfffffff6,%eax
cmp    %eax,%ebp
jne    12ff <main+0x15f>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   1070 <strlen@plt>
lea    -0xa(%rax),%edx
lea    0x40(%rsp),%rdi
mov    %rbp,%rsi
call   1030 <strncmp@plt>
test   %rax,%rax
jne    12ff <main+0x15f>
mov    %rbx,%rdi
call   1060 <fclose@plt>
test   %rax,%rax
jne    12ff <main+0x15f>
lea    0xd27(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
lea    0xd19(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xcf3(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <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 = [6 x i8] c"fail\0A\00"
@.str.2 = [9 x i8] c"truncate\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [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 %28, 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 %28, label %24
24: ; preds = %15
%25 = load i32*, i32** %2
%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]* %3, i64 0, i64 0
%33 = getelementptr inbounds [47 x i8], [47 x i8]* %4, i64 0, i64 0
%34 = call i32 @strlen(i8* %33)
%35 = sub nsw i32 %34, 10
%36 = call i64 @truncate(i8* %32, i32 %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %31
%39 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %31
%42 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 0
%43 = call i32* @fopen(i8* %42, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %43, i32** %2
%44 = load i32*, i32** %2
%45 = icmp eq i32* %44, null
br i1 %45, label %66, label %46
46: ; preds = %41
%47 = getelementptr inbounds [75 x i8], [75 x i8]* %6, i64 0, i64 0
%48 = load i32*, i32** %2
%49 = call i32 @fread(i8* %47, i32 1, i32 75, i32* %48)
%50 = getelementptr inbounds [47 x i8], [47 x i8]* %4, i64 0, i64 0
%51 = call i32 @strlen(i8* %50)
%52 = sub nsw i32 %51, 10
%53 = icmp ne i32 %49, %52
br i1 %53, label %66, label %54
54: ; preds = %46
%55 = getelementptr inbounds [75 x i8], [75 x i8]* %6, i64 0, i64 0
%56 = getelementptr inbounds [47 x i8], [47 x i8]* %4, i64 0, i64 0
%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, 10
%60 = call i64 @strncmp(i8* %55, i8* %56, i32 %59)
%61 = icmp ne i64 %60, 0
br i1 %61, label %66, label %62
62: ; preds = %54
%63 = load i32*, i32** %2
%64 = call i64 @fclose(i32* %63)
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %62, %54, %46, %41
%67 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%68 = call i32 @exit(i32 1)
unreachable
69: ; preds = %62
%70 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%71 = 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.2 = [9 x i8] c"truncate\00"
@.str.3 = [2 x i8] c"r\00"
@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 [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 %17, 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 %14, label %17
14: ; preds = %9
%15 = call i64 @fclose(i32* %7)
%16 = icmp eq i64 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %14, %9, %0
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.6, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %14
%21 = call i32 @strlen(i8* %5)
%22 = add nsw i32 %21, -10
%23 = call i64 @truncate(i8* %4, i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %20
%26 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %20
%29 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%30 = icmp eq i32* %29, null
br i1 %30, label %44, label %31
31: ; preds = %28
%32 = call i32 @fread(i8* %6, i32 1, i32 75, i32* %29)
%33 = call i32 @strlen(i8* %5)
%34 = add nsw i32 %33, -10
%35 = icmp eq i32 %32, %34
br i1 %35, label %36, label %44
36: ; preds = %31
%37 = call i32 @strlen(i8* %5)
%38 = add nsw i32 %37, -10
%39 = call i64 @strncmp(i8* %6, i8* %5, i32 %38)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = call i64 @fclose(i32* %29)
%43 = icmp eq i64 %42, 0
br i1 %43, label %47, label %44
44: ; preds = %41, %36, %31, %28
%45 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.6, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %41
%48 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
%49 = call i32 @exit(i32 0)
unreachable
}