#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 */
struct timespec {
  int tv_nsec;
  int /*<<< orphan*/ tv_sec;
};
typedef int /*<<< orphan*/ sem_t;

/* Variables and functions */
scalar_t__ EINVAL;
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ sem_destroy(int /*<<< orphan*/ *);
int sem_init(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
int sem_timedwait(int /*<<< orphan*/ *, struct timespec *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main(void) {
  sem_t mysemp;
  struct timespec ts;
  int sts;
  if (sem_init(&mysemp, 0, 0) == -1) {
    perror("unexpected error: "
           "sem_timedwait"
           " "
           "5-1"
           ": "
           "sem_init");
    return 2;
  }
  ts.tv_sec = time(NULL);
  ts.tv_nsec = 1000000000;
  sts = sem_timedwait(&mysemp, &ts);
  if (errno == EINVAL && sts == -1) {
    puts("TEST PASSED");
    sem_destroy(&mysemp);
    return 0;
  } else {
    puts("TEST FAILED");
    return 1;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x8(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1060 <sem_init@plt>
cmp    $0xffffffff,%eax
jne    119d <main+0x3d>
lea    0xe74(%rip),%rdi
call   1070 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1218 <main+0xb8>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <time@plt>
mov    %eax,-0xc(%rbp)
movl   $0x3b9aca00,-0x10(%rbp)
lea    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1040 <sem_timedwait@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e19(%rip),%rax
mov    (%rax),%rax
mov    0x2e07(%rip),%rcx
cmp    (%rcx),%rax
jne    1205 <main+0xa5>
cmpl   $0xffffffff,-0x14(%rbp)
jne    1205 <main+0xa5>
lea    0xe43(%rip),%rdi
call   1030 <puts@plt>
lea    -0x8(%rbp),%rdi
call   1080 <sem_destroy@plt>
movl   $0x0,-0x4(%rbp)
jmp    1218 <main+0xb8>
lea    0xe2e(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x18,%rsp
lea    0xc(%rsp),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <sem_init@plt>
cmp    $0xffffffff,%eax
je     11e7 <main+0x87>
xor    %edi,%edi
call   1050 <time@plt>
mov    %eax,0x14(%rsp)
movl   $0x3b9aca00,0x10(%rsp)
3b
lea    0xc(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1040 <sem_timedwait@plt>
mov    0x2e40(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2e2e(%rip),%rdx
cmp    (%rdx),%rcx
jne    11d1 <main+0x71>
cmp    $0xffffffff,%eax
jne    11d1 <main+0x71>
lea    0xe73(%rip),%rdi
call   1030 <puts@plt>
lea    0xc(%rsp),%rdi
call   1080 <sem_destroy@plt>
xor    %eax,%eax
add    $0x18,%rsp
ret
lea    0xe62(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
add    $0x18,%rsp
ret
lea    0xe12(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%eax
add    $0x18,%rsp
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@.str = [46 x i8] c"unexpected error: sem_timedwait 5-1: sem_init\00"
@errno = global i64 0
@EINVAL = global i64 0
@.str.1 = [12 x i8] c"TEST PASSED\00"
@.str.2 = [12 x i8] c"TEST FAILED\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.timespec
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 @sem_init(i32* %2, i32 0, i32 0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %1
br label %25
9: ; preds = %0
%10 = call i32 @time(i32* null)
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i32 1000000000, i32* %12
%13 = call i32 @sem_timedwait(i32* %2, %struct.timespec* %3)
store i32 %13, i32* %4
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINVAL
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %23
17: ; preds = %9
%18 = load i32, i32* %4
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @sem_destroy(i32* %2)
store i32 0, i32* %1
br label %25
23: ; preds = %17, %9
%24 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %25
25: ; preds = %23, %20, %7
%26 = load i32, i32* %1
ret i32 %26
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@.str = [46 x i8] c"unexpected error: sem_timedwait 5-1: sem_init\00"
@errno = global i64 0
@EINVAL = global i64 0
@.str.1 = [12 x i8] c"TEST PASSED\00"
@.str.2 = [12 x i8] c"TEST FAILED\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.timespec
%3 = bitcast i32* %1 to i8*
%4 = bitcast %struct.timespec* %2 to i8*
%5 = call i32 @sem_init(i32* %1, i32 0, i32 0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
br label %24
9: ; preds = %0
%10 = call i32 @time(i32* null)
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i32 1000000000, i32* %12
%13 = call i32 @sem_timedwait(i32* %1, %struct.timespec* %2)
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINVAL
%16 = icmp eq i64 %14, %15
%17 = icmp eq i32 %13, -1
%18 = select i1 %16, i1 %17, i1 false
br i1 %18, label %19, label %22
19: ; preds = %9
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @sem_destroy(i32* %1)
br label %24
22: ; preds = %9
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %22, %19, %7
%25 = phi i32 [ 2, %7 ], [ 0, %19 ], [ 1, %22 ]
ret i32 %25
}