#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 */

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int IPC_CREAT;
int atoi(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ get_msg(int, int);
int getopt(int, char **, char *);
int msgget(int, int);
int /*<<< orphan*/ optarg;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ send_msg(int, int);
int /*<<< orphan*/ usage(char *, char *);
int main(int argc, char *argv[]) {
  int qid, opt;
  int mode = 0;
  int msgtype = 1;
  int msgkey = 1234;
  while ((opt = getopt(argc, argv, "srt:k:")) != -1) {
    switch (opt) {
    case 's':
      mode = 1;
      break;
    case 'r':
      mode = 2;
      break;
    case 't':
      msgtype = atoi(optarg);
      if (msgtype <= 0)
        usage(argv[0], "-t option must be greater than 0\n");
      break;
    case 'k':
      msgkey = atoi(optarg);
      break;
    default:
      usage(argv[0], "Unrecognized option\n");
    }
  }
  if (mode == 0)
    usage(argv[0], "must use either -s or -r option\n");
  qid = msgget(msgkey, IPC_CREAT | 0666);
  if (qid == -1) {
    perror("msgget");
    exit(EXIT_FAILURE);
  }
  if (mode == 2)
    get_msg(qid, msgtype);
  else
    send_msg(qid, msgtype);
  exit(EXIT_SUCCESS);
}


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)
movl   $0x0,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
movl   $0x4d2,-0x24(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe6f(%rip),%rdx
call   1080 <getopt@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
je     126c <main+0xec>
mov    -0x18(%rbp),%eax
add    $0xffffff95,%eax
mov    %eax,%ecx
mov    %rcx,-0x30(%rbp)
sub    $0x9,%eax
ja     1254 <main+0xd4>
mov    -0x30(%rbp),%rax
lea    0xe16(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movl   $0x1,-0x1c(%rbp)
jmp    1267 <main+0xe7>
movl   $0x2,-0x1c(%rbp)
jmp    1267 <main+0xe7>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
call   1090 <atoi@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jg     1239 <main+0xb9>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xdfb(%rip),%rsi
call   1040 <usage@plt>
jmp    1267 <main+0xe7>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
call   1090 <atoi@plt>
mov    %eax,-0x24(%rbp)
jmp    1267 <main+0xe7>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xdef(%rip),%rsi
call   1040 <usage@plt>
jmp    11ab <main+0x2b>
cmpl   $0x0,-0x1c(%rbp)
jne    1289 <main+0x109>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xde2(%rip),%rsi
call   1040 <usage@plt>
mov    -0x24(%rbp),%edi
mov    0x2d35(%rip),%rax
mov    (%rax),%esi
or     $0x1b6,%esi
call   1030 <msgget@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    12c7 <main+0x147>
lea    0xdd3(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpl   $0x2,-0x1c(%rbp)
jne    12e1 <main+0x161>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1050 <get_msg@plt>
jmp    12ec <main+0x16c>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1070 <send_msg@plt>
mov    0x2cdd(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbp
mov    %edi,%ebx
movl   $0x4d2,0x4(%rsp)
movl   $0x1,(%rsp)
xor    %r14d,%r14d
lea    0xe7f(%rip),%r12
lea    0xea1(%rip),%r13
lea    0xe49(%rip),%r15
jmp    11d1 <main+0x51>
nopl   0x0(%rax)
cmp    $0xffffffff,%eax
je     123a <main+0xba>
mov    0x0(%rbp),%rdi
mov    %r13,%rsi
call   1040 <usage@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r12,%rdx
call   1080 <getopt@plt>
lea    -0x6b(%rax),%ecx
cmp    $0x9,%ecx
ja     11c0 <main+0x40>
movslq (%r15,%rcx,4),%rax
add    %r15,%rax
jmp    *%rax
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
call   1090 <atoi@plt>
mov    %eax,0x4(%rsp)
jmp    11d1 <main+0x51>
mov    $0x2,%r14d
jmp    11d1 <main+0x51>
mov    $0x1,%r14d
jmp    11d1 <main+0x51>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
call   1090 <atoi@plt>
mov    %eax,(%rsp)
test   %eax,%eax
jg     11d1 <main+0x51>
mov    0x0(%rbp),%rdi
lea    0xdfc(%rip),%rsi
call   1040 <usage@plt>
jmp    11d1 <main+0x51>
test   %r14d,%r14d
jne    124f <main+0xcf>
mov    0x0(%rbp),%rdi
lea    0xe1c(%rip),%rsi
call   1040 <usage@plt>
mov    0x2d72(%rip),%rax
mov    $0x1b6,%esi
or     (%rax),%esi
mov    0x4(%rsp),%edi
call   1030 <msgget@plt>
cmp    $0xffffffff,%eax
jne    1285 <main+0x105>
lea    0xe15(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    %eax,%edi
cmp    $0x2,%r14d
jne    1297 <main+0x117>
mov    (%rsp),%esi
call   1050 <get_msg@plt>
jmp    129f <main+0x11f>
mov    (%rsp),%esi
call   1070 <send_msg@plt>
mov    0x2d2a(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"srt:k:\00"
@optarg = global i32 0
@.str.1 = [34 x i8] c"-t option must be greater than 0\0A\00"
@.str.2 = [21 x i8] c"Unrecognized option\0A\00"
@.str.3 = [33 x i8] c"must use either -s or -r option\0A\00"
@IPC_CREAT = global i32 0
@.str.4 = [7 x i8] c"msgget\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = 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
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %8
store i32 1, i32* %9
store i32 1234, i32* %10
br label %11
11: ; preds = %39, %2
%12 = load i32, i32* %4
%13 = load i8**, i8*** %5
%14 = call i32 @getopt(i32 %12, i8** %13, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 %14, i32* %7
%15 = icmp ne i32 %14, -1
br i1 %15, label %16, label %40
16: ; preds = %11
%17 = load i32, i32* %7
switch i32 %17, label %34 [
i32 115, label %18
i32 114, label %19
i32 116, label %20
i32 107, label %31
]
18: ; preds = %16
store i32 1, i32* %8
br label %39
19: ; preds = %16
store i32 2, i32* %8
br label %39
20: ; preds = %16
%21 = load i32, i32* @optarg
%22 = call i32 @atoi(i32 %21)
store i32 %22, i32* %9
%23 = load i32, i32* %9
%24 = icmp sle i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %20
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 0
%28 = load i8*, i8** %27
%29 = call i32 @usage(i8* %28, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %25, %20
br label %39
31: ; preds = %16
%32 = load i32, i32* @optarg
%33 = call i32 @atoi(i32 %32)
store i32 %33, i32* %10
br label %39
34: ; preds = %16
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 0
%37 = load i8*, i8** %36
%38 = call i32 @usage(i8* %37, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %39
39: ; preds = %34, %31, %30, %19, %18
br label %11
40: ; preds = %11
%41 = load i32, i32* %8
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %40
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 0
%46 = load i8*, i8** %45
%47 = call i32 @usage(i8* %46, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
br label %48
48: ; preds = %43, %40
%49 = load i32, i32* %10
%50 = load i32, i32* @IPC_CREAT
%51 = or i32 %50, 438
%52 = call i32 @msgget(i32 %49, i32 %51)
store i32 %52, i32* %6
%53 = load i32, i32* %6
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %59
55: ; preds = %48
%56 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%57 = load i32, i32* @EXIT_FAILURE
%58 = call i32 @exit(i32 %57)
unreachable
59: ; preds = %48
%60 = load i32, i32* %8
%61 = icmp eq i32 %60, 2
br i1 %61, label %62, label %66
62: ; preds = %59
%63 = load i32, i32* %6
%64 = load i32, i32* %9
%65 = call i32 @get_msg(i32 %63, i32 %64)
br label %70
66: ; preds = %59
%67 = load i32, i32* %6
%68 = load i32, i32* %9
%69 = call i32 @send_msg(i32 %67, i32 %68)
br label %70
70: ; preds = %66, %62
%71 = load i32, i32* @EXIT_SUCCESS
%72 = call i32 @exit(i32 %71)
unreachable
}

LLVM IR (-O2):
@.str = [7 x i8] c"srt:k:\00"
@optarg = global i32 0
@.str.1 = [34 x i8] c"-t option must be greater than 0\0A\00"
@.str.2 = [21 x i8] c"Unrecognized option\0A\00"
@.str.3 = [33 x i8] c"must use either -s or -r option\0A\00"
@IPC_CREAT = global i32 0
@.str.4 = [7 x i8] c"msgget\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
br label %3
3: ; preds = %15, %2
%4 = phi i32 [ 0, %2 ], [ %16, %15 ]
%5 = phi i32 [ 1, %2 ], [ %8, %15 ]
%6 = phi i32 [ 1234, %2 ], [ %11, %15 ]
br label %7
7: ; preds = %24, %3
%8 = phi i32 [ %5, %3 ], [ %19, %24 ]
%9 = phi i32 [ %6, %3 ], [ %11, %24 ]
br label %10
10: ; preds = %7, %25
%11 = phi i32 [ %9, %7 ], [ %27, %25 ]
br label %12
12: ; preds = %10, %28
%13 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
switch i32 %13, label %28 [
i32 -1, label %31
i32 115, label %15
i32 114, label %14
i32 116, label %17
i32 107, label %25
]
14: ; preds = %12
br label %15
15: ; preds = %12, %14
%16 = phi i32 [ 2, %14 ], [ 1, %12 ]
br label %3
17: ; preds = %12
%18 = load i32, i32* @optarg
%19 = tail call i32 @atoi(i32 %18)
%20 = icmp slt i32 %19, 1
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i8*, i8** %1
%23 = tail call i32 @usage(i8* %22, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %21, %17
br label %7
25: ; preds = %12
%26 = load i32, i32* @optarg
%27 = tail call i32 @atoi(i32 %26)
br label %10
28: ; preds = %12
%29 = load i8*, i8** %1
%30 = tail call i32 @usage(i8* %29, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %12
31: ; preds = %12
%32 = icmp eq i32 %4, 0
br i1 %32, label %33, label %36
33: ; preds = %31
%34 = load i8*, i8** %1
%35 = tail call i32 @usage(i8* %34, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
br label %36
36: ; preds = %33, %31
%37 = load i32, i32* @IPC_CREAT
%38 = or i32 %37, 438
%39 = tail call i32 @msgget(i32 %11, i32 %38)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %45
41: ; preds = %36
%42 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%43 = load i32, i32* @EXIT_FAILURE
%44 = tail call i32 @exit(i32 %43)
unreachable
45: ; preds = %36
%46 = icmp eq i32 %4, 2
br i1 %46, label %47, label %49
47: ; preds = %45
%48 = tail call i32 @get_msg(i32 %39, i32 %8)
br label %51
49: ; preds = %45
%50 = tail call i32 @send_msg(i32 %39, i32 %8)
br label %51
51: ; preds = %49, %47
%52 = load i32, i32* @EXIT_SUCCESS
%53 = tail call i32 @exit(i32 %52)
unreachable
}