#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 */

/* Variables and functions */
int file_to_buf(char *, char *, int);
scalar_t__ libres_ulong(char *, unsigned long *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *, char *,
                            char *);
size_t cgmemread(char *cg, char *file) {
  char buf[128];
  char fn[256];
  unsigned long ret;
  snprintf(fn, 256, "%s/%s%s/%s", "/sys/fs/cgroup", "memory", cg, file);
  if (file_to_buf(fn, buf, 128) == -1)
    return 0;
  if (libres_ulong(buf, &ret) != 0) {
    return 0;
  } else {
    return ret / 1024;
  }
}


x86 assembly (clang, -O0):
<cgmemread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1b0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x1a0(%rbp),%rdi
mov    -0x10(%rbp),%r9
mov    -0x18(%rbp),%rax
mov    $0x100,%esi
lea    0xea2(%rip),%rdx
lea    0xea6(%rip),%rcx
lea    0xeae(%rip),%r8
mov    %rax,(%rsp)
call   1030 <snprintf@plt>
lea    -0x1a0(%rbp),%rdi
lea    -0xa0(%rbp),%rsi
mov    $0x80,%edx
call   1050 <file_to_buf@plt>
cmp    $0xffffffff,%eax
jne    11a3 <cgmemread+0x73>
movq   $0x0,-0x8(%rbp)
jmp    11dc <cgmemread+0xac>
lea    -0xa0(%rbp),%rdi
lea    -0x1a8(%rbp),%rsi
call   1040 <libres_ulong@plt>
cmp    $0x0,%rax
je     11cd <cgmemread+0x9d>
movq   $0x0,-0x8(%rbp)
jmp    11dc <cgmemread+0xac>
mov    -0x1a8(%rbp),%rax
shr    $0xa,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cgmemread>:
push   %rbx
sub    $0x190,%rsp
mov    %rdi,%r9
mov    %rsi,(%rsp)
lea    0xeba(%rip),%rdx
lea    0xebe(%rip),%rcx
lea    0xec6(%rip),%r8
lea    0x90(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1030 <snprintf@plt>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x80,%edx
call   1050 <file_to_buf@plt>
cmp    $0xffffffff,%eax
je     1194 <cgmemread+0x64>
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1040 <libres_ulong@plt>
test   %rax,%rax
je     1198 <cgmemread+0x68>
xor    %eax,%eax
jmp    11a1 <cgmemread+0x71>
mov    0x8(%rsp),%rax
shr    $0xa,%rax
add    $0x190,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [11 x i8] c"%s/%s%s/%s\00"
@.str.1 = [15 x i8] c"/sys/fs/cgroup\00"
@.str.2 = [7 x i8] c"memory\00"
define i64 @cgmemread(i8* %0, i8* %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [128 x i8]
%7 = alloca [256 x i8]
%8 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%10 = load i8*, i8** %4
%11 = load i8*, i8** %5
%12 = call i32 @snprintf(i8* %9, i32 256, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %10, i8* %11)
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%14 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%15 = call i32 @file_to_buf(i8* %13, i8* %14, i32 128)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %2
store i64 0, i64* %3
br label %26
18: ; preds = %2
%19 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%20 = call i64 @libres_ulong(i8* %19, i64* %8)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %18
store i64 0, i64* %3
br label %26
23: ; preds = %18
%24 = load i64, i64* %8
%25 = udiv i64 %24, 1024
store i64 %25, i64* %3
br label %26
26: ; preds = %23, %22, %17
%27 = load i64, i64* %3
ret i64 %27
}

LLVM IR (-O2):
@.str = [11 x i8] c"%s/%s%s/%s\00"
@.str.1 = [15 x i8] c"/sys/fs/cgroup\00"
@.str.2 = [7 x i8] c"memory\00"
define i64 @cgmemread(i8* %0, i8* %1) {
%3 = alloca [128 x i8]
%4 = alloca [256 x i8]
%5 = alloca i64
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%8 = bitcast i64* %5 to i8*
%9 = call i32 @snprintf(i8* %7, i32 256, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %1)
%10 = call i32 @file_to_buf(i8* %7, i8* %6, i32 128)
%11 = icmp eq i32 %10, -1
br i1 %11, label %18, label %12
12: ; preds = %2
%13 = call i64 @libres_ulong(i8* %6, i64* %5)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i64, i64* %5
%17 = lshr i64 %16, 10
br label %18
18: ; preds = %12, %2, %15
%19 = phi i64 [ %17, %15 ], [ 0, %2 ], [ 0, %12 ]
ret i64 %19
}