#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;
  void *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 *);
void *time(int /*<<< orphan*/ *);
int main(void) {
  sem_t mysemp[2];
  struct timespec ts[2];
  int val[2], sts[2];
  int i;
  for (i = 0; i < 2; i++) {
    if (sem_init(&mysemp[i], 0, 1) == -1) {
      perror("unexpected error: "
             "sem_timedwait"
             " "
             "11-1"
             ": "
             "sem_init");
      return 2;
    }
    if (i == 0) {
      ts[i].tv_sec = time(NULL) + 2;
      ts[i].tv_nsec = 0;
    } else if (i == 1) {
      ts[i].tv_sec = time(NULL) + -2;
      ts[i].tv_nsec = 0;
    }
    sts[i] = sem_timedwait(&mysemp[i], &ts[i]);
    if (sts[i] == -1) {
      perror("unexpected error: "
             "sem_timedwait"
             " "
             "11-1"
             ": "
             "sem_timedwait");
      return 2;
    }
    if (sem_getvalue(&mysemp[i], &val[i]) == -1) {
      perror("unexpected error: "
             "sem_timedwait"
             " "
             "11-1"
             ": "
             "sem_getvalue");
      return 2;
    }
    if ((val[i] == 0) && (sts[i] == 0)) {
      puts("TEST PASSED");
      sem_destroy(&mysemp[i]);
      return 0;
    } else {
      puts("TEST FAILED");
      sem_destroy(&mysemp[i]);
      return 1;
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x44(%rbp)
cmpl   $0x2,-0x44(%rbp)
jge    1365 <main+0x1f5>
movslq -0x44(%rbp),%rax
lea    -0xc(%rbp),%rdi
shl    $0x2,%rax
add    %rax,%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1070 <sem_init@plt>
cmp    $0xffffffff,%eax
jne    11cc <main+0x5c>
lea    0xe45(%rip),%rdi
call   1080 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1365 <main+0x1f5>
cmpl   $0x0,-0x44(%rbp)
jne    1214 <main+0xa4>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    %rax,%rcx
add    $0x2,%rcx
movslq -0x44(%rbp),%rdx
lea    -0x30(%rbp),%rax
shl    $0x4,%rdx
add    %rdx,%rax
mov    %rcx,0x8(%rax)
movslq -0x44(%rbp),%rcx
lea    -0x30(%rbp),%rax
shl    $0x4,%rcx
add    %rcx,%rax
movq   $0x0,(%rax)
jmp    125c <main+0xec>
cmpl   $0x1,-0x44(%rbp)
jne    1257 <main+0xe7>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    %rax,%rcx
add    $0xfffffffffffffffe,%rcx
movslq -0x44(%rbp),%rdx
lea    -0x30(%rbp),%rax
shl    $0x4,%rdx
add    %rdx,%rax
mov    %rcx,0x8(%rax)
movslq -0x44(%rbp),%rcx
lea    -0x30(%rbp),%rax
shl    $0x4,%rcx
add    %rcx,%rax
movq   $0x0,(%rax)
jmp    125c <main+0xec>
movslq -0x44(%rbp),%rax
lea    -0xc(%rbp),%rdi
shl    $0x2,%rax
add    %rax,%rdi
movslq -0x44(%rbp),%rax
lea    -0x30(%rbp),%rsi
shl    $0x4,%rax
add    %rax,%rsi
call   1040 <sem_timedwait@plt>
mov    %eax,%ecx
movslq -0x44(%rbp),%rax
mov    %ecx,-0x40(%rbp,%rax,4)
movslq -0x44(%rbp),%rax
cmpl   $0xffffffff,-0x40(%rbp,%rax,4)
jne    12b0 <main+0x140>
lea    0xd90(%rip),%rdi
call   1080 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1365 <main+0x1f5>
movslq -0x44(%rbp),%rax
lea    -0xc(%rbp),%rdi
shl    $0x2,%rax
add    %rax,%rdi
movslq -0x44(%rbp),%rax
lea    -0x38(%rbp),%rsi
shl    $0x2,%rax
add    %rax,%rsi
call   1050 <sem_getvalue@plt>
cmp    $0xffffffff,%eax
jne    12f4 <main+0x184>
lea    0xd80(%rip),%rdi
call   1080 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1365 <main+0x1f5>
movslq -0x44(%rbp),%rax
cmpl   $0x0,-0x38(%rbp,%rax,4)
jne    133e <main+0x1ce>
movslq -0x44(%rbp),%rax
cmpl   $0x0,-0x40(%rbp,%rax,4)
jne    133e <main+0x1ce>
lea    0xd7d(%rip),%rdi
call   1030 <puts@plt>
movslq -0x44(%rbp),%rax
lea    -0xc(%rbp),%rdi
shl    $0x2,%rax
add    %rax,%rdi
call   1090 <sem_destroy@plt>
movl   $0x0,-0x4(%rbp)
jmp    1365 <main+0x1f5>
lea    0xd5d(%rip),%rdi
call   1030 <puts@plt>
movslq -0x44(%rbp),%rax
lea    -0xc(%rbp),%rdi
shl    $0x2,%rax
add    %rax,%rdi
call   1090 <sem_destroy@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x30,%rsp
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1070 <sem_init@plt>
cmp    $0xffffffff,%eax
je     1201 <main+0x91>
xor    %edi,%edi
call   1060 <time@plt>
add    $0x2,%rax
mov    %rax,0x18(%rsp)
movq   $0x0,0x10(%rsp)
mov    %rsp,%rdi
lea    0x10(%rsp),%rsi
call   1040 <sem_timedwait@plt>
cmp    $0xffffffff,%eax
je     120a <main+0x9a>
mov    %eax,%ebx
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
call   1050 <sem_getvalue@plt>
cmp    $0xffffffff,%eax
je     1213 <main+0xa3>
or     0x8(%rsp),%ebx
jne    11e6 <main+0x76>
lea    0xec1(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rdi
call   1090 <sem_destroy@plt>
xor    %eax,%eax
jmp    1224 <main+0xb4>
lea    0xeb5(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rdi
call   1090 <sem_destroy@plt>
mov    $0x1,%eax
jmp    1224 <main+0xb4>
lea    0xdf8(%rip),%rdi
jmp    121a <main+0xaa>
lea    0xe1e(%rip),%rdi
jmp    121a <main+0xaa>
lea    0xe49(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%eax
add    $0x30,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i8* }
@.str = [47 x i8] c"unexpected error: sem_timedwait 11-1: sem_init\00"
@.str.1 = [52 x i8] c"unexpected error: sem_timedwait 11-1: sem_timedwait\00"
@.str.2 = [51 x i8] c"unexpected error: sem_timedwait 11-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 [2 x i32]
%3 = alloca [2 x %struct.timespec]
%4 = alloca [2 x i32]
%5 = alloca [2 x i32]
%6 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %6
br label %7
7: ; preds = %100, %0
%8 = load i32, i32* %6
%9 = icmp slt i32 %8, 2
br i1 %9, label %10, label %103
10: ; preds = %7
%11 = load i32, i32* %6
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 %12
%14 = call i32 @sem_init(i32* %13, i32 0, i32 1)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %10
%17 = call i32 @perror(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %1
br label %103
18: ; preds = %10
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %32
21: ; preds = %18
%22 = call i8* @time(i32* null)
%23 = getelementptr i8, i8* %22, i64 2
%24 = load i32, i32* %6
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [2 x %struct.timespec], [2 x %struct.timespec]* %3, i64 0, i64 %25
%27 = getelementptr inbounds %struct.timespec, %struct.timespec* %26, i32 0, i32 1
store i8* %23, i8** %27
%28 = load i32, i32* %6
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [2 x %struct.timespec], [2 x %struct.timespec]* %3, i64 0, i64 %29
%31 = getelementptr inbounds %struct.timespec, %struct.timespec* %30, i32 0, i32 0
store i64 0, i64* %31
br label %47
32: ; preds = %18
%33 = load i32, i32* %6
%34 = icmp eq i32 %33, 1
br i1 %34, label %35, label %46
35: ; preds = %32
%36 = call i8* @time(i32* null)
%37 = getelementptr i8, i8* %36, i64 -2
%38 = load i32, i32* %6
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [2 x %struct.timespec], [2 x %struct.timespec]* %3, i64 0, i64 %39
%41 = getelementptr inbounds %struct.timespec, %struct.timespec* %40, i32 0, i32 1
store i8* %37, i8** %41
%42 = load i32, i32* %6
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [2 x %struct.timespec], [2 x %struct.timespec]* %3, i64 0, i64 %43
%45 = getelementptr inbounds %struct.timespec, %struct.timespec* %44, i32 0, i32 0
store i64 0, i64* %45
br label %46
46: ; preds = %35, %32
br label %47
47: ; preds = %46, %21
%48 = load i32, i32* %6
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 %49
%51 = load i32, i32* %6
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [2 x %struct.timespec], [2 x %struct.timespec]* %3, i64 0, i64 %52
%54 = call i32 @sem_timedwait(i32* %50, %struct.timespec* %53)
%55 = load i32, i32* %6
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 %56
store i32 %54, i32* %57
%58 = load i32, i32* %6
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 %59
%61 = load i32, i32* %60
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %65
63: ; preds = %47
%64 = call i32 @perror(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %1
br label %103
65: ; preds = %47
%66 = load i32, i32* %6
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 %67
%69 = load i32, i32* %6
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 %70
%72 = call i32 @sem_getvalue(i32* %68, i32* %71)
%73 = icmp eq i32 %72, -1
br i1 %73, label %74, label %76
74: ; preds = %65
%75 = call i32 @perror(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %1
br label %103
76: ; preds = %65
%77 = load i32, i32* %6
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 %78
%80 = load i32, i32* %79
%81 = icmp eq i32 %80, 0
br i1 %81, label %82, label %94
82: ; preds = %76
%83 = load i32, i32* %6
%84 = sext i32 %83 to i64
%85 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 %84
%86 = load i32, i32* %85
%87 = icmp eq i32 %86, 0
br i1 %87, label %88, label %94
88: ; preds = %82
%89 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%90 = load i32, i32* %6
%91 = sext i32 %90 to i64
%92 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 %91
%93 = call i32 @sem_destroy(i32* %92)
store i32 0, i32* %1
br label %103
94: ; preds = %82, %76
%95 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%96 = load i32, i32* %6
%97 = sext i32 %96 to i64
%98 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 %97
%99 = call i32 @sem_destroy(i32* %98)
store i32 1, i32* %1
br label %103
100: ; No predecessors!
%101 = load i32, i32* %6
%102 = add nsw i32 %101, 1
store i32 %102, i32* %6
br label %7
103: ; preds = %16, %63, %74, %88, %94, %7
%104 = load i32, i32* %1
ret i32 %104
}

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