#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*/ *);
int /*<<< orphan*/ fgets(char *, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ parseGroupLine(char *, struct group *);
int getgrgid_r(gid_t gid, struct group *grp, char *buf, size_t buflen,
               struct group **result) {
  FILE *groupFile = fopen("/etc/group", "r");
  if (!groupFile) {
    *result = NULL;
    return -1;
  }
  char *line = buf;
  while (1) {
    if (!fgets(line, buflen, groupFile)) {
      break;
    }
    parseGroupLine(line, grp);
    if (grp->gr_gid == gid) {
      *result = grp;
      fclose(groupFile);
      return 0;
    }
  }
  *result = NULL;
  fclose(groupFile);
  return -1;
}


x86 assembly (clang, -O0):
<getgrgid_r>:
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    %r8,-0x30(%rbp)
lea    0xe9d(%rip),%rdi
lea    0xea1(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1195 <getgrgid_r+0x55>
mov    -0x30(%rbp),%rax
movq   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    121a <getgrgid_r+0xda>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1040 <fgets@plt>
cmp    $0x0,%eax
jne    11bc <getgrgid_r+0x7c>
jmp    11ff <getgrgid_r+0xbf>
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <parseGroupLine@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
cmp    -0x10(%rbp),%rax
jne    11fa <getgrgid_r+0xba>
mov    -0x18(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    121a <getgrgid_r+0xda>
jmp    119d <getgrgid_r+0x5d>
mov    -0x30(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getgrgid_r>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r14
mov    %rcx,%r15
mov    %rdx,%rbx
mov    %rsi,%r12
mov    %rdi,%r13
lea    0xe9f(%rip),%rdi
lea    0xea3(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11b2 <getgrgid_r+0x72>
mov    %rax,%rbp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %rbp,%rdx
call   1040 <fgets@plt>
test   %eax,%eax
je     11bb <getgrgid_r+0x7b>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1050 <parseGroupLine@plt>
cmp    %r13,(%r12)
jne    1180 <getgrgid_r+0x40>
mov    %r12,(%r14)
mov    %rbp,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    11cf <getgrgid_r+0x8f>
movq   $0x0,(%r14)
jmp    11ca <getgrgid_r+0x8a>
movq   $0x0,(%r14)
mov    %rbp,%rdi
call   1030 <fclose@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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