#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;
};

/* Variables and functions */
#define E2BIG 133
#define EACCES 132
#define EAGAIN 131
#define EIDRM 130
#define EINTR 129
#define EINVAL 128
int errno;
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ semop(int, struct sembuf *, int);
int pop(int semid) {
  struct sembuf buf;
  buf.sem_num = 0;
  buf.sem_op = -1;
  buf.sem_flg = 0;
  if (semop(semid, &buf, 1)) {
    switch (errno) {
    case E2BIG:
      printf("Too many operations!\n");
      break;
    case EACCES:
      printf("Insufficient permissions!\n");
      break;
    case EAGAIN:
      printf("IPC_NOWAIT\n");
      break;
    case EINTR:
      printf("Semop syscall was interrupted by a signal\n");
      break;
    case EIDRM:
      printf("Semaphor has been removed removed!\n");
      break;
    case EINVAL:
      printf("No corresponding semaphor for id: %d\n", semid);
      break;
    }
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<pop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0xffffffff,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1040 <semop@plt>
cmp    $0x0,%rax
je     1208 <pop+0xe8>
mov    0x2e7c(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
mov    %eax,%ecx
mov    %rcx,-0x28(%rbp)
sub    $0x5,%eax
ja     11fc <pop+0xdc>
mov    -0x28(%rbp),%rax
lea    0xe7d(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xe85(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11fc <pop+0xdc>
lea    0xe88(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11fc <pop+0xdc>
lea    0xe90(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11fc <pop+0xdc>
lea    0xe89(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11fc <pop+0xdc>
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11fc <pop+0xdc>
mov    -0x8(%rbp),%esi
lea    0xeaf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120f <pop+0xef>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pop>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
movl   $0xffffffff,(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x8(%rsp)
mov    %rsp,%rsi
mov    $0x1,%edx
call   1050 <semop@plt>
test   %rax,%rax
je     1191 <pop+0x61>
mov    0x2e80(%rip),%rax
mov    (%rax),%eax
mov    $0xffffffff,%ebx
add    $0xffffff80,%eax
cmp    $0x5,%eax
ja     11c5 <pop+0x95>
lea    0xe8a(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xe92(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11c5 <pop+0x95>
xor    %ebx,%ebx
jmp    11c5 <pop+0x95>
lea    0xec5(%rip),%rdi
jmp    11c0 <pop+0x90>
lea    0xe99(%rip),%rdi
jmp    11c0 <pop+0x90>
lea    0xedd(%rip),%rdi
jmp    11c0 <pop+0x90>
lea    0xedf(%rip),%rdi
jmp    11c0 <pop+0x90>
lea    0xef0(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sembuf = type { i32, i64, i64 }
@errno = global i32 0
@.str = [22 x i8] c"Too many operations!\0A\00"
@.str.1 = [27 x i8] c"Insufficient permissions!\0A\00"
@.str.2 = [12 x i8] c"IPC_NOWAIT\0A\00"
@.str.3 = [43 x i8] c"Semop syscall was interrupted by a signal\0A\00"
@.str.4 = [36 x i8] c"Semaphor has been removed removed!\0A\00"
@.str.5 = [38 x i8] c"No corresponding semaphor for id: %d\0A\00"
define i32 @pop(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sembuf
store i32 %0, i32* %3
%5 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %4, i32 0, i32 2
store i64 0, i64* %5
%6 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %4, i32 0, i32 0
store i32 -1, i32* %6
%7 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %4, i32 0, i32 1
store i64 0, i64* %7
%8 = load i32, i32* %3
%9 = call i64 @semop(i32 %8, %struct.sembuf* %4, i32 1)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %27
11: ; preds = %1
%12 = load i32, i32* @errno
switch i32 %12, label %26 [
i32 133, label %13
i32 132, label %15
i32 131, label %17
i32 129, label %19
i32 130, label %21
i32 128, label %23
]
13: ; preds = %11
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %26
15: ; preds = %11
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %26
17: ; preds = %11
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %26
19: ; preds = %11
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0))
br label %26
21: ; preds = %11
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
br label %26
23: ; preds = %11
%24 = load i32, i32* %3
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i32 %24)
br label %26
26: ; preds = %11, %23, %21, %19, %17, %15, %13
store i32 -1, i32* %2
br label %28
27: ; preds = %1
store i32 0, i32* %2
br label %28
28: ; preds = %27, %26
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
%struct.sembuf = type { i32, i64, i64 }
@errno = global i32 0
@.str.5 = [38 x i8] c"No corresponding semaphor for id: %d\0A\00"
@str = [35 x i8] c"Semaphor has been removed removed!\00"
@str.6 = [42 x i8] c"Semop syscall was interrupted by a signal\00"
@str.7 = [11 x i8] c"IPC_NOWAIT\00"
@str.8 = [26 x i8] c"Insufficient permissions!\00"
@str.9 = [21 x i8] c"Too many operations!\00"
define i32 @pop(i32 %0) {
%2 = alloca %struct.sembuf
%3 = bitcast %struct.sembuf* %2 to i8*
%4 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %2, i64 0, i32 0
store i32 -1, i32* %4
%5 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %2, i64 0, i32 1
%6 = bitcast i64* %5 to <2 x i64>*
store <2 x i64> zeroinitializer, <2 x i64>* %6
%7 = call i64 @semop(i32 %0, %struct.sembuf* %2, i32 1)
%8 = icmp eq i64 %7, 0
br i1 %8, label %23, label %9
9: ; preds = %1
%10 = load i32, i32* @errno
switch i32 %10, label %23 [
i32 133, label %11
i32 132, label %13
i32 131, label %15
i32 129, label %17
i32 130, label %19
i32 128, label %21
]
11: ; preds = %9
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.9, i64 0, i64 0))
br label %23
13: ; preds = %9
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.8, i64 0, i64 0))
br label %23
15: ; preds = %9
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.7, i64 0, i64 0))
br label %23
17: ; preds = %9
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str.6, i64 0, i64 0))
br label %23
19: ; preds = %9
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
br label %23
21: ; preds = %9
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i32 %0)
br label %23
23: ; preds = %1, %11, %13, %15, %17, %19, %21, %9
%24 = phi i32 [ -1, %9 ], [ -1, %21 ], [ -1, %19 ], [ -1, %17 ], [ -1, %15 ], [ -1, %13 ], [ -1, %11 ], [ 0, %1 ]
ret i32 %24
}