#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 semaphoreReserve(int);
int dsLock(void *mem, unsigned char operations) {
  int rsemid, wsemid;
  rsemid = getReadLock(mem);
  wsemid = getWriteLock(mem);
  if (operations & (1 << 0)) {
    if (semaphoreReserve(rsemid) == -1)
      return -1;
  }
  if (operations & (1 << 1)) {
    if (semaphoreReserve(wsemid) == -1)
      return -1;
  }
  return 0;
}


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

x86 assembly (clang, -O2):
<dsLock>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%rbp
call   1050 <getReadLock@plt>
mov    %eax,%r15d
mov    %rbp,%rdi
call   1040 <getWriteLock@plt>
mov    %eax,%r14d
test   $0x1,%bl
je     1161 <dsLock+0x31>
mov    %r15d,%edi
call   1030 <semaphoreReserve@plt>
cmp    $0xffffffff,%eax
je     1177 <dsLock+0x47>
test   $0x2,%bl
je     1173 <dsLock+0x43>
mov    %r14d,%edi
call   1030 <semaphoreReserve@plt>
cmp    $0xffffffff,%eax
je     1177 <dsLock+0x47>
xor    %eax,%eax
jmp    117c <dsLock+0x4c>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @dsLock(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, 1
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %2
%17 = load i32, i32* %6
%18 = call i32 @semaphoreReserve(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, 2
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %22
%28 = load i32, i32* %7
%29 = call i32 @semaphoreReserve(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 @dsLock(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, 1
%7 = icmp eq i32 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = tail call i32 @semaphoreReserve(i32 %3)
%10 = icmp eq i32 %9, -1
br i1 %10, label %18, label %11
11: ; preds = %8, %2
%12 = and i32 %5, 2
%13 = icmp eq i32 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = tail call i32 @semaphoreReserve(i32 %4)
%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
}