#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;
};
struct sembuf {
  int sem_op;
  scalar_t__ sem_flg;
  scalar_t__ sem_num;
};

/* Variables and functions */
int semInit(int, int);
int semget(int, int, int);
int semtimedop(int, struct sembuf *, int, struct timespec *);
int semPtimeout(int key, int secs) {
  int semId = semget(key, 1, 0666);
  if (semId < 0) {
    semId = semInit(1, key);
    if (!semId)
      return 0;
  }
  struct sembuf operations[1];
  operations[0].sem_num = 0;
  operations[0].sem_op = -1;
  operations[0].sem_flg = 0;
  struct timespec ts;
  ts.tv_sec = secs;
  ts.tv_nsec = 0;
  int retval = semtimedop(semId, operations, 1, &ts);
  if (retval == 0) {
    ;
    return (1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<semPtimeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
mov    $0x1b6,%edx
call   1030 <semget@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1188 <semPtimeout+0x58>
mov    -0x8(%rbp),%esi
mov    $0x1,%edi
call   1050 <semInit@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    1183 <semPtimeout+0x53>
movl   $0x0,-0x4(%rbp)
jmp    11e2 <semPtimeout+0xb2>
jmp    1188 <semPtimeout+0x58>
movq   $0x0,-0x20(%rbp)
movl   $0xffffffff,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x40(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x1,%edx
lea    -0x40(%rbp),%rcx
call   1040 <semtimedop@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jne    11db <semPtimeout+0xab>
movl   $0x1,-0x4(%rbp)
jmp    11e2 <semPtimeout+0xb2>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<semPtimeout>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
mov    $0x1,%esi
mov    $0x1b6,%edx
call   1030 <semget@plt>
test   %eax,%eax
jns    115d <semPtimeout+0x2d>
mov    $0x1,%edi
mov    %ebp,%esi
call   1050 <semInit@plt>
test   %eax,%eax
je     119d <semPtimeout+0x6d>
movl   $0xffffffff,0x10(%rsp)
ff
xorps  %xmm0,%xmm0
movups %xmm0,0x18(%rsp)
mov    %ebx,(%rsp)
movq   $0x0,0x8(%rsp)
lea    0x10(%rsp),%rsi
mov    %rsp,%rcx
mov    %eax,%edi
mov    $0x1,%edx
call   1040 <semtimedop@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
sete   %al
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret
xor    %eax,%eax
jmp    1196 <semPtimeout+0x66>

LLVM IR (-O0):
%struct.sembuf = type { i32, i64, i64 }
%struct.timespec = type { i32, i64 }
define i32 @semPtimeout(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [1 x %struct.sembuf]
%8 = alloca %struct.timespec
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%10 = load i32, i32* %4
%11 = call i32 @semget(i32 %10, i32 1, i32 438)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %21
14: ; preds = %2
%15 = load i32, i32* %4
%16 = call i32 @semInit(i32 1, i32 %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp ne i32 %17, 0
br i1 %18, label %20, label %19
19: ; preds = %14
store i32 0, i32* %3
br label %38
20: ; preds = %14
br label %21
21: ; preds = %20, %2
%22 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %7, i64 0, i64 0
%23 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %22, i32 0, i32 2
store i64 0, i64* %23
%24 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %7, i64 0, i64 0
%25 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %24, i32 0, i32 0
store i32 -1, i32* %25
%26 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %7, i64 0, i64 0
%27 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %26, i32 0, i32 1
store i64 0, i64* %27
%28 = load i32, i32* %5
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
store i32 %28, i32* %29
%30 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 1
store i64 0, i64* %30
%31 = load i32, i32* %6
%32 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %7, i64 0, i64 0
%33 = call i32 @semtimedop(i32 %31, %struct.sembuf* %32, i32 1, %struct.timespec* %8)
store i32 %33, i32* %9
%34 = load i32, i32* %9
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %21
store i32 1, i32* %3
br label %38
37: ; preds = %21
store i32 0, i32* %3
br label %38
38: ; preds = %37, %36, %19
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
%struct.sembuf = type { i32, i64, i64 }
%struct.timespec = type { i32, i64 }
define i32 @semPtimeout(i32 %0, i32 %1) {
%3 = alloca [1 x %struct.sembuf]
%4 = alloca %struct.timespec
%5 = tail call i32 @semget(i32 %0, i32 1, i32 438)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @semInit(i32 1, i32 %0)
%9 = icmp eq i32 %8, 0
br i1 %9, label %23, label %10
10: ; preds = %7, %2
%11 = phi i32 [ %8, %7 ], [ %5, %2 ]
%12 = bitcast [1 x %struct.sembuf]* %3 to i8*
%13 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %3, i64 0, i64 0
%14 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %3, i64 0, i64 0, i32 0
store i32 -1, i32* %14
%15 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %3, i64 0, i64 0, i32 1
%16 = bitcast i64* %15 to <2 x i64>*
store <2 x i64> zeroinitializer, <2 x i64>* %16
%17 = bitcast %struct.timespec* %4 to i8*
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
store i32 %1, i32* %18
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 1
store i64 0, i64* %19
%20 = call i32 @semtimedop(i32 %11, %struct.sembuf* %13, i32 1, %struct.timespec* %4)
%21 = icmp eq i32 %20, 0
%22 = zext i1 %21 to i32
br label %23
23: ; preds = %7, %10
%24 = phi i32 [ %22, %10 ], [ 0, %7 ]
ret i32 %24
}