#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 {
  scalar_t__ gr_gid;
};
typedef scalar_t__ gid_t;
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 *);
struct group *getgrgid(gid_t gid) {
  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 (entry->gr_gid == gid) {
      fclose(stream);
      return entry;
    }
  }
  fclose(stream);
  return NULL;
}


x86 assembly (clang, -O0):
<getgrgid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xead(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1174 <getgrgid+0x34>
movq   $0x0,-0x8(%rbp)
jmp    11f9 <getgrgid+0xb9>
mov    -0x18(%rbp),%rdi
lea    0xe8c(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a0 <getgrgid+0x60>
movq   $0x0,-0x8(%rbp)
jmp    11f9 <getgrgid+0xb9>
jmp    11a5 <getgrgid+0x65>
mov    -0x20(%rbp),%rdi
call   1060 <fgetgrent@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     11e8 <getgrgid+0xa8>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
cmp    -0x10(%rbp),%rax
jne    11e3 <getgrgid+0xa3>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11f9 <getgrgid+0xb9>
jmp    11a5 <getgrgid+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):
<getgrgid>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
lea    0xeb1(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1187 <getgrgid+0x47>
lea    0xeab(%rip),%rsi
mov    %rax,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     1187 <getgrgid+0x47>
mov    %rax,%r15
mov    %r15,%rdi
call   1060 <fgetgrent@plt>
test   %rax,%rax
je     118b <getgrgid+0x4b>
mov    %rax,%rbx
cmp    %r14,(%rax)
jne    1170 <getgrgid+0x30>
jmp    118d <getgrgid+0x4d>
xor    %ebx,%ebx
jmp    1195 <getgrgid+0x55>
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 { i64 }
@.str = [11 x i8] c"MOCK_GROUP\00"
@.str.1 = [2 x i8] c"r\00"
define %struct.group* @getgrgid(i64 %0) {
%2 = alloca %struct.group*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca %struct.group*
store i64 %0, i64* %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 %36
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 %36
17: ; preds = %11
br label %18
18: ; preds = %32, %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 %33
22: ; preds = %18
%23 = load %struct.group*, %struct.group** %6
%24 = getelementptr inbounds %struct.group, %struct.group* %23, i32 0, i32 0
%25 = load i64, i64* %24
%26 = load i64, i64* %3
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %32
28: ; preds = %22
%29 = load i32*, i32** %5
%30 = call i32 @fclose(i32* %29)
%31 = load %struct.group*, %struct.group** %6
store %struct.group* %31, %struct.group** %2
br label %36
32: ; preds = %22
br label %18
33: ; preds = %18
%34 = load i32*, i32** %5
%35 = call i32 @fclose(i32* %34)
store %struct.group* null, %struct.group** %2
br label %36
36: ; preds = %33, %28, %16, %10
%37 = load %struct.group*, %struct.group** %2
ret %struct.group* %37
}

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