#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;
struct aiocb {
  int aio_fildes;
  char *aio_buf;
  int aio_nbytes;
  int aio_offset;
};
typedef int /*<<< orphan*/ aiocb_write;
typedef int /*<<< orphan*/ aiocb_fsync;

/* Variables and functions */
int EINPROGRESS;
int O_CREAT;
int O_EXCL;
int O_RDWR;
int /*<<< orphan*/ O_SYNC;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ _SC_ASYNCHRONOUS_IO;
int aio_error(struct aiocb *);
scalar_t__ aio_fsync(int /*<<< orphan*/, struct aiocb *);
int aio_write(struct aiocb *);
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ exit(int);
int getpid();
int /*<<< orphan*/ memset(struct aiocb *, int /*<<< orphan*/, int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
char *strerror(int);
long sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ usleep(int);
int main(void) {
  char tmpfname[256];
  char buf[111];
  int fd;
  int ret;
  struct aiocb aiocb_write;
  struct aiocb aiocb_fsync;
  if (sysconf(_SC_ASYNCHRONOUS_IO) < 200112L)
    return 4;
  snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_aio_fsync_8_4_%d", getpid());
  unlink(tmpfname);
  fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
  if (fd == -1) {
    printf("aio_fsync/8-4.c"
           " Error at open(): %s\n",
           strerror(errno));
    exit(2);
  }
  unlink(tmpfname);
  memset(&aiocb_write, 0, sizeof(aiocb_write));
  aiocb_write.aio_fildes = fd;
  aiocb_write.aio_buf = buf;
  aiocb_write.aio_nbytes = 111;
  if (aio_write(&aiocb_write) == -1) {
    printf("aio_fsync/8-4.c"
           " Error at aio_write(): %s\n",
           strerror(errno));
    exit(1);
  }
  do {
    usleep(10000);
    ret = aio_error(&aiocb_write);
  } while (ret == EINPROGRESS);
  if (ret < 0) {
    printf("aio_fsync/8-4.c"
           " Error at aio_error() : %s\n",
           strerror(ret));
    exit(1);
  }
  memset(&aiocb_fsync, 0, sizeof(aiocb_fsync));
  aiocb_fsync.aio_fildes = fd;
  aiocb_fsync.aio_offset = -1;
  if (aio_fsync(O_SYNC, &aiocb_fsync) != 0) {
    printf("aio_fsync/8-4.c"
           " Error at aio_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    $0x1c0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
call   10d0 <sysconf@plt>
cmp    $0x30db0,%rax
jge    1218 <main+0x38>
movl   $0x4,-0x4(%rbp)
jmp    1443 <main+0x263>
lea    -0x110(%rbp),%rax
mov    %rax,-0x1c0(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x1c0(%rbp),%rdi
mov    %eax,%ecx
mov    $0x100,%esi
lea    0xdbe(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x110(%rbp),%rdi
mov    0x2d97(%rip),%rax
mov    (%rax),%esi
mov    0x2d76(%rip),%rax
or     (%rax),%esi
mov    0x2d3d(%rip),%rax
or     (%rax),%esi
mov    0x2d24(%rip),%rax
mov    (%rax),%edx
mov    0x2d3b(%rip),%rax
or     (%rax),%edx
call   10c0 <open@plt>
mov    %eax,-0x184(%rbp)
cmpl   $0xffffffff,-0x184(%rbp)
jne    12c8 <main+0xe8>
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    %rax,%rsi
lea    0xd63(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x2,%edi
call   10e0 <exit@plt>
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x1a0(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1080 <memset@plt>
mov    -0x184(%rbp),%eax
mov    %eax,-0x1a0(%rbp)
lea    -0x180(%rbp),%rax
mov    %rax,-0x198(%rbp)
movl   $0x6f,-0x190(%rbp)
lea    -0x1a0(%rbp),%rdi
call   1050 <aio_write@plt>
cmp    $0xffffffff,%eax
jne    1349 <main+0x169>
mov    0x2c81(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    %rax,%rsi
lea    0xd07(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
jmp    134e <main+0x16e>
mov    $0x2710,%edi
call   1100 <usleep@plt>
lea    -0x1a0(%rbp),%rdi
call   10a0 <aio_error@plt>
mov    %eax,-0x188(%rbp)
mov    -0x188(%rbp),%eax
mov    0x2c59(%rip),%rcx
cmp    (%rcx),%eax
je     134e <main+0x16e>
cmpl   $0x0,-0x188(%rbp)
jge    13b2 <main+0x1d2>
mov    -0x188(%rbp),%edi
call   10f0 <strerror@plt>
mov    %rax,%rsi
lea    0xcc8(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    -0x1b8(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1080 <memset@plt>
mov    -0x184(%rbp),%eax
mov    %eax,-0x1b8(%rbp)
movl   $0xffffffff,-0x1a4(%rbp)
ff ff ff
mov    0x2bf6(%rip),%rax
mov    (%rax),%edi
lea    -0x1b8(%rbp),%rsi
call   10b0 <aio_fsync@plt>
cmp    $0x0,%rax
je     1423 <main+0x243>
mov    0x2ba7(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    %rax,%rsi
lea    0xc82(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x184(%rbp),%edi
call   1090 <close@plt>
lea    0xc89(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1a8,%rsp
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
call   10e0 <sysconf@plt>
mov    $0x4,%ebp
cmp    $0x30db0,%rax
jl     134b <main+0x15b>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xdd6(%rip),%rdx
lea    0xa0(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %eax,%ecx
call   1080 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    0x2da8(%rip),%rax
mov    0x2d89(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d4e(%rip),%rax
or     (%rax),%esi
mov    0x2d35(%rip),%rax
mov    0x2d4e(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
call   10d0 <open@plt>
cmp    $0xffffffff,%eax
je     135b <main+0x16b>
mov    %eax,%r15d
lea    0xa0(%rsp),%rdi
call   1030 <unlink@plt>
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1090 <memset@plt>
mov    %r15d,(%rsp)
lea    0x30(%rsp),%rax
mov    %rax,0x8(%rsp)
movl   $0x6f,0x10(%rsp)
mov    %rbp,%rdi
call   1060 <aio_write@plt>
cmp    $0xffffffff,%eax
je     1384 <main+0x194>
mov    %rsp,%rbp
mov    0x2cf6(%rip),%rbx
nopw   0x0(%rax,%rax,1)
mov    $0x2710,%edi
call   1110 <usleep@plt>
mov    %rbp,%rdi
call   10b0 <aio_error@plt>
cmp    (%rbx),%eax
je     12e0 <main+0xf0>
test   %eax,%eax
js     139b <main+0x1ab>
lea    0x18(%rsp),%r14
xor    %ebp,%ebp
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1090 <memset@plt>
mov    %r15d,0x18(%rsp)
movl   $0xffffffff,0x2c(%rsp)
ff
mov    0x2cb0(%rip),%rax
mov    (%rax),%edi
mov    %r14,%rsi
call   10c0 <aio_fsync@plt>
test   %rax,%rax
jne    13ab <main+0x1bb>
mov    %r15d,%edi
call   10a0 <close@plt>
lea    0xd78(%rip),%rdi
call   1040 <puts@plt>
mov    %ebp,%eax
add    $0x1a8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c46(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
lea    0xcaa(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x2,%edi
call   10f0 <exit@plt>
mov    0x2c1d(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
lea    0xca6(%rip),%rdi
jmp    13c0 <main+0x1d0>
mov    %eax,%edi
call   1100 <strerror@plt>
lea    0xcc0(%rip),%rdi
jmp    13c0 <main+0x1d0>
mov    0x2bf6(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
lea    0xcd4(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.aiocb = type { i32, i8*, i32, i32 }
@_SC_ASYNCHRONOUS_IO = global i32 0
@.str = [26 x i8] c"/tmp/pts_aio_fsync_8_4_%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 = [37 x i8] c"aio_fsync/8-4.c Error at open(): %s\0A\00"
@errno = global i32 0
@.str.2 = [42 x i8] c"aio_fsync/8-4.c Error at aio_write(): %s\0A\00"
@EINPROGRESS = global i32 0
@.str.3 = [43 x i8] c"aio_fsync/8-4.c Error at aio_error() : %s\0A\00"
@O_SYNC = global i32 0
@.str.4 = [42 x i8] c"aio_fsync/8-4.c Error at aio_fsync(): %s\0A\00"
@.str.5 = [13 x i8] c"Test PASSED\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = alloca [111 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.aiocb
%7 = alloca %struct.aiocb
store i32 0, i32* %1
%8 = load i32, i32* @_SC_ASYNCHRONOUS_IO
%9 = call i64 @sysconf(i32 %8)
%10 = icmp slt i64 %9, 200112
br i1 %10, label %11, label %12
11: ; preds = %0
store i32 4, i32* %1
br label %84
12: ; preds = %0
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%14 = call i32 (...) @getpid()
%15 = call i32 @snprintf(i8* %13, i32 256, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%17 = call i32 @unlink(i8* %16)
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%19 = load i32, i32* @O_CREAT
%20 = load i32, i32* @O_RDWR
%21 = or i32 %19, %20
%22 = load i32, i32* @O_EXCL
%23 = or i32 %21, %22
%24 = load i32, i32* @S_IRUSR
%25 = load i32, i32* @S_IWUSR
%26 = or i32 %24, %25
%27 = call i32 @open(i8* %18, i32 %23, i32 %26)
store i32 %27, i32* %4
%28 = load i32, i32* %4
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %35
30: ; preds = %12
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %32)
%34 = call i32 @exit(i32 2)
unreachable
35: ; preds = %12
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%37 = call i32 @unlink(i8* %36)
%38 = call i32 @memset(%struct.aiocb* %6, i32 0, i32 24)
%39 = load i32, i32* %4
%40 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %6, i32 0, i32 0
store i32 %39, i32* %40
%41 = getelementptr inbounds [111 x i8], [111 x i8]* %3, i64 0, i64 0
%42 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %6, i32 0, i32 1
store i8* %41, i8** %42
%43 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %6, i32 0, i32 2
store i32 111, i32* %43
%44 = call i32 @aio_write(%struct.aiocb* %6)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %51
46: ; preds = %35
%47 = load i32, i32* @errno
%48 = call i8* @strerror(i32 %47)
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* %48)
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %35
br label %52
52: ; preds = %55, %51
%53 = call i32 @usleep(i32 10000)
%54 = call i32 @aio_error(%struct.aiocb* %6)
store i32 %54, i32* %5
br label %55
55: ; preds = %52
%56 = load i32, i32* %5
%57 = load i32, i32* @EINPROGRESS
%58 = icmp eq i32 %56, %57
br i1 %58, label %52, label %59
59: ; preds = %55
%60 = load i32, i32* %5
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %67
62: ; preds = %59
%63 = load i32, i32* %5
%64 = call i8* @strerror(i32 %63)
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i8* %64)
%66 = call i32 @exit(i32 1)
unreachable
67: ; preds = %59
%68 = call i32 @memset(%struct.aiocb* %7, i32 0, i32 24)
%69 = load i32, i32* %4
%70 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %7, i32 0, i32 0
store i32 %69, i32* %70
%71 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %7, i32 0, i32 3
store i32 -1, i32* %71
%72 = load i32, i32* @O_SYNC
%73 = call i64 @aio_fsync(i32 %72, %struct.aiocb* %7)
%74 = icmp ne i64 %73, 0
br i1 %74, label %75, label %80
75: ; preds = %67
%76 = load i32, i32* @errno
%77 = call i8* @strerror(i32 %76)
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i8* %77)
%79 = call i32 @exit(i32 1)
unreachable
80: ; preds = %67
%81 = load i32, i32* %4
%82 = call i32 @close(i32 %81)
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %1
br label %84
84: ; preds = %80, %11
%85 = load i32, i32* %1
ret i32 %85
}

LLVM IR (-O2):
%struct.aiocb = type { i32, i8*, i32, i32 }
@_SC_ASYNCHRONOUS_IO = global i32 0
@.str = [26 x i8] c"/tmp/pts_aio_fsync_8_4_%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 = [37 x i8] c"aio_fsync/8-4.c Error at open(): %s\0A\00"
@errno = global i32 0
@.str.2 = [42 x i8] c"aio_fsync/8-4.c Error at aio_write(): %s\0A\00"
@EINPROGRESS = global i32 0
@.str.3 = [43 x i8] c"aio_fsync/8-4.c Error at aio_error() : %s\0A\00"
@O_SYNC = global i32 0
@.str.4 = [42 x i8] c"aio_fsync/8-4.c Error at aio_fsync(): %s\0A\00"
@str = [12 x i8] c"Test PASSED\00"
define i32 @main() {
%1 = alloca [256 x i8]
%2 = alloca [111 x i8]
%3 = alloca %struct.aiocb
%4 = alloca %struct.aiocb
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%6 = getelementptr inbounds [111 x i8], [111 x i8]* %2, i64 0, i64 0
%7 = bitcast %struct.aiocb* %3 to i8*
%8 = bitcast %struct.aiocb* %4 to i8*
%9 = load i32, i32* @_SC_ASYNCHRONOUS_IO
%10 = tail call i64 @sysconf(i32 %9)
%11 = icmp slt i64 %10, 200112
br i1 %11, label %70, label %12
12: ; preds = %0
%13 = tail call i32 (...) @getpid()
%14 = call i32 @snprintf(i8* %5, i32 256, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = call i32 @unlink(i8* %5)
%16 = load i32, i32* @O_CREAT
%17 = load i32, i32* @O_RDWR
%18 = or i32 %17, %16
%19 = load i32, i32* @O_EXCL
%20 = or i32 %18, %19
%21 = load i32, i32* @S_IRUSR
%22 = load i32, i32* @S_IWUSR
%23 = or i32 %22, %21
%24 = call i32 @open(i8* %5, i32 %20, i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %31
26: ; preds = %12
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = call i32 @exit(i32 2)
unreachable
31: ; preds = %12
%32 = call i32 @unlink(i8* %5)
%33 = call i32 @memset(%struct.aiocb* %3, i32 0, i32 24)
%34 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %3, i64 0, i32 0
store i32 %24, i32* %34
%35 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %3, i64 0, i32 1
store i8* %6, i8** %35
%36 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %3, i64 0, i32 2
store i32 111, i32* %36
%37 = call i32 @aio_write(%struct.aiocb* %3)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %44
39: ; preds = %31
%40 = load i32, i32* @errno
%41 = call i8* @strerror(i32 %40)
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* %41)
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %31, %44
%45 = call i32 @usleep(i32 10000)
%46 = call i32 @aio_error(%struct.aiocb* %3)
%47 = load i32, i32* @EINPROGRESS
%48 = icmp eq i32 %46, %47
br i1 %48, label %44, label %49
49: ; preds = %44
%50 = icmp slt i32 %46, 0
br i1 %50, label %51, label %55
51: ; preds = %49
%52 = call i8* @strerror(i32 %46)
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i8* %52)
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %49
%56 = call i32 @memset(%struct.aiocb* %4, i32 0, i32 24)
%57 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %4, i64 0, i32 0
store i32 %24, i32* %57
%58 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %4, i64 0, i32 3
store i32 -1, i32* %58
%59 = load i32, i32* @O_SYNC
%60 = call i64 @aio_fsync(i32 %59, %struct.aiocb* %4)
%61 = icmp eq i64 %60, 0
br i1 %61, label %67, label %62
62: ; preds = %55
%63 = load i32, i32* @errno
%64 = call i8* @strerror(i32 %63)
%65 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i8* %64)
%66 = call i32 @exit(i32 1)
unreachable
67: ; preds = %55
%68 = call i32 @close(i32 %24)
%69 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %70
70: ; preds = %0, %67
%71 = phi i32 [ 0, %67 ], [ 4, %0 ]
ret i32 %71
}