#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_qnum;
};

/* Variables and functions */
int /*<<< orphan*/ IPC_STAT;
int errno;
int msgctl(int, int /*<<< orphan*/, struct msqid_ds *);
int /*<<< orphan*/ printf(char *, int, int, char *);
char *strerror(int);
int queueGetNumMessages(int queue) {
  int num = -1;
  struct msqid_ds buf;
  if (msgctl(queue, IPC_STAT, &buf) != -1) {
    num = buf.msg_qnum;
  } else {
    printf("Error checking queue %d (%d - %s)\n", queue, errno,
           strerror(errno));
  }
  return (num);
}


x86 assembly (clang, -O0):
<queueGetNumMessages>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0xffffffff,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e94(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1040 <msgctl@plt>
cmp    $0xffffffff,%eax
je     116b <queueGetNumMessages+0x3b>
mov    -0x10(%rbp),%eax
mov    %eax,-0x8(%rbp)
jmp    11a0 <queueGetNumMessages+0x70>
mov    -0x4(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e60(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    %rax,%rcx
lea    0xe65(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<queueGetNumMessages>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2ea4(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1040 <msgctl@plt>
cmp    $0xffffffff,%eax
je     1150 <queueGetNumMessages+0x20>
mov    (%rsp),%eax
jmp    1178 <queueGetNumMessages+0x48>
mov    0x2e81(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   1050 <strerror@plt>
lea    0xe99(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %rax,%rcx
call   1030 <printf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.msqid_ds = type { i32 }
@IPC_STAT = global i32 0
@.str = [35 x i8] c"Error checking queue %d (%d - %s)\0A\00"
@errno = global i32 0
define i32 @queueGetNumMessages(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.msqid_ds
store i32 %0, i32* %2
store i32 -1, i32* %3
%5 = load i32, i32* %2
%6 = load i32, i32* @IPC_STAT
%7 = call i32 @msgctl(i32 %5, i32 %6, %struct.msqid_ds* %4)
%8 = icmp ne i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = getelementptr inbounds %struct.msqid_ds, %struct.msqid_ds* %4, i32 0, i32 0
%11 = load i32, i32* %10
store i32 %11, i32* %3
br label %18
12: ; preds = %1
%13 = load i32, i32* %2
%14 = load i32, i32* @errno
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %13, i32 %14, i8* %16)
br label %18
18: ; preds = %12, %9
%19 = load i32, i32* %3
ret i32 %19
}

LLVM IR (-O2):
%struct.msqid_ds = type { i32 }
@IPC_STAT = global i32 0
@.str = [35 x i8] c"Error checking queue %d (%d - %s)\0A\00"
@errno = global i32 0
define i32 @queueGetNumMessages(i32 %0) {
%2 = alloca %struct.msqid_ds
%3 = bitcast %struct.msqid_ds* %2 to i8*
%4 = load i32, i32* @IPC_STAT
%5 = call i32 @msgctl(i32 %0, i32 %4, %struct.msqid_ds* %2)
%6 = icmp eq i32 %5, -1
br i1 %6, label %10, label %7
7: ; preds = %1
%8 = getelementptr inbounds %struct.msqid_ds, %struct.msqid_ds* %2, i64 0, i32 0
%9 = load i32, i32* %8
br label %14
10: ; preds = %1
%11 = load i32, i32* @errno
%12 = call i8* @strerror(i32 %11)
%13 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %11, i8* %12)
br label %14
14: ; preds = %10, %7
%15 = phi i32 [ %9, %7 ], [ -1, %10 ]
ret i32 %15
}