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

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ exit(int);
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
scalar_t__ fread(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ fwrite(char const *, int, scalar_t__, int /*<<< orphan*/ *);
int open(char const *, int, ...);
int /*<<< orphan*/ perr(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ strcmp(char *, char const *);
scalar_t__ strlen(char const *);
int main(void) {
  FILE *f;
  int fd;
  const char fname[] = "sk1test.dat";
  const char tsttxt1[] = "This is the first and only line of this file.\n";
  char buf[sizeof(tsttxt1)] = "";
  fd = open(fname, O_WRONLY | O_TRUNC | O_CREAT, S_IRWXU);
  if (fd <= 0)
    perr("open-w");
  f = fdopen(fd, "w");
  if (f == NULL || fwrite(tsttxt1, 1, strlen(tsttxt1), f) != strlen(tsttxt1))
    perr("fdopen or fwrite");
  if (fclose(f) != 0)
    perr("fclose");
  fd = open(fname, O_RDONLY);
  if (fd <= 0)
    perr("open-r");
  f = fdopen(fd, "r");
  if (f == NULL || fread(buf, 1, sizeof(buf), f) != strlen(tsttxt1) ||
      strcmp(buf, tsttxt1) != 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    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe27(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe25(%rip),%eax
mov    %eax,-0x18(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xe4f(%rip),%rsi
mov    $0x2f,%edx
call   10a0 <memcpy@plt>
lea    -0x80(%rbp),%rdi
xor    %esi,%esi
mov    $0x2f,%edx
call   1080 <memset@plt>
lea    -0x20(%rbp),%rdi
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
mov    0x2dd1(%rip),%rax
or     (%rax),%esi
mov    0x2dd0(%rip),%rax
or     (%rax),%esi
mov    0x2da7(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   10c0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     1253 <main+0x93>
lea    0xdbe(%rip),%rdi
call   1070 <perr@plt>
mov    -0x14(%rbp),%edi
lea    0xdb4(%rip),%rsi
call   10b0 <fdopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     12c0 <main+0x100>
lea    -0x50(%rbp),%rax
mov    %rax,-0x90(%rbp)
lea    -0x50(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x90(%rbp),%rdi
mov    %rax,%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   10e0 <fwrite@plt>
mov    %rax,-0x88(%rbp)
lea    -0x50(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x88(%rbp),%rax
cmp    %rcx,%rax
je     12cc <main+0x10c>
lea    0xd4c(%rip),%rdi
call   1070 <perr@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     12eb <main+0x12b>
lea    0xd3e(%rip),%rdi
call   1070 <perr@plt>
lea    -0x20(%rbp),%rdi
mov    0x2cda(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   10c0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     1318 <main+0x158>
lea    0xd18(%rip),%rdi
call   1070 <perr@plt>
mov    -0x14(%rbp),%edi
lea    0xd0e(%rip),%rsi
call   10b0 <fdopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     139a <main+0x1da>
lea    -0x80(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x2f,%edx
call   1030 <fread@plt>
mov    %rax,-0x98(%rbp)
lea    -0x50(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x98(%rbp),%rax
cmp    %rcx,%rax
jne    139a <main+0x1da>
lea    -0x80(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    139a <main+0x1da>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     13b0 <main+0x1f0>
lea    0xc91(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xc81(%rip),%rdi
call   1060 <printf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x70,%rsp
movabs $0x2e74736574316b73,%rax
73 74 2e
mov    %rax,(%rsp)
movl   $0x746164,0x8(%rsp)
movups 0xe99(%rip),%xmm0
movups %xmm0,0x2f(%rsp)
movaps 0xe7e(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe62(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x5f(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
mov    0x2dbc(%rip),%rax
mov    0x2ded(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dea(%rip),%rax
or     (%rax),%esi
mov    0x2dc1(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rdi
xor    %eax,%eax
call   10a0 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jg     1235 <main+0x95>
lea    0xddc(%rip),%rdi
call   1070 <perr@plt>
lea    0xdd5(%rip),%rsi
mov    %ebx,%edi
call   1090 <fdopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     127b <main+0xdb>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1060 <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   1060 <strlen@plt>
cmp    %rax,%r15
je     1287 <main+0xe7>
lea    0xd91(%rip),%rdi
call   1070 <perr@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
test   %rax,%rax
je     12a0 <main+0x100>
lea    0xd89(%rip),%rdi
call   1070 <perr@plt>
mov    0x2d29(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
xor    %eax,%eax
call   10a0 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jg     12c5 <main+0x125>
lea    0xd6b(%rip),%rdi
call   1070 <perr@plt>
lea    0xd64(%rip),%rsi
mov    %ebx,%edi
call   1090 <fdopen@plt>
test   %rax,%rax
je     1338 <main+0x198>
mov    %rax,%rbx
lea    0x40(%rsp),%rdi
mov    $0x1,%esi
mov    $0x2f,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
mov    %rax,%r14
lea    0x10(%rsp),%rdi
call   1060 <strlen@plt>
cmp    %rax,%r14
jne    1338 <main+0x198>
lea    0x40(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
jne    1338 <main+0x198>
mov    %rbx,%rdi
call   1050 <fclose@plt>
test   %rax,%rax
jne    1338 <main+0x198>
lea    0xd06(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
lea    0xcf8(%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.tsttxt1 = [47 x i8] c"This is the first and only line of this file.\0A\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
@.str = [7 x i8] c"open-w\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [17 x i8] c"fdopen or fwrite\00"
@.str.3 = [7 x i8] c"fclose\00"
@O_RDONLY = global i32 0
@.str.4 = [7 x i8] c"open-r\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 i32
%4 = alloca [12 x i8]
%5 = alloca [47 x i8]
%6 = alloca [47 x i8]
store i32 0, i32* %1
%7 = bitcast [12 x i8]* %4 to i8*
%8 = bitcast [47 x i8]* %5 to i8*
%9 = bitcast [47 x i8]* %6 to i8*
%10 = getelementptr inbounds [12 x i8], [12 x i8]* %4, i64 0, i64 0
%11 = load i32, i32* @O_WRONLY
%12 = load i32, i32* @O_TRUNC
%13 = or i32 %11, %12
%14 = load i32, i32* @O_CREAT
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IRWXU
%17 = call i32 (i8*, i32, ...) @open(i8* %10, i32 %15, i32 %16)
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = icmp sle i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %0
%21 = call i32 @perr(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20, %0
%23 = load i32, i32* %3
%24 = call i32* @fdopen(i32 %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %24, i32** %2
%25 = load i32*, i32** %2
%26 = icmp eq i32* %25, null
br i1 %26, label %36, label %27
27: ; preds = %22
%28 = getelementptr inbounds [47 x i8], [47 x i8]* %5, i64 0, i64 0
%29 = getelementptr inbounds [47 x i8], [47 x i8]* %5, i64 0, i64 0
%30 = call i64 @strlen(i8* %29)
%31 = load i32*, i32** %2
%32 = call i64 @fwrite(i8* %28, i32 1, i64 %30, i32* %31)
%33 = getelementptr inbounds [47 x i8], [47 x i8]* %5, i64 0, i64 0
%34 = call i64 @strlen(i8* %33)
%35 = icmp ne i64 %32, %34
br i1 %35, label %36, label %38
36: ; preds = %27, %22
%37 = call i32 @perr(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %38
38: ; preds = %36, %27
%39 = load i32*, i32** %2
%40 = call i64 @fclose(i32* %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %38
%43 = call i32 @perr(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %44
44: ; preds = %42, %38
%45 = getelementptr inbounds [12 x i8], [12 x i8]* %4, i64 0, i64 0
%46 = load i32, i32* @O_RDONLY
%47 = call i32 (i8*, i32, ...) @open(i8* %45, i32 %46)
store i32 %47, i32* %3
%48 = load i32, i32* %3
%49 = icmp sle i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %44
%51 = call i32 @perr(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %52
52: ; preds = %50, %44
%53 = load i32, i32* %3
%54 = call i32* @fdopen(i32 %53, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %54, i32** %2
%55 = load i32*, i32** %2
%56 = icmp eq i32* %55, null
br i1 %56, label %73, label %57
57: ; preds = %52
%58 = getelementptr inbounds [47 x i8], [47 x i8]* %6, i64 0, i64 0
%59 = load i32*, i32** %2
%60 = call i64 @fread(i8* %58, i32 1, i32 47, i32* %59)
%61 = getelementptr inbounds [47 x i8], [47 x i8]* %5, i64 0, i64 0
%62 = call i64 @strlen(i8* %61)
%63 = icmp ne i64 %60, %62
br i1 %63, label %73, label %64
64: ; preds = %57
%65 = getelementptr inbounds [47 x i8], [47 x i8]* %6, i64 0, i64 0
%66 = getelementptr inbounds [47 x i8], [47 x i8]* %5, i64 0, i64 0
%67 = call i64 @strcmp(i8* %65, i8* %66)
%68 = icmp ne i64 %67, 0
br i1 %68, label %73, label %69
69: ; preds = %64
%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, %64, %57, %52
%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"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
@.str = [7 x i8] c"open-w\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [17 x i8] c"fdopen or fwrite\00"
@.str.3 = [7 x i8] c"fclose\00"
@O_RDONLY = global i32 0
@.str.4 = [7 x i8] c"open-r\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 [47 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 [47 x i8], [47 x i8]* %3, i64 0, i64 0
%7 = load i32, i32* @O_WRONLY
%8 = load i32, i32* @O_TRUNC
%9 = or i32 %8, %7
%10 = load i32, i32* @O_CREAT
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IRWXU
%13 = call i32 (i8*, i32, ...) @open(i8* %4, i32 %11, i32 %12)
%14 = icmp slt i32 %13, 1
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = tail call i32 @perr(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %0
%18 = tail call i32* @fdopen(i32 %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %25, label %20
20: ; preds = %17
%21 = call i64 @strlen(i8* %5)
%22 = call i64 @fwrite(i8* %5, i32 1, i64 %21, i32* %18)
%23 = call i64 @strlen(i8* %5)
%24 = icmp eq i64 %22, %23
br i1 %24, label %27, label %25
25: ; preds = %20, %17
%26 = tail call i32 @perr(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %25, %20
%28 = tail call i64 @fclose(i32* %18)
%29 = icmp eq i64 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %27
%31 = tail call i32 @perr(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %32
32: ; preds = %30, %27
%33 = load i32, i32* @O_RDONLY
%34 = call i32 (i8*, i32, ...) @open(i8* %4, i32 %33)
%35 = icmp slt i32 %34, 1
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = tail call i32 @perr(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %38
38: ; preds = %36, %32
%39 = tail call i32* @fdopen(i32 %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%40 = icmp eq i32* %39, null
br i1 %40, label %51, label %41
41: ; preds = %38
%42 = call i64 @fread(i8* %6, i32 1, i32 47, i32* %39)
%43 = call i64 @strlen(i8* %5)
%44 = icmp eq i64 %42, %43
br i1 %44, label %45, label %51
45: ; preds = %41
%46 = call i64 @strcmp(i8* %6, i8* %5)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %45
%49 = call i64 @fclose(i32* %39)
%50 = icmp eq i64 %49, 0
br i1 %50, label %54, label %51
51: ; preds = %48, %45, %41, %38
%52 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.8, i64 0, i64 0))
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %48
%55 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
%56 = call i32 @exit(i32 0)
unreachable
}