#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 key_t;

/* Variables and functions */
int /*<<< orphan*/ err_sys(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
scalar_t__ memcmp(char *, char *, int);
int msgget(int, int);
int msgsnd(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdin;
int main(void) {
  int msqid = 0;
  key_t key = 2222;
  char send_buf[512] = {0};
  msqid = msgget(key, 0666);
  if (msqid == -1) {
    err_sys("msgget error");
  } else {
    printf("msqid = %d\n", msqid);
  }
  while (1) {
    printf("输入要发送的信息：\n");
    if (fgets(send_buf, 512, stdin) == NULL) {
      err_sys("fgets error");
    }
    if (msgsnd(msqid, send_buf, 512, 0) == -1) {
      err_sys("msgsnd error");
    }
    if (memcmp(send_buf, "q", 1) == 0) {
      break;
    }
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x210,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x8ae,-0xc(%rbp)
lea    -0x210(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1060 <memset@plt>
mov    -0xc(%rbp),%edi
mov    $0x1b6,%esi
call   1030 <msgget@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11de <main+0x5e>
lea    0xe2c(%rip),%rdi
call   1040 <err_sys@plt>
jmp    11ef <main+0x6f>
mov    -0x8(%rbp),%esi
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11f4 <main+0x74>
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x210(%rbp),%rdi
mov    0x2dd8(%rip),%rax
mov    (%rax),%edx
mov    $0x200,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
jne    1232 <main+0xb2>
lea    0xe09(%rip),%rdi
call   1040 <err_sys@plt>
mov    -0x8(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
call   1090 <msgsnd@plt>
cmp    $0xffffffff,%eax
jne    125d <main+0xdd>
lea    0xdea(%rip),%rdi
call   1040 <err_sys@plt>
lea    -0x210(%rbp),%rdi
lea    0xde4(%rip),%rsi
mov    $0x1,%edx
call   1070 <memcmp@plt>
cmp    $0x0,%rax
jne    1284 <main+0x104>
jmp    1289 <main+0x109>
jmp    11f4 <main+0x74>
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %rsp,%rdi
mov    $0x200,%edx
xor    %esi,%esi
call   1070 <memset@plt>
mov    $0x8ae,%edi
mov    $0x1b6,%esi
call   1030 <msgget@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11d4 <main+0x44>
lea    0xe33(%rip),%rdi
call   1050 <err_sys@plt>
jmp    11e4 <main+0x54>
lea    0xe32(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe49(%rip),%r14
mov    0x2df6(%rip),%r15
mov    %rsp,%rbx
lea    0xe29(%rip),%r12
lea    0xe2f(%rip),%r13
jmp    1225 <main+0x95>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    %r13,%rsi
mov    $0x1,%edx
call   1080 <memcmp@plt>
test   %rax,%rax
je     126e <main+0xde>
mov    %r14,%rdi
call   1040 <puts@plt>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0x200,%esi
call   1090 <fgets@plt>
test   %rax,%rax
jne    124e <main+0xbe>
lea    0xdd0(%rip),%rdi
call   1050 <err_sys@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
call   10a0 <msgsnd@plt>
cmp    $0xffffffff,%eax
jne    1210 <main+0x80>
mov    %r12,%rdi
call   1050 <err_sys@plt>
jmp    1210 <main+0x80>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"msgget error\00"
@.str.1 = [12 x i8] c"msqid = %d\0A\00"
@.str.2 = [29 x i8] c"\E8\BE\93\E5\85\A5\E8\A6\81\E5\8F\91\E9\80\81\E7\9A\84\E4\BF\A1\E6\81\AF\EF\BC\9A\0A\00"
@stdin = global i32 0
@.str.3 = [12 x i8] c"fgets error\00"
@.str.4 = [13 x i8] c"msgsnd error\00"
@.str.5 = [2 x i8] c"q\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [512 x i8]
store i32 0, i32* %1
store i32 0, i32* %2
store i32 2222, i32* %3
%5 = bitcast [512 x i8]* %4 to i8*
%6 = load i32, i32* %3
%7 = call i32 @msgget(i32 %6, i32 438)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %15
12: ; preds = %0
%13 = load i32, i32* %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %13)
br label %15
15: ; preds = %12, %10
br label %16
16: ; preds = %15, %36
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%18 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%19 = load i32, i32* @stdin
%20 = call i32* @fgets(i8* %18, i32 512, i32 %19)
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %24
24: ; preds = %22, %16
%25 = load i32, i32* %2
%26 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%27 = call i32 @msgsnd(i32 %25, i8* %26, i32 512, i32 0)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %31
31: ; preds = %29, %24
%32 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%33 = call i64 @memcmp(i8* %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 1)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %31
br label %37
36: ; preds = %31
br label %16
37: ; preds = %35
%38 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [13 x i8] c"msgget error\00"
@.str.1 = [12 x i8] c"msqid = %d\0A\00"
@stdin = global i32 0
@.str.3 = [12 x i8] c"fgets error\00"
@.str.4 = [13 x i8] c"msgsnd error\00"
@.str.5 = [2 x i8] c"q\00"
@str = [28 x i8] c"\E8\BE\93\E5\85\A5\E8\A6\81\E5\8F\91\E9\80\81\E7\9A\84\E4\BF\A1\E6\81\AF\EF\BC\9A\00"
define i32 @main() {
%1 = alloca [512 x i8]
%2 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @msgget(i32 2222, i32 438)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %9
7: ; preds = %0
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %3)
br label %9
9: ; preds = %7, %5
br label %10
10: ; preds = %9, %22
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%12 = load i32, i32* @stdin
%13 = call i32* @fgets(i8* %2, i32 512, i32 %12)
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %17
17: ; preds = %15, %10
%18 = call i32 @msgsnd(i32 %3, i8* %2, i32 512, i32 0)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %22
22: ; preds = %20, %17
%23 = call i64 @memcmp(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 1)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %10
25: ; preds = %22
%26 = call i32 @exit(i32 0)
unreachable
}