#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*/ O_RDONLY;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int openat(int, char *, int /*<<< orphan*/);
size_t read(int, char *, int);
char *realloc(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char const *);
int strlen(char const *);
char *strstr(char *, char *);
int getProcessStatus(int proc_fd, const char *process, const char *field) {
  int ret = -1;
  char *buf = malloc(strlen(process) + 80);
  sprintf(buf, "%s/status", process);
  int fd = openat(proc_fd, buf, O_RDONLY);
  if (fd >= 0) {
    buf = realloc(buf, 4097);
    size_t sz = read(fd, buf, 4096);
    if (sz > 0) {
      buf[sz] = '\000';
      char *f = malloc(strlen(field) + 4);
      sprintf(f, "\n%s:\t", field);
      char *ptr = strstr(buf, f);
      if (ptr) {
        ret = atoi(ptr + strlen(f));
      }
      free(f);
    }
    close(fd);
  }
  free(buf);
  return ret;
}


x86 assembly (clang, -O0):
<getProcessStatus>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%edi
add    $0x50,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe20(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
call   1050 <openat@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jl     12bf <getProcessStatus+0x11f>
mov    -0x28(%rbp),%rdi
mov    $0x1001,%esi
call   1090 <realloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    $0x1000,%edx
call   1070 <read@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jbe    12b7 <getProcessStatus+0x117>
mov    -0x28(%rbp),%rax
mov    -0x38(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%edi
add    $0x4,%edi
call   1080 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xd9f(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   10c0 <strstr@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     12ae <getProcessStatus+0x10e>
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x40(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x50(%rbp),%rdi
cltq
add    %rax,%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getProcessStatus>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbp
mov    %edi,%r15d
mov    %rsi,%rdi
call   1040 <strlen@plt>
lea    0x50(%rax),%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
lea    0xe35(%rip),%rsi
mov    %rax,%rdi
mov    %rbp,%rdx
call   10b0 <sprintf@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
mov    %rbx,%rsi
call   1050 <openat@plt>
test   %eax,%eax
js     126b <getProcessStatus+0xcb>
mov    %eax,%r12d
mov    %rbx,%rdi
mov    $0x1001,%esi
call   1090 <realloc@plt>
mov    %rax,%rbx
mov    %r12d,%edi
mov    %rax,%rsi
mov    $0x1000,%edx
call   1070 <read@plt>
test   %rax,%rax
je     1272 <getProcessStatus+0xd2>
movb   $0x0,(%rbx,%rax,1)
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    0x4(%rax),%edi
call   1080 <malloc@plt>
mov    %rax,%r15
lea    0xdd6(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
call   10b0 <sprintf@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   10c0 <strstr@plt>
test   %rax,%rax
je     1279 <getProcessStatus+0xd9>
mov    %rax,%rbp
mov    %r15,%rdi
call   1040 <strlen@plt>
cltq
add    %rax,%rbp
mov    %rbp,%rdi
call   10a0 <atoi@plt>
mov    %eax,%ebp
jmp    127e <getProcessStatus+0xde>
mov    $0xffffffff,%ebp
jmp    128e <getProcessStatus+0xee>
mov    $0xffffffff,%ebp
jmp    1286 <getProcessStatus+0xe6>
mov    $0xffffffff,%ebp
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r12d,%edi
call   1060 <close@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"%s/status\00"
@O_RDONLY = global i32 0
@.str.1 = [6 x i8] c"\0A%s:\09\00"
define i32 @getProcessStatus(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i8* %2, i8** %6
store i32 -1, i32* %7
%13 = load i8*, i8** %5
%14 = call i32 @strlen(i8* %13)
%15 = add nsw i32 %14, 80
%16 = call i8* @malloc(i32 %15)
store i8* %16, i8** %8
%17 = load i8*, i8** %8
%18 = load i8*, i8** %5
%19 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = load i32, i32* %4
%21 = load i8*, i8** %8
%22 = load i32, i32* @O_RDONLY
%23 = call i32 @openat(i32 %20, i8* %21, i32 %22)
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = icmp sge i32 %24, 0
br i1 %25, label %26, label %63
26: ; preds = %3
%27 = load i8*, i8** %8
%28 = call i8* @realloc(i8* %27, i32 4097)
store i8* %28, i8** %8
%29 = load i32, i32* %9
%30 = load i8*, i8** %8
%31 = call i64 @read(i32 %29, i8* %30, i32 4096)
store i64 %31, i64* %10
%32 = load i64, i64* %10
%33 = icmp ugt i64 %32, 0
br i1 %33, label %34, label %60
34: ; preds = %26
%35 = load i8*, i8** %8
%36 = load i64, i64* %10
%37 = getelementptr inbounds i8, i8* %35, i64 %36
store i8 0, i8* %37
%38 = load i8*, i8** %6
%39 = call i32 @strlen(i8* %38)
%40 = add nsw i32 %39, 4
%41 = call i8* @malloc(i32 %40)
store i8* %41, i8** %11
%42 = load i8*, i8** %11
%43 = load i8*, i8** %6
%44 = call i32 @sprintf(i8* %42, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %43)
%45 = load i8*, i8** %8
%46 = load i8*, i8** %11
%47 = call i8* @strstr(i8* %45, i8* %46)
store i8* %47, i8** %12
%48 = load i8*, i8** %12
%49 = icmp ne i8* %48, null
br i1 %49, label %50, label %57
50: ; preds = %34
%51 = load i8*, i8** %12
%52 = load i8*, i8** %11
%53 = call i32 @strlen(i8* %52)
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %51, i64 %54
%56 = call i32 @atoi(i8* %55)
store i32 %56, i32* %7
br label %57
57: ; preds = %50, %34
%58 = load i8*, i8** %11
%59 = call i32 @free(i8* %58)
br label %60
60: ; preds = %57, %26
%61 = load i32, i32* %9
%62 = call i32 @close(i32 %61)
br label %63
63: ; preds = %60, %3
%64 = load i8*, i8** %8
%65 = call i32 @free(i8* %64)
%66 = load i32, i32* %7
ret i32 %66
}

LLVM IR (-O2):
@.str = [10 x i8] c"%s/status\00"
@O_RDONLY = global i32 0
@.str.1 = [6 x i8] c"\0A%s:\09\00"
define i32 @getProcessStatus(i32 %0, i8* %1, i8* %2) {
%4 = tail call i32 @strlen(i8* %1)
%5 = add nsw i32 %4, 80
%6 = tail call i8* @malloc(i32 %5)
%7 = tail call i32 @sprintf(i8* dereferenceable(1) %6, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %1)
%8 = load i32, i32* @O_RDONLY
%9 = tail call i32 @openat(i32 %0, i8* %6, i32 %8)
%10 = icmp sgt i32 %9, -1
br i1 %10, label %11, label %34
11: ; preds = %3
%12 = tail call i8* @realloc(i8* %6, i32 4097)
%13 = tail call i64 @read(i32 %9, i8* %12, i32 4096)
%14 = icmp eq i64 %13, 0
br i1 %14, label %31, label %15
15: ; preds = %11
%16 = getelementptr inbounds i8, i8* %12, i64 %13
store i8 0, i8* %16
%17 = tail call i32 @strlen(i8* %2)
%18 = add nsw i32 %17, 4
%19 = tail call i8* @malloc(i32 %18)
%20 = tail call i32 @sprintf(i8* dereferenceable(1) %19, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%21 = tail call i8* @strstr(i8* dereferenceable(1) %12, i8* dereferenceable(1) %19)
%22 = icmp eq i8* %21, null
br i1 %22, label %28, label %23
23: ; preds = %15
%24 = tail call i32 @strlen(i8* %19)
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %21, i64 %25
%27 = tail call i32 @atoi(i8* %26)
br label %28
28: ; preds = %23, %15
%29 = phi i32 [ %27, %23 ], [ -1, %15 ]
%30 = tail call i32 @free(i8* %19)
br label %31
31: ; preds = %28, %11
%32 = phi i32 [ %29, %28 ], [ -1, %11 ]
%33 = tail call i32 @close(i32 %9)
br label %34
34: ; preds = %31, %3
%35 = phi i8* [ %12, %31 ], [ %6, %3 ]
%36 = phi i32 [ %32, %31 ], [ -1, %3 ]
%37 = tail call i32 @free(i8* %35)
ret i32 %36
}