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

/* Variables and functions */
int /*<<< orphan*/ IPC_CREAT;
int /*<<< orphan*/ IPC_PRIVATE;
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ IPC_STAT;
int /*<<< orphan*/ errno;
int msgctl(int, int /*<<< orphan*/, struct msqid_ds *);
int msgget(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char const *, ...);
char *strerror(int /*<<< orphan*/);
int _msgctl(char const *progname) {
  int qid;
  struct msqid_ds ds;
  int d;
  printf("%s: Testing %s()\n", progname, "msgctl");
  if ((qid = msgget(IPC_PRIVATE, IPC_CREAT)) == -1) {
    printf("%s: msgget(IPC_PRIVATE) => %d (%s)\n", progname, qid,
           strerror(errno));
    return 2;
  }
  if ((d = msgctl(qid, IPC_STAT, &ds)) != 0)
    printf("%s: msgctl(IPC_STAT) => %d (%s)\n", progname, d, strerror(errno));
  if ((d = msgctl(qid, IPC_RMID, NULL)) != 0) {
    printf("%s: msgctl(IPC_RMID) => %d (%s)\n", progname, d, strerror(errno));
    return 2;
  }
  return 0;
}


x86 assembly (clang, -O0):
<_msgctl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xea9(%rip),%rdi
lea    0xeb4(%rip),%rdx
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e7c(%rip),%rax
mov    (%rax),%edi
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
call   1030 <msgget@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11c8 <_msgctl+0x88>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%edx
mov    %rax,%rcx
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1285 <_msgctl+0x145>
mov    -0x14(%rbp),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1050 <msgctl@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
je     121d <_msgctl+0xdd>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    -0x2c(%rbp),%edx
mov    %rax,%rcx
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <msgctl@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
je     127e <_msgctl+0x13e>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x48(%rbp),%rsi
mov    -0x3c(%rbp),%edx
mov    %rax,%rcx
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1285 <_msgctl+0x145>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_msgctl>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
lea    0xeae(%rip),%rdi
lea    0xeb9(%rip),%rdx
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e7e(%rip),%rax
mov    (%rax),%edi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
call   1030 <msgget@plt>
cmp    $0xffffffff,%eax
je     11f6 <_msgctl+0xb6>
mov    %eax,%ebp
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1050 <msgctl@plt>
test   %eax,%eax
je     11c0 <_msgctl+0x80>
mov    %eax,%ebx
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe8c(%rip),%rdi
mov    %r14,%rsi
mov    %ebx,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
mov    %ebp,%edi
xor    %edx,%edx
call   1050 <msgctl@plt>
test   %eax,%eax
je     1222 <_msgctl+0xe2>
mov    %eax,%ebp
mov    0x2def(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe6f(%rip),%rdi
mov    %r14,%rsi
mov    %ebp,%edx
jmp    1213 <_msgctl+0xd3>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe0e(%rip),%rdi
mov    %r14,%rsi
mov    $0xffffffff,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x2,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.msqid_ds = type { i32 }
@.str = [18 x i8] c"%s: Testing %s()\0A\00"
@.str.1 = [7 x i8] c"msgctl\00"
@IPC_PRIVATE = global i32 0
@IPC_CREAT = global i32 0
@.str.2 = [36 x i8] c"%s: msgget(IPC_PRIVATE) => %d (%s)\0A\00"
@errno = global i32 0
@IPC_STAT = global i32 0
@.str.3 = [33 x i8] c"%s: msgctl(IPC_STAT) => %d (%s)\0A\00"
@IPC_RMID = global i32 0
@.str.4 = [33 x i8] c"%s: msgctl(IPC_RMID) => %d (%s)\0A\00"
define i32 @_msgctl(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.msqid_ds
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32, i32* @IPC_PRIVATE
%10 = load i32, i32* @IPC_CREAT
%11 = call i32 @msgget(i32 %9, i32 %10)
store i32 %11, i32* %4
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %19
13: ; preds = %1
%14 = load i8*, i8** %3
%15 = load i32, i32* %4
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %14, i32 %15, i8* %17)
store i32 2, i32* %2
br label %42
19: ; preds = %1
%20 = load i32, i32* %4
%21 = load i32, i32* @IPC_STAT
%22 = call i32 @msgctl(i32 %20, i32 %21, %struct.msqid_ds* %5)
store i32 %22, i32* %6
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %19
%25 = load i8*, i8** %3
%26 = load i32, i32* %6
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %25, i32 %26, i8* %28)
br label %30
30: ; preds = %24, %19
%31 = load i32, i32* %4
%32 = load i32, i32* @IPC_RMID
%33 = call i32 @msgctl(i32 %31, i32 %32, %struct.msqid_ds* null)
store i32 %33, i32* %6
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %30
%36 = load i8*, i8** %3
%37 = load i32, i32* %6
%38 = load i32, i32* @errno
%39 = call i8* @strerror(i32 %38)
%40 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %36, i32 %37, i8* %39)
store i32 2, i32* %2
br label %42
41: ; preds = %30
store i32 0, i32* %2
br label %42
42: ; preds = %41, %35, %13
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
%struct.msqid_ds = type { i32 }
@.str = [18 x i8] c"%s: Testing %s()\0A\00"
@.str.1 = [7 x i8] c"msgctl\00"
@IPC_PRIVATE = global i32 0
@IPC_CREAT = global i32 0
@.str.2 = [36 x i8] c"%s: msgget(IPC_PRIVATE) => %d (%s)\0A\00"
@errno = global i32 0
@IPC_STAT = global i32 0
@.str.3 = [33 x i8] c"%s: msgctl(IPC_STAT) => %d (%s)\0A\00"
@IPC_RMID = global i32 0
@.str.4 = [33 x i8] c"%s: msgctl(IPC_RMID) => %d (%s)\0A\00"
define i32 @_msgctl(i8* %0) {
%2 = alloca %struct.msqid_ds
%3 = bitcast %struct.msqid_ds* %2 to i8*
%4 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%5 = load i32, i32* @IPC_PRIVATE
%6 = load i32, i32* @IPC_CREAT
%7 = tail call i32 @msgget(i32 %5, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 -1, i8* %11)
br label %29
13: ; preds = %1
%14 = load i32, i32* @IPC_STAT
%15 = call i32 @msgctl(i32 %7, i32 %14, %struct.msqid_ds* %2)
%16 = icmp eq i32 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %13
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %0, i32 %15, i8* %19)
br label %21
21: ; preds = %17, %13
%22 = load i32, i32* @IPC_RMID
%23 = call i32 @msgctl(i32 %7, i32 %22, %struct.msqid_ds* null)
%24 = icmp eq i32 %23, 0
br i1 %24, label %29, label %25
25: ; preds = %21
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %0, i32 %23, i8* %27)
br label %29
29: ; preds = %21, %25, %9
%30 = phi i32 [ 2, %9 ], [ 2, %25 ], [ 0, %21 ]
ret i32 %30
}