#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int mode;
  int /*<<< orphan*/ gid;
  int /*<<< orphan*/ uid;
};
struct msqid_ds {
  scalar_t__ msg_qbytes;
  TYPE_1__ msg_perm;
};

/* Variables and functions */
int /*<<< orphan*/ IPC_SET;
int /*<<< orphan*/ err_sys(char *);
int /*<<< orphan*/ getpgid(int /*<<< orphan*/);
int /*<<< orphan*/ getpid();
int msgctl(int, int /*<<< orphan*/, struct msqid_ds *);
int /*<<< orphan*/ printf(char *);
void set_mode(int mid, int mode) {
  struct msqid_ds mbuf;
  mbuf.msg_perm.uid = getpid();
  mbuf.msg_perm.gid = getpgid(getpid());
  mbuf.msg_perm.mode = mode;
  mbuf.msg_qbytes = 0;
  int ret = msgctl(mid, IPC_SET, &mbuf);
  if (ret < 0)
    err_sys("msgctl to stat queue failed");
  printf("set queue mode ok\n");
}


x86 assembly (clang, -O0):
<set_mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%edi
call   1070 <getpgid@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <msgctl@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11b5 <set_mode+0x65>
lea    0xe50(%rip),%rdi
call   1030 <err_sys@plt>
lea    0xe60(%rip),%rdi
call   1050 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_mode>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,0x10(%rsp)
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%edi
call   1070 <getpgid@plt>
mov    %eax,0xc(%rsp)
mov    %ebx,0x8(%rsp)
movq   $0x0,(%rsp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1060 <msgctl@plt>
test   %eax,%eax
jns    11a6 <set_mode+0x56>
lea    0xe5f(%rip),%rdi
call   1040 <err_sys@plt>
lea    0xe6f(%rip),%rdi
call   1030 <puts@plt>
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.msqid_ds = type { i64, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32, i32, i32 }
@IPC_SET = global i32 0
@.str = [28 x i8] c"msgctl to stat queue failed\00"
@.str.1 = [19 x i8] c"set queue mode ok\0A\00"
define void @set_mode(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.msqid_ds
%6 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = call i32 (...) @getpid()
%8 = getelementptr inbounds %struct.msqid_ds, %struct.msqid_ds* %5, i32 0, i32 1
%9 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %8, i32 0, i32 2
store i32 %7, i32* %9
%10 = call i32 (...) @getpid()
%11 = call i32 @getpgid(i32 %10)
%12 = getelementptr inbounds %struct.msqid_ds, %struct.msqid_ds* %5, i32 0, i32 1
%13 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %12, i32 0, i32 1
store i32 %11, i32* %13
%14 = load i32, i32* %4
%15 = getelementptr inbounds %struct.msqid_ds, %struct.msqid_ds* %5, i32 0, i32 1
%16 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %15, i32 0, i32 0
store i32 %14, i32* %16
%17 = getelementptr inbounds %struct.msqid_ds, %struct.msqid_ds* %5, i32 0, i32 0
store i64 0, i64* %17
%18 = load i32, i32* %3
%19 = load i32, i32* @IPC_SET
%20 = call i32 @msgctl(i32 %18, i32 %19, %struct.msqid_ds* %5)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %2
%24 = call i32 @err_sys(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %23, %2
%26 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
%struct.msqid_ds = type { i64, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32, i32, i32 }
@IPC_SET = global i32 0
@.str = [28 x i8] c"msgctl to stat queue failed\00"
@str = [18 x i8] c"set queue mode ok\00"
define void @set_mode(i32 %0, i32 %1) {
%3 = alloca %struct.msqid_ds
%4 = bitcast %struct.msqid_ds* %3 to i8*
%5 = tail call i32 (...) @getpid()
%6 = getelementptr inbounds %struct.msqid_ds, %struct.msqid_ds* %3, i64 0, i32 1, i32 2
store i32 %5, i32* %6
%7 = tail call i32 (...) @getpid()
%8 = tail call i32 @getpgid(i32 %7)
%9 = getelementptr inbounds %struct.msqid_ds, %struct.msqid_ds* %3, i64 0, i32 1, i32 1
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.msqid_ds, %struct.msqid_ds* %3, i64 0, i32 1, i32 0
store i32 %1, i32* %10
%11 = getelementptr inbounds %struct.msqid_ds, %struct.msqid_ds* %3, i64 0, i32 0
store i64 0, i64* %11
%12 = load i32, i32* @IPC_SET
%13 = call i32 @msgctl(i32 %0, i32 %12, %struct.msqid_ds* %3)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @err_sys(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %2
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
ret void
}