#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 ENOENT;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
int sigar_proc_nfs_gets(char *file, char *tok, char *buffer, size_t size) {
  int status = ENOENT;
  int len = strlen(tok);
  FILE *fp = fopen(file, "r");
  if (!fp) {
    return (20000 + 1);
  }
  while (fgets(buffer, size, fp)) {
    if ((strncmp(buffer, tok, len) == 0)) {
      status = 0;
      break;
    }
  }
  fclose(fp);
  return status;
}


x86 assembly (clang, -O0):
<sigar_proc_nfs_gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    0x2e69(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe75(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11ab <sigar_proc_nfs_gets+0x5b>
movl   $0x4e21,-0x4(%rbp)
jmp    1205 <sigar_proc_nfs_gets+0xb5>
jmp    11b0 <sigar_proc_nfs_gets+0x60>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     11f6 <sigar_proc_nfs_gets+0xa6>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11f1 <sigar_proc_nfs_gets+0xa1>
movl   $0x0,-0x2c(%rbp)
jmp    11f6 <sigar_proc_nfs_gets+0xa6>
jmp    11b0 <sigar_proc_nfs_gets+0x60>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sigar_proc_nfs_gets>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%rbp
mov    %rsi,%r12
mov    %rdi,%rbx
mov    0x2e6a(%rip),%rax
mov    (%rax),%r14d
mov    %rsi,%rdi
call   1050 <strlen@plt>
mov    %eax,%r13d
lea    0xe7d(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11d3 <sigar_proc_nfs_gets+0x83>
mov    %rax,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     11c9 <sigar_proc_nfs_gets+0x79>
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %r13d,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11a0 <sigar_proc_nfs_gets+0x50>
xor    %r14d,%r14d
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    11d9 <sigar_proc_nfs_gets+0x89>
mov    $0x4e21,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@ENOENT = global i32 0
@.str = [2 x i8] c"r\00"
define i32 @sigar_proc_nfs_gets(i8* %0, i8* %1, i8* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i64 %3, i64* %9
%13 = load i32, i32* @ENOENT
store i32 %13, i32* %10
%14 = load i8*, i8** %7
%15 = call i32 @strlen(i8* %14)
store i32 %15, i32* %11
%16 = load i8*, i8** %6
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %17, i32** %12
%18 = load i32*, i32** %12
%19 = icmp ne i32* %18, null
br i1 %19, label %21, label %20
20: ; preds = %4
store i32 20001, i32* %5
br label %40
21: ; preds = %4
br label %22
22: ; preds = %35, %21
%23 = load i8*, i8** %8
%24 = load i64, i64* %9
%25 = load i32*, i32** %12
%26 = call i64 @fgets(i8* %23, i64 %24, i32* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %36
28: ; preds = %22
%29 = load i8*, i8** %8
%30 = load i8*, i8** %7
%31 = load i32, i32* %11
%32 = call i64 @strncmp(i8* %29, i8* %30, i32 %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %28
store i32 0, i32* %10
br label %36
35: ; preds = %28
br label %22
36: ; preds = %34, %22
%37 = load i32*, i32** %12
%38 = call i32 @fclose(i32* %37)
%39 = load i32, i32* %10
store i32 %39, i32* %5
br label %40
40: ; preds = %36, %20
%41 = load i32, i32* %5
ret i32 %41
}

LLVM IR (-O2):
@ENOENT = global i32 0
@.str = [2 x i8] c"r\00"
define i32 @sigar_proc_nfs_gets(i8* nocapture %0, i8* %1, i8* %2, i64 %3) {
%5 = load i32, i32* @ENOENT
%6 = tail call i32 @strlen(i8* %1)
%7 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %18, label %9
9: ; preds = %4, %12
%10 = tail call i64 @fgets(i8* %2, i64 %3, i32* %7)
%11 = icmp eq i64 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %9
%13 = tail call i64 @strncmp(i8* %2, i8* %1, i32 %6)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %9
15: ; preds = %12, %9
%16 = phi i32 [ %5, %9 ], [ 0, %12 ]
%17 = tail call i32 @fclose(i32* %7)
br label %18
18: ; preds = %4, %15
%19 = phi i32 [ %16, %15 ], [ 20001, %4 ]
ret i32 %19
}