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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
int /*<<< orphan*/ sscanf(char *, char *, char *, int *);
int /*<<< orphan*/ *strstr(char *, char *);
uint32_t cma_pages_available(void) {
  FILE *fp;
  char *line = NULL;
  size_t len = 0;
  ssize_t read;
  char *cmp = "nr_free_cma";
  char dummy[20];
  int num = -1;
  fp = fopen("/proc/vmstat", "r");
  if (fp == NULL)
    return -1;
  while ((read = getline(&line, &len, fp)) != -1) {
    if (strstr(line, cmp) != NULL) {
      sscanf(line, "%s %d", dummy, &num);
    }
  }
  fclose(fp);
  return num;
}


x86 assembly (clang, -O0):
<cma_pages_available>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    0xe91(%rip),%rax
mov    %rax,-0x30(%rbp)
movl   $0xffffffff,-0x54(%rbp)
lea    0xe8b(%rip),%rdi
lea    0xe91(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a8 <cma_pages_available+0x58>
movl   $0xffffffff,-0x4(%rbp)
jmp    120d <cma_pages_available+0xbd>
jmp    11ad <cma_pages_available+0x5d>
mov    -0x10(%rbp),%rdx
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <getline@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
je     11fe <cma_pages_available+0xae>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1070 <strstr@plt>
cmp    $0x0,%rax
je     11f9 <cma_pages_available+0xa9>
mov    -0x18(%rbp),%rdi
lea    -0x50(%rbp),%rdx
lea    0xe2b(%rip),%rsi
lea    -0x54(%rbp),%rcx
call   1040 <sscanf@plt>
jmp    11ad <cma_pages_available+0x5d>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x54(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cma_pages_available>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
movq   $0x0,0x10(%rsp)
movq   $0x0,0x18(%rsp)
movl   $0xffffffff,0xc(%rsp)
ff
lea    0xe8d(%rip),%rdi
lea    0xe93(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     121f <cma_pages_available+0xcf>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
lea    0x18(%rsp),%rsi
mov    %rax,%rdx
call   1060 <getline@plt>
cmp    $0xffffffff,%eax
je     1211 <cma_pages_available+0xc1>
lea    0xe4b(%rip),%r14
lea    0x10(%rsp),%r15
lea    0x18(%rsp),%r12
lea    0xc(%rsp),%r13
jmp    11e3 <cma_pages_available+0x93>
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1060 <getline@plt>
cmp    $0xffffffff,%eax
je     1211 <cma_pages_available+0xc1>
mov    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    %r14,%rsi
call   1070 <strstr@plt>
test   %rax,%rax
je     11d0 <cma_pages_available+0x80>
mov    %rbp,%rdi
lea    0xe19(%rip),%rsi
lea    0x20(%rsp),%rdx
mov    %r13,%rcx
call   1040 <sscanf@plt>
jmp    11d0 <cma_pages_available+0x80>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0xc(%rsp),%eax
jmp    1224 <cma_pages_available+0xd4>
mov    $0xffffffff,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"nr_free_cma\00"
@.str.1 = [13 x i8] c"/proc/vmstat\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [6 x i8] c"%s %d\00"
define i32 @cma_pages_available() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca [20 x i8]
%8 = alloca i32
store i8* null, i8** %3
store i64 0, i64* %4
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %6
store i32 -1, i32* %8
%9 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %9, i32** %2
%10 = load i32*, i32** %2
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %13
12: ; preds = %0
store i32 -1, i32* %1
br label %32
13: ; preds = %0
br label %14
14: ; preds = %27, %13
%15 = load i32*, i32** %2
%16 = call i32 @getline(i8** %3, i64* %4, i32* %15)
store i32 %16, i32* %5
%17 = icmp ne i32 %16, -1
br i1 %17, label %18, label %28
18: ; preds = %14
%19 = load i8*, i8** %3
%20 = load i8*, i8** %6
%21 = call i32* @strstr(i8* %19, i8* %20)
%22 = icmp ne i32* %21, null
br i1 %22, label %23, label %27
23: ; preds = %18
%24 = load i8*, i8** %3
%25 = getelementptr inbounds [20 x i8], [20 x i8]* %7, i64 0, i64 0
%26 = call i32 @sscanf(i8* %24, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %25, i32* %8)
br label %27
27: ; preds = %23, %18
br label %14
28: ; preds = %14
%29 = load i32*, i32** %2
%30 = call i32 @fclose(i32* %29)
%31 = load i32, i32* %8
store i32 %31, i32* %1
br label %32
32: ; preds = %28, %12
%33 = load i32, i32* %1
ret i32 %33
}

LLVM IR (-O2):
@.str = [12 x i8] c"nr_free_cma\00"
@.str.1 = [13 x i8] c"/proc/vmstat\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [6 x i8] c"%s %d\00"
define i32 @cma_pages_available() {
%1 = alloca i8*
%2 = alloca i64
%3 = alloca [20 x i8]
%4 = alloca i32
%5 = bitcast i8** %1 to i8*
store i8* null, i8** %1
%6 = bitcast i64* %2 to i8*
store i64 0, i64* %2
%7 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%8 = bitcast i32* %4 to i8*
store i32 -1, i32* %4
%9 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %26, label %11
11: ; preds = %0
%12 = call i32 @getline(i8** %1, i64* %2, i32* %9)
%13 = icmp eq i32 %12, -1
br i1 %13, label %23, label %14
14: ; preds = %11, %20
%15 = load i8*, i8** %1
%16 = call i32* @strstr(i8* dereferenceable(1) %15, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %20, label %18
18: ; preds = %14
%19 = call i32 @sscanf(i8* %15, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %7, i32* %4)
br label %20
20: ; preds = %18, %14
%21 = call i32 @getline(i8** %1, i64* %2, i32* %9)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %14
23: ; preds = %20, %11
%24 = call i32 @fclose(i32* %9)
%25 = load i32, i32* %4
br label %26
26: ; preds = %0, %23
%27 = phi i32 [ %25, %23 ], [ -1, %0 ]
ret i32 %27
}