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

/* Variables and functions */
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ sem_destroy(int /*<<< orphan*/ *);
int sem_init(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int sem_post(int /*<<< orphan*/ *);
int sem_timedwait(int /*<<< orphan*/ *, struct timespec *);
scalar_t__ time(int /*<<< orphan*/ *);
int main(void) {
  sem_t mysemp;
  struct timespec ts;
  int unresolved = 0, sts;
  if (sem_init(&mysemp, 0, 1) == -1) {
    perror("unexpected error: "
           "sem_timedwait"
           " "
           "4-1"
           ": "
           "sem_init");
    unresolved = 1;
  }
  ts.tv_sec = time(NULL) + 1;
  ts.tv_nsec = 0;
  sts = sem_timedwait(&mysemp, &ts);
  if (sts == -1) {
    perror("unexpected error: "
           "sem_timedwait"
           " "
           "4-1"
           ": "
           "sem_timedwait");
    unresolved = 1;
  }
  if (sem_post(&mysemp) == -1) {
    perror("unexpected error: "
           "sem_timedwait"
           " "
           "4-1"
           ": "
           "sem_post");
    unresolved = 1;
  }
  if ((sts == 0) && (unresolved == 0)) {
    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)
movl   $0x0,-0x1c(%rbp)
lea    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1070 <sem_init@plt>
cmp    $0xffffffff,%eax
jne    11b2 <main+0x42>
lea    0xe5a(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x1c(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <sem_timedwait@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11f8 <main+0x88>
lea    0xe42(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x1c(%rbp)
lea    -0x8(%rbp),%rdi
call   1050 <sem_post@plt>
cmp    $0xffffffff,%eax
jne    121d <main+0xad>
lea    0xe50(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x1c(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    1252 <main+0xe2>
cmpl   $0x0,-0x1c(%rbp)
jne    1252 <main+0xe2>
lea    0xe57(%rip),%rdi
call   1030 <puts@plt>
lea    -0x8(%rbp),%rdi
call   1090 <sem_destroy@plt>
movl   $0x0,-0x4(%rbp)
jmp    1265 <main+0xf5>
lea    0xe42(%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>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
lea    0x4(%rsp),%rdi
xor    %ebp,%ebp
xor    %esi,%esi
mov    $0x1,%edx
call   1070 <sem_init@plt>
cmp    $0xffffffff,%eax
je     11e3 <main+0x73>
xor    %edi,%edi
call   1060 <time@plt>
add    $0x1,%rax
mov    %rax,0x10(%rsp)
movq   $0x0,0x8(%rsp)
lea    0x4(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1040 <sem_timedwait@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11f6 <main+0x86>
lea    0x4(%rsp),%rdi
call   1050 <sem_post@plt>
cmp    $0xffffffff,%eax
je     1209 <main+0x99>
or     %ebx,%ebp
je     121e <main+0xae>
lea    0xec4(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    1236 <main+0xc6>
lea    0xe16(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%ebp
jmp    118e <main+0x1e>
lea    0xe31(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%ebp
jmp    11bd <main+0x4d>
lea    0xe51(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%ebp
or     %ebx,%ebp
jne    11d0 <main+0x60>
lea    0xe6a(%rip),%rdi
call   1030 <puts@plt>
lea    0x4(%rsp),%rdi
call   1090 <sem_destroy@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@.str = [46 x i8] c"unexpected error: sem_timedwait 4-1: sem_init\00"
@.str.1 = [51 x i8] c"unexpected error: sem_timedwait 4-1: sem_timedwait\00"
@.str.2 = [46 x i8] c"unexpected error: sem_timedwait 4-1: sem_post\00"
@.str.3 = [12 x i8] c"TEST PASSED\00"
@.str.4 = [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 1)
%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 %9
9: ; preds = %7, %0
%10 = phi i32 [ 1, %7 ], [ 0, %0 ]
%11 = call i64 @time(i32* null)
%12 = add nsw i64 %11, 1
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i64 %12, i64* %13
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i64 0, i64* %14
%15 = call i32 @sem_timedwait(i32* %1, %struct.timespec* %2)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %9
%18 = call i32 @perror(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %9
%20 = phi i32 [ 1, %17 ], [ %10, %9 ]
%21 = call i32 @sem_post(i32* %1)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %23, %19
%26 = phi i32 [ 1, %23 ], [ %20, %19 ]
%27 = or i32 %26, %15
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%31 = call i32 @sem_destroy(i32* %1)
br label %34
32: ; preds = %25
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %34
34: ; preds = %32, %29
%35 = phi i32 [ 0, %29 ], [ 1, %32 ]
ret i32 %35
}