#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*/ tmpfname;

/* Variables and functions */
int O_CREAT;
int O_EXCL;
int O_RDWR;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(char *);
int fsync(int);
int getpid();
char *malloc(int);
int /*<<< orphan*/ memset(char *, char, int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char *);
int write(int, char *, int);
int main(void) {
  char tmpfname[256];
  char *data;
  int total_size = 1024;
  int fd;
  snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_fsync_4_1_%d", getpid());
  unlink(tmpfname);
  fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
  if (fd == -1) {
    printf("fsync/4-1.c"
           " Error at open(): %s\n",
           strerror(errno));
    exit(2);
  }
  unlink(tmpfname);
  data = malloc(total_size);
  memset(data, 'a', total_size);
  if (write(fd, data, total_size) != total_size) {
    printf("fsync/4-1.c"
           "Error at write(): %s\n",
           strerror(errno));
    free(data);
    exit(2);
  }
  free(data);
  if (fsync(fd) == -1) {
    printf("fsync/4-1.c"
           "Error at fsync(): %s\n",
           strerror(errno));
    exit(1);
  }
  close(fd);
  printf("Test PASSED\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x400,-0x11c(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x128(%rbp)
mov    $0x0,%al
call   1060 <getpid@plt>
mov    -0x128(%rbp),%rdi
mov    %eax,%ecx
mov    $0x100,%esi
lea    0xdea(%rip),%rdx
call   1080 <snprintf@plt>
lea    -0x110(%rbp),%rdi
call   1040 <unlink@plt>
lea    -0x110(%rbp),%rdi
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
mov    0x2da2(%rip),%rax
or     (%rax),%esi
mov    0x2d81(%rip),%rax
or     (%rax),%esi
mov    0x2d68(%rip),%rax
mov    (%rax),%edx
mov    0x2d7f(%rip),%rax
or     (%rax),%edx
call   10d0 <open@plt>
mov    %eax,-0x120(%rbp)
cmpl   $0xffffffff,-0x120(%rbp)
jne    129c <main+0xcc>
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    %rax,%rsi
lea    0xd8b(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x2,%edi
call   10e0 <exit@plt>
lea    -0x110(%rbp),%rdi
call   1040 <unlink@plt>
mov    -0x11c(%rbp),%edi
call   10b0 <malloc@plt>
mov    %rax,-0x118(%rbp)
mov    -0x118(%rbp),%rdi
mov    -0x11c(%rbp),%edx
mov    $0x61,%esi
call   1090 <memset@plt>
mov    -0x120(%rbp),%edi
mov    -0x118(%rbp),%rsi
mov    -0x11c(%rbp),%edx
call   1050 <write@plt>
cmp    -0x11c(%rbp),%eax
je     132a <main+0x15a>
mov    0x2cc4(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    %rax,%rsi
lea    0xd2a(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x118(%rbp),%rdi
call   1030 <free@plt>
mov    $0x2,%edi
call   10e0 <exit@plt>
mov    -0x118(%rbp),%rdi
call   1030 <free@plt>
mov    -0x120(%rbp),%edi
call   10c0 <fsync@plt>
cmp    $0xffffffff,%eax
jne    1373 <main+0x1a3>
mov    0x2c6f(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    %rax,%rsi
lea    0xcf6(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x120(%rbp),%edi
call   10a0 <close@plt>
lea    0xcf4(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
xor    %eax,%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x108,%rsp
xor    %eax,%eax
call   1070 <getpid@plt>
lea    0xe08(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %eax,%ecx
call   1090 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <unlink@plt>
mov    0x2ddf(%rip),%rax
mov    0x2dc0(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d9d(%rip),%rax
or     (%rax),%esi
mov    0x2d84(%rip),%rax
mov    0x2d9d(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
call   10e0 <open@plt>
cmp    $0xffffffff,%eax
je     12bf <main+0xdf>
mov    %eax,%ebx
mov    %rsp,%rdi
call   1040 <unlink@plt>
mov    $0x400,%edi
call   10c0 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x61,%esi
mov    $0x400,%edx
call   10a0 <memset@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1060 <write@plt>
cmp    $0x400,%eax
jne    12e8 <main+0x108>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebx,%edi
call   10d0 <fsync@plt>
cmp    $0xffffffff,%eax
je     1319 <main+0x139>
mov    %ebx,%edi
call   10b0 <close@plt>
lea    0xdcc(%rip),%rdi
call   1050 <puts@plt>
xor    %eax,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2cfa(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
lea    0xd42(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    $0x2,%edi
call   10f0 <exit@plt>
mov    0x2cd1(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
lea    0xd3a(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    $0x2,%edi
call   10f0 <exit@plt>
mov    0x2ca0(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
lea    0xd2a(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
@.str = [22 x i8] c"/tmp/pts_fsync_4_1_%d\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [33 x i8] c"fsync/4-1.c Error at open(): %s\0A\00"
@errno = global i32 0
@.str.2 = [33 x i8] c"fsync/4-1.cError at write(): %s\0A\00"
@.str.3 = [33 x i8] c"fsync/4-1.cError at fsync(): %s\0A\00"
@.str.4 = [13 x i8] c"Test PASSED\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
store i32 1024, i32* %4
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%7 = call i32 (...) @getpid()
%8 = call i32 @snprintf(i8* %6, i32 256, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%10 = call i32 @unlink(i8* %9)
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%12 = load i32, i32* @O_CREAT
%13 = load i32, i32* @O_RDWR
%14 = or i32 %12, %13
%15 = load i32, i32* @O_EXCL
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IRUSR
%18 = load i32, i32* @S_IWUSR
%19 = or i32 %17, %18
%20 = call i32 @open(i8* %11, i32 %16, i32 %19)
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %28
23: ; preds = %0
%24 = load i32, i32* @errno
%25 = call i8* @strerror(i32 %24)
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = call i32 @exit(i32 2)
unreachable
28: ; preds = %0
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%30 = call i32 @unlink(i8* %29)
%31 = load i32, i32* %4
%32 = call i8* @malloc(i32 %31)
store i8* %32, i8** %3
%33 = load i8*, i8** %3
%34 = load i32, i32* %4
%35 = call i32 @memset(i8* %33, i8 signext 97, i32 %34)
%36 = load i32, i32* %5
%37 = load i8*, i8** %3
%38 = load i32, i32* %4
%39 = call i32 @write(i32 %36, i8* %37, i32 %38)
%40 = load i32, i32* %4
%41 = icmp ne i32 %39, %40
br i1 %41, label %42, label %49
42: ; preds = %28
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %44)
%46 = load i8*, i8** %3
%47 = call i32 @free(i8* %46)
%48 = call i32 @exit(i32 2)
unreachable
49: ; preds = %28
%50 = load i8*, i8** %3
%51 = call i32 @free(i8* %50)
%52 = load i32, i32* %5
%53 = call i32 @fsync(i32 %52)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %60
55: ; preds = %49
%56 = load i32, i32* @errno
%57 = call i8* @strerror(i32 %56)
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %57)
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %49
%61 = load i32, i32* %5
%62 = call i32 @close(i32 %61)
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [22 x i8] c"/tmp/pts_fsync_4_1_%d\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [33 x i8] c"fsync/4-1.c Error at open(): %s\0A\00"
@errno = global i32 0
@.str.2 = [33 x i8] c"fsync/4-1.cError at write(): %s\0A\00"
@.str.3 = [33 x i8] c"fsync/4-1.cError at fsync(): %s\0A\00"
@str = [12 x i8] c"Test PASSED\00"
define i32 @main() {
%1 = alloca [256 x i8]
%2 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (...) @getpid()
%4 = call i32 @snprintf(i8* %2, i32 256, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = call i32 @unlink(i8* %2)
%6 = load i32, i32* @O_CREAT
%7 = load i32, i32* @O_RDWR
%8 = or i32 %7, %6
%9 = load i32, i32* @O_EXCL
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IRUSR
%12 = load i32, i32* @S_IWUSR
%13 = or i32 %12, %11
%14 = call i32 @open(i8* %2, i32 %10, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %21
16: ; preds = %0
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 2)
unreachable
21: ; preds = %0
%22 = call i32 @unlink(i8* %2)
%23 = call dereferenceable_or_null(1024) i8* @malloc(i32 1024)
%24 = call i32 @memset(i8* %23, i8 signext 97, i32 1024)
%25 = call i32 @write(i32 %14, i8* %23, i32 1024)
%26 = icmp eq i32 %25, 1024
br i1 %26, label %33, label %27
27: ; preds = %21
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %29)
%31 = call i32 @free(i8* %23)
%32 = call i32 @exit(i32 2)
unreachable
33: ; preds = %21
%34 = call i32 @free(i8* %23)
%35 = call i32 @fsync(i32 %14)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %42
37: ; preds = %33
%38 = load i32, i32* @errno
%39 = call i8* @strerror(i32 %38)
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %39)
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %33
%43 = call i32 @close(i32 %14)
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
ret i32 0
}