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

/* Variables and functions */
scalar_t__ ETIMEDOUT;
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(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 *);
void *time(int /*<<< orphan*/ *);
int main(void) {
  sem_t mysemp;
  struct timespec ts, ts_2;
  int rc;
  if (sem_init(&mysemp, 0, 0) == -1) {
    perror("unexpected error: "
           "sem_timedwait"
           " "
           "10-1"
           ": "
           "sem_init");
    return 2;
  }
  ts.tv_sec = time(NULL);
  ts.tv_sec += 1;
  ts.tv_nsec = 0;
  rc = sem_timedwait(&mysemp, &ts);
  if (rc != -1 || (rc == -1 && errno != ETIMEDOUT)) {
    perror("unexpected error: "
           "sem_timedwait"
           " "
           "10-1"
           ": "
           "sem_timedwait");
    printf("Expect timedout\n");
    return 2;
  }
  ts_2.tv_sec = time(NULL);
  if (ts_2.tv_sec == ts.tv_sec) {
    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    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x8(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1070 <sem_init@plt>
cmp    $0xffffffff,%eax
jne    11ad <main+0x3d>
lea    0xe64(%rip),%rdi
call   1080 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    127f <main+0x10f>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
lea    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <sem_timedwait@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    120c <main+0x9c>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1230 <main+0xc0>
mov    0x2de7(%rip),%rax
mov    (%rax),%rax
mov    0x2dd5(%rip),%rcx
cmp    (%rcx),%rax
je     1230 <main+0xc0>
lea    0xe1c(%rip),%rdi
call   1080 <perror@plt>
lea    0xe44(%rip),%rdi
call   1050 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    127f <main+0x10f>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    126c <main+0xfc>
lea    0xe22(%rip),%rdi
call   1030 <puts@plt>
lea    -0x8(%rbp),%rdi
call   1090 <sem_destroy@plt>
movl   $0x0,-0x4(%rbp)
jmp    127f <main+0x10f>
lea    0xe0d(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
lea    0xc(%rsp),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <sem_init@plt>
cmp    $0xffffffff,%eax
je     1214 <main+0xb4>
xor    %edi,%edi
call   1050 <time@plt>
add    $0x1,%rax
mov    %rax,0x10(%rsp)
movq   $0x0,0x18(%rsp)
lea    0xc(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1040 <sem_timedwait@plt>
cmp    $0xffffffff,%eax
jne    11fa <main+0x9a>
mov    0x2e30(%rip),%rax
mov    (%rax),%rax
mov    0x2e1e(%rip),%rcx
cmp    (%rcx),%rax
jne    11fa <main+0x9a>
xor    %ebx,%ebx
xor    %edi,%edi
call   1050 <time@plt>
cmp    %rax,0x10(%rsp)
jne    11e7 <main+0x87>
lea    0xe8d(%rip),%rdi
call   1030 <puts@plt>
lea    0xc(%rsp),%rdi
call   1080 <sem_destroy@plt>
jmp    1225 <main+0xc5>
lea    0xe81(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
jmp    1225 <main+0xc5>
lea    0xe2e(%rip),%rdi
call   1070 <perror@plt>
lea    0xe6e(%rip),%rdi
call   1030 <puts@plt>
jmp    1220 <main+0xc0>
lea    0xde5(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@.str = [47 x i8] c"unexpected error: sem_timedwait 10-1: sem_init\00"
@errno = global i64 0
@ETIMEDOUT = global i64 0
@.str.1 = [52 x i8] c"unexpected error: sem_timedwait 10-1: sem_timedwait\00"
@.str.2 = [17 x i8] c"Expect timedout\0A\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 %struct.timespec
%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 ([47 x i8], [47 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %1
br label %45
10: ; preds = %0
%11 = call i8* @time(i32* null)
%12 = ptrtoint i8* %11 to i64
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i64 %12, i64* %13
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%15 = load i64, i64* %14
%16 = add nsw i64 %15, 1
store i64 %16, i64* %14
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i64 0, i64* %17
%18 = call i32 @sem_timedwait(i32* %2, %struct.timespec* %3)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp ne i32 %19, -1
br i1 %20, label %28, label %21
21: ; preds = %10
%22 = load i32, i32* %5
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %31
24: ; preds = %21
%25 = load i64, i64* @errno
%26 = load i64, i64* @ETIMEDOUT
%27 = icmp ne i64 %25, %26
br i1 %27, label %28, label %31
28: ; preds = %24, %10
%29 = call i32 @perror(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %1
br label %45
31: ; preds = %24, %21
%32 = call i8* @time(i32* null)
%33 = ptrtoint i8* %32 to i64
%34 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
store i64 %33, i64* %34
%35 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%36 = load i64, i64* %35
%37 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%38 = load i64, i64* %37
%39 = icmp eq i64 %36, %38
br i1 %39, label %40, label %43
40: ; preds = %31
%41 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%42 = call i32 @sem_destroy(i32* %2)
store i32 0, i32* %1
br label %45
43: ; preds = %31
%44 = 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 %45
45: ; preds = %43, %40, %28, %8
%46 = load i32, i32* %1
ret i32 %46
}

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