#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 */
typedef int /*<<< orphan*/ key_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ atoi(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ ftok(char *, char);
int getopt(int, char **, char *);
int /*<<< orphan*/ optarg;
int /*<<< orphan*/ sem_create(int /*<<< orphan*/);
int /*<<< orphan*/ sem_d(int);
int /*<<< orphan*/ sem_getmode(int);
int /*<<< orphan*/ sem_getval(int);
int sem_open(int /*<<< orphan*/);
int /*<<< orphan*/ sem_p(int);
int /*<<< orphan*/ sem_setmode(int, char *);
int /*<<< orphan*/ sem_setval(int, int /*<<< orphan*/);
int /*<<< orphan*/ sem_v(int);
int /*<<< orphan*/ usage();
int main(int argc, char *argv[]) {
  int opt;
  opt = getopt(argc, argv, "cdpvs:gfm:");
  if (opt == '?')
    exit(EXIT_FAILURE);
  if (opt == -1) {
    usage();
    exit(EXIT_FAILURE);
  }
  key_t key = ftok(".", 's');
  int semid;
  switch (opt) {
  case 'c':
    sem_create(key);
    break;
  case 'p':
    semid = sem_open(key);
    sem_p(semid);
    sem_getval(semid);
    break;
  case 'v':
    semid = sem_open(key);
    sem_v(semid);
    sem_getval(semid);
    break;
  case 'd':
    semid = sem_open(key);
    sem_d(semid);
    break;
  case 's':
    semid = sem_open(key);
    sem_setval(semid, atoi(optarg));
    break;
  case 'g':
    semid = sem_open(key);
    sem_getval(semid);
    break;
  case 'f':
    semid = sem_open(key);
    sem_getmode(semid);
    break;
  case 'm':
    semid = sem_open(key);
    sem_setmode(semid, argv[2]);
    break;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe4c(%rip),%rdx
call   10b0 <getopt@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x3f,-0x14(%rbp)
jne    1224 <main+0x44>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
cmpl   $0xffffffff,-0x14(%rbp)
jne    1243 <main+0x63>
mov    $0x0,%al
call   1090 <usage@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
lea    0xe11(%rip),%rdi
mov    $0x73,%esi
call   1100 <ftok@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffff9d,%eax
mov    %eax,%ecx
mov    %rcx,-0x28(%rbp)
sub    $0x13,%eax
ja     135e <main+0x17e>
mov    -0x28(%rbp),%rax
lea    0xd89(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x18(%rbp),%edi
call   1070 <sem_create@plt>
jmp    135e <main+0x17e>
mov    -0x18(%rbp),%edi
call   1060 <sem_open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   10e0 <sem_p@plt>
mov    -0x1c(%rbp),%edi
call   10a0 <sem_getval@plt>
jmp    135e <main+0x17e>
mov    -0x18(%rbp),%edi
call   1060 <sem_open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <sem_v@plt>
mov    -0x1c(%rbp),%edi
call   10a0 <sem_getval@plt>
jmp    135e <main+0x17e>
mov    -0x18(%rbp),%edi
call   1060 <sem_open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   10f0 <sem_d@plt>
jmp    135e <main+0x17e>
mov    -0x18(%rbp),%edi
call   1060 <sem_open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
call   10c0 <atoi@plt>
mov    -0x2c(%rbp),%edi
mov    %eax,%esi
call   1080 <sem_setval@plt>
jmp    135e <main+0x17e>
mov    -0x18(%rbp),%edi
call   1060 <sem_open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   10a0 <sem_getval@plt>
jmp    135e <main+0x17e>
mov    -0x18(%rbp),%edi
call   1060 <sem_open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <sem_getmode@plt>
jmp    135e <main+0x17e>
mov    -0x18(%rbp),%edi
call   1060 <sem_open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
call   1030 <sem_setmode@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
lea    0xe62(%rip),%rdx
call   10b0 <getopt@plt>
cmp    $0xffffffff,%eax
je     12d4 <main+0xf4>
mov    %eax,%ebx
cmp    $0x3f,%eax
je     12db <main+0xfb>
lea    0xe4d(%rip),%rdi
mov    $0x73,%esi
call   1100 <ftok@plt>
add    $0xffffff9d,%ebx
cmp    $0x13,%ebx
ja     12ca <main+0xea>
lea    0xdd5(%rip),%rcx
movslq (%rcx,%rbx,4),%rdx
add    %rcx,%rdx
jmp    *%rdx
mov    %eax,%edi
call   1070 <sem_create@plt>
jmp    12ca <main+0xea>
mov    %eax,%edi
call   1060 <sem_open@plt>
mov    %eax,%edi
call   10f0 <sem_d@plt>
jmp    12ca <main+0xea>
mov    %eax,%edi
call   1060 <sem_open@plt>
mov    %eax,%edi
call   1050 <sem_getmode@plt>
jmp    12ca <main+0xea>
mov    %eax,%edi
call   1060 <sem_open@plt>
mov    %eax,%edi
jmp    12c5 <main+0xe5>
mov    %eax,%edi
call   1060 <sem_open@plt>
mov    0x10(%r14),%rsi
mov    %eax,%edi
call   1030 <sem_setmode@plt>
jmp    12ca <main+0xea>
mov    %eax,%edi
call   1060 <sem_open@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   10e0 <sem_p@plt>
jmp    12c3 <main+0xe3>
mov    %eax,%edi
call   1060 <sem_open@plt>
mov    %eax,%ebx
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
call   10c0 <atoi@plt>
mov    %ebx,%edi
mov    %eax,%esi
call   1080 <sem_setval@plt>
jmp    12ca <main+0xea>
mov    %eax,%edi
call   1060 <sem_open@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1040 <sem_v@plt>
mov    %ebx,%edi
call   10a0 <sem_getval@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %eax,%eax
call   1090 <usage@plt>
mov    0x2cf6(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"cdpvs:gfm:\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c".\00"
@optarg = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = load i8**, i8*** %5
%11 = call i32 @getopt(i32 %9, i8** %10, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, 63
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %2
%18 = load i32, i32* %6
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = call i32 (...) @usage()
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %17
%25 = call i32 @ftok(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8 signext 115)
store i32 %25, i32* %7
%26 = load i32, i32* %6
switch i32 %26, label %74 [
i32 99, label %27
i32 112, label %30
i32 118, label %37
i32 100, label %44
i32 115, label %49
i32 103, label %56
i32 102, label %61
i32 109, label %66
]
27: ; preds = %24
%28 = load i32, i32* %7
%29 = call i32 @sem_create(i32 %28)
br label %74
30: ; preds = %24
%31 = load i32, i32* %7
%32 = call i32 @sem_open(i32 %31)
store i32 %32, i32* %8
%33 = load i32, i32* %8
%34 = call i32 @sem_p(i32 %33)
%35 = load i32, i32* %8
%36 = call i32 @sem_getval(i32 %35)
br label %74
37: ; preds = %24
%38 = load i32, i32* %7
%39 = call i32 @sem_open(i32 %38)
store i32 %39, i32* %8
%40 = load i32, i32* %8
%41 = call i32 @sem_v(i32 %40)
%42 = load i32, i32* %8
%43 = call i32 @sem_getval(i32 %42)
br label %74
44: ; preds = %24
%45 = load i32, i32* %7
%46 = call i32 @sem_open(i32 %45)
store i32 %46, i32* %8
%47 = load i32, i32* %8
%48 = call i32 @sem_d(i32 %47)
br label %74
49: ; preds = %24
%50 = load i32, i32* %7
%51 = call i32 @sem_open(i32 %50)
store i32 %51, i32* %8
%52 = load i32, i32* %8
%53 = load i32, i32* @optarg
%54 = call i32 @atoi(i32 %53)
%55 = call i32 @sem_setval(i32 %52, i32 %54)
br label %74
56: ; preds = %24
%57 = load i32, i32* %7
%58 = call i32 @sem_open(i32 %57)
store i32 %58, i32* %8
%59 = load i32, i32* %8
%60 = call i32 @sem_getval(i32 %59)
br label %74
61: ; preds = %24
%62 = load i32, i32* %7
%63 = call i32 @sem_open(i32 %62)
store i32 %63, i32* %8
%64 = load i32, i32* %8
%65 = call i32 @sem_getmode(i32 %64)
br label %74
66: ; preds = %24
%67 = load i32, i32* %7
%68 = call i32 @sem_open(i32 %67)
store i32 %68, i32* %8
%69 = load i32, i32* %8
%70 = load i8**, i8*** %5
%71 = getelementptr inbounds i8*, i8** %70, i64 2
%72 = load i8*, i8** %71
%73 = call i32 @sem_setmode(i32 %69, i8* %72)
br label %74
74: ; preds = %24, %66, %61, %56, %49, %44, %37, %30, %27
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"cdpvs:gfm:\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c".\00"
@optarg = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
switch i32 %3, label %11 [
i32 63, label %4
i32 -1, label %7
]
4: ; preds = %2
%5 = load i32, i32* @EXIT_FAILURE
%6 = tail call i32 @exit(i32 %5)
unreachable
7: ; preds = %2
%8 = tail call i32 (...) @usage()
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %2
%12 = tail call i32 @ftok(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8 signext 115)
switch i32 %3, label %42 [
i32 99, label %13
i32 112, label %15
i32 118, label %19
i32 100, label %23
i32 115, label %26
i32 103, label %31
i32 102, label %34
i32 109, label %37
]
13: ; preds = %11
%14 = tail call i32 @sem_create(i32 %12)
br label %42
15: ; preds = %11
%16 = tail call i32 @sem_open(i32 %12)
%17 = tail call i32 @sem_p(i32 %16)
%18 = tail call i32 @sem_getval(i32 %16)
br label %42
19: ; preds = %11
%20 = tail call i32 @sem_open(i32 %12)
%21 = tail call i32 @sem_v(i32 %20)
%22 = tail call i32 @sem_getval(i32 %20)
br label %42
23: ; preds = %11
%24 = tail call i32 @sem_open(i32 %12)
%25 = tail call i32 @sem_d(i32 %24)
br label %42
26: ; preds = %11
%27 = tail call i32 @sem_open(i32 %12)
%28 = load i32, i32* @optarg
%29 = tail call i32 @atoi(i32 %28)
%30 = tail call i32 @sem_setval(i32 %27, i32 %29)
br label %42
31: ; preds = %11
%32 = tail call i32 @sem_open(i32 %12)
%33 = tail call i32 @sem_getval(i32 %32)
br label %42
34: ; preds = %11
%35 = tail call i32 @sem_open(i32 %12)
%36 = tail call i32 @sem_getmode(i32 %35)
br label %42
37: ; preds = %11
%38 = tail call i32 @sem_open(i32 %12)
%39 = getelementptr inbounds i8*, i8** %1, i64 2
%40 = load i8*, i8** %39
%41 = tail call i32 @sem_setmode(i32 %38, i8* %40)
br label %42
42: ; preds = %11, %37, %34, %31, %26, %23, %19, %15, %13
ret i32 0
}