#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 */

/* Variables and functions */
int getReadLock(void *);
int getWriteLock(void *);
int semaphoreRelease(int);
int dsUnlock(void *mem, unsigned char operations) {
  int rsemid, wsemid;
  rsemid = getReadLock(mem);
  wsemid = getWriteLock(mem);
  if (operations & (1 << 1)) {
    if (semaphoreRelease(wsemid) == -1)
      return -1;
  }
  if (operations & (1 << 0)) {
    if (semaphoreRelease(rsemid) == -1)
      return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<dsUnlock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %rdi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <getReadLock@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <getWriteLock@plt>
mov    %eax,-0x1c(%rbp)
movzbl -0x11(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     118c <dsUnlock+0x5c>
mov    -0x1c(%rbp),%edi
call   1050 <semaphoreRelease@plt>
cmp    $0xffffffff,%eax
jne    1187 <dsUnlock+0x57>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c5 <dsUnlock+0x95>
jmp    118c <dsUnlock+0x5c>
movzbl -0x11(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     11be <dsUnlock+0x8e>
mov    -0x18(%rbp),%edi
call   1050 <semaphoreRelease@plt>
cmp    $0xffffffff,%eax
jne    11b9 <dsUnlock+0x89>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c5 <dsUnlock+0x95>
jmp    11be <dsUnlock+0x8e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dsUnlock>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebx
mov    %rdi,%rbp
call   1040 <getReadLock@plt>
mov    %eax,%r14d
mov    %rbp,%rdi
call   1030 <getWriteLock@plt>
test   $0x2,%bl
je     115a <dsUnlock+0x2a>
mov    %eax,%edi
call   1050 <semaphoreRelease@plt>
cmp    $0xffffffff,%eax
je     1170 <dsUnlock+0x40>
test   $0x1,%bl
je     116c <dsUnlock+0x3c>
mov    %r14d,%edi
call   1050 <semaphoreRelease@plt>
cmp    $0xffffffff,%eax
je     1170 <dsUnlock+0x40>
xor    %eax,%eax
jmp    1175 <dsUnlock+0x45>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @dsUnlock(i8* %0, i8 zeroext %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i8 %1, i8* %5
%8 = load i8*, i8** %4
%9 = call i32 @getReadLock(i8* %8)
store i32 %9, i32* %6
%10 = load i8*, i8** %4
%11 = call i32 @getWriteLock(i8* %10)
store i32 %11, i32* %7
%12 = load i8, i8* %5
%13 = zext i8 %12 to i32
%14 = and i32 %13, 2
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %2
%17 = load i32, i32* %7
%18 = call i32 @semaphoreRelease(i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %21
20: ; preds = %16
store i32 -1, i32* %3
br label %34
21: ; preds = %16
br label %22
22: ; preds = %21, %2
%23 = load i8, i8* %5
%24 = zext i8 %23 to i32
%25 = and i32 %24, 1
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %22
%28 = load i32, i32* %6
%29 = call i32 @semaphoreRelease(i32 %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %27
store i32 -1, i32* %3
br label %34
32: ; preds = %27
br label %33
33: ; preds = %32, %22
store i32 0, i32* %3
br label %34
34: ; preds = %33, %31, %20
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
define i32 @dsUnlock(i8* %0, i8 zeroext %1) {
%3 = tail call i32 @getReadLock(i8* %0)
%4 = tail call i32 @getWriteLock(i8* %0)
%5 = zext i8 %1 to i32
%6 = and i32 %5, 2
%7 = icmp eq i32 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = tail call i32 @semaphoreRelease(i32 %4)
%10 = icmp eq i32 %9, -1
br i1 %10, label %18, label %11
11: ; preds = %8, %2
%12 = and i32 %5, 1
%13 = icmp eq i32 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = tail call i32 @semaphoreRelease(i32 %3)
%16 = icmp eq i32 %15, -1
br i1 %16, label %18, label %17
17: ; preds = %14, %11
br label %18
18: ; preds = %14, %8, %17
%19 = phi i32 [ 0, %17 ], [ -1, %8 ], [ -1, %14 ]
ret i32 %19
}