#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 */
scalar_t__ EIDRM;
int semget(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ semop(int, struct sembuf *, int);
int sm_unlock(key_t key) {
  int id;
  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;
  if (semop(id, &sb, 1) == EIDRM) {
    ;
    return (-1);
  };
  return (0);
}


x86 assembly (clang, -O0):
<sm_unlock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
jmp    1130 <sm_unlock+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_unlock+0x35>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a0 <sm_unlock+0x80>
movq   $0x0,-0x18(%rbp)
movl   $0x1,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x1,%edx
call   1040 <semop@plt>
mov    0x2e5c(%rip),%rcx
cmp    (%rcx),%rax
jne    1199 <sm_unlock+0x79>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a0 <sm_unlock+0x80>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sm_unlock>:
sub    $0x18,%rsp
xor    %esi,%esi
xor    %edx,%edx
call   1030 <semget@plt>
cmp    $0xffffffff,%eax
je     1169 <sm_unlock+0x49>
movl   $0x1,(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x8(%rsp)
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x1,%edx
call   1040 <semop@plt>
mov    %rax,%rcx
mov    0x2e86(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
sete   %al
neg    %eax
add    $0x18,%rsp
ret
mov    $0xffffffff,%eax
add    $0x18,%rsp
ret

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

LLVM IR (-O2):
%struct.sembuf = type { i32, i64, i64 }
@EIDRM = global i64 0
define i32 @sm_unlock(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 %14, 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
%10 = call i64 @semop(i32 %4, %struct.sembuf* %2, i32 1)
%11 = load i64, i64* @EIDRM
%12 = icmp eq i64 %10, %11
%13 = sext i1 %12 to i32
br label %14
14: ; preds = %6, %1
%15 = phi i32 [ -1, %1 ], [ %13, %6 ]
ret i32 %15
}