#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 IPC_CREAT;
int /*<<< orphan*/ ftok(char *, char);
int mrecv(int);
int msend(int, char *);
int msgget(int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ usage();
int main(int argc, char **argv) {
  if (argc < 2) {
    usage();
    return -1;
  }
  int msgid;
  key_t key;
  key = ftok("/etc/", 'a');
  msgid = msgget(key, IPC_CREAT | 00666);
  if (msgid == -1) {
    perror("create msg error");
    return -1;
  }
  if (strcmp(argv[1], "send") == 0 || strcmp(argv[1], "recv") == 0) {
    if (!strcmp(argv[1], "send")) {
      if (argc != 3) {
        usage();
        return -1;
      }
      return msend(msgid, argv[2]);
    } else {
      return mrecv(msgid);
    }
  } else {
    usage();
    return -1;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11a3 <main+0x33>
mov    $0x0,%al
call   1060 <usage@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a0 <main+0x130>
lea    0xe56(%rip),%rdi
mov    $0x61,%esi
call   1090 <ftok@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
or     $0x1b6,%esi
call   1030 <msgget@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11f3 <main+0x83>
lea    0xe24(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a0 <main+0x130>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe15(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     122f <main+0xbf>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdfc(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    1292 <main+0x122>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdd9(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    1282 <main+0x112>
cmpl   $0x3,-0x8(%rbp)
je     126a <main+0xfa>
mov    $0x0,%al
call   1060 <usage@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a0 <main+0x130>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
call   1080 <msend@plt>
mov    %eax,-0x4(%rbp)
jmp    12a0 <main+0x130>
mov    -0x14(%rbp),%edi
call   1040 <mrecv@plt>
mov    %eax,-0x4(%rbp)
jmp    12a0 <main+0x130>
mov    $0x0,%al
call   1060 <usage@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x1,%edi
jle    1210 <main+0xa0>
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xe77(%rip),%rdi
mov    $0x61,%esi
call   1090 <ftok@plt>
mov    0x2e46(%rip),%rcx
mov    $0x1b6,%esi
or     (%rcx),%esi
mov    %eax,%edi
call   1030 <msgget@plt>
cmp    $0xffffffff,%eax
je     1221 <main+0xb1>
mov    %eax,%r14d
mov    0x8(%rbx),%rdi
lea    0xe5c(%rip),%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11da <main+0x6a>
mov    0x8(%rbx),%rdi
lea    0xe4c(%rip),%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    1210 <main+0xa0>
mov    0x8(%rbx),%rdi
lea    0xe32(%rip),%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11fb <main+0x8b>
mov    %r14d,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <mrecv@plt>
cmp    $0x3,%ebp
jne    1210 <main+0xa0>
mov    0x10(%rbx),%rsi
mov    %r14d,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1080 <msend@plt>
xor    %eax,%eax
call   1060 <usage@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdde(%rip),%rdi
call   1070 <perror@plt>
jmp    1217 <main+0xa7>

LLVM IR (-O0):
@.str = [6 x i8] c"/etc/\00"
@IPC_CREAT = global i32 0
@.str.1 = [17 x i8] c"create msg error\00"
@.str.2 = [5 x i8] c"send\00"
@.str.3 = [5 x i8] c"recv\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 slt i32 %8, 2
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 (...) @usage()
store i32 -1, i32* %3
br label %56
12: ; preds = %2
%13 = call i32 @ftok(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8 signext 97)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = load i32, i32* @IPC_CREAT
%16 = or i32 %15, 438
%17 = call i32 @msgget(i32 %14, i32 %16)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %12
%21 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %56
22: ; preds = %12
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i64 @strcmp(i8* %25, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %34, label %28
28: ; preds = %22
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i64 @strcmp(i8* %31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %54
34: ; preds = %28, %22
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 1
%37 = load i8*, i8** %36
%38 = call i64 @strcmp(i8* %37, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%39 = icmp ne i64 %38, 0
br i1 %39, label %51, label %40
40: ; preds = %34
%41 = load i32, i32* %4
%42 = icmp ne i32 %41, 3
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = call i32 (...) @usage()
store i32 -1, i32* %3
br label %56
45: ; preds = %40
%46 = load i32, i32* %6
%47 = load i8**, i8*** %5
%48 = getelementptr inbounds i8*, i8** %47, i64 2
%49 = load i8*, i8** %48
%50 = call i32 @msend(i32 %46, i8* %49)
store i32 %50, i32* %3
br label %56
51: ; preds = %34
%52 = load i32, i32* %6
%53 = call i32 @mrecv(i32 %52)
store i32 %53, i32* %3
br label %56
54: ; preds = %28
%55 = call i32 (...) @usage()
store i32 -1, i32* %3
br label %56
56: ; preds = %54, %51, %45, %43, %20, %10
%57 = load i32, i32* %3
ret i32 %57
}

LLVM IR (-O2):
@.str = [6 x i8] c"/etc/\00"
@IPC_CREAT = global i32 0
@.str.1 = [17 x i8] c"create msg error\00"
@.str.2 = [5 x i8] c"send\00"
@.str.3 = [5 x i8] c"recv\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 (...) @usage()
br label %39
6: ; preds = %2
%7 = tail call i32 @ftok(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8 signext 97)
%8 = load i32, i32* @IPC_CREAT
%9 = or i32 %8, 438
%10 = tail call i32 @msgget(i32 %7, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %6
%13 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %39
14: ; preds = %6
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = tail call i64 @strcmp(i8* %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %14
%20 = load i8*, i8** %15
%21 = tail call i64 @strcmp(i8* %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %37
23: ; preds = %19, %14
%24 = load i8*, i8** %15
%25 = tail call i64 @strcmp(i8* %24, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %35
27: ; preds = %23
%28 = icmp eq i32 %0, 3
br i1 %28, label %31, label %29
29: ; preds = %27
%30 = tail call i32 (...) @usage()
br label %39
31: ; preds = %27
%32 = getelementptr inbounds i8*, i8** %1, i64 2
%33 = load i8*, i8** %32
%34 = tail call i32 @msend(i32 %10, i8* %33)
br label %39
35: ; preds = %23
%36 = tail call i32 @mrecv(i32 %10)
br label %39
37: ; preds = %19
%38 = tail call i32 (...) @usage()
br label %39
39: ; preds = %12, %29, %31, %35, %37, %4
%40 = phi i32 [ -1, %4 ], [ -1, %12 ], [ %36, %35 ], [ -1, %29 ], [ %34, %31 ], [ -1, %37 ]
ret i32 %40
}