#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 /*<<< orphan*/ isdigit(char const);
char *memstr(char const *, char const *, unsigned int);
unsigned int parse_unsigned_int(char const *);
int /*<<< orphan*/ strlen(char const *);
int parse_meminfo_field(const char *meminfo, const unsigned int size,
                        const char *field, unsigned int *value) {
  const char *p = meminfo;
  unsigned int len = size;
  *value = 0;
  do {
    p = memstr(p, field, len);
    if (!p) {
      return -1;
    }
  } while (p != meminfo && p[-1] != '\n' && p++ && len--);
  p += strlen(field);
  while (!isdigit(*p))
    p++;
  *value = parse_unsigned_int(p);
  return 0;
}


x86 assembly (clang, -O0):
<parse_meminfo_field>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1050 <memstr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    119a <parse_meminfo_field+0x5a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1268 <parse_meminfo_field+0x128>
jmp    119f <parse_meminfo_field+0x5f>
mov    -0x30(%rbp),%rcx
xor    %eax,%eax
cmp    -0x10(%rbp),%rcx
mov    %al,-0x35(%rbp)
je     11fa <parse_meminfo_field+0xba>
mov    -0x30(%rbp),%rax
movsbl -0x1(%rax),%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x35(%rbp)
je     11fa <parse_meminfo_field+0xba>
mov    -0x30(%rbp),%rcx
mov    %rcx,%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x35(%rbp)
je     11fa <parse_meminfo_field+0xba>
mov    -0x34(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x34(%rbp)
cmp    $0x0,%eax
setne  %al
mov    %al,-0x35(%rbp)
mov    -0x35(%rbp),%al
test   $0x1,%al
jne    116f <parse_meminfo_field+0x2f>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movsbl (%rax),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    123f <parse_meminfo_field+0xff>
jmp    1250 <parse_meminfo_field+0x110>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    121e <parse_meminfo_field+0xde>
mov    -0x30(%rbp),%rdi
call   1060 <parse_unsigned_int@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_meminfo_field>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %rdx,%r15
mov    %esi,%ebp
mov    %rdi,%r12
movl   $0x0,(%rcx)
mov    %rdi,%rbx
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1050 <memstr@plt>
test   %rax,%rax
je     118b <parse_meminfo_field+0x4b>
mov    %rax,%rbx
cmp    %r12,%rax
je     1192 <parse_meminfo_field+0x52>
cmpb   $0xa,-0x1(%rbx)
je     1195 <parse_meminfo_field+0x55>
add    $0x1,%rbx
add    $0xffffffff,%ebp
jb     1160 <parse_meminfo_field+0x20>
jmp    1195 <parse_meminfo_field+0x55>
mov    $0xffffffff,%eax
jmp    11ce <parse_meminfo_field+0x8e>
mov    %r12,%rbx
mov    %r15,%rdi
call   1040 <strlen@plt>
cltq
add    %rax,%rbx
add    $0xffffffffffffffff,%rbx
cs nopw 0x0(%rax,%rax,1)
movsbl 0x1(%rbx),%edi
add    $0x1,%rbx
call   1030 <isdigit@plt>
test   %eax,%eax
je     11b0 <parse_meminfo_field+0x70>
mov    %rbx,%rdi
call   1060 <parse_unsigned_int@plt>
mov    %eax,(%r14)
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @parse_meminfo_field(i8* %0, i32 %1, i8* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i8*
%11 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32* %3, i32** %9
%12 = load i8*, i8** %6
store i8* %12, i8** %10
%13 = load i32, i32* %7
store i32 %13, i32* %11
%14 = load i32*, i32** %9
store i32 0, i32* %14
br label %15
15: ; preds = %42, %4
%16 = load i8*, i8** %10
%17 = load i8*, i8** %8
%18 = load i32, i32* %11
%19 = call i8* @memstr(i8* %16, i8* %17, i32 %18)
store i8* %19, i8** %10
%20 = load i8*, i8** %10
%21 = icmp ne i8* %20, null
br i1 %21, label %23, label %22
22: ; preds = %15
store i32 -1, i32* %5
br label %63
23: ; preds = %15
br label %24
24: ; preds = %23
%25 = load i8*, i8** %10
%26 = load i8*, i8** %6
%27 = icmp ne i8* %25, %26
br i1 %27, label %28, label %42
28: ; preds = %24
%29 = load i8*, i8** %10
%30 = getelementptr inbounds i8, i8* %29, i64 -1
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp ne i32 %32, 10
br i1 %33, label %34, label %42
34: ; preds = %28
%35 = load i8*, i8** %10
%36 = getelementptr inbounds i8, i8* %35, i32 1
store i8* %36, i8** %10
%37 = icmp ne i8* %35, null
br i1 %37, label %38, label %42
38: ; preds = %34
%39 = load i32, i32* %11
%40 = add i32 %39, -1
store i32 %40, i32* %11
%41 = icmp ne i32 %39, 0
br label %42
42: ; preds = %38, %34, %28, %24
%43 = phi i1 [ false, %34 ], [ false, %28 ], [ false, %24 ], [ %41, %38 ]
br i1 %43, label %15, label %44
44: ; preds = %42
%45 = load i8*, i8** %8
%46 = call i32 @strlen(i8* %45)
%47 = load i8*, i8** %10
%48 = sext i32 %46 to i64
%49 = getelementptr inbounds i8, i8* %47, i64 %48
store i8* %49, i8** %10
br label %50
50: ; preds = %56, %44
%51 = load i8*, i8** %10
%52 = load i8, i8* %51
%53 = call i32 @isdigit(i8 signext %52)
%54 = icmp ne i32 %53, 0
%55 = xor i1 %54, true
br i1 %55, label %56, label %59
56: ; preds = %50
%57 = load i8*, i8** %10
%58 = getelementptr inbounds i8, i8* %57, i32 1
store i8* %58, i8** %10
br label %50
59: ; preds = %50
%60 = load i8*, i8** %10
%61 = call i32 @parse_unsigned_int(i8* %60)
%62 = load i32*, i32** %9
store i32 %61, i32* %62
store i32 0, i32* %5
br label %63
63: ; preds = %59, %22
%64 = load i32, i32* %5
ret i32 %64
}

LLVM IR (-O2):
define i32 @parse_meminfo_field(i8* %0, i32 %1, i8* %2, i32* nocapture writeonly %3) {
store i32 0, i32* %3
br label %5
5: ; preds = %16, %4
%6 = phi i8* [ %0, %4 ], [ %17, %16 ]
%7 = phi i32 [ %1, %4 ], [ %18, %16 ]
%8 = tail call i8* @memstr(i8* %6, i8* %2, i32 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %33, label %10
10: ; preds = %5
%11 = icmp eq i8* %8, %0
br i1 %11, label %20, label %12
12: ; preds = %10
%13 = getelementptr inbounds i8, i8* %8, i64 -1
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 10
br i1 %15, label %20, label %16
16: ; preds = %12
%17 = getelementptr inbounds i8, i8* %8, i64 1
%18 = add i32 %7, -1
%19 = icmp eq i32 %7, 0
br i1 %19, label %20, label %5
20: ; preds = %10, %12, %16
%21 = phi i8* [ %17, %16 ], [ %8, %12 ], [ %0, %10 ]
%22 = tail call i32 @strlen(i8* %2)
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
br label %25
25: ; preds = %25, %20
%26 = phi i8* [ %24, %20 ], [ %30, %25 ]
%27 = load i8, i8* %26
%28 = tail call i32 @isdigit(i8 signext %27)
%29 = icmp eq i32 %28, 0
%30 = getelementptr inbounds i8, i8* %26, i64 1
br i1 %29, label %25, label %31
31: ; preds = %25
%32 = tail call i32 @parse_unsigned_int(i8* %26)
store i32 %32, i32* %3
br label %33
33: ; preds = %5, %31
%34 = phi i32 [ 0, %31 ], [ -1, %5 ]
ret i32 %34
}