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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ printf(char *, scalar_t__);
int semop(int, struct sembuf *, int);
int semaphore_p(int sem_id, int idx) {
  struct sembuf sem_b;
  sem_b.sem_num = idx;
  sem_b.sem_op = -1;
  sem_b.sem_flg = 0;
  while (1) {
    if (semop(sem_id, &sem_b, 1) == -1) {
      if (errno == EINTR)
        continue;
      printf("mplayer: semaphore_p failed errno=%d\n", errno);
      return 0;
    }
    break;
  }
  return 1;
}


x86 assembly (clang, -O0):
<semaphore_p>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1040 <semop@plt>
cmp    $0xffffffff,%eax
jne    119e <semaphore_p+0x7e>
mov    0x2e7c(%rip),%rax
mov    (%rax),%rax
mov    0x2e6a(%rip),%rcx
cmp    (%rcx),%rax
jne    117c <semaphore_p+0x5c>
jmp    1143 <semaphore_p+0x23>
mov    0x2e5d(%rip),%rax
mov    (%rax),%rsi
lea    0xe73(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11aa <semaphore_p+0x8a>
jmp    11a3 <semaphore_p+0x83>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<semaphore_p>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    %esi,(%rsp)
movl   $0xffffffff,0x4(%rsp)
ff
movq   $0x0,0x8(%rsp)
mov    %rsp,%r14
mov    0x2e94(%rip),%r15
mov    0x2e85(%rip),%r12
mov    $0x1,%ebp
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <semop@plt>
cmp    $0xffffffff,%eax
jne    118b <semaphore_p+0x6b>
mov    (%r15),%rsi
cmp    (%r12),%rsi
je     1160 <semaphore_p+0x40>
lea    0xe7c(%rip),%rdi
call   1030 <printf@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sembuf = type { i32, i32, i64 }
@errno = global i64 0
@EINTR = global i64 0
@.str = [38 x i8] c"mplayer: semaphore_p failed errno=%d\0A\00"
define i32 @semaphore_p(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sembuf
store i32 %0, i32* %4
store i32 %1, i32* %5
%7 = load i32, i32* %5
%8 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %6, i32 0, i32 0
store i32 %7, i32* %8
%9 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %6, i32 0, i32 1
store i32 -1, i32* %9
%10 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %6, i32 0, i32 2
store i64 0, i64* %10
br label %11
11: ; preds = %2, %19
%12 = load i32, i32* %4
%13 = call i32 @semop(i32 %12, %struct.sembuf* %6, i32 1)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %23
15: ; preds = %11
%16 = load i64, i64* @errno
%17 = load i64, i64* @EINTR
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %20
19: ; preds = %15
br label %11
20: ; preds = %15
%21 = load i64, i64* @errno
%22 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i64 %21)
store i32 0, i32* %3
br label %25
23: ; preds = %11
br label %24
24: ; preds = %23
store i32 1, i32* %3
br label %25
25: ; preds = %24, %20
%26 = load i32, i32* %3
ret i32 %26
}

LLVM IR (-O2):
%struct.sembuf = type { i32, i32, i64 }
@errno = global i64 0
@EINTR = global i64 0
@.str = [38 x i8] c"mplayer: semaphore_p failed errno=%d\0A\00"
define i32 @semaphore_p(i32 %0, i32 %1) {
%3 = alloca %struct.sembuf
%4 = bitcast %struct.sembuf* %3 to i8*
%5 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %3, i64 0, i32 0
store i32 %1, i32* %5
%6 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %3, i64 0, i32 1
store i32 -1, i32* %6
%7 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %3, i64 0, i32 2
store i64 0, i64* %7
br label %8
8: ; preds = %11, %2
%9 = call i32 @semop(i32 %0, %struct.sembuf* %3, i32 1)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %17
11: ; preds = %8
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
br i1 %14, label %8, label %15
15: ; preds = %11
%16 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i64 %12)
br label %17
17: ; preds = %8, %15
%18 = phi i32 [ 0, %15 ], [ 1, %8 ]
ret i32 %18
}