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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ assert(int);
int clock_gettime(int /*<<< orphan*/, struct timespec *);
int sem_timedwait(int /*<<< orphan*/ *, struct timespec *);
int wait_for_semaphore(sem_t *sem, unsigned int secs) {
  struct timespec ts;
  int ret;
  ret = clock_gettime(CLOCK_REALTIME, &ts);
  assert(ret == 0);
  ts.tv_sec += secs;
  ret = sem_timedwait(sem, &ts);
  return ret;
}


x86 assembly (clang, -O0):
<wait_for_semaphore>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e9a(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0xc(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1040 <sem_timedwait@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_semaphore>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
mov    0x2e9b(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%r15
mov    %r15,%rsi
call   1030 <clock_gettime@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1050 <assert@plt>
add    %ebx,0x8(%rsp)
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <sem_timedwait@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
@CLOCK_REALTIME = global i32 0
define i32 @wait_for_semaphore(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca %struct.timespec
%6 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%7 = load i32, i32* @CLOCK_REALTIME
%8 = call i32 @clock_gettime(i32 %7, %struct.timespec* %5)
store i32 %8, i32* %6
%9 = load i32, i32* %6
%10 = icmp eq i32 %9, 0
%11 = zext i1 %10 to i32
%12 = call i32 @assert(i32 %11)
%13 = load i32, i32* %4
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%15 = load i32, i32* %14
%16 = add i32 %15, %13
store i32 %16, i32* %14
%17 = load i32*, i32** %3
%18 = call i32 @sem_timedwait(i32* %17, %struct.timespec* %5)
store i32 %18, i32* %6
%19 = load i32, i32* %6
ret i32 %19
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@CLOCK_REALTIME = global i32 0
define i32 @wait_for_semaphore(i32* %0, i32 %1) {
%3 = alloca %struct.timespec
%4 = bitcast %struct.timespec* %3 to i8*
%5 = load i32, i32* @CLOCK_REALTIME
%6 = call i32 @clock_gettime(i32 %5, %struct.timespec* %3)
%7 = icmp eq i32 %6, 0
%8 = zext i1 %7 to i32
%9 = call i32 @assert(i32 %8)
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%11 = load i32, i32* %10
%12 = add i32 %11, %1
store i32 %12, i32* %10
%13 = call i32 @sem_timedwait(i32* %0, %struct.timespec* %3)
ret i32 %13
}