#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_op;
  scalar_t__ sem_flg;
  scalar_t__ sem_num;
};
typedef int /*<<< orphan*/ key_t;

/* Variables and functions */
int EIDRM;
int EINTR;
int semget(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int semop(int, struct sembuf *, int);
int sm_lock(key_t key) {
  int id, rv;
  struct sembuf sb;
  do {
    ;
  } while (0);
  id = semget(key, 0, 0);
  if (id == -1) {
    ;
    return (-1);
  }
  sb.sem_num = 0;
  sb.sem_op = -1;
  sb.sem_flg = 0;
  while (1) {
    rv = semop(id, &sb, 1);
    if (rv == EINTR)
      continue;
    if (rv == EIDRM) {
      ;
      return (-1);
    }
    break;
  };
  return (0);
}


x86 assembly (clang, -O0):
<sm_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
jmp    1130 <sm_lock+0x10>
mov    -0x8(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1030 <semget@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1155 <sm_lock+0x35>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c1 <sm_lock+0xa1>
movq   $0x0,-0x18(%rbp)
movl   $0xffffffff,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x1,%edx
call   1040 <semop@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    0x2e4e(%rip),%rcx
cmp    (%rcx),%eax
jne    1197 <sm_lock+0x77>
jmp    116c <sm_lock+0x4c>
mov    -0x10(%rbp),%eax
mov    0x2e3f(%rip),%rcx
cmp    (%rcx),%eax
jne    11b5 <sm_lock+0x95>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c1 <sm_lock+0xa1>
jmp    11ba <sm_lock+0x9a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sm_lock>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
xor    %esi,%esi
xor    %edx,%edx
call   1030 <semget@plt>
cmp    $0xffffffff,%eax
je     1188 <sm_lock+0x68>
mov    %eax,%ebx
movl   $0xffffffff,0x8(%rsp)
ff
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
lea    0x8(%rsp),%r14
mov    0x2e83(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <semop@plt>
cmp    (%r15),%eax
je     1160 <sm_lock+0x40>
mov    %eax,%ecx
mov    0x2e63(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%ecx
sete   %al
neg    %eax
jmp    118d <sm_lock+0x6d>
mov    $0xffffffff,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sembuf = type { i32, i64, i64 }
@EINTR = global i32 0
@EIDRM = global i32 0
define i32 @sm_lock(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sembuf
store i32 %0, i32* %3
br label %7
7: ; preds = %1
br label %8
8: ; preds = %7
%9 = load i32, i32* %3
%10 = call i32 @semget(i32 %9, i32 0, i32 0)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %8
store i32 -1, i32* %2
br label %32
14: ; preds = %8
%15 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %6, i32 0, i32 2
store i64 0, i64* %15
%16 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %6, i32 0, i32 0
store i32 -1, i32* %16
%17 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %6, i32 0, i32 1
store i64 0, i64* %17
br label %18
18: ; preds = %14, %24
%19 = load i32, i32* %4
%20 = call i32 @semop(i32 %19, %struct.sembuf* %6, i32 1)
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = load i32, i32* @EINTR
%23 = icmp eq i32 %21, %22
br i1 %23, label %24, label %25
24: ; preds = %18
br label %18
25: ; preds = %18
%26 = load i32, i32* %5
%27 = load i32, i32* @EIDRM
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %25
store i32 -1, i32* %2
br label %32
30: ; preds = %25
br label %31
31: ; preds = %30
store i32 0, i32* %2
br label %32
32: ; preds = %31, %29, %13
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
%struct.sembuf = type { i32, i64, i64 }
@EINTR = global i32 0
@EIDRM = global i32 0
define i32 @sm_lock(i32 %0) {
%2 = alloca %struct.sembuf
%3 = bitcast %struct.sembuf* %2 to i8*
%4 = tail call i32 @semget(i32 %0, i32 0, i32 0)
%5 = icmp eq i32 %4, -1
br i1 %5, label %18, label %6
6: ; preds = %1
%7 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %2, i64 0, i32 0
store i32 -1, i32* %7
%8 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %2, i64 0, i32 1
%9 = bitcast i64* %8 to <2 x i64>*
store <2 x i64> zeroinitializer, <2 x i64>* %9
br label %10
10: ; preds = %10, %6
%11 = call i32 @semop(i32 %4, %struct.sembuf* %2, i32 1)
%12 = load i32, i32* @EINTR
%13 = icmp eq i32 %11, %12
br i1 %13, label %10, label %14
14: ; preds = %10
%15 = load i32, i32* @EIDRM
%16 = icmp eq i32 %11, %15
%17 = sext i1 %16 to i32
br label %18
18: ; preds = %14, %1
%19 = phi i32 [ -1, %1 ], [ %17, %14 ]
ret i32 %19
}