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

/* Variables and functions */
int /*<<< orphan*/ WEXITSTATUS(int);
int fork();
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_post(int /*<<< orphan*/ *);
int sem_timedwait(int /*<<< orphan*/ *, struct timespec *);
int /*<<< orphan*/ sleep(int);
scalar_t__ time(int /*<<< orphan*/ *);
int wait(int *);
int main(void) {
  sem_t mysemp;
  struct timespec ts;
  int pid;
  if (sem_init(&mysemp, 0, 0) == -1) {
    perror("unexpected error: "
           "sem_timedwait"
           " "
           "2-1"
           ": "
           "sem_init");
    return 2;
  }
  pid = fork();
  if (pid == 0) {
    ts.tv_sec = time(NULL) + 2;
    ts.tv_nsec = 0;
    if (sem_timedwait(&mysemp, &ts) == -1) {
      puts("TEST FAILED");
      return 1;
    } else {
      puts("TEST PASSED");
      sem_destroy(&mysemp);
      return 0;
    }
  } else if (pid > 0) {
    int i;
    sleep(1);
    if (sem_post(&mysemp) == -1) {
      perror("unexpected error: "
             "sem_timedwait"
             " "
             "2-1"
             ": "
             "sem_post");
      return 1;
    }
    if (wait(&i) == -1) {
      perror("Error waiting for child to exit");
      return 2;
    }
    if (!WEXITSTATUS(i)) {
      return 1;
    }
    puts("TEST PASSED");
    sem_destroy(&mysemp);
    return 0;
  }
  return 2;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x8(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1070 <sem_init@plt>
cmp    $0xffffffff,%eax
jne    11ed <main+0x3d>
lea    0xe24(%rip),%rdi
call   1090 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    131b <main+0x16b>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1269 <main+0xb9>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
add    $0x2,%rax
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <sem_timedwait@plt>
cmp    $0xffffffff,%eax
jne    1248 <main+0x98>
lea    0xdf7(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    131b <main+0x16b>
lea    0xdeb(%rip),%rdi
call   1030 <puts@plt>
lea    -0x8(%rbp),%rdi
call   10c0 <sem_destroy@plt>
movl   $0x0,-0x4(%rbp)
jmp    131b <main+0x16b>
cmpl   $0x0,-0x1c(%rbp)
jle    130f <main+0x15f>
mov    $0x1,%edi
call   10a0 <sleep@plt>
lea    -0x8(%rbp),%rdi
call   1050 <sem_post@plt>
cmp    $0xffffffff,%eax
jne    12a7 <main+0xf7>
lea    0xdb0(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    131b <main+0x16b>
lea    -0x20(%rbp),%rdi
call   10b0 <wait@plt>
cmp    $0xffffffff,%eax
jne    12d1 <main+0x121>
lea    0xdb4(%rip),%rdi
call   1090 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    131b <main+0x16b>
mov    -0x20(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
cmp    $0x0,%eax
jne    12ee <main+0x13e>
movl   $0x1,-0x4(%rbp)
jmp    131b <main+0x16b>
lea    0xd45(%rip),%rdi
call   1030 <puts@plt>
lea    -0x8(%rbp),%rdi
call   10c0 <sem_destroy@plt>
movl   $0x0,-0x4(%rbp)
jmp    131b <main+0x16b>
jmp    1314 <main+0x164>
movl   $0x2,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%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   1070 <sem_init@plt>
cmp    $0xffffffff,%eax
je     1296 <main+0xe6>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
je     123c <main+0x8c>
jle    12b9 <main+0x109>
mov    $0x1,%ebx
mov    $0x1,%edi
call   10a0 <sleep@plt>
lea    0xc(%rsp),%rdi
call   1050 <sem_post@plt>
cmp    $0xffffffff,%eax
je     129f <main+0xef>
lea    0x10(%rsp),%rdi
call   10b0 <wait@plt>
cmp    $0xffffffff,%eax
je     12ad <main+0xfd>
mov    0x10(%rsp),%edi
call   1080 <WEXITSTATUS@plt>
test   %eax,%eax
je     128f <main+0xdf>
lea    0xe14(%rip),%rdi
call   1030 <puts@plt>
lea    0xc(%rsp),%rdi
call   10c0 <sem_destroy@plt>
xor    %ebx,%ebx
jmp    12be <main+0x10e>
xor    %ebx,%ebx
xor    %edi,%edi
call   1060 <time@plt>
add    $0x2,%rax
mov    %rax,0x18(%rsp)
movq   $0x0,0x10(%rsp)
lea    0xc(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1040 <sem_timedwait@plt>
cmp    $0xffffffff,%eax
je     1283 <main+0xd3>
lea    0xdc8(%rip),%rdi
call   1030 <puts@plt>
lea    0xc(%rsp),%rdi
call   10c0 <sem_destroy@plt>
jmp    12be <main+0x10e>
lea    0xda4(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
jmp    12be <main+0x10e>
lea    0xd63(%rip),%rdi
jmp    12b4 <main+0x104>
lea    0xda0(%rip),%rdi
call   1090 <perror@plt>
jmp    12be <main+0x10e>
lea    0xdc0(%rip),%rdi
call   1090 <perror@plt>
mov    $0x2,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@.str = [46 x i8] c"unexpected error: sem_timedwait 2-1: sem_init\00"
@.str.1 = [12 x i8] c"TEST FAILED\00"
@.str.2 = [12 x i8] c"TEST PASSED\00"
@.str.3 = [46 x i8] c"unexpected error: sem_timedwait 2-1: sem_post\00"
@.str.4 = [32 x i8] c"Error waiting for child to exit\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 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 %50
10: ; preds = %0
%11 = call i32 (...) @fork()
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %26
14: ; preds = %10
%15 = call i64 @time(i32* null)
%16 = add nsw i64 %15, 2
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i64 %16, i64* %17
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i64 0, i64* %18
%19 = call i32 @sem_timedwait(i32* %2, %struct.timespec* %3)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %50
23: ; preds = %14
%24 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @sem_destroy(i32* %2)
store i32 0, i32* %1
br label %50
26: ; preds = %10
%27 = load i32, i32* %4
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %48
29: ; preds = %26
%30 = call i32 @sleep(i32 1)
%31 = call i32 @sem_post(i32* %2)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %50
35: ; preds = %29
%36 = call i32 @wait(i32* %5)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
store i32 2, i32* %1
br label %50
40: ; preds = %35
%41 = load i32, i32* %5
%42 = call i32 @WEXITSTATUS(i32 %41)
%43 = icmp ne i32 %42, 0
br i1 %43, label %45, label %44
44: ; preds = %40
store i32 1, i32* %1
br label %50
45: ; preds = %40
%46 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%47 = call i32 @sem_destroy(i32* %2)
store i32 0, i32* %1
br label %50
48: ; preds = %26
br label %49
49: ; preds = %48
store i32 2, i32* %1
br label %50
50: ; preds = %49, %45, %44, %38, %33, %23, %21, %8
%51 = load i32, i32* %1
ret i32 %51
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@.str = [46 x i8] c"unexpected error: sem_timedwait 2-1: sem_init\00"
@.str.1 = [12 x i8] c"TEST FAILED\00"
@.str.2 = [12 x i8] c"TEST PASSED\00"
@.str.3 = [46 x i8] c"unexpected error: sem_timedwait 2-1: sem_post\00"
@.str.4 = [32 x i8] c"Error waiting for child to exit\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 = call i32 @sem_init(i32* %1, 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))
br label %48
10: ; preds = %0
%11 = call i32 (...) @fork()
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %25
13: ; preds = %10
%14 = call i64 @time(i32* null)
%15 = add nsw i64 %14, 2
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i64 %15, i64* %16
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i64 0, i64* %17
%18 = call i32 @sem_timedwait(i32* %1, %struct.timespec* %2)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %13
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %48
22: ; preds = %13
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @sem_destroy(i32* %1)
br label %48
25: ; preds = %10
%26 = icmp sgt i32 %11, 0
br i1 %26, label %27, label %48
27: ; preds = %25
%28 = bitcast i32* %3 to i8*
%29 = call i32 @sleep(i32 1)
%30 = call i32 @sem_post(i32* %1)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %27
%33 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0))
br label %46
34: ; preds = %27
%35 = call i32 @wait(i32* %3)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
br label %46
39: ; preds = %34
%40 = load i32, i32* %3
%41 = call i32 @WEXITSTATUS(i32 %40)
%42 = icmp eq i32 %41, 0
br i1 %42, label %46, label %43
43: ; preds = %39
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%45 = call i32 @sem_destroy(i32* %1)
br label %46
46: ; preds = %39, %43, %37, %32
%47 = phi i32 [ 1, %32 ], [ 2, %37 ], [ 0, %43 ], [ 1, %39 ]
br label %48
48: ; preds = %25, %46, %22, %20, %8
%49 = phi i32 [ 2, %8 ], [ 1, %20 ], [ 0, %22 ], [ %47, %46 ], [ 2, %25 ]
ret i32 %49
}