#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 {
  int tv_sec;
  scalar_t__ tv_nsec;
};
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 /*<<< orphan*/ sem_post(int /*<<< orphan*/ *);
int sem_timedwait(int /*<<< orphan*/ *, struct timespec *);
int time(int /*<<< orphan*/ *);
int main(void) {
  struct timespec ts;
  sem_t mysemp;
  int i = 0;
  int val;
  if (sem_init(&mysemp, 0, 0) == -1) {
    perror("unexpected error: "
           "sem_timedwait"
           " "
           "3-1"
           ": "
           "sem_init");
    return 2;
  }
  ts.tv_sec = time(NULL);
  ts.tv_nsec = 0;
  while (sem_timedwait(&mysemp, &ts) == -1) {
    ts.tv_sec += 1;
    i++;
    if (i == 5) {
      sem_post(&mysemp);
    }
  }
  if (sem_getvalue(&mysemp, &val) == -1) {
    perror("unexpected error: "
           "sem_timedwait"
           " "
           "3-1"
           ": "
           "sem_getvalue");
    return 2;
  }
  if (val == 0) {
    puts("TEST PASSED: Sem unlocked after 5 timeouts");
    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)
movl   $0x0,-0x20(%rbp)
lea    -0x1c(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1080 <sem_init@plt>
cmp    $0xffffffff,%eax
jne    11c4 <main+0x44>
lea    0xe4d(%rip),%rdi
call   1090 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1284 <main+0x104>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <time@plt>
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
lea    -0x1c(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <sem_timedwait@plt>
cmp    $0xffffffff,%eax
jne    1218 <main+0x98>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x5,-0x20(%rbp)
jne    1213 <main+0x93>
lea    -0x1c(%rbp),%rdi
call   1060 <sem_post@plt>
jmp    11d8 <main+0x58>
lea    -0x1c(%rbp),%rdi
lea    -0x24(%rbp),%rsi
call   1050 <sem_getvalue@plt>
cmp    $0xffffffff,%eax
jne    1246 <main+0xc6>
lea    0xdf9(%rip),%rdi
call   1090 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1284 <main+0x104>
cmpl   $0x0,-0x24(%rbp)
jne    1271 <main+0xf1>
lea    0xe09(%rip),%rdi
call   1030 <puts@plt>
lea    -0x1c(%rbp),%rdi
call   10a0 <sem_destroy@plt>
movl   $0x0,-0x4(%rbp)
jmp    1284 <main+0x104>
lea    0xe13(%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   %r14
push   %rbx
sub    $0x28,%rsp
lea    0xc(%rsp),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <sem_init@plt>
cmp    $0xffffffff,%eax
je     12bd <main+0x13d>
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,0x10(%rsp)
movq   $0x0,0x18(%rsp)
lea    0xc(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1040 <sem_timedwait@plt>
cmp    $0xffffffff,%eax
jne    1275 <main+0xf5>
addl   $0x1,0x10(%rsp)
lea    0xc(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1040 <sem_timedwait@plt>
cmp    $0xffffffff,%eax
jne    1275 <main+0xf5>
addl   $0x1,0x10(%rsp)
lea    0xc(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1040 <sem_timedwait@plt>
cmp    $0xffffffff,%eax
jne    1275 <main+0xf5>
addl   $0x1,0x10(%rsp)
lea    0xc(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1040 <sem_timedwait@plt>
cmp    $0xffffffff,%eax
jne    1275 <main+0xf5>
addl   $0x1,0x10(%rsp)
lea    0xc(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1040 <sem_timedwait@plt>
cmp    $0xffffffff,%eax
jne    1275 <main+0xf5>
addl   $0x1,0x10(%rsp)
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <sem_post@plt>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
call   1040 <sem_timedwait@plt>
cmp    $0xffffffff,%eax
jne    1275 <main+0xf5>
lea    0xc(%rsp),%r14
lea    0x10(%rsp),%rbx
addl   $0x1,0x10(%rsp)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <sem_timedwait@plt>
cmp    $0xffffffff,%eax
je     1260 <main+0xe0>
lea    0xc(%rsp),%rdi
lea    0x24(%rsp),%rsi
call   1050 <sem_getvalue@plt>
cmp    $0xffffffff,%eax
je     12c6 <main+0x146>
cmpl   $0x0,0x24(%rsp)
je     12a3 <main+0x123>
lea    0xdf4(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    12d7 <main+0x157>
lea    0xdb6(%rip),%rdi
call   1030 <puts@plt>
lea    0xc(%rsp),%rdi
call   10a0 <sem_destroy@plt>
xor    %eax,%eax
jmp    12d7 <main+0x157>
lea    0xd3c(%rip),%rdi
jmp    12cd <main+0x14d>
lea    0xd61(%rip),%rdi
call   1090 <perror@plt>
mov    $0x2,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@.str = [46 x i8] c"unexpected error: sem_timedwait 3-1: sem_init\00"
@.str.1 = [50 x i8] c"unexpected error: sem_timedwait 3-1: sem_getvalue\00"
@.str.2 = [43 x i8] c"TEST PASSED: Sem unlocked after 5 timeouts\00"
@.str.3 = [12 x i8] c"TEST FAILED\00"
define i32 @main() {
%1 = alloca %struct.timespec
%2 = alloca i32
%3 = alloca i32
%4 = bitcast %struct.timespec* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = call i32 @sem_init(i32* %2, 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 %61
11: ; preds = %0
%12 = call i32 @time(i32* null)
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
store i64 0, i64* %14
%15 = call i32 @sem_timedwait(i32* %2, %struct.timespec* %1)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %48
17: ; preds = %11
%18 = load i32, i32* %13
%19 = add nsw i32 %18, 1
store i32 %19, i32* %13
%20 = call i32 @sem_timedwait(i32* %2, %struct.timespec* %1)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %48
22: ; preds = %17
%23 = load i32, i32* %13
%24 = add nsw i32 %23, 1
store i32 %24, i32* %13
%25 = call i32 @sem_timedwait(i32* %2, %struct.timespec* %1)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %48
27: ; preds = %22
%28 = load i32, i32* %13
%29 = add nsw i32 %28, 1
store i32 %29, i32* %13
%30 = call i32 @sem_timedwait(i32* %2, %struct.timespec* %1)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %48
32: ; preds = %27
%33 = load i32, i32* %13
%34 = add nsw i32 %33, 1
store i32 %34, i32* %13
%35 = call i32 @sem_timedwait(i32* %2, %struct.timespec* %1)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %48
37: ; preds = %32
%38 = load i32, i32* %13
%39 = add nsw i32 %38, 1
store i32 %39, i32* %13
%40 = call i32 @sem_post(i32* %2)
%41 = call i32 @sem_timedwait(i32* %2, %struct.timespec* %1)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %48
43: ; preds = %37, %43
%44 = load i32, i32* %13
%45 = add nsw i32 %44, 1
store i32 %45, i32* %13
%46 = call i32 @sem_timedwait(i32* %2, %struct.timespec* %1)
%47 = icmp eq i32 %46, -1
br i1 %47, label %43, label %48
48: ; preds = %43, %17, %22, %27, %32, %37, %11
%49 = call i32 @sem_getvalue(i32* %2, i32* %3)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %53
51: ; preds = %48
%52 = call i32 @perror(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0))
br label %61
53: ; preds = %48
%54 = load i32, i32* %3
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %53
%57 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
%58 = call i32 @sem_destroy(i32* %2)
br label %61
59: ; preds = %53
%60 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %61
61: ; preds = %59, %56, %51, %9
%62 = phi i32 [ 2, %9 ], [ 2, %51 ], [ 0, %56 ], [ 1, %59 ]
ret i32 %62
}