#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 */
struct msqid_ds {
  int msg_qbytes;
};
typedef int /*<<< orphan*/ key_t;

/* Variables and functions */
int IPC_CREAT;
int /*<<< orphan*/ IPC_SET;
int /*<<< orphan*/ IPC_STAT;
int msgctl(int, int /*<<< orphan*/, struct msqid_ds *);
int msgget(int /*<<< orphan*/, int);
int open_queue(key_t keyval, int flags) {
  int qid;
  struct msqid_ds ds;
  if ((qid = msgget(keyval, flags)) == -1)
    return -1;
  if (flags & ~IPC_CREAT) {
    if (msgctl(qid, IPC_STAT, &ds) != -1) {
      ds.msg_qbytes = 327680;
      msgctl(qid, IPC_SET, &ds);
    }
  }
  return (qid);
}


x86 assembly (clang, -O0):
<open_queue>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1030 <msgget@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
jne    1151 <open_queue+0x31>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b0 <open_queue+0x90>
mov    -0xc(%rbp),%eax
mov    0x2e75(%rip),%rcx
mov    (%rcx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     11aa <open_queue+0x8a>
mov    -0x10(%rbp),%edi
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1040 <msgctl@plt>
cmp    $0xffffffff,%eax
je     11a5 <open_queue+0x85>
movl   $0x50000,-0x18(%rbp)
mov    -0x10(%rbp),%edi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1040 <msgctl@plt>
jmp    11aa <open_queue+0x8a>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_queue>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
call   1030 <msgget@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1172 <open_queue+0x52>
mov    0x2e98(%rip),%rax
mov    (%rax),%eax
not    %eax
test   %ebp,%eax
je     1172 <open_queue+0x52>
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <msgctl@plt>
cmp    $0xffffffff,%eax
je     1172 <open_queue+0x52>
movl   $0x50000,(%rsp)
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <msgctl@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.msqid_ds = type { i32 }
@IPC_CREAT = global i32 0
@IPC_STAT = global i32 0
@IPC_SET = global i32 0
define i32 @open_queue(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.msqid_ds
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* %4
%9 = load i32, i32* %5
%10 = call i32 @msgget(i32 %8, i32 %9)
store i32 %10, i32* %6
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 -1, i32* %3
br label %32
13: ; preds = %2
%14 = load i32, i32* %5
%15 = load i32, i32* @IPC_CREAT
%16 = xor i32 %15, -1
%17 = and i32 %14, %16
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %30
19: ; preds = %13
%20 = load i32, i32* %6
%21 = load i32, i32* @IPC_STAT
%22 = call i32 @msgctl(i32 %20, i32 %21, %struct.msqid_ds* %7)
%23 = icmp ne i32 %22, -1
br i1 %23, label %24, label %29
24: ; preds = %19
%25 = getelementptr inbounds %struct.msqid_ds, %struct.msqid_ds* %7, i32 0, i32 0
store i32 327680, i32* %25
%26 = load i32, i32* %6
%27 = load i32, i32* @IPC_SET
%28 = call i32 @msgctl(i32 %26, i32 %27, %struct.msqid_ds* %7)
br label %29
29: ; preds = %24, %19
br label %30
30: ; preds = %29, %13
%31 = load i32, i32* %6
store i32 %31, i32* %3
br label %32
32: ; preds = %30, %12
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
%struct.msqid_ds = type { i32 }
@IPC_CREAT = global i32 0
@IPC_STAT = global i32 0
@IPC_SET = global i32 0
define i32 @open_queue(i32 %0, i32 %1) {
%3 = alloca %struct.msqid_ds
%4 = bitcast %struct.msqid_ds* %3 to i8*
%5 = tail call i32 @msgget(i32 %0, i32 %1)
%6 = icmp eq i32 %5, -1
br i1 %6, label %20, label %7
7: ; preds = %2
%8 = load i32, i32* @IPC_CREAT
%9 = xor i32 %8, -1
%10 = and i32 %9, %1
%11 = icmp eq i32 %10, 0
br i1 %11, label %20, label %12
12: ; preds = %7
%13 = load i32, i32* @IPC_STAT
%14 = call i32 @msgctl(i32 %5, i32 %13, %struct.msqid_ds* %3)
%15 = icmp eq i32 %14, -1
br i1 %15, label %20, label %16
16: ; preds = %12
%17 = getelementptr inbounds %struct.msqid_ds, %struct.msqid_ds* %3, i64 0, i32 0
store i32 327680, i32* %17
%18 = load i32, i32* @IPC_SET
%19 = call i32 @msgctl(i32 %5, i32 %18, %struct.msqid_ds* %3)
br label %20
20: ; preds = %7, %16, %12, %2
ret i32 %5
}