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

/* Variables and functions */
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ exit(int);
int ftok(char *, char);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ semctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int semget(int, int, int);
int semop(int, struct sembuf *, int);
int get_semaphores_set(int nsems, int semflg) {
  if (nsems < 1) {
    printf("%s\n", "Wrong arg @nsem to get_semaphores_set()");
    exit(1);
  }
  key_t key;
  int semid;
  struct sembuf sb;
  if ((key = ftok("questions.txt", 'S')) == -1) {
    perror("ftok");
    printf("Did you create %s ?\n", "questions.txt");
    exit(1);
  }
  semid = semget(key, nsems, semflg);
  if (semid < 0) {
    printf("%s\n", "Remove a set of semaphores, please");
    exit(1);
  } else {
    printf("DBG: attached to semaphores set with id: %d\n", semid);
  }
  if (0 != semflg) {
    printf("DBG: Init semaphores with 1 (free)\n");
    sb.sem_flg = 0;
    sb.sem_op = 1;
    for (sb.sem_num = 0; sb.sem_num < nsems; sb.sem_num++) {
      if (semop(semid, &sb, 1) == -1) {
        perror("semop:");
        semctl(semid, 0, IPC_RMID);
        exit(1);
      }
    }
  }
  return semid;
}


x86 assembly (clang, -O0):
<get_semaphores_set>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
cmpl   $0x1,-0x4(%rbp)
jge    11a7 <get_semaphores_set+0x37>
lea    0xe71(%rip),%rdi
lea    0xe6e(%rip),%rsi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe7e(%rip),%rdi
mov    $0x53,%esi
call   1090 <ftok@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    11ef <get_semaphores_set+0x7f>
lea    0xe6f(%rip),%rdi
call   1050 <perror@plt>
lea    0xe68(%rip),%rdi
lea    0xe4e(%rip),%rsi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0xc(%rbp),%edi
mov    -0x4(%rbp),%esi
mov    -0x8(%rbp),%edx
call   1030 <semget@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1229 <get_semaphores_set+0xb9>
lea    0xdef(%rip),%rdi
lea    0xe3c(%rip),%rsi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%esi
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
cmp    -0x8(%rbp),%eax
je     12d0 <get_semaphores_set+0x160>
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x18(%rbp)
movl   $0x1,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    12cb <get_semaphores_set+0x15b>
mov    -0x10(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
cmp    $0xffffffff,%eax
jne    12b8 <get_semaphores_set+0x148>
lea    0xe32(%rip),%rdi
call   1050 <perror@plt>
mov    -0x10(%rbp),%edi
mov    0x2d3b(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <semctl@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    12bd <get_semaphores_set+0x14d>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1269 <get_semaphores_set+0xf9>
jmp    12d0 <get_semaphores_set+0x160>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_semaphores_set>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
test   %edi,%edi
jle    1256 <get_semaphores_set+0xd6>
mov    %esi,%r14d
mov    %edi,%ebx
lea    0xe8c(%rip),%rdi
mov    $0x53,%esi
call   10a0 <ftok@plt>
cmp    $0xffffffff,%eax
je     125f <get_semaphores_set+0xdf>
mov    %eax,%edi
mov    %ebx,%esi
mov    %r14d,%edx
call   1030 <semget@plt>
test   %eax,%eax
js     128a <get_semaphores_set+0x10a>
mov    %eax,%ebp
lea    0xea7(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
test   %r14d,%r14d
je     1223 <get_semaphores_set+0xa3>
lea    0xec6(%rip),%rdi
call   1040 <puts@plt>
movq   $0x0,0x8(%rsp)
movq   $0x1,(%rsp)
mov    %rsp,%r14
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1080 <semop@plt>
cmp    $0xffffffff,%eax
je     122e <get_semaphores_set+0xae>
mov    0x4(%rsp),%eax
add    $0x1,%eax
mov    %eax,0x4(%rsp)
cmp    %ebx,%eax
jl     1200 <get_semaphores_set+0x80>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe6b(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1070 <semctl@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xda3(%rip),%rdi
jmp    1291 <get_semaphores_set+0x111>
lea    0xdd0(%rip),%rdi
call   1060 <perror@plt>
lea    0xdc9(%rip),%rdi
lea    0xdaf(%rip),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xdbf(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.sembuf = type { i32, i32, i64 }
@.str = [4 x i8] c"%s\0A\00"
@.str.1 = [40 x i8] c"Wrong arg @nsem to get_semaphores_set()\00"
@.str.2 = [14 x i8] c"questions.txt\00"
@.str.3 = [5 x i8] c"ftok\00"
@.str.4 = [21 x i8] c"Did you create %s ?\0A\00"
@.str.5 = [35 x i8] c"Remove a set of semaphores, please\00"
@.str.6 = [45 x i8] c"DBG: attached to semaphores set with id: %d\0A\00"
@.str.7 = [36 x i8] c"DBG: Init semaphores with 1 (free)\0A\00"
@.str.8 = [7 x i8] c"semop:\00"
@IPC_RMID = global i32 0
define i32 @get_semaphores_set(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sembuf
store i32 %0, i32* %3
store i32 %1, i32* %4
%8 = load i32, i32* %3
%9 = icmp slt i32 %8, 1
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = call i32 @ftok(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8 signext 83)
store i32 %14, i32* %5
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %13
%21 = load i32, i32* %5
%22 = load i32, i32* %3
%23 = load i32, i32* %4
%24 = call i32 @semget(i32 %21, i32 %22, i32 %23)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %20
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %20
%31 = load i32, i32* %6
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0), i32 %31)
br label %33
33: ; preds = %30
%34 = load i32, i32* %4
%35 = icmp ne i32 0, %34
br i1 %35, label %36, label %62
36: ; preds = %33
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.7, i64 0, i64 0))
%38 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %7, i32 0, i32 2
store i64 0, i64* %38
%39 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %7, i32 0, i32 0
store i32 1, i32* %39
%40 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %7, i32 0, i32 1
store i32 0, i32* %40
br label %41
41: ; preds = %57, %36
%42 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %7, i32 0, i32 1
%43 = load i32, i32* %42
%44 = load i32, i32* %3
%45 = icmp slt i32 %43, %44
br i1 %45, label %46, label %61
46: ; preds = %41
%47 = load i32, i32* %6
%48 = call i32 @semop(i32 %47, %struct.sembuf* %7, i32 1)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %56
50: ; preds = %46
%51 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
%52 = load i32, i32* %6
%53 = load i32, i32* @IPC_RMID
%54 = call i32 @semctl(i32 %52, i32 0, i32 %53)
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %46
br label %57
57: ; preds = %56
%58 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %7, i32 0, i32 1
%59 = load i32, i32* %58
%60 = add nsw i32 %59, 1
store i32 %60, i32* %58
br label %41
61: ; preds = %41
br label %62
62: ; preds = %61, %33
%63 = load i32, i32* %6
ret i32 %63
}

LLVM IR (-O2):
%struct.sembuf = type { i32, i32, i64 }
@.str.1 = [40 x i8] c"Wrong arg @nsem to get_semaphores_set()\00"
@.str.2 = [14 x i8] c"questions.txt\00"
@.str.3 = [5 x i8] c"ftok\00"
@.str.4 = [21 x i8] c"Did you create %s ?\0A\00"
@.str.5 = [35 x i8] c"Remove a set of semaphores, please\00"
@.str.6 = [45 x i8] c"DBG: attached to semaphores set with id: %d\0A\00"
@.str.8 = [7 x i8] c"semop:\00"
@IPC_RMID = global i32 0
@str = [35 x i8] c"DBG: Init semaphores with 1 (free)\00"
define i32 @get_semaphores_set(i32 %0, i32 %1) {
%3 = alloca %struct.sembuf
%4 = icmp slt i32 %0, 1
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = bitcast %struct.sembuf* %3 to i8*
%10 = tail call i32 @ftok(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8 signext 83)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %8
%13 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %8
%17 = tail call i32 @semget(i32 %10, i32 %0, i32 %1)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %16
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0), i32 %17)
%24 = icmp eq i32 %1, 0
br i1 %24, label %42, label %25
25: ; preds = %22
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
%27 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %3, i64 0, i32 2
store i64 0, i64* %27
%28 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %3, i64 0, i32 0
store i32 1, i32* %28
%29 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %3, i64 0, i32 1
store i32 0, i32* %29
br label %30
30: ; preds = %25, %38
%31 = call i32 @semop(i32 %17, %struct.sembuf* %3, i32 1)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %38
33: ; preds = %30
%34 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
%35 = load i32, i32* @IPC_RMID
%36 = call i32 @semctl(i32 %17, i32 0, i32 %35)
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %30
%39 = load i32, i32* %29
%40 = add nsw i32 %39, 1
store i32 %40, i32* %29
%41 = icmp slt i32 %40, %0
br i1 %41, label %30, label %42
42: ; preds = %38, %22
ret i32 %17
}