#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 */
int ENOENT;
int ENOMEM;
int ERANGE;
int /*<<< orphan*/ _SC_GETGR_R_SIZE_MAX;
int /*<<< orphan*/ free(char *);
int getgrgid_r(int /*<<< orphan*/, struct group *, char *, size_t,
               struct group **);
char *malloc(int);
size_t sysconf(int /*<<< orphan*/);
int getGroupDetails(gid_t group, char **grpBuf) {
  struct group *grp = NULL;
  size_t currBufferSize = sysconf(_SC_GETGR_R_SIZE_MAX);
  if (currBufferSize < 1024) {
    currBufferSize = 1024;
  }
  *grpBuf = NULL;
  char *buf = (char *)malloc(sizeof(char) * currBufferSize);
  if (!buf) {
    return ENOMEM;
  }
  int error;
  for (;;) {
    error = getgrgid_r(group, (struct group *)buf, buf + sizeof(struct group),
                       currBufferSize - sizeof(struct group), &grp);
    if (error != ERANGE) {
      break;
    }
    free(buf);
    currBufferSize *= 2;
    buf = malloc(sizeof(char) * currBufferSize);
    if (!buf) {
      return ENOMEM;
    }
  }
  if (!grp && !error) {
    free(buf);
    return ENOENT;
  } else if (error) {
    free(buf);
    return error;
  }
  *grpBuf = buf;
  return 0;
}


x86 assembly (clang, -O0):
<getGroupDetails>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0x2e9a(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x400,-0x20(%rbp)
jae    117f <getGroupDetails+0x3f>
movq   $0x400,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b9 <getGroupDetails+0x79>
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12a5 <getGroupDetails+0x165>
jmp    11be <getGroupDetails+0x7e>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x28(%rbp),%rdx
add    $0x4,%rdx
mov    -0x20(%rbp),%rcx
sub    $0x4,%rcx
lea    -0x18(%rbp),%r8
call   1050 <getgrgid_r@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    0x2df5(%rip),%rcx
cmp    (%rcx),%eax
je     11f8 <getGroupDetails+0xb8>
jmp    1241 <getGroupDetails+0x101>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    123c <getGroupDetails+0xfc>
mov    0x2d9e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12a5 <getGroupDetails+0x165>
jmp    11be <getGroupDetails+0x7e>
cmpq   $0x0,-0x18(%rbp)
jne    1270 <getGroupDetails+0x130>
cmpl   $0x0,-0x2c(%rbp)
jne    1270 <getGroupDetails+0x130>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    0x2d5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12a5 <getGroupDetails+0x165>
cmpl   $0x0,-0x2c(%rbp)
je     128e <getGroupDetails+0x14e>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12a5 <getGroupDetails+0x165>
jmp    1293 <getGroupDetails+0x153>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getGroupDetails>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %edi,%r15d
movq   $0x0,0x8(%rsp)
mov    0x2e94(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
cmp    $0x401,%rax
mov    $0x400,%ebx
cmovae %rax,%rbx
movq   $0x0,(%r14)
mov    %ebx,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     11e5 <getGroupDetails+0xa5>
mov    %rax,%rbp
mov    %r14,0x10(%rsp)
lea    0x8(%rsp),%r12
mov    0x2e3f(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    0x4(%rbp),%rdx
lea    -0x4(%rbx),%rcx
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %r12,%r8
call   1050 <getgrgid_r@plt>
cmp    (%r14),%eax
jne    1201 <getGroupDetails+0xc1>
mov    %rbp,%rdi
call   1030 <free@plt>
add    %rbx,%rbx
mov    %ebx,%edi
call   1040 <malloc@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    11b0 <getGroupDetails+0x70>
mov    0x2de4(%rip),%rax
mov    (%rax),%r13d
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%r13d
cmpq   $0x0,0x8(%rsp)
jne    1222 <getGroupDetails+0xe2>
test   %r13d,%r13d
jne    1222 <getGroupDetails+0xe2>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    0x2da0(%rip),%rax
jmp    11ec <getGroupDetails+0xac>
test   %r13d,%r13d
je     1231 <getGroupDetails+0xf1>
mov    %rbp,%rdi
call   1030 <free@plt>
jmp    11ef <getGroupDetails+0xaf>
mov    0x10(%rsp),%rax
mov    %rbp,(%rax)
xor    %r13d,%r13d
jmp    11ef <getGroupDetails+0xaf>

LLVM IR (-O0):
%struct.group = type { i32 }
@_SC_GETGR_R_SIZE_MAX = global i32 0
@ENOMEM = global i32 0
@ERANGE = global i32 0
@ENOENT = global i32 0
define i32 @getGroupDetails(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.group*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
store i32 %0, i32* %4
store i8** %1, i8*** %5
store %struct.group* null, %struct.group** %6
%10 = load i32, i32* @_SC_GETGR_R_SIZE_MAX
%11 = call i64 @sysconf(i32 %10)
store i64 %11, i64* %7
%12 = load i64, i64* %7
%13 = icmp ult i64 %12, 1024
br i1 %13, label %14, label %15
14: ; preds = %2
store i64 1024, i64* %7
br label %15
15: ; preds = %14, %2
%16 = load i8**, i8*** %5
store i8* null, i8** %16
%17 = load i64, i64* %7
%18 = mul i64 1, %17
%19 = trunc i64 %18 to i32
%20 = call i8* @malloc(i32 %19)
store i8* %20, i8** %8
%21 = load i8*, i8** %8
%22 = icmp ne i8* %21, null
br i1 %22, label %25, label %23
23: ; preds = %15
%24 = load i32, i32* @ENOMEM
store i32 %24, i32* %3
br label %74
25: ; preds = %15
br label %26
26: ; preds = %52, %25
%27 = load i32, i32* %4
%28 = load i8*, i8** %8
%29 = bitcast i8* %28 to %struct.group*
%30 = load i8*, i8** %8
%31 = getelementptr inbounds i8, i8* %30, i64 4
%32 = load i64, i64* %7
%33 = sub i64 %32, 4
%34 = call i32 @getgrgid_r(i32 %27, %struct.group* %29, i8* %31, i64 %33, %struct.group** %6)
store i32 %34, i32* %9
%35 = load i32, i32* %9
%36 = load i32, i32* @ERANGE
%37 = icmp ne i32 %35, %36
br i1 %37, label %38, label %39
38: ; preds = %26
br label %53
39: ; preds = %26
%40 = load i8*, i8** %8
%41 = call i32 @free(i8* %40)
%42 = load i64, i64* %7
%43 = mul i64 %42, 2
store i64 %43, i64* %7
%44 = load i64, i64* %7
%45 = mul i64 1, %44
%46 = trunc i64 %45 to i32
%47 = call i8* @malloc(i32 %46)
store i8* %47, i8** %8
%48 = load i8*, i8** %8
%49 = icmp ne i8* %48, null
br i1 %49, label %52, label %50
50: ; preds = %39
%51 = load i32, i32* @ENOMEM
store i32 %51, i32* %3
br label %74
52: ; preds = %39
br label %26
53: ; preds = %38
%54 = load %struct.group*, %struct.group** %6
%55 = icmp ne %struct.group* %54, null
br i1 %55, label %63, label %56
56: ; preds = %53
%57 = load i32, i32* %9
%58 = icmp ne i32 %57, 0
br i1 %58, label %63, label %59
59: ; preds = %56
%60 = load i8*, i8** %8
%61 = call i32 @free(i8* %60)
%62 = load i32, i32* @ENOENT
store i32 %62, i32* %3
br label %74
63: ; preds = %56, %53
%64 = load i32, i32* %9
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %70
66: ; preds = %63
%67 = load i8*, i8** %8
%68 = call i32 @free(i8* %67)
%69 = load i32, i32* %9
store i32 %69, i32* %3
br label %74
70: ; preds = %63
br label %71
71: ; preds = %70
%72 = load i8*, i8** %8
%73 = load i8**, i8*** %5
store i8* %72, i8** %73
store i32 0, i32* %3
br label %74
74: ; preds = %71, %66, %59, %50, %23
%75 = load i32, i32* %3
ret i32 %75
}

LLVM IR (-O2):
%struct.group = type { i32 }
@_SC_GETGR_R_SIZE_MAX = global i32 0
@ENOMEM = global i32 0
@ERANGE = global i32 0
@ENOENT = global i32 0
define i32 @getGroupDetails(i32 %0, i8** nocapture writeonly %1) {
%3 = alloca %struct.group*
%4 = bitcast %struct.group** %3 to i8*
store %struct.group* null, %struct.group** %3
%5 = load i32, i32* @_SC_GETGR_R_SIZE_MAX
%6 = tail call i64 @sysconf(i32 %5)
%7 = icmp ugt i64 %6, 1024
%8 = select i1 %7, i64 %6, i64 1024
store i8* null, i8** %1
%9 = trunc i64 %8 to i32
%10 = tail call i8* @malloc(i32 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = load i32, i32* @ENOMEM
br label %43
14: ; preds = %2, %23
%15 = phi i64 [ %25, %23 ], [ %8, %2 ]
%16 = phi i8* [ %27, %23 ], [ %10, %2 ]
%17 = bitcast i8* %16 to %struct.group*
%18 = getelementptr inbounds i8, i8* %16, i64 4
%19 = add i64 %15, -4
%20 = call i32 @getgrgid_r(i32 %0, %struct.group* %17, i8* %18, i64 %19, %struct.group** %3)
%21 = load i32, i32* @ERANGE
%22 = icmp eq i32 %20, %21
br i1 %22, label %23, label %31
23: ; preds = %14
%24 = call i32 @free(i8* %16)
%25 = shl i64 %15, 1
%26 = trunc i64 %25 to i32
%27 = call i8* @malloc(i32 %26)
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %14
29: ; preds = %23
%30 = load i32, i32* @ENOMEM
br label %43
31: ; preds = %14
%32 = load %struct.group*, %struct.group** %3
%33 = icmp ne %struct.group* %32, null
%34 = icmp ne i32 %20, 0
%35 = select i1 %33, i1 true, i1 %34
br i1 %35, label %39, label %36
36: ; preds = %31
%37 = call i32 @free(i8* %16)
%38 = load i32, i32* @ENOENT
br label %43
39: ; preds = %31
br i1 %34, label %40, label %42
40: ; preds = %39
%41 = call i32 @free(i8* %16)
br label %43
42: ; preds = %39
store i8* %16, i8** %1
br label %43
43: ; preds = %29, %36, %40, %42, %12
%44 = phi i32 [ %13, %12 ], [ %20, %40 ], [ 0, %42 ], [ %38, %36 ], [ %30, %29 ]
ret i32 %44
}