#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_getvalue(int /*<<< orphan*/ *, int *);
int sem_init(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
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 sts, val;
  if (sem_init(&mysemp, 0, 0) == -1) {
    perror("unexpected error: "
           "sem_timedwait"
           " "
           "2-2"
           ": "
           "sem_init");
    return 2;
  }
  ts.tv_sec = time(NULL) + 1;
  ts.tv_nsec = 0;
  sts = sem_timedwait(&mysemp, &ts);
  if (sem_post(&mysemp) == -1) {
    perror("unexpected error: "
           "sem_timedwait"
           " "
           "2-2"
           ": "
           "sem_post");
    return 2;
  }
  if (sem_getvalue(&mysemp, &val) == -1) {
    perror("unexpected error: "
           "sem_timedwait"
           " "
           "2-2"
           ": "
           "sem_getvalue");
    return 2;
  }
  if ((val == 1) && (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   1080 <sem_init@plt>
cmp    $0xffffffff,%eax
jne    11bd <main+0x3d>
lea    0xe54(%rip),%rdi
call   1090 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1286 <main+0x106>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <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,-0x1c(%rbp)
lea    -0x8(%rbp),%rdi
call   1060 <sem_post@plt>
cmp    $0xffffffff,%eax
jne    1210 <main+0x90>
lea    0xe2f(%rip),%rdi
call   1090 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1286 <main+0x106>
lea    -0x8(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <sem_getvalue@plt>
cmp    $0xffffffff,%eax
jne    123e <main+0xbe>
lea    0xe2f(%rip),%rdi
call   1090 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1286 <main+0x106>
cmpl   $0x1,-0x20(%rbp)
jne    1273 <main+0xf3>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1273 <main+0xf3>
lea    0xe35(%rip),%rdi
call   1030 <puts@plt>
lea    -0x8(%rbp),%rdi
call   10a0 <sem_destroy@plt>
movl   $0x0,-0x4(%rbp)
jmp    1286 <main+0x106>
lea    0xe20(%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
mov    %rsp,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <sem_init@plt>
cmp    $0xffffffff,%eax
je     121c <main+0x9c>
xor    %edi,%edi
call   1070 <time@plt>
add    $0x1,%rax
mov    %rax,0x10(%rsp)
movq   $0x0,0x8(%rsp)
mov    %rsp,%rbx
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1040 <sem_timedwait@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1060 <sem_post@plt>
cmp    $0xffffffff,%eax
je     1225 <main+0xa5>
mov    %rsp,%rdi
lea    0x4(%rsp),%rsi
call   1050 <sem_getvalue@plt>
cmp    $0xffffffff,%eax
je     122e <main+0xae>
cmpl   $0x1,0x4(%rsp)
jne    1209 <main+0x89>
cmp    $0xffffffff,%ebp
jne    1209 <main+0x89>
lea    0xe96(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rdi
call   10a0 <sem_destroy@plt>
xor    %eax,%eax
jmp    123f <main+0xbf>
lea    0xe8a(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    123f <main+0xbf>
lea    0xddd(%rip),%rdi
jmp    1235 <main+0xb5>
lea    0xe02(%rip),%rdi
jmp    1235 <main+0xb5>
lea    0xe27(%rip),%rdi
call   1090 <perror@plt>
mov    $0x2,%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 2-2: sem_init\00"
@.str.1 = [46 x i8] c"unexpected error: sem_timedwait 2-2: sem_post\00"
@.str.2 = [50 x i8] c"unexpected error: sem_timedwait 2-2: sem_getvalue\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
%6 = call i32 @sem_init(i32* %2, i32 0, i32 0)
%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 2, i32* %1
br label %36
10: ; preds = %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* %4
%16 = call i32 @sem_post(i32* %2)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %10
%19 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %1
br label %36
20: ; preds = %10
%21 = call i32 @sem_getvalue(i32* %2, i32* %5)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 @perror(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %1
br label %36
25: ; preds = %20
%26 = load i32, i32* %5
%27 = icmp eq i32 %26, 1
br i1 %27, label %28, label %34
28: ; preds = %25
%29 = load i32, i32* %4
%30 = icmp eq i32 %29, -1
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, %23, %18, %8
%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 2-2: sem_init\00"
@.str.1 = [46 x i8] c"unexpected error: sem_timedwait 2-2: sem_post\00"
@.str.2 = [50 x i8] c"unexpected error: sem_timedwait 2-2: sem_getvalue\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 = alloca i32
%4 = bitcast i32* %1 to i8*
%5 = bitcast %struct.timespec* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = call i32 @sem_init(i32* %1, i32 0, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
br label %36
11: ; preds = %0
%12 = call i64 @time(i32* null)
%13 = add nsw i64 %12, 1
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i64 %13, i64* %14
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i64 0, i64* %15
%16 = call i32 @sem_timedwait(i32* %1, %struct.timespec* %2)
%17 = call i32 @sem_post(i32* %1)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %11
%20 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
br label %36
21: ; preds = %11
%22 = call i32 @sem_getvalue(i32* %1, i32* %3)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @perror(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0))
br label %36
26: ; preds = %21
%27 = load i32, i32* %3
%28 = icmp eq i32 %27, 1
%29 = icmp eq i32 %16, -1
%30 = select i1 %28, i1 %29, i1 false
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 @sem_destroy(i32* %1)
br label %36
34: ; preds = %26
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %36
36: ; preds = %34, %31, %24, %19, %9
%37 = phi i32 [ 2, %9 ], [ 2, %19 ], [ 2, %24 ], [ 0, %31 ], [ 1, %34 ]
ret i32 %37
}