#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct timeval {
  scalar_t__ tv_sec;
};
struct timespec {
  int tv_sec;
  scalar_t__ tv_nsec;
};
struct TYPE_2__ {
  int /*<<< orphan*/ sigev_notify;
};
struct aiocb {
  int aio_fildes;
  int aio_nbytes;
  TYPE_1__ aio_sigevent;
  scalar_t__ aio_offset;
  void *aio_buf;
  scalar_t__ aio_reqprio;
  int /*<<< orphan*/ aio_lio_opcode;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ ENOSYS;
int /*<<< orphan*/ LIO_WRITE;
int /*<<< orphan*/ SIGEV_NONE;
scalar_t__ aio_read(struct aiocb *);
int aio_suspend(struct aiocb const *const *, int, struct timespec *);
scalar_t__ errno;
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ puts(char *);
int do_test(void) {
  struct aiocb *arr[1];
  struct aiocb cb;
  char buf[100];
  struct timeval before;
  struct timeval after;
  struct timespec timeout;
  int fd[2];
  int result = 0;
  if (pipe(fd) != 0) {
    printf("cannot create pipe: %m\n");
    return 1;
  }
  arr[0] = &cb;
  cb.aio_fildes = fd[0];
  cb.aio_lio_opcode = LIO_WRITE;
  cb.aio_reqprio = 0;
  cb.aio_buf = (void *)buf;
  cb.aio_nbytes = sizeof(buf) - 1;
  cb.aio_offset = 0;
  cb.aio_sigevent.sigev_notify = SIGEV_NONE;
  if (aio_read(arr[0]) < 0) {
    if (errno == ENOSYS) {
      puts("no aio support in this configuration");
      return 0;
    }
    printf("aio_read failed: %m\n");
    return 1;
  }
  gettimeofday(&before, NULL);
  timeout.tv_sec = 3;
  timeout.tv_nsec = 0;
  if (aio_suspend((const struct aiocb *const *)arr, 1, &timeout) != -1) {
    puts("aio_suspend() didn't return -1");
    result = 1;
  } else if (errno != EAGAIN) {
    puts("error not set to EAGAIN");
    result = 1;
  } else {
    gettimeofday(&after, NULL);
    if (after.tv_sec < before.tv_sec + 1) {
      puts("timeout came too early");
      result = 1;
    }
  }
  return result;
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
movl   $0x0,-0xdc(%rbp)
lea    -0xd8(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
je     11a3 <do_test+0x43>
lea    0xe6e(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1332 <do_test+0x1d2>
lea    -0x40(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0xd8(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0xb0(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x63,-0x3c(%rbp)
movq   $0x0,-0x30(%rbp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <aio_read@plt>
cmp    $0x0,%rax
jge    124b <do_test+0xeb>
mov    0x2dc0(%rip),%rax
mov    (%rax),%rax
mov    0x2dc6(%rip),%rcx
cmp    (%rcx),%rax
jne    1233 <do_test+0xd3>
lea    0xdf6(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x4(%rbp)
jmp    1332 <do_test+0x1d2>
lea    0xe03(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1332 <do_test+0x1d2>
lea    -0xb8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
movl   $0x3,-0xd0(%rbp)
movq   $0x0,-0xc8(%rbp)
lea    -0x10(%rbp),%rdi
mov    $0x1,%esi
lea    -0xd0(%rbp),%rdx
call   1080 <aio_suspend@plt>
cmp    $0xffffffff,%eax
je     12a9 <do_test+0x149>
lea    0xdbd(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0xdc(%rbp)
jmp    1329 <do_test+0x1c9>
mov    0x2d18(%rip),%rax
mov    (%rax),%rax
mov    0x2cfe(%rip),%rcx
cmp    (%rcx),%rax
je     12de <do_test+0x17e>
lea    0xda7(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0xdc(%rbp)
jmp    1324 <do_test+0x1c4>
lea    -0xc0(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
mov    -0xc0(%rbp),%rax
mov    -0xb8(%rbp),%rcx
add    $0x1,%rcx
cmp    %rcx,%rax
jge    131f <do_test+0x1bf>
lea    0xd79(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0xdc(%rbp)
jmp    1324 <do_test+0x1c4>
jmp    1329 <do_test+0x1c9>
mov    -0xdc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %rbx
sub    $0xd0,%rsp
lea    0x18(%rsp),%rdi
call   1060 <pipe@plt>
test   %rax,%rax
je     1188 <do_test+0x28>
lea    0xe82(%rip),%rdi
call   1040 <printf@plt>
jmp    1297 <do_test+0x137>
lea    0x30(%rsp),%rdi
mov    %rdi,0x10(%rsp)
mov    0x18(%rsp),%eax
mov    %eax,0x30(%rsp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x58(%rsp)
movq   $0x0,0x50(%rsp)
lea    0x60(%rsp),%rax
mov    %rax,0x48(%rsp)
movl   $0x63,0x34(%rsp)
movq   $0x0,0x40(%rsp)
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x38(%rsp)
call   1070 <aio_read@plt>
test   %rax,%rax
js     1221 <do_test+0xc1>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
movl   $0x3,0x20(%rsp)
movq   $0x0,0x28(%rsp)
lea    0x10(%rsp),%rdi
lea    0x20(%rsp),%rdx
mov    $0x1,%esi
call   1080 <aio_suspend@plt>
cmp    $0xffffffff,%eax
je     1247 <do_test+0xe7>
lea    0xe33(%rip),%rdi
jmp    1292 <do_test+0x132>
mov    0x2da0(%rip),%rax
mov    (%rax),%rax
mov    0x2da6(%rip),%rcx
cmp    (%rcx),%rax
jne    127d <do_test+0x11d>
lea    0xdda(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
jmp    129c <do_test+0x13c>
mov    0x2d7a(%rip),%rax
mov    (%rax),%rax
mov    0x2d60(%rip),%rcx
cmp    (%rcx),%rax
jne    128b <do_test+0x12b>
xor    %ebx,%ebx
mov    %rsp,%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
mov    (%rsp),%rax
cmp    0x8(%rsp),%rax
jg     129c <do_test+0x13c>
lea    0xe0e(%rip),%rdi
jmp    1292 <do_test+0x132>
lea    0xdb9(%rip),%rdi
call   1040 <printf@plt>
jmp    1297 <do_test+0x137>
lea    0xddf(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0xd0,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.aiocb = type { i32, i32, %struct.TYPE_2__, i64, i8*, i64, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.timeval = type { i64 }
%struct.timespec = type { i32, i64 }
@.str = [24 x i8] c"cannot create pipe: %m\0A\00"
@LIO_WRITE = global i32 0
@SIGEV_NONE = global i32 0
@errno = global i64 0
@ENOSYS = global i64 0
@.str.1 = [37 x i8] c"no aio support in this configuration\00"
@.str.2 = [21 x i8] c"aio_read failed: %m\0A\00"
@.str.3 = [31 x i8] c"aio_suspend() didn't return -1\00"
@EAGAIN = global i64 0
@.str.4 = [24 x i8] c"error not set to EAGAIN\00"
@.str.5 = [23 x i8] c"timeout came too early\00"
define i32 @do_test() {
%1 = alloca i32
%2 = alloca [1 x %struct.aiocb*]
%3 = alloca %struct.aiocb
%4 = alloca [100 x i8]
%5 = alloca %struct.timeval
%6 = alloca %struct.timeval
%7 = alloca %struct.timespec
%8 = alloca [2 x i32]
%9 = alloca i32
store i32 0, i32* %9
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%11 = call i64 @pipe(i32* %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %71
15: ; preds = %0
%16 = getelementptr inbounds [1 x %struct.aiocb*], [1 x %struct.aiocb*]* %2, i64 0, i64 0
store %struct.aiocb* %3, %struct.aiocb** %16
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %3, i32 0, i32 0
store i32 %18, i32* %19
%20 = load i32, i32* @LIO_WRITE
%21 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %3, i32 0, i32 6
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %3, i32 0, i32 5
store i64 0, i64* %22
%23 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%24 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %3, i32 0, i32 4
store i8* %23, i8** %24
%25 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %3, i32 0, i32 1
store i32 99, i32* %25
%26 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %3, i32 0, i32 3
store i64 0, i64* %26
%27 = load i32, i32* @SIGEV_NONE
%28 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %3, i32 0, i32 2
%29 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %28, i32 0, i32 0
store i32 %27, i32* %29
%30 = getelementptr inbounds [1 x %struct.aiocb*], [1 x %struct.aiocb*]* %2, i64 0, i64 0
%31 = load %struct.aiocb*, %struct.aiocb** %30
%32 = call i64 @aio_read(%struct.aiocb* %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %42
34: ; preds = %15
%35 = load i64, i64* @errno
%36 = load i64, i64* @ENOSYS
%37 = icmp eq i64 %35, %36
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 @puts(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %71
40: ; preds = %34
%41 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %71
42: ; preds = %15
%43 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%44 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 0
store i32 3, i32* %44
%45 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 1
store i64 0, i64* %45
%46 = getelementptr inbounds [1 x %struct.aiocb*], [1 x %struct.aiocb*]* %2, i64 0, i64 0
%47 = call i32 @aio_suspend(%struct.aiocb** %46, i32 1, %struct.timespec* %7)
%48 = icmp ne i32 %47, -1
br i1 %48, label %49, label %51
49: ; preds = %42
%50 = call i32 @puts(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %9
br label %69
51: ; preds = %42
%52 = load i64, i64* @errno
%53 = load i64, i64* @EAGAIN
%54 = icmp ne i64 %52, %53
br i1 %54, label %55, label %57
55: ; preds = %51
%56 = call i32 @puts(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %9
br label %68
57: ; preds = %51
%58 = call i32 @gettimeofday(%struct.timeval* %6, i32* null)
%59 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
%60 = load i64, i64* %59
%61 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%62 = load i64, i64* %61
%63 = add nsw i64 %62, 1
%64 = icmp slt i64 %60, %63
br i1 %64, label %65, label %67
65: ; preds = %57
%66 = call i32 @puts(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %9
br label %67
67: ; preds = %65, %57
br label %68
68: ; preds = %67, %55
br label %69
69: ; preds = %68, %49
%70 = load i32, i32* %9
store i32 %70, i32* %1
br label %71
71: ; preds = %69, %40, %38, %13
%72 = load i32, i32* %1
ret i32 %72
}

LLVM IR (-O2):
%struct.aiocb = type { i32, i32, %struct.TYPE_2__, i64, i8*, i64, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.timeval = type { i64 }
%struct.timespec = type { i32, i64 }
@.str = [24 x i8] c"cannot create pipe: %m\0A\00"
@LIO_WRITE = global i32 0
@SIGEV_NONE = global i32 0
@errno = global i64 0
@ENOSYS = global i64 0
@.str.1 = [37 x i8] c"no aio support in this configuration\00"
@.str.2 = [21 x i8] c"aio_read failed: %m\0A\00"
@.str.3 = [31 x i8] c"aio_suspend() didn't return -1\00"
@EAGAIN = global i64 0
@.str.4 = [24 x i8] c"error not set to EAGAIN\00"
@.str.5 = [23 x i8] c"timeout came too early\00"
define i32 @do_test() {
%1 = alloca [1 x %struct.aiocb*]
%2 = alloca %struct.aiocb
%3 = alloca [100 x i8]
%4 = alloca %struct.timeval
%5 = alloca %struct.timeval
%6 = alloca %struct.timespec
%7 = alloca [2 x i32]
%8 = bitcast [1 x %struct.aiocb*]* %1 to i8*
%9 = bitcast %struct.aiocb* %2 to i8*
%10 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%11 = bitcast %struct.timeval* %4 to i8*
%12 = bitcast %struct.timeval* %5 to i8*
%13 = bitcast %struct.timespec* %6 to i8*
%14 = bitcast [2 x i32]* %7 to i8*
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%16 = call i64 @pipe(i32* %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %0
%19 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %65
20: ; preds = %0
%21 = getelementptr inbounds [1 x %struct.aiocb*], [1 x %struct.aiocb*]* %1, i64 0, i64 0
store %struct.aiocb* %2, %struct.aiocb** %21
%22 = load i32, i32* %15
%23 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %2, i64 0, i32 0
store i32 %22, i32* %23
%24 = load i32, i32* @LIO_WRITE
%25 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %2, i64 0, i32 6
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %2, i64 0, i32 5
store i64 0, i64* %26
%27 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %2, i64 0, i32 4
store i8* %10, i8** %27
%28 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %2, i64 0, i32 1
store i32 99, i32* %28
%29 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %2, i64 0, i32 3
store i64 0, i64* %29
%30 = load i32, i32* @SIGEV_NONE
%31 = getelementptr inbounds %struct.aiocb, %struct.aiocb* %2, i64 0, i32 2, i32 0
store i32 %30, i32* %31
%32 = call i64 @aio_read(%struct.aiocb* %2)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %42
34: ; preds = %20
%35 = load i64, i64* @errno
%36 = load i64, i64* @ENOSYS
%37 = icmp eq i64 %35, %36
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
br label %65
40: ; preds = %34
%41 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %65
42: ; preds = %20
%43 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%44 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i64 0, i32 0
store i32 3, i32* %44
%45 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i64 0, i32 1
store i64 0, i64* %45
%46 = call i32 @aio_suspend(%struct.aiocb** %21, i32 1, %struct.timespec* %6)
%47 = icmp eq i32 %46, -1
br i1 %47, label %50, label %48
48: ; preds = %42
%49 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
br label %65
50: ; preds = %42
%51 = load i64, i64* @errno
%52 = load i64, i64* @EAGAIN
%53 = icmp eq i64 %51, %52
br i1 %53, label %56, label %54
54: ; preds = %50
%55 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %65
56: ; preds = %50
%57 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%58 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
%59 = load i64, i64* %58
%60 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%61 = load i64, i64* %60
%62 = icmp sgt i64 %59, %61
br i1 %62, label %65, label %63
63: ; preds = %56
%64 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
br label %65
65: ; preds = %48, %56, %63, %54, %40, %38, %18
%66 = phi i32 [ 1, %18 ], [ 0, %38 ], [ 1, %40 ], [ 1, %48 ], [ 1, %54 ], [ 1, %63 ], [ 0, %56 ]
ret i32 %66
}