#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__ const gr_gid;
};
typedef scalar_t__ gid_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
struct group *__getgrent(int);
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
struct group *getgrgid(const gid_t gid) {
  struct group *group;
  int grp_fd;
  if ((grp_fd = open("/etc/group", O_RDONLY)) < 0)
    return NULL;
  while ((group = __getgrent(grp_fd)) != NULL)
    if (group->gr_gid == gid) {
      close(grp_fd);
      return group;
    }
  close(grp_fd);
  return NULL;
}


x86 assembly (clang, -O0):
<getgrgid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2ea5(%rip),%rax
mov    (%rax),%esi
lea    0xeb4(%rip),%rdi
call   1040 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    116a <getgrgid+0x3a>
movq   $0x0,-0x8(%rbp)
jmp    11c0 <getgrgid+0x90>
jmp    116f <getgrgid+0x3f>
mov    -0x1c(%rbp),%edi
call   1050 <__getgrent@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11b0 <getgrgid+0x80>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
cmp    -0x10(%rbp),%rax
jne    11ab <getgrgid+0x7b>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c0 <getgrgid+0x90>
jmp    116f <getgrgid+0x3f>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getgrgid>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
mov    0x2eaa(%rip),%rax
mov    (%rax),%esi
lea    0xeb9(%rip),%rdi
call   1040 <open@plt>
test   %eax,%eax
js     1176 <getgrgid+0x46>
mov    %eax,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebp,%edi
call   1050 <__getgrent@plt>
test   %rax,%rax
je     117a <getgrgid+0x4a>
mov    %rax,%rbx
cmp    %r14,(%rax)
jne    1160 <getgrgid+0x30>
jmp    117c <getgrgid+0x4c>
xor    %ebx,%ebx
jmp    1183 <getgrgid+0x53>
xor    %ebx,%ebx
mov    %ebp,%edi
call   1030 <close@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.group = type { i64 }
@.str = [11 x i8] c"/etc/group\00"
@O_RDONLY = global i32 0
define %struct.group* @getgrgid(i64 %0) {
%2 = alloca %struct.group*
%3 = alloca i64
%4 = alloca %struct.group*
%5 = alloca i32
store i64 %0, i64* %3
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %5
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %1
store %struct.group* null, %struct.group** %2
br label %29
10: ; preds = %1
br label %11
11: ; preds = %25, %10
%12 = load i32, i32* %5
%13 = call %struct.group* @__getgrent(i32 %12)
store %struct.group* %13, %struct.group** %4
%14 = icmp ne %struct.group* %13, null
br i1 %14, label %15, label %26
15: ; preds = %11
%16 = load %struct.group*, %struct.group** %4
%17 = getelementptr inbounds %struct.group, %struct.group* %16, i32 0, i32 0
%18 = load i64, i64* %17
%19 = load i64, i64* %3
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %25
21: ; preds = %15
%22 = load i32, i32* %5
%23 = call i32 @close(i32 %22)
%24 = load %struct.group*, %struct.group** %4
store %struct.group* %24, %struct.group** %2
br label %29
25: ; preds = %15
br label %11
26: ; preds = %11
%27 = load i32, i32* %5
%28 = call i32 @close(i32 %27)
store %struct.group* null, %struct.group** %2
br label %29
29: ; preds = %26, %21, %9
%30 = load %struct.group*, %struct.group** %2
ret %struct.group* %30
}

LLVM IR (-O2):
%struct.group = type { i64 }
@.str = [11 x i8] c"/etc/group\00"
@O_RDONLY = global i32 0
define %struct.group* @getgrgid(i64 %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %15, label %5
5: ; preds = %1, %8
%6 = tail call %struct.group* @__getgrent(i32 %3)
%7 = icmp eq %struct.group* %6, null
br i1 %7, label %12, label %8
8: ; preds = %5
%9 = getelementptr inbounds %struct.group, %struct.group* %6, i64 0, i32 0
%10 = load i64, i64* %9
%11 = icmp eq i64 %10, %0
br i1 %11, label %12, label %5
12: ; preds = %5, %8
%13 = phi %struct.group* [ %6, %8 ], [ null, %5 ]
%14 = tail call i32 @close(i32 %3)
br label %15
15: ; preds = %12, %1
%16 = phi %struct.group* [ null, %1 ], [ %13, %12 ]
ret %struct.group* %16
}