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

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ getenvvar(char **, size_t *, int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char const *);
int /*<<< orphan*/ strncmp(char *, char const *, int /*<<< orphan*/);
int has_makeflags(int pid) {
  int rc = 0;
  char buf[1024];
  sprintf(buf, "/proc/%d/environ", pid);
  FILE *f = fopen(buf, "r");
  if (!f) {
    fprintf(stderr, "can't open %s: %s\n", buf, strerror(errno));
    return -1;
  }
  char *nameval = NULL;
  size_t size = 0;
  const char *needle = "MAKEFLAGS=";
  while (getenvvar(&nameval, &size, f) > 0) {
    if (!strncmp(nameval, needle, strlen(needle))) {
      rc = 1;
      break;
    }
  }
  if (nameval)
    free(nameval);
  fclose(f);
  return rc;
}


x86 assembly (clang, -O0):
<has_makeflags>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe4a(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x410(%rbp),%rdi
lea    0xe48(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
jne    1236 <has_makeflags+0xa6>
mov    0x2df6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x43c(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x438(%rbp)
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x43c(%rbp),%edi
mov    -0x438(%rbp),%rdx
mov    %rax,%rcx
lea    0xdee(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1301 <has_makeflags+0x171>
movq   $0x0,-0x420(%rbp)
movq   $0x0,-0x428(%rbp)
lea    0xdd3(%rip),%rax
mov    %rax,-0x430(%rbp)
mov    -0x418(%rbp),%rdx
lea    -0x420(%rbp),%rdi
lea    -0x428(%rbp),%rsi
call   1040 <getenvvar@plt>
cmp    $0x0,%rax
jle    12d5 <has_makeflags+0x145>
mov    -0x420(%rbp),%rax
mov    %rax,-0x450(%rbp)
mov    -0x430(%rbp),%rax
mov    %rax,-0x448(%rbp)
mov    -0x430(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x450(%rbp),%rdi
mov    -0x448(%rbp),%rsi
mov    %eax,%edx
call   1050 <strncmp@plt>
cmp    $0x0,%eax
jne    12d0 <has_makeflags+0x140>
movl   $0x1,-0xc(%rbp)
jmp    12d5 <has_makeflags+0x145>
jmp    125a <has_makeflags+0xca>
cmpq   $0x0,-0x420(%rbp)
je     12ef <has_makeflags+0x15f>
mov    -0x420(%rbp),%rdi
call   1030 <free@plt>
mov    -0x418(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<has_makeflags>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x410,%rsp
mov    %edi,%edx
lea    0xe57(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   10a0 <sprintf@plt>
lea    0xe54(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1227 <has_makeflags+0x97>
mov    %rax,%r12
movq   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
mov    %rsp,%r14
lea    0x8(%rsp),%r15
lea    0xe39(%rip),%r13
nopl   (%rax)
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1040 <getenvvar@plt>
test   %rax,%rax
jle    125b <has_makeflags+0xcb>
mov    (%rsp),%rbx
mov    %r13,%rdi
call   1070 <strlen@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %eax,%edx
call   1050 <strncmp@plt>
test   %eax,%eax
jne    11f0 <has_makeflags+0x60>
mov    $0x1,%ebx
jmp    125d <has_makeflags+0xcd>
mov    0x2db2(%rip),%rax
mov    (%rax),%ebx
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdce(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
mov    %rax,%rcx
call   1080 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    1273 <has_makeflags+0xe3>
xor    %ebx,%ebx
mov    (%rsp),%rdi
test   %rdi,%rdi
je     126b <has_makeflags+0xdb>
call   1030 <free@plt>
mov    %r12,%rdi
call   1060 <fclose@plt>
mov    %ebx,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/proc/%d/environ\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [19 x i8] c"can't open %s: %s\0A\00"
@errno = global i32 0
@.str.3 = [11 x i8] c"MAKEFLAGS=\00"
define i32 @has_makeflags(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
store i32 %0, i32* %3
store i32 0, i32* %4
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%11 = load i32, i32* %3
%12 = call i32 @sprintf(i8* %10, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %6
%15 = load i32*, i32** %6
%16 = icmp ne i32* %15, null
br i1 %16, label %23, label %17
17: ; preds = %1
%18 = load i32, i32* @stderr
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %19, i8* %21)
store i32 -1, i32* %2
br label %47
23: ; preds = %1
store i8* null, i8** %7
store i64 0, i64* %8
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8** %9
br label %24
24: ; preds = %36, %23
%25 = load i32*, i32** %6
%26 = call i64 @getenvvar(i8** %7, i64* %8, i32* %25)
%27 = icmp sgt i64 %26, 0
br i1 %27, label %28, label %37
28: ; preds = %24
%29 = load i8*, i8** %7
%30 = load i8*, i8** %9
%31 = load i8*, i8** %9
%32 = call i32 @strlen(i8* %31)
%33 = call i32 @strncmp(i8* %29, i8* %30, i32 %32)
%34 = icmp ne i32 %33, 0
br i1 %34, label %36, label %35
35: ; preds = %28
store i32 1, i32* %4
br label %37
36: ; preds = %28
br label %24
37: ; preds = %35, %24
%38 = load i8*, i8** %7
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = load i8*, i8** %7
%42 = call i32 @free(i8* %41)
br label %43
43: ; preds = %40, %37
%44 = load i32*, i32** %6
%45 = call i32 @fclose(i32* %44)
%46 = load i32, i32* %4
store i32 %46, i32* %2
br label %47
47: ; preds = %43, %17
%48 = load i32, i32* %2
ret i32 %48
}

LLVM IR (-O2):
@.str = [17 x i8] c"/proc/%d/environ\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [19 x i8] c"can't open %s: %s\0A\00"
@errno = global i32 0
@.str.3 = [11 x i8] c"MAKEFLAGS=\00"
define i32 @has_makeflags(i32 %0) {
%2 = alloca [1024 x i8]
%3 = alloca i8*
%4 = alloca i64
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%6 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %14
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = tail call i8* @strerror(i32 %11)
%13 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %5, i8* %12)
br label %33
14: ; preds = %1
%15 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%16 = bitcast i64* %4 to i8*
store i64 0, i64* %4
br label %17
17: ; preds = %20, %14
%18 = call i64 @getenvvar(i8** %3, i64* %4, i32* %7)
%19 = icmp sgt i64 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %17
%21 = load i8*, i8** %3
%22 = call i32 @strlen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%23 = call i32 @strncmp(i8* %21, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %17
25: ; preds = %20, %17
%26 = phi i32 [ 0, %17 ], [ 1, %20 ]
%27 = load i8*, i8** %3
%28 = icmp eq i8* %27, null
br i1 %28, label %31, label %29
29: ; preds = %25
%30 = call i32 @free(i8* %27)
br label %31
31: ; preds = %29, %25
%32 = call i32 @fclose(i32* %7)
br label %33
33: ; preds = %31, %9
%34 = phi i32 [ %26, %31 ], [ -1, %9 ]
ret i32 %34
}