#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 EIO;
int ERANGE;
int /*<<< orphan*/ __grpmemparse(struct group *, char *);
int __grpnextent(int /*<<< orphan*/ *, struct group *, char *, char **);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ strcmp(int /*<<< orphan*/, char const *);
int getgrnam_r(const char *name, struct group *grp, char *buffer, size_t bufsiz,
               struct group **result) {
  if (bufsiz < 0x1000) {
    return ERANGE;
  };
  FILE *fp = fopen("/etc/group", "r");
  if (fp == NULL) {
    return EIO;
  };
  char *nextbuf;
  while (__grpnextent(fp, grp, buffer, &nextbuf) != -1) {
    if (strcmp(grp->gr_name, name) == 0) {
      __grpmemparse(grp, nextbuf);
      *result = grp;
      fclose(fp);
      return 0;
    };
  };
  fclose(fp);
  *result = NULL;
  return 0;
}


x86 assembly (clang, -O0):
<getgrnam_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)
cmpq   $0x1000,-0x28(%rbp)
jae    118b <getgrnam_r+0x3b>
mov    0x2e67(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1247 <getgrnam_r+0xf7>
lea    0xe6e(%rip),%rdi
lea    0xe72(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11be <getgrnam_r+0x6e>
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1247 <getgrnam_r+0xf7>
jmp    11c3 <getgrnam_r+0x73>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1040 <__grpnextent@plt>
cmp    $0xffffffff,%eax
je     122c <getgrnam_r+0xdc>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1227 <getgrnam_r+0xd7>
mov    -0x18(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1050 <__grpmemparse@plt>
mov    -0x18(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    1247 <getgrnam_r+0xf7>
jmp    11c3 <getgrnam_r+0x73>
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x30(%rbp),%rax
movq   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getgrnam_r>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0xfff,%rcx
ja     1172 <getgrnam_r+0x22>
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
jmp    11fa <getgrnam_r+0xaa>
mov    %r8,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r12
lea    0xe7b(%rip),%rdi
lea    0xe7f(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11de <getgrnam_r+0x8e>
mov    %rax,%r13
mov    %rsp,%rbp
nopl   0x0(%rax)
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %rbp,%rcx
call   1040 <__grpnextent@plt>
cmp    $0xffffffff,%eax
je     11e9 <getgrnam_r+0x99>
mov    (%rbx),%edi
mov    %r12,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    11a0 <getgrnam_r+0x50>
mov    (%rsp),%rsi
mov    %rbx,%rdi
call   1050 <__grpmemparse@plt>
mov    %rbx,(%r14)
mov    %r13,%rdi
call   1030 <fclose@plt>
jmp    11f8 <getgrnam_r+0xa8>
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
jmp    11fa <getgrnam_r+0xaa>
mov    %r13,%rdi
call   1030 <fclose@plt>
movq   $0x0,(%r14)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.group = type { i32 }
@ERANGE = global i32 0
@.str = [11 x i8] c"/etc/group\00"
@.str.1 = [2 x i8] c"r\00"
@EIO = global i32 0
define i32 @getgrnam_r(i8* %0, %struct.group* %1, i8* %2, i64 %3, %struct.group** nocapture writeonly %4) {
%6 = alloca i8*
%7 = icmp ult i64 %3, 4096
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = load i32, i32* @ERANGE
br label %32
10: ; preds = %5
%11 = 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))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = load i32, i32* @EIO
br label %32
15: ; preds = %10
%16 = bitcast i8** %6 to i8*
%17 = getelementptr inbounds %struct.group, %struct.group* %1, i64 0, i32 0
br label %18
18: ; preds = %21, %15
%19 = call i32 @__grpnextent(i32* %11, %struct.group* %1, i8* %2, i8** %6)
%20 = icmp eq i32 %19, -1
br i1 %20, label %29, label %21
21: ; preds = %18
%22 = load i32, i32* %17
%23 = call i64 @strcmp(i32 %22, i8* %0)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %18
25: ; preds = %21
%26 = load i8*, i8** %6
%27 = call i32 @__grpmemparse(%struct.group* %1, i8* %26)
store %struct.group* %1, %struct.group** %4
%28 = call i32 @fclose(i32* %11)
br label %31
29: ; preds = %18
%30 = call i32 @fclose(i32* %11)
store %struct.group* null, %struct.group** %4
br label %31
31: ; preds = %29, %25
br label %32
32: ; preds = %13, %31, %8
%33 = phi i32 [ %9, %8 ], [ %14, %13 ], [ 0, %31 ]
ret i32 %33
}