#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 */
scalar_t__ ETIMEDOUT;
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) {
  struct timespec ts;
  sem_t mysemp;
  if (sem_init(&mysemp, 0, 0) == -1) {
    perror("unexpected error: "
           "sem_timedwait"
           " "
           "7-1"
           ": "
           "sem_init");
    return 2;
  }
  ts.tv_sec = time(NULL);
  ts.tv_nsec = 0;
  while (sem_timedwait(&mysemp, &ts) == -1) {
    if (errno == ETIMEDOUT) {
      puts("TEST PASSED");
      sem_destroy(&mysemp);
      return 0;
    } else {
      puts("TEST FAILED");
      return 1;
    }
    return 2;
  }
  return 2;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x1c(%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    1221 <main+0xc1>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <time@plt>
mov    %eax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0x1c(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <sem_timedwait@plt>
cmp    $0xffffffff,%eax
jne    121a <main+0xba>
mov    0x2e12(%rip),%rax
mov    (%rax),%rax
mov    0x2e00(%rip),%rcx
cmp    (%rcx),%rax
jne    1202 <main+0xa2>
lea    0xe46(%rip),%rdi
call   1030 <puts@plt>
lea    -0x1c(%rbp),%rdi
call   1080 <sem_destroy@plt>
movl   $0x0,-0x4(%rbp)
jmp    1221 <main+0xc1>
lea    0xe31(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    1221 <main+0xc1>
movl   $0x2,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x18,%rsp
lea    0x4(%rsp),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <sem_init@plt>
cmp    $0xffffffff,%eax
je     11f4 <main+0x94>
xor    %edi,%edi
call   1050 <time@plt>
mov    %eax,0x10(%rsp)
movq   $0x0,0x8(%rsp)
lea    0x4(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1040 <sem_timedwait@plt>
mov    %eax,%ecx
mov    $0x2,%eax
cmp    $0xffffffff,%ecx
je     11ab <main+0x4b>
add    $0x18,%rsp
ret
mov    0x2e2e(%rip),%rax
mov    (%rax),%rax
mov    0x2e1c(%rip),%rcx
cmp    (%rcx),%rax
jne    11de <main+0x7e>
lea    0xe66(%rip),%rdi
call   1030 <puts@plt>
lea    0x4(%rsp),%rdi
call   1080 <sem_destroy@plt>
xor    %eax,%eax
add    $0x18,%rsp
ret
lea    0xe55(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
add    $0x18,%rsp
ret
lea    0xe05(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%eax
add    $0x18,%rsp
ret

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

LLVM IR (-O2):
%struct.timespec = type { i64, i32 }
@.str = [46 x i8] c"unexpected error: sem_timedwait 7-1: sem_init\00"
@errno = global i64 0
@ETIMEDOUT = 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 %struct.timespec
%2 = alloca i32
%3 = bitcast %struct.timespec* %1 to i8*
%4 = bitcast i32* %2 to i8*
%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))
br label %24
9: ; preds = %0
%10 = call i32 @time(i32* null)
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
store i64 0, i64* %12
%13 = call i32 @sem_timedwait(i32* %2, %struct.timespec* %1)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %24
15: ; preds = %9
%16 = load i64, i64* @errno
%17 = load i64, i64* @ETIMEDOUT
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %22
19: ; preds = %15
%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* %2)
br label %24
22: ; preds = %15
%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 = %9, %22, %19, %7
%25 = phi i32 [ 2, %7 ], [ 0, %19 ], [ 1, %22 ], [ 2, %9 ]
ret i32 %25
}