#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 dummy;
};
typedef int /*<<< orphan*/ gid_t;

/* Variables and functions */
scalar_t__ ERANGE;
size_t ULONG_MAX;
int /*<<< orphan*/ _SC_GETGR_R_SIZE_MAX;
scalar_t__ errno;
size_t get_max_value(size_t, size_t, size_t);
int /*<<< orphan*/ getgrgid_r(int /*<<< orphan*/, struct group *, char *,
                              size_t, struct group **);
void *malloc(size_t);
char *realloc(char *, size_t);
size_t sysconf(int /*<<< orphan*/);
struct group *get_group_entry(gid_t gid) {
  struct group *group_entry;
  char *buf;
  size_t buflen;
  group_entry = malloc(sizeof(struct group));
  buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
  if (buflen == -1) {
    buflen = 1024;
  }
  buf = malloc(buflen);
  while (1) {
    getgrgid_r(gid, group_entry, buf, buflen, &group_entry);
    if (errno == ERANGE && buflen != ULONG_MAX) {
      buflen = get_max_value(buflen, buflen * 2, ULONG_MAX);
      buf = realloc(buf, buflen);
    } else {
      break;
    }
  }
  return group_entry;
}


x86 assembly (clang, -O0):
<get_group_entry>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x4,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    0x2e88(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    118e <get_group_entry+0x3e>
movq   $0x400,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    -0x10(%rbp),%r8
call   1060 <getgrgid_r@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%rax
mov    0x2e1c(%rip),%rcx
cmp    (%rcx),%rax
jne    1216 <get_group_entry+0xc6>
mov    -0x20(%rbp),%rax
mov    0x2de8(%rip),%rcx
cmp    (%rcx),%rax
je     1216 <get_group_entry+0xc6>
mov    -0x20(%rbp),%rdi
mov    -0x20(%rbp),%rsi
shl    $0x1,%rsi
mov    0x2dcc(%rip),%rax
mov    (%rax),%rdx
call   1030 <get_max_value@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x18(%rbp)
jmp    121b <get_group_entry+0xcb>
jmp    1220 <get_group_entry+0xd0>
jmp    119b <get_group_entry+0x4b>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_group_entry>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r14d
mov    $0x4,%edi
call   1040 <malloc@plt>
mov    %rax,%r15
mov    %rax,(%rsp)
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
cmp    $0xffffffffffffffff,%rax
mov    $0x400,%ebx
cmovne %rax,%rbx
mov    %rbx,%rdi
call   1040 <malloc@plt>
mov    %rax,%rbp
mov    %rsp,%r8
mov    %r14d,%edi
mov    %r15,%rsi
mov    %rax,%rdx
mov    %rbx,%rcx
call   1060 <getgrgid_r@plt>
mov    0x2e20(%rip),%r12
mov    (%r12),%rax
mov    0x2e25(%rip),%r13
cmp    0x0(%r13),%rax
jne    1214 <get_group_entry+0xc4>
mov    0x2df8(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    (%r15),%rdx
cmp    %rdx,%rbx
je     1214 <get_group_entry+0xc4>
lea    (%rbx,%rbx,1),%rsi
mov    %rbx,%rdi
call   1030 <get_max_value@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
mov    %rax,%rsi
call   1050 <realloc@plt>
mov    %rax,%rbp
mov    (%rsp),%rsi
mov    %r14d,%edi
mov    %rax,%rdx
mov    %rbx,%rcx
mov    %rsp,%r8
call   1060 <getgrgid_r@plt>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     11d0 <get_group_entry+0x80>
mov    (%rsp),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.group = type { i32 }
@_SC_GETGR_R_SIZE_MAX = global i32 0
@errno = global i64 0
@ERANGE = global i64 0
@ULONG_MAX = global i64 0
define %struct.group* @get_group_entry(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.group*
%4 = alloca i8*
%5 = alloca i64
store i32 %0, i32* %2
%6 = call i8* @malloc(i64 4)
%7 = bitcast i8* %6 to %struct.group*
store %struct.group* %7, %struct.group** %3
%8 = load i32, i32* @_SC_GETGR_R_SIZE_MAX
%9 = call i64 @sysconf(i32 %8)
store i64 %9, i64* %5
%10 = load i64, i64* %5
%11 = icmp eq i64 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %1
store i64 1024, i64* %5
br label %13
13: ; preds = %12, %1
%14 = load i64, i64* %5
%15 = call i8* @malloc(i64 %14)
store i8* %15, i8** %4
br label %16
16: ; preds = %13, %39
%17 = load i32, i32* %2
%18 = load %struct.group*, %struct.group** %3
%19 = load i8*, i8** %4
%20 = load i64, i64* %5
%21 = call i32 @getgrgid_r(i32 %17, %struct.group* %18, i8* %19, i64 %20, %struct.group** %3)
%22 = load i64, i64* @errno
%23 = load i64, i64* @ERANGE
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %38
25: ; preds = %16
%26 = load i64, i64* %5
%27 = load i64, i64* @ULONG_MAX
%28 = icmp ne i64 %26, %27
br i1 %28, label %29, label %38
29: ; preds = %25
%30 = load i64, i64* %5
%31 = load i64, i64* %5
%32 = mul i64 %31, 2
%33 = load i64, i64* @ULONG_MAX
%34 = call i64 @get_max_value(i64 %30, i64 %32, i64 %33)
store i64 %34, i64* %5
%35 = load i8*, i8** %4
%36 = load i64, i64* %5
%37 = call i8* @realloc(i8* %35, i64 %36)
store i8* %37, i8** %4
br label %39
38: ; preds = %25, %16
br label %40
39: ; preds = %29
br label %16
40: ; preds = %38
%41 = load %struct.group*, %struct.group** %3
ret %struct.group* %41
}

LLVM IR (-O2):
%struct.group = type { i32 }
@_SC_GETGR_R_SIZE_MAX = global i32 0
@errno = global i64 0
@ERANGE = global i64 0
@ULONG_MAX = global i64 0
define %struct.group* @get_group_entry(i32 %0) {
%2 = alloca %struct.group*
%3 = bitcast %struct.group** %2 to i8*
%4 = tail call dereferenceable_or_null(4) i8* @malloc(i64 4)
%5 = bitcast %struct.group** %2 to i8**
store i8* %4, i8** %5
%6 = load i32, i32* @_SC_GETGR_R_SIZE_MAX
%7 = tail call i64 @sysconf(i32 %6)
%8 = icmp eq i64 %7, -1
%9 = select i1 %8, i64 1024, i64 %7
%10 = tail call i8* @malloc(i64 %9)
%11 = bitcast i8* %4 to %struct.group*
%12 = call i32 @getgrgid_r(i32 %0, %struct.group* %11, i8* %10, i64 %9, %struct.group** %2)
%13 = load i64, i64* @errno
%14 = load i64, i64* @ERANGE
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %30
16: ; preds = %1, %21
%17 = phi i64 [ %23, %21 ], [ %9, %1 ]
%18 = phi i8* [ %24, %21 ], [ %10, %1 ]
%19 = load i64, i64* @ULONG_MAX
%20 = icmp eq i64 %17, %19
br i1 %20, label %30, label %21
21: ; preds = %16
%22 = shl i64 %17, 1
%23 = call i64 @get_max_value(i64 %17, i64 %22, i64 %19)
%24 = call i8* @realloc(i8* %18, i64 %23)
%25 = load %struct.group*, %struct.group** %2
%26 = call i32 @getgrgid_r(i32 %0, %struct.group* %25, i8* %24, i64 %23, %struct.group** %2)
%27 = load i64, i64* @errno
%28 = load i64, i64* @ERANGE
%29 = icmp eq i64 %27, %28
br i1 %29, label %16, label %30
30: ; preds = %16, %21, %1
%31 = load %struct.group*, %struct.group** %2
ret %struct.group* %31
}