#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*/ atoi(char *);
int /*<<< orphan*/ changemode(int, char *);
int /*<<< orphan*/ createsem(int *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ ftok(char *, char);
int /*<<< orphan*/ locksem(int, int /*<<< orphan*/);
int /*<<< orphan*/ opensem(int *, int /*<<< orphan*/);
int /*<<< orphan*/ removesem(int);
int tolower(char);
int /*<<< orphan*/ unlocksem(int, int /*<<< orphan*/);
int /*<<< orphan*/ usage();
int main(int argc, char *argv[]) {
  key_t key;
  int semset_id;
  if (argc == 1)
    usage();
  key = ftok(".", 's');
  switch (tolower(argv[1][0])) {
  case 'c':
    if (argc != 3)
      usage();
    createsem(&semset_id, key, atoi(argv[2]));
    break;
  case 'l':
    if (argc != 3)
      usage();
    opensem(&semset_id, key);
    locksem(semset_id, atoi(argv[2]));
    break;
  case 'u':
    if (argc != 3)
      usage();
    opensem(&semset_id, key);
    unlocksem(semset_id, atoi(argv[2]));
    break;
  case 'd':
    opensem(&semset_id, key);
    removesem(semset_id);
    break;
  case 'm':
    opensem(&semset_id, key);
    changemode(semset_id, argv[2]);
    break;
  default:
    usage();
  }
  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)
cmpl   $0x1,-0x8(%rbp)
jne    11c7 <main+0x27>
mov    $0x0,%al
call   1090 <usage@plt>
lea    0xe7e(%rip),%rdi
mov    $0x73,%esi
call   10c0 <ftok@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movsbl (%rax),%edi
call   1070 <tolower@plt>
add    $0xffffff9d,%eax
mov    %eax,%ecx
mov    %rcx,-0x20(%rbp)
sub    $0x12,%eax
ja     1300 <main+0x160>
mov    -0x20(%rbp),%rax
lea    0xdf8(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
cmpl   $0x3,-0x8(%rbp)
je     1222 <main+0x82>
mov    $0x0,%al
call   1090 <usage@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10a0 <atoi@plt>
mov    -0x24(%rbp),%esi
mov    %eax,%edx
lea    -0x18(%rbp),%rdi
call   1030 <createsem@plt>
jmp    1307 <main+0x167>
cmpl   $0x3,-0x8(%rbp)
je     1259 <main+0xb9>
mov    $0x0,%al
call   1090 <usage@plt>
mov    -0x14(%rbp),%esi
lea    -0x18(%rbp),%rdi
call   1050 <opensem@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10a0 <atoi@plt>
mov    -0x28(%rbp),%edi
mov    %eax,%esi
call   10b0 <locksem@plt>
jmp    1307 <main+0x167>
cmpl   $0x3,-0x8(%rbp)
je     1298 <main+0xf8>
mov    $0x0,%al
call   1090 <usage@plt>
mov    -0x14(%rbp),%esi
lea    -0x18(%rbp),%rdi
call   1050 <opensem@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10a0 <atoi@plt>
mov    -0x2c(%rbp),%edi
mov    %eax,%esi
call   1080 <unlocksem@plt>
jmp    1307 <main+0x167>
mov    -0x14(%rbp),%esi
lea    -0x18(%rbp),%rdi
call   1050 <opensem@plt>
mov    -0x18(%rbp),%edi
call   1040 <removesem@plt>
jmp    1307 <main+0x167>
mov    -0x14(%rbp),%esi
lea    -0x18(%rbp),%rdi
call   1050 <opensem@plt>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
call   1060 <changemode@plt>
jmp    1307 <main+0x167>
mov    $0x0,%al
call   1090 <usage@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
cmp    $0x1,%edi
jne    11b9 <main+0x19>
xor    %eax,%eax
call   1090 <usage@plt>
lea    0xe8c(%rip),%rdi
mov    $0x73,%esi
call   10c0 <ftok@plt>
mov    %eax,%r14d
mov    0x8(%rbx),%rax
movsbl (%rax),%edi
call   1070 <tolower@plt>
add    $0xffffff9d,%eax
cmp    $0x12,%eax
ja     1235 <main+0x95>
lea    0xe18(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
cmp    $0x3,%ebp
je     11fd <main+0x5d>
xor    %eax,%eax
call   1090 <usage@plt>
mov    0x10(%rbx),%rdi
call   10a0 <atoi@plt>
lea    0xc(%rsp),%rdi
mov    %r14d,%esi
mov    %eax,%edx
call   1030 <createsem@plt>
jmp    12ba <main+0x11a>
lea    0xc(%rsp),%rdi
mov    %r14d,%esi
call   1050 <opensem@plt>
mov    0xc(%rsp),%edi
call   1040 <removesem@plt>
jmp    12ba <main+0x11a>
xor    %eax,%eax
call   1090 <usage@plt>
jmp    12ba <main+0x11a>
cmp    $0x3,%ebp
je     124a <main+0xaa>
xor    %eax,%eax
call   1090 <usage@plt>
lea    0xc(%rsp),%rdi
mov    %r14d,%esi
call   1050 <opensem@plt>
mov    0xc(%rsp),%ebp
mov    0x10(%rbx),%rdi
call   10a0 <atoi@plt>
mov    %ebp,%edi
mov    %eax,%esi
call   10b0 <locksem@plt>
jmp    12ba <main+0x11a>
lea    0xc(%rsp),%rdi
mov    %r14d,%esi
call   1050 <opensem@plt>
mov    0xc(%rsp),%edi
mov    0x10(%rbx),%rsi
call   1060 <changemode@plt>
jmp    12ba <main+0x11a>
cmp    $0x3,%ebp
je     1297 <main+0xf7>
xor    %eax,%eax
call   1090 <usage@plt>
lea    0xc(%rsp),%rdi
mov    %r14d,%esi
call   1050 <opensem@plt>
mov    0xc(%rsp),%ebp
mov    0x10(%rbx),%rdi
call   10a0 <atoi@plt>
mov    %ebp,%edi
mov    %eax,%esi
call   1080 <unlocksem@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 (...) @usage()
br label %12
12: ; preds = %10, %2
%13 = call i32 @ftok(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8 signext 115)
store i32 %13, i32* %6
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = getelementptr inbounds i8, i8* %16, i64 0
%18 = load i8, i8* %17
%19 = call i32 @tolower(i8 signext %18)
switch i32 %19, label %73 [
i32 99, label %20
i32 108, label %32
i32 117, label %46
i32 100, label %60
i32 109, label %65
]
20: ; preds = %12
%21 = load i32, i32* %4
%22 = icmp ne i32 %21, 3
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 (...) @usage()
br label %25
25: ; preds = %23, %20
%26 = load i32, i32* %6
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
%30 = call i32 @atoi(i8* %29)
%31 = call i32 @createsem(i32* %7, i32 %26, i32 %30)
br label %75
32: ; preds = %12
%33 = load i32, i32* %4
%34 = icmp ne i32 %33, 3
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = call i32 (...) @usage()
br label %37
37: ; preds = %35, %32
%38 = load i32, i32* %6
%39 = call i32 @opensem(i32* %7, i32 %38)
%40 = load i32, i32* %7
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 2
%43 = load i8*, i8** %42
%44 = call i32 @atoi(i8* %43)
%45 = call i32 @locksem(i32 %40, i32 %44)
br label %75
46: ; preds = %12
%47 = load i32, i32* %4
%48 = icmp ne i32 %47, 3
br i1 %48, label %49, label %51
49: ; preds = %46
%50 = call i32 (...) @usage()
br label %51
51: ; preds = %49, %46
%52 = load i32, i32* %6
%53 = call i32 @opensem(i32* %7, i32 %52)
%54 = load i32, i32* %7
%55 = load i8**, i8*** %5
%56 = getelementptr inbounds i8*, i8** %55, i64 2
%57 = load i8*, i8** %56
%58 = call i32 @atoi(i8* %57)
%59 = call i32 @unlocksem(i32 %54, i32 %58)
br label %75
60: ; preds = %12
%61 = load i32, i32* %6
%62 = call i32 @opensem(i32* %7, i32 %61)
%63 = load i32, i32* %7
%64 = call i32 @removesem(i32 %63)
br label %75
65: ; preds = %12
%66 = load i32, i32* %6
%67 = call i32 @opensem(i32* %7, i32 %66)
%68 = load i32, i32* %7
%69 = load i8**, i8*** %5
%70 = getelementptr inbounds i8*, i8** %69, i64 2
%71 = load i8*, i8** %70
%72 = call i32 @changemode(i32 %68, i8* %71)
br label %75
73: ; preds = %12
%74 = call i32 (...) @usage()
br label %75
75: ; preds = %73, %65, %60, %51, %37, %25
ret i32 0
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp eq i32 %0, 1
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 (...) @usage()
br label %8
8: ; preds = %6, %2
%9 = tail call i32 @ftok(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8 signext 115)
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = load i8, i8* %11
%13 = tail call i32 @tolower(i8 signext %12)
switch i32 %13, label %55 [
i32 99, label %14
i32 108, label %23
i32 117, label %34
i32 100, label %45
i32 109, label %49
]
14: ; preds = %8
%15 = icmp eq i32 %0, 3
br i1 %15, label %18, label %16
16: ; preds = %14
%17 = tail call i32 (...) @usage()
br label %18
18: ; preds = %16, %14
%19 = getelementptr inbounds i8*, i8** %1, i64 2
%20 = load i8*, i8** %19
%21 = tail call i32 @atoi(i8* %20)
%22 = call i32 @createsem(i32* %3, i32 %9, i32 %21)
br label %57
23: ; preds = %8
%24 = icmp eq i32 %0, 3
br i1 %24, label %27, label %25
25: ; preds = %23
%26 = tail call i32 (...) @usage()
br label %27
27: ; preds = %25, %23
%28 = call i32 @opensem(i32* %3, i32 %9)
%29 = load i32, i32* %3
%30 = getelementptr inbounds i8*, i8** %1, i64 2
%31 = load i8*, i8** %30
%32 = call i32 @atoi(i8* %31)
%33 = call i32 @locksem(i32 %29, i32 %32)
br label %57
34: ; preds = %8
%35 = icmp eq i32 %0, 3
br i1 %35, label %38, label %36
36: ; preds = %34
%37 = tail call i32 (...) @usage()
br label %38
38: ; preds = %36, %34
%39 = call i32 @opensem(i32* %3, i32 %9)
%40 = load i32, i32* %3
%41 = getelementptr inbounds i8*, i8** %1, i64 2
%42 = load i8*, i8** %41
%43 = call i32 @atoi(i8* %42)
%44 = call i32 @unlocksem(i32 %40, i32 %43)
br label %57
45: ; preds = %8
%46 = call i32 @opensem(i32* %3, i32 %9)
%47 = load i32, i32* %3
%48 = call i32 @removesem(i32 %47)
br label %57
49: ; preds = %8
%50 = call i32 @opensem(i32* %3, i32 %9)
%51 = load i32, i32* %3
%52 = getelementptr inbounds i8*, i8** %1, i64 2
%53 = load i8*, i8** %52
%54 = call i32 @changemode(i32 %51, i8* %53)
br label %57
55: ; preds = %8
%56 = tail call i32 (...) @usage()
br label %57
57: ; preds = %55, %49, %45, %38, %27, %18
ret i32 0
}