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

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ SEM_UNDO;
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
scalar_t__ semop(int, struct sembuf *, int);
int up_sem_v(int semid, int sem_num) {
  struct sembuf buf = {
      .sem_num = sem_num,
      .sem_op = 1,
      .sem_flg = SEM_UNDO,
  };
  while (semop(semid, &buf, 1) < 0) {
    if (errno == EINTR) {
      continue;
    } else {
      do {
        perror("semop");
        return -1;
      } while (0);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<up_sem_v>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
movl   $0x1,-0x14(%rbp)
mov    0x2eae(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x1,%edx
call   1040 <semop@plt>
cmp    $0x0,%rax
jge    119e <up_sem_v+0x7e>
mov    0x2e6f(%rip),%rax
mov    (%rax),%rax
mov    0x2e5d(%rip),%rcx
cmp    (%rcx),%rax
jne    1181 <up_sem_v+0x61>
jmp    1147 <up_sem_v+0x27>
jmp    1186 <up_sem_v+0x66>
lea    0xe73(%rip),%rdi
call   1030 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a5 <up_sem_v+0x85>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<up_sem_v>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
mov    %esi,0x8(%rsp)
movl   $0x1,0xc(%rsp)
mov    0x2eb0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
lea    0x8(%rsp),%r14
mov    0x2e86(%rip),%r15
mov    0x2e77(%rip),%r12
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <semop@plt>
test   %rax,%rax
jns    1190 <up_sem_v+0x70>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1160 <up_sem_v+0x40>
lea    0xe7c(%rip),%rdi
call   1030 <perror@plt>
mov    $0xffffffff,%eax
jmp    1192 <up_sem_v+0x72>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sembuf = type { i32, i32, i32 }
@SEM_UNDO = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str = [6 x i8] c"semop\00"
define i32 @up_sem_v(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 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %6, i32 0, i32 0
%8 = load i32, i32* %5
store i32 %8, i32* %7
%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
%11 = load i32, i32* @SEM_UNDO
store i32 %11, i32* %10
br label %12
12: ; preds = %25, %20, %2
%13 = load i32, i32* %4
%14 = call i64 @semop(i32 %13, %struct.sembuf* %6, i32 1)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %26
16: ; preds = %12
%17 = load i64, i64* @errno
%18 = load i64, i64* @EINTR
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %21
20: ; preds = %16
br label %12
21: ; preds = %16
br label %22
22: ; preds = %21
%23 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %27
24: ; No predecessors!
br label %25
25: ; preds = %24
br label %12
26: ; preds = %12
store i32 0, i32* %3
br label %27
27: ; preds = %26, %22
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
%struct.sembuf = type { i32, i32, i32 }
@SEM_UNDO = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str = [6 x i8] c"semop\00"
define i32 @up_sem_v(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
%8 = load i32, i32* @SEM_UNDO
store i32 %8, i32* %7
br label %9
9: ; preds = %12, %2
%10 = call i64 @semop(i32 %0, %struct.sembuf* %3, i32 1)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %9
%13 = load i64, i64* @errno
%14 = load i64, i64* @EINTR
%15 = icmp eq i64 %13, %14
br i1 %15, label %9, label %16
16: ; preds = %12
%17 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %9, %16
%19 = phi i32 [ -1, %16 ], [ 0, %9 ]
ret i32 %19
}