#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 /*<<< orphan*/ buffer;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int NAME_MAX;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ stpcpy(char *, char *);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int strlen(char const *);
void dump_mqueue_state(const char *queue_name) {
  char filename[sizeof("/dev/mqueue") + NAME_MAX];
  FILE *f;
  char buffer[4096];
  if (strlen(queue_name) > NAME_MAX) {
    printf("Queue name is too long to dump the queue.\n");
    return;
  }
  strcpy(stpcpy(filename, "/dev/mqueue"), queue_name);
  f = fopen(filename, "r");
  if (!f) {
    perror("fopen(/dev/mqueue/...)");
    return;
  }
  printf("Content of %s: ", filename);
  while (fgets(buffer, sizeof(buffer), f)) {
    fputs(buffer, stdout);
  }
  fclose(f);
}


x86 assembly (clang, -O0):
<dump_mqueue_state>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e2a(%rip),%rax
movslq (%rax),%rdx
mov    %rdx,%rax
add    $0xc,%rax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
add    $0x1b,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x1030(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    0x2ded(%rip),%rcx
cmp    (%rcx),%eax
jle    1208 <dump_mqueue_state+0x78>
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x1024(%rbp)
jmp    12c4 <dump_mqueue_state+0x134>
mov    -0x1030(%rbp),%rdi
lea    0xe15(%rip),%rsi
call   1050 <stpcpy@plt>
mov    %eax,%edi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x1030(%rbp),%rdi
lea    0xe03(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1263 <dump_mqueue_state+0xd3>
lea    0xdea(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x1024(%rbp)
jmp    12c4 <dump_mqueue_state+0x134>
mov    -0x1030(%rbp),%rsi
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x1020(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x1000,%esi
call   1090 <fgets@plt>
cmp    $0x0,%rax
je     12b1 <dump_mqueue_state+0x121>
lea    -0x1020(%rbp),%rdi
mov    0x2d3b(%rip),%rax
mov    (%rax),%esi
call   1080 <fputs@plt>
jmp    1278 <dump_mqueue_state+0xe8>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x1024(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump_mqueue_state>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %rdi,%rbx
mov    0x2e16(%rip),%r15
movslq (%r15),%rax
mov    %rsp,%r14
add    $0x1b,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
call   1070 <strlen@plt>
cmp    (%r15),%eax
jle    11ee <dump_mqueue_state+0x4e>
lea    0xe56(%rip),%rdi
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <puts@plt>
lea    0xe0b(%rip),%rsi
mov    %r14,%rdi
call   1060 <stpcpy@plt>
mov    %eax,%edi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
lea    0xdfe(%rip),%rsi
mov    %r14,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1293 <dump_mqueue_state+0xf3>
mov    %rax,%r15
lea    0xe00(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
lea    -0x1020(%rbp),%rdi
mov    $0x1000,%esi
mov    %r15,%rdx
call   10a0 <fgets@plt>
test   %rax,%rax
je     1280 <dump_mqueue_state+0xe0>
mov    0x2d91(%rip),%r14
lea    -0x1020(%rbp),%rbx
cs nopw 0x0(%rax,%rax,1)
mov    (%r14),%esi
mov    %rbx,%rdi
call   1090 <fputs@plt>
mov    %rbx,%rdi
mov    $0x1000,%esi
mov    %r15,%rdx
call   10a0 <fgets@plt>
test   %rax,%rax
jne    1260 <dump_mqueue_state+0xc0>
mov    %r15,%rdi
call   1050 <fclose@plt>
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd74(%rip),%rdi
call   10c0 <perror@plt>
jmp    1288 <dump_mqueue_state+0xe8>

LLVM IR (-O0):
@NAME_MAX = global i32 0
@.str = [43 x i8] c"Queue name is too long to dump the queue.\0A\00"
@.str.1 = [12 x i8] c"/dev/mqueue\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [23 x i8] c"fopen(/dev/mqueue/...)\00"
@.str.4 = [16 x i8] c"Content of %s: \00"
@stdout = global i32 0
define void @dump_mqueue_state(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32*
%6 = alloca [4096 x i8]
%7 = alloca i32
store i8* %0, i8** %2
%8 = load i32, i32* @NAME_MAX
%9 = sext i32 %8 to i64
%10 = add i64 12, %9
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %3
%12 = alloca i8, i64 %10
store i64 %10, i64* %4
%13 = load i8*, i8** %2
%14 = call i32 @strlen(i8* %13)
%15 = load i32, i32* @NAME_MAX
%16 = icmp sgt i32 %14, %15
br i1 %16, label %17, label %19
17: ; preds = %1
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %7
br label %42
19: ; preds = %1
%20 = call i32 @stpcpy(i8* %12, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%21 = load i8*, i8** %2
%22 = call i32 @strcpy(i32 %20, i8* %21)
%23 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %23, i32** %5
%24 = load i32*, i32** %5
%25 = icmp ne i32* %24, null
br i1 %25, label %28, label %26
26: ; preds = %19
%27 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %7
br label %42
28: ; preds = %19
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %12)
br label %30
30: ; preds = %35, %28
%31 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%32 = load i32*, i32** %5
%33 = call i64 @fgets(i8* %31, i32 4096, i32* %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %30
%36 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%37 = load i32, i32* @stdout
%38 = call i32 @fputs(i8* %36, i32 %37)
br label %30
39: ; preds = %30
%40 = load i32*, i32** %5
%41 = call i32 @fclose(i32* %40)
store i32 0, i32* %7
br label %42
42: ; preds = %39, %26, %17
%43 = load i8*, i8** %3
%44 = load i32, i32* %7
switch i32 %44, label %46 [
i32 0, label %45
i32 1, label %45
]
45: ; preds = %42, %42
ret void
46: ; preds = %42
unreachable
}

LLVM IR (-O2):
@NAME_MAX = global i32 0
@.str.1 = [12 x i8] c"/dev/mqueue\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [23 x i8] c"fopen(/dev/mqueue/...)\00"
@.str.4 = [16 x i8] c"Content of %s: \00"
@stdout = global i32 0
@str = [42 x i8] c"Queue name is too long to dump the queue.\00"
define void @dump_mqueue_state(i8* %0) {
%2 = alloca [4096 x i8]
%3 = load i32, i32* @NAME_MAX
%4 = sext i32 %3 to i64
%5 = add nsw i64 %4, 12
%6 = alloca i8, i64 %5
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%8 = tail call i32 @strlen(i8* %0)
%9 = load i32, i32* @NAME_MAX
%10 = icmp sgt i32 %8, %9
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str, i64 0, i64 0))
br label %31
13: ; preds = %1
%14 = call i32 @stpcpy(i8* %6, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @strcpy(i32 %14, i8* %0)
%16 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %31
20: ; preds = %13
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %6)
%22 = call i64 @fgets(i8* %7, i32 4096, i32* %16)
%23 = icmp eq i64 %22, 0
br i1 %23, label %29, label %24
24: ; preds = %20, %24
%25 = load i32, i32* @stdout
%26 = call i32 @fputs(i8* %7, i32 %25)
%27 = call i64 @fgets(i8* %7, i32 4096, i32* %16)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %24
29: ; preds = %24, %20
%30 = call i32 @fclose(i32* %16)
br label %31
31: ; preds = %29, %18, %11
ret void
}