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

/* Variables and functions */
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
long strtol(char const *, char **, int);
long get_msgmni(void) {
  FILE *fp;
  char buf[1024];
  char *endptr;
  long msgmni;
  if (!(fp = fopen("/proc/sys/kernel/msgmni", "r"))) {
    return -1;
  } else {
    fgets(buf, sizeof(buf), fp);
    msgmni = strtol((const char *)buf, &endptr, 10);
    return msgmni;
  }
}


x86 assembly (clang, -O0):
<get_msgmni>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
lea    0xebe(%rip),%rdi
lea    0xecf(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1169 <get_msgmni+0x39>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11a8 <get_msgmni+0x78>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x400,%esi
call   1030 <fgets@plt>
lea    -0x410(%rbp),%rdi
lea    -0x418(%rbp),%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %rax,-0x420(%rbp)
mov    -0x420(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_msgmni>:
push   %rbx
sub    $0x410,%rsp
lea    0xec1(%rip),%rdi
lea    0xed2(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1179 <get_msgmni+0x49>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1030 <fgets@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
mov    $0xa,%edx
call   1040 <strtol@plt>
jmp    1180 <get_msgmni+0x50>
mov    $0xffffffffffffffff,%rax
add    $0x410,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [24 x i8] c"/proc/sys/kernel/msgmni\00"
@.str.1 = [2 x i8] c"r\00"
define i64 @get_msgmni() {
%1 = alloca i64
%2 = alloca i32*
%3 = alloca [1024 x i8]
%4 = alloca i8*
%5 = alloca i64
%6 = call i32* @fopen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %2
%7 = icmp ne i32* %6, null
br i1 %7, label %9, label %8
8: ; preds = %0
store i64 -1, i64* %1
br label %16
9: ; preds = %0
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%11 = load i32*, i32** %2
%12 = call i32 @fgets(i8* %10, i32 1024, i32* %11)
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%14 = call i64 @strtol(i8* %13, i8** %4, i32 10)
store i64 %14, i64* %5
%15 = load i64, i64* %5
store i64 %15, i64* %1
br label %16
16: ; preds = %9, %8
%17 = load i64, i64* %1
ret i64 %17
}

LLVM IR (-O2):
@.str = [24 x i8] c"/proc/sys/kernel/msgmni\00"
@.str.1 = [2 x i8] c"r\00"
define i64 @get_msgmni() {
%1 = alloca [1024 x i8]
%2 = alloca i8*
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%4 = bitcast i8** %2 to i8*
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %10, label %7
7: ; preds = %0
%8 = call i32 @fgets(i8* %3, i32 1024, i32* %5)
%9 = call i64 @strtol(i8* %3, i8** %2, i32 10)
br label %10
10: ; preds = %0, %7
%11 = phi i64 [ %9, %7 ], [ -1, %0 ]
ret i64 %11
}