#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;
};
struct flock {
  scalar_t__ l_len;
  scalar_t__ l_start;
  int /*<<< orphan*/ l_whence;
  int /*<<< orphan*/ l_type;
};
typedef scalar_t__ gid_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ EINTR;
int EIO;
int ERANGE;
int /*<<< orphan*/ F_RDLCK;
int /*<<< orphan*/ F_SETLKW;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ __grpmemparse(struct group *, char *);
int __grpnextent(int /*<<< orphan*/ *, struct group *, char *, char **);
scalar_t__ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fcntl(int /*<<< orphan*/, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ memset(struct flock *, int /*<<< orphan*/, int);
int getgrgid_r(gid_t gid, 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;
  };
  struct flock lock;
  memset(&lock, 0, sizeof(struct flock));
  lock.l_type = F_RDLCK;
  lock.l_whence = SEEK_SET;
  lock.l_start = 0;
  lock.l_len = 0;
  int status;
  do {
    status = fcntl(fileno(fp), F_SETLKW, &lock);
  } while (status != 0 && errno == EINTR);
  char *nextbuf;
  while (__grpnextent(fp, grp, buffer, &nextbuf) != -1) {
    if (grp->gr_gid == gid) {
      __grpmemparse(grp, nextbuf);
      *result = grp;
      fclose(fp);
      return 0;
    };
  };
  fclose(fp);
  *result = NULL;
  return 0;
}


x86 assembly (clang, -O0):
<getgrgid_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%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    11ab <getgrgid_r+0x3b>
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12eb <getgrgid_r+0x17b>
lea    0xe4e(%rip),%rdi
lea    0xe52(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11de <getgrgid_r+0x6e>
mov    0x2dfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12eb <getgrgid_r+0x17b>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    0x2db3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
mov    -0x38(%rbp),%rdi
call   1080 <fileno@plt>
mov    %eax,%edi
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
lea    -0x50(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x54(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x54(%rbp)
mov    %al,-0x61(%rbp)
je     125f <getgrgid_r+0xef>
mov    0x2d7c(%rip),%rax
mov    (%rax),%rax
mov    0x2d62(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x61(%rbp)
mov    -0x61(%rbp),%al
test   $0x1,%al
jne    1216 <getgrgid_r+0xa6>
jmp    126f <getgrgid_r+0xff>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    -0x60(%rbp),%rcx
call   1050 <__grpnextent@plt>
cmp    $0xffffffff,%eax
je     12d0 <getgrgid_r+0x160>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
cmp    -0x10(%rbp),%rax
jne    12cb <getgrgid_r+0x15b>
mov    -0x18(%rbp),%rdi
mov    -0x60(%rbp),%rsi
call   1070 <__grpmemparse@plt>
mov    -0x18(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    12eb <getgrgid_r+0x17b>
jmp    126f <getgrgid_r+0xff>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rax
movq   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getgrgid_r>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
cmp    $0xfff,%rcx
ja     1195 <getgrgid_r+0x25>
mov    0x2e52(%rip),%rax
mov    (%rax),%eax
jmp    12b2 <getgrgid_r+0x142>
mov    %r8,%r14
mov    %rdx,%r15
mov    %rsi,%r13
mov    %rdi,%rbp
lea    0xe58(%rip),%rdi
lea    0xe5c(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1296 <getgrgid_r+0x126>
mov    %rax,%rbx
mov    %rbp,(%rsp)
mov    %r15,0x8(%rsp)
mov    %r14,0x10(%rsp)
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x34(%rsp)
mov    0x2dca(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
mov    0x2ddd(%rip),%r14
mov    0x2db6(%rip),%r12
mov    0x2d9f(%rip),%r15
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1080 <fileno@plt>
mov    (%r14),%esi
mov    %eax,%edi
mov    %rbp,%rdx
call   1030 <fcntl@plt>
test   %eax,%eax
je     1242 <getgrgid_r+0xd2>
mov    (%r15),%rax
cmp    %rax,(%r12)
je     1220 <getgrgid_r+0xb0>
lea    0x18(%rsp),%r15
mov    0x10(%rsp),%r14
mov    0x8(%rsp),%rbp
mov    (%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
mov    %r15,%rcx
call   1050 <__grpnextent@plt>
cmp    $0xffffffff,%eax
je     12a1 <getgrgid_r+0x131>
cmp    %r12,0x0(%r13)
jne    1260 <getgrgid_r+0xf0>
mov    0x18(%rsp),%rsi
mov    %r13,%rdi
call   1070 <__grpmemparse@plt>
mov    %r13,(%r14)
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    12b0 <getgrgid_r+0x140>
mov    0x2d33(%rip),%rax
mov    (%rax),%eax
jmp    12b2 <getgrgid_r+0x142>
mov    %rbx,%rdi
call   1040 <fclose@plt>
movq   $0x0,(%r14)
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.group = type { i64 }
%struct.flock = type { i64, i64, i32, 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
@F_RDLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLKW = global i32 0
@errno = global i64 0
@EINTR = global i64 0
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 %struct.flock
%14 = alloca i32
%15 = 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
%16 = load i64, i64* %10
%17 = icmp ult i64 %16, 4096
br i1 %17, label %18, label %20
18: ; preds = %5
%19 = load i32, i32* @ERANGE
store i32 %19, i32* %6
br label %74
20: ; preds = %5
%21 = 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* %21, i32** %12
%22 = load i32*, i32** %12
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = load i32, i32* @EIO
store i32 %25, i32* %6
br label %74
26: ; preds = %20
%27 = call i32 @memset(%struct.flock* %13, i32 0, i32 24)
%28 = load i32, i32* @F_RDLCK
%29 = getelementptr inbounds %struct.flock, %struct.flock* %13, i32 0, i32 3
store i32 %28, i32* %29
%30 = load i32, i32* @SEEK_SET
%31 = getelementptr inbounds %struct.flock, %struct.flock* %13, i32 0, i32 2
store i32 %30, i32* %31
%32 = getelementptr inbounds %struct.flock, %struct.flock* %13, i32 0, i32 1
store i64 0, i64* %32
%33 = getelementptr inbounds %struct.flock, %struct.flock* %13, i32 0, i32 0
store i64 0, i64* %33
br label %34
34: ; preds = %46, %26
%35 = load i32*, i32** %12
%36 = call i32 @fileno(i32* %35)
%37 = load i32, i32* @F_SETLKW
%38 = call i32 @fcntl(i32 %36, i32 %37, %struct.flock* %13)
store i32 %38, i32* %14
br label %39
39: ; preds = %34
%40 = load i32, i32* %14
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %39
%43 = load i64, i64* @errno
%44 = load i64, i64* @EINTR
%45 = icmp eq i64 %43, %44
br label %46
46: ; preds = %42, %39
%47 = phi i1 [ false, %39 ], [ %45, %42 ]
br i1 %47, label %34, label %48
48: ; preds = %46
br label %49
49: ; preds = %69, %48
%50 = load i32*, i32** %12
%51 = load %struct.group*, %struct.group** %8
%52 = load i8*, i8** %9
%53 = call i32 @__grpnextent(i32* %50, %struct.group* %51, i8* %52, i8** %15)
%54 = icmp ne i32 %53, -1
br i1 %54, label %55, label %70
55: ; preds = %49
%56 = load %struct.group*, %struct.group** %8
%57 = getelementptr inbounds %struct.group, %struct.group* %56, i32 0, i32 0
%58 = load i64, i64* %57
%59 = load i64, i64* %7
%60 = icmp eq i64 %58, %59
br i1 %60, label %61, label %69
61: ; preds = %55
%62 = load %struct.group*, %struct.group** %8
%63 = load i8*, i8** %15
%64 = call i32 @__grpmemparse(%struct.group* %62, i8* %63)
%65 = load %struct.group*, %struct.group** %8
%66 = load %struct.group**, %struct.group*** %11
store %struct.group* %65, %struct.group** %66
%67 = load i32*, i32** %12
%68 = call i32 @fclose(i32* %67)
store i32 0, i32* %6
br label %74
69: ; preds = %55
br label %49
70: ; preds = %49
%71 = load i32*, i32** %12
%72 = call i32 @fclose(i32* %71)
%73 = load %struct.group**, %struct.group*** %11
store %struct.group* null, %struct.group** %73
store i32 0, i32* %6
br label %74
74: ; preds = %70, %61, %24, %18
%75 = load i32, i32* %6
ret i32 %75
}

LLVM IR (-O2):
%struct.group = type { i64 }
%struct.flock = type { i64, i64, i32, 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
@F_RDLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLKW = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @getgrgid_r(i64 %0, %struct.group* %1, i8* %2, i64 %3, %struct.group** nocapture writeonly %4) {
%6 = alloca %struct.flock
%7 = alloca i8*
%8 = icmp ult i64 %3, 4096
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = load i32, i32* @ERANGE
br label %49
11: ; preds = %5
%12 = 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))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = load i32, i32* @EIO
br label %49
16: ; preds = %11
%17 = bitcast %struct.flock* %6 to i8*
%18 = call i32 @memset(%struct.flock* %6, i32 0, i32 24)
%19 = load i32, i32* @F_RDLCK
%20 = getelementptr inbounds %struct.flock, %struct.flock* %6, i64 0, i32 3
store i32 %19, i32* %20
%21 = load i32, i32* @SEEK_SET
%22 = getelementptr inbounds %struct.flock, %struct.flock* %6, i64 0, i32 2
store i32 %21, i32* %22
%23 = bitcast %struct.flock* %6 to i8*
br label %24
24: ; preds = %24, %16
%25 = call i32 @fileno(i32* %12)
%26 = load i32, i32* @F_SETLKW
%27 = call i32 @fcntl(i32 %25, i32 %26, %struct.flock* %6)
%28 = icmp ne i32 %27, 0
%29 = load i64, i64* @errno
%30 = load i64, i64* @EINTR
%31 = icmp eq i64 %29, %30
%32 = select i1 %28, i1 %31, i1 false
br i1 %32, label %24, label %33
33: ; preds = %24
%34 = bitcast i8** %7 to i8*
%35 = getelementptr inbounds %struct.group, %struct.group* %1, i64 0, i32 0
br label %36
36: ; preds = %39, %33
%37 = call i32 @__grpnextent(i32* %12, %struct.group* %1, i8* %2, i8** %7)
%38 = icmp eq i32 %37, -1
br i1 %38, label %46, label %39
39: ; preds = %36
%40 = load i64, i64* %35
%41 = icmp eq i64 %40, %0
br i1 %41, label %42, label %36
42: ; preds = %39
%43 = load i8*, i8** %7
%44 = call i32 @__grpmemparse(%struct.group* %1, i8* %43)
store %struct.group* %1, %struct.group** %4
%45 = call i32 @fclose(i32* %12)
br label %48
46: ; preds = %36
%47 = call i32 @fclose(i32* %12)
store %struct.group* null, %struct.group** %4
br label %48
48: ; preds = %46, %42
br label %49
49: ; preds = %14, %48, %9
%50 = phi i32 [ %10, %9 ], [ %15, %14 ], [ 0, %48 ]
ret i32 %50
}