#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 */
struct group {
  int /*<<< orphan*/ gr_name;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
struct group *fgetgrent(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
char *getenv(char *);
scalar_t__ strcmp(int /*<<< orphan*/, char const *);
struct group *getgrnam(const char *name) {
  const char *path = getenv("MOCK_GROUP");
  if (!path)
    return NULL;
  FILE *stream = fopen(path, "r");
  if (!stream)
    return NULL;
  struct group *entry;
  while ((entry = fgetgrent(stream))) {
    if (strcmp(entry->gr_name, name) == 0) {
      fclose(stream);
      return entry;
    }
  }
  fclose(stream);
  return NULL;
}


x86 assembly (clang, -O0):
<getgrnam>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe9d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1184 <getgrnam+0x34>
movq   $0x0,-0x8(%rbp)
jmp    1211 <getgrnam+0xc1>
mov    -0x18(%rbp),%rdi
lea    0xe7c(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b0 <getgrnam+0x60>
movq   $0x0,-0x8(%rbp)
jmp    1211 <getgrnam+0xc1>
jmp    11b5 <getgrnam+0x65>
mov    -0x20(%rbp),%rdi
call   1070 <fgetgrent@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1200 <getgrnam+0xb0>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    11fb <getgrnam+0xab>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1211 <getgrnam+0xc1>
jmp    11b5 <getgrnam+0x65>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getgrnam>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
lea    0xea1(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11a1 <getgrnam+0x51>
lea    0xe9b(%rip),%rsi
mov    %rax,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11a1 <getgrnam+0x51>
mov    %rax,%r15
mov    %r15,%rdi
call   1070 <fgetgrent@plt>
test   %rax,%rax
je     11a5 <getgrnam+0x55>
mov    %rax,%rbx
mov    (%rax),%edi
mov    %r14,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    1180 <getgrnam+0x30>
jmp    11a7 <getgrnam+0x57>
xor    %ebx,%ebx
jmp    11af <getgrnam+0x5f>
xor    %ebx,%ebx
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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