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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ *SEM_FAILED;
int clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, long);
int /*<<< orphan*/ *sem_open(char *, int /*<<< orphan*/);
int sem_timedwait(int /*<<< orphan*/ *, struct timespec *);
int main() {
  sem_t *sem;
  struct timespec ts;
  sem = sem_open("/mysem", 0);
  if (sem == SEM_FAILED) {
    perror("sem_open");
    exit(errno);
  };
  if (clock_gettime(CLOCK_REALTIME, &ts) == -1) {
    perror("clock_gettime-CLOCK_REALTIME");
    exit(errno);
  };
  ts.tv_sec += 10;
  if (sem_timedwait(sem, &ts) == -1) {
    perror("sem_timedwait");
    exit(errno);
  };
  printf("%ld sem_wait() succeeded\n", (long)getpid());
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rdi
xor    %esi,%esi
call   1070 <sem_open@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e4c(%rip),%rcx
cmp    (%rcx),%rax
jne    11bf <main+0x4f>
lea    0xe5b(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0xffffffff,%eax
jne    11f4 <main+0x84>
lea    0xe2f(%rip),%rdi
call   1080 <perror@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%eax
add    $0xa,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <sem_timedwait@plt>
cmp    $0xffffffff,%eax
jne    122d <main+0xbd>
lea    0xe13(%rip),%rdi
call   1080 <perror@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,%rsi
lea    0xdfd(%rip),%rdi
call   1060 <printf@plt>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0xe84(%rip),%rdi
xor    %esi,%esi
call   1070 <sem_open@plt>
mov    %rax,%rbx
mov    0x2e5b(%rip),%rax
cmp    (%rax),%rbx
jne    119b <main+0x2b>
lea    0xe6e(%rip),%rdi
jmp    11da <main+0x6a>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0xffffffff,%eax
jne    11bc <main+0x4c>
lea    0xe56(%rip),%rdi
jmp    11da <main+0x6a>
addl   $0xa,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <sem_timedwait@plt>
cmp    $0xffffffff,%eax
jne    11ed <main+0x7d>
lea    0xe53(%rip),%rdi
call   1080 <perror@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe40(%rip),%rdi
mov    %rax,%rsi
call   1060 <printf@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.timespec = type { i32 }
@.str = [7 x i8] c"/mysem\00"
@SEM_FAILED = global i32* null
@.str.1 = [9 x i8] c"sem_open\00"
@errno = global i32 0
@CLOCK_REALTIME = global i32 0
@.str.2 = [29 x i8] c"clock_gettime-CLOCK_REALTIME\00"
@.str.3 = [14 x i8] c"sem_timedwait\00"
@.str.4 = [26 x i8] c"%ld sem_wait() succeeded\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca %struct.timespec
store i32 0, i32* %1
%4 = call i32* @sem_open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 0)
store i32* %4, i32** %2
%5 = load i32*, i32** %2
%6 = load i32*, i32** @SEM_FAILED
%7 = icmp eq i32* %5, %6
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%10 = load i32, i32* @errno
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
%13 = load i32, i32* @CLOCK_REALTIME
%14 = call i32 @clock_gettime(i32 %13, %struct.timespec* %3)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %12
%17 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%18 = load i32, i32* @errno
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %12
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%22 = load i32, i32* %21
%23 = add nsw i32 %22, 10
store i32 %23, i32* %21
%24 = load i32*, i32** %2
%25 = call i32 @sem_timedwait(i32* %24, %struct.timespec* %3)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %20
%28 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%29 = load i32, i32* @errno
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %20
%32 = call i64 (...) @getpid()
%33 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i64 %32)
%34 = load i32, i32* @EXIT_SUCCESS
%35 = call i32 @exit(i32 %34)
unreachable
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@.str = [7 x i8] c"/mysem\00"
@SEM_FAILED = global i32* null
@.str.1 = [9 x i8] c"sem_open\00"
@errno = global i32 0
@CLOCK_REALTIME = global i32 0
@.str.2 = [29 x i8] c"clock_gettime-CLOCK_REALTIME\00"
@.str.3 = [14 x i8] c"sem_timedwait\00"
@.str.4 = [26 x i8] c"%ld sem_wait() succeeded\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca %struct.timespec
%2 = bitcast %struct.timespec* %1 to i8*
%3 = tail call i32* @sem_open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 0)
%4 = load i32*, i32** @SEM_FAILED
%5 = icmp eq i32* %3, %4
br i1 %5, label %6, label %10
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%8 = load i32, i32* @errno
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %0
%11 = load i32, i32* @CLOCK_REALTIME
%12 = call i32 @clock_gettime(i32 %11, %struct.timespec* %1)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%16 = load i32, i32* @errno
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %10
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%20 = load i32, i32* %19
%21 = add nsw i32 %20, 10
store i32 %21, i32* %19
%22 = call i32 @sem_timedwait(i32* %3, %struct.timespec* %1)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %28
24: ; preds = %18
%25 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%26 = load i32, i32* @errno
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %18
%29 = call i64 (...) @getpid()
%30 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i64 %29)
%31 = load i32, i32* @EXIT_SUCCESS
%32 = call i32 @exit(i32 %31)
unreachable
}