#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;
  int /*<<< orphan*/ 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);
int sem_timedwait(int /*<<< orphan*/ *, struct timespec *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main(void) {
  sem_t mysemp;
  struct timespec ts;
  int val, sts;
  if (sem_init(&mysemp, 0, 1) == -1) {
    perror("unexpected error: "
           "sem_timedwait"
           " "
           "1-1"
           ": "
           "sem_init");
    return 2;
  }
  ts.tv_sec = time(NULL);
  ts.tv_nsec = 0;
  sts = sem_timedwait(&mysemp, &ts);
  if (sts == -1) {
    perror("unexpected error: "
           "sem_timedwait"
           " "
           "1-1"
           ": "
           "sem_timedwait");
    return 2;
  }
  if (sem_getvalue(&mysemp, &val) == -1) {
    perror("unexpected error: "
           "sem_timedwait"
           " "
           "1-1"
           ": "
           "sem_getvalue");
    return 2;
  }
  if (val == 0 && sts == 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)
lea    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1070 <sem_init@plt>
cmp    $0xffffffff,%eax
jne    11b0 <main+0x40>
lea    0xe61(%rip),%rdi
call   1080 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    126c <main+0xfc>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    %eax,-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    11f6 <main+0x86>
lea    0xe49(%rip),%rdi
call   1080 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    126c <main+0xfc>
lea    -0x8(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
call   1050 <sem_getvalue@plt>
cmp    $0xffffffff,%eax
jne    1224 <main+0xb4>
lea    0xe4e(%rip),%rdi
call   1080 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    126c <main+0xfc>
cmpl   $0x0,-0x1c(%rbp)
jne    1259 <main+0xe9>
cmpl   $0x0,-0x20(%rbp)
jne    1259 <main+0xe9>
lea    0xe54(%rip),%rdi
call   1030 <puts@plt>
lea    -0x8(%rbp),%rdi
call   1090 <sem_destroy@plt>
movl   $0x0,-0x4(%rbp)
jmp    126c <main+0xfc>
lea    0xe3f(%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   %rbx
sub    $0x20,%rsp
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1070 <sem_init@plt>
cmp    $0xffffffff,%eax
je     11fc <main+0x8c>
xor    %edi,%edi
call   1060 <time@plt>
mov    %eax,0x18(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1040 <sem_timedwait@plt>
cmp    $0xffffffff,%eax
je     1205 <main+0x95>
mov    %eax,%ebx
lea    0x8(%rsp),%rdi
lea    0xc(%rsp),%rsi
call   1050 <sem_getvalue@plt>
cmp    $0xffffffff,%eax
je     120e <main+0x9e>
or     0xc(%rsp),%ebx
jne    11e9 <main+0x79>
lea    0xebd(%rip),%rdi
call   1030 <puts@plt>
lea    0x8(%rsp),%rdi
call   1090 <sem_destroy@plt>
xor    %eax,%eax
jmp    121f <main+0xaf>
lea    0xeaf(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    121f <main+0xaf>
lea    0xdfd(%rip),%rdi
jmp    1215 <main+0xa5>
lea    0xe22(%rip),%rdi
jmp    1215 <main+0xa5>
lea    0xe4c(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%eax
add    $0x20,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
%struct.timespec = type { i64, i32 }
@.str = [46 x i8] c"unexpected error: sem_timedwait 1-1: sem_init\00"
@.str.1 = [51 x i8] c"unexpected error: sem_timedwait 1-1: sem_timedwait\00"
@.str.2 = [50 x i8] c"unexpected error: sem_timedwait 1-1: 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 1)
%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 %34
11: ; preds = %0
%12 = call i32 @time(i32* null)
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i32 %12, i32* %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 = %11
%18 = call i32 @perror(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0))
br label %34
19: ; preds = %11
%20 = call i32 @sem_getvalue(i32* %1, i32* %3)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 @perror(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0))
br label %34
24: ; preds = %19
%25 = load i32, i32* %3
%26 = icmp eq i32 %25, 0
%27 = icmp eq i32 %15, 0
%28 = select i1 %26, i1 %27, i1 false
br i1 %28, label %29, label %32
29: ; preds = %24
%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 = %24
%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, %22, %17, %9
%35 = phi i32 [ 2, %9 ], [ 2, %17 ], [ 2, %22 ], [ 0, %29 ], [ 1, %32 ]
ret i32 %35
}