#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);
int sysconf(int /*<<< orphan*/);
int getGroupDetails(gid_t group, char **grpBuf) {
  struct group *grp = NULL;
  size_t currBufferSize =
      (sysconf(_SC_GETGR_R_SIZE_MAX) > 2048 ? sysconf(_SC_GETGR_R_SIZE_MAX)
                                            : 2048);
  *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>
cmp    $0x800,%eax
jle    1186 <getGroupDetails+0x46>
mov    0x2e81(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    %eax,-0x30(%rbp)
jmp    1193 <getGroupDetails+0x53>
mov    $0x800,%eax
mov    %eax,-0x30(%rbp)
jmp    1193 <getGroupDetails+0x53>
mov    -0x30(%rbp),%eax
cltq
mov    %rax,-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    11d6 <getGroupDetails+0x96>
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12c2 <getGroupDetails+0x182>
jmp    11db <getGroupDetails+0x9b>
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    0x2dd8(%rip),%rcx
cmp    (%rcx),%eax
je     1215 <getGroupDetails+0xd5>
jmp    125e <getGroupDetails+0x11e>
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    1259 <getGroupDetails+0x119>
mov    0x2d81(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12c2 <getGroupDetails+0x182>
jmp    11db <getGroupDetails+0x9b>
cmpq   $0x0,-0x18(%rbp)
jne    128d <getGroupDetails+0x14d>
cmpl   $0x0,-0x2c(%rbp)
jne    128d <getGroupDetails+0x14d>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    0x2d3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12c2 <getGroupDetails+0x182>
cmpl   $0x0,-0x2c(%rbp)
je     12ab <getGroupDetails+0x16b>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12c2 <getGroupDetails+0x182>
jmp    12b0 <getGroupDetails+0x170>
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),%rbx
mov    (%rbx),%edi
call   1060 <sysconf@plt>
mov    $0x800,%ebp
cmp    $0x801,%eax
jl     1180 <getGroupDetails+0x40>
mov    (%rbx),%edi
call   1060 <sysconf@plt>
mov    %eax,%ebp
movq   $0x0,(%r14)
mov    %ebp,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     11e5 <getGroupDetails+0xa5>
mov    %rax,%rbx
mov    %r14,0x10(%rsp)
movslq %ebp,%rbp
lea    0x8(%rsp),%r12
mov    0x2e36(%rip),%r14
nopw   0x0(%rax,%rax,1)
lea    0x4(%rbx),%rdx
lea    -0x4(%rbp),%rcx
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %r12,%r8
call   1050 <getgrgid_r@plt>
cmp    (%r14),%eax
jne    1201 <getGroupDetails+0xc1>
mov    %rbx,%rdi
call   1030 <free@plt>
add    %rbp,%rbp
mov    %ebp,%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
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    %rbx,%rdi
call   1030 <free@plt>
mov    0x2da0(%rip),%rax
jmp    11ec <getGroupDetails+0xac>
test   %r13d,%r13d
je     1231 <getGroupDetails+0xf1>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    11ef <getGroupDetails+0xaf>
mov    0x10(%rsp),%rax
mov    %rbx,(%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 i32 @sysconf(i32 %10)
%12 = icmp sgt i32 %11, 2048
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @_SC_GETGR_R_SIZE_MAX
%15 = call i32 @sysconf(i32 %14)
br label %17
16: ; preds = %2
br label %17
17: ; preds = %16, %13
%18 = phi i32 [ %15, %13 ], [ 2048, %16 ]
%19 = sext i32 %18 to i64
store i64 %19, i64* %7
%20 = load i8**, i8*** %5
store i8* null, i8** %20
%21 = load i64, i64* %7
%22 = mul i64 1, %21
%23 = trunc i64 %22 to i32
%24 = call i8* @malloc(i32 %23)
store i8* %24, i8** %8
%25 = load i8*, i8** %8
%26 = icmp ne i8* %25, null
br i1 %26, label %29, label %27
27: ; preds = %17
%28 = load i32, i32* @ENOMEM
store i32 %28, i32* %3
br label %78
29: ; preds = %17
br label %30
30: ; preds = %56, %29
%31 = load i32, i32* %4
%32 = load i8*, i8** %8
%33 = bitcast i8* %32 to %struct.group*
%34 = load i8*, i8** %8
%35 = getelementptr inbounds i8, i8* %34, i64 4
%36 = load i64, i64* %7
%37 = sub i64 %36, 4
%38 = call i32 @getgrgid_r(i32 %31, %struct.group* %33, i8* %35, i64 %37, %struct.group** %6)
store i32 %38, i32* %9
%39 = load i32, i32* %9
%40 = load i32, i32* @ERANGE
%41 = icmp ne i32 %39, %40
br i1 %41, label %42, label %43
42: ; preds = %30
br label %57
43: ; preds = %30
%44 = load i8*, i8** %8
%45 = call i32 @free(i8* %44)
%46 = load i64, i64* %7
%47 = mul i64 %46, 2
store i64 %47, i64* %7
%48 = load i64, i64* %7
%49 = mul i64 1, %48
%50 = trunc i64 %49 to i32
%51 = call i8* @malloc(i32 %50)
store i8* %51, i8** %8
%52 = load i8*, i8** %8
%53 = icmp ne i8* %52, null
br i1 %53, label %56, label %54
54: ; preds = %43
%55 = load i32, i32* @ENOMEM
store i32 %55, i32* %3
br label %78
56: ; preds = %43
br label %30
57: ; preds = %42
%58 = load %struct.group*, %struct.group** %6
%59 = icmp ne %struct.group* %58, null
br i1 %59, label %67, label %60
60: ; preds = %57
%61 = load i32, i32* %9
%62 = icmp ne i32 %61, 0
br i1 %62, label %67, label %63
63: ; preds = %60
%64 = load i8*, i8** %8
%65 = call i32 @free(i8* %64)
%66 = load i32, i32* @ENOENT
store i32 %66, i32* %3
br label %78
67: ; preds = %60, %57
%68 = load i32, i32* %9
%69 = icmp ne i32 %68, 0
br i1 %69, label %70, label %74
70: ; preds = %67
%71 = load i8*, i8** %8
%72 = call i32 @free(i8* %71)
%73 = load i32, i32* %9
store i32 %73, i32* %3
br label %78
74: ; preds = %67
br label %75
75: ; preds = %74
%76 = load i8*, i8** %8
%77 = load i8**, i8*** %5
store i8* %76, i8** %77
store i32 0, i32* %3
br label %78
78: ; preds = %75, %70, %63, %54, %27
%79 = load i32, i32* %3
ret i32 %79
}

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 i32 @sysconf(i32 %5)
%7 = icmp sgt i32 %6, 2048
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i32, i32* @_SC_GETGR_R_SIZE_MAX
%10 = tail call i32 @sysconf(i32 %9)
br label %11
11: ; preds = %2, %8
%12 = phi i32 [ %10, %8 ], [ 2048, %2 ]
store i8* null, i8** %1
%13 = tail call i8* @malloc(i32 %12)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = load i32, i32* @ENOMEM
br label %48
17: ; preds = %11
%18 = sext i32 %12 to i64
br label %19
19: ; preds = %28, %17
%20 = phi i64 [ %18, %17 ], [ %30, %28 ]
%21 = phi i8* [ %13, %17 ], [ %32, %28 ]
%22 = bitcast i8* %21 to %struct.group*
%23 = getelementptr inbounds i8, i8* %21, i64 4
%24 = add i64 %20, -4
%25 = call i32 @getgrgid_r(i32 %0, %struct.group* %22, i8* %23, i64 %24, %struct.group** %3)
%26 = load i32, i32* @ERANGE
%27 = icmp eq i32 %25, %26
br i1 %27, label %28, label %36
28: ; preds = %19
%29 = call i32 @free(i8* %21)
%30 = shl i64 %20, 1
%31 = trunc i64 %30 to i32
%32 = call i8* @malloc(i32 %31)
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %19
34: ; preds = %28
%35 = load i32, i32* @ENOMEM
br label %48
36: ; preds = %19
%37 = load %struct.group*, %struct.group** %3
%38 = icmp ne %struct.group* %37, null
%39 = icmp ne i32 %25, 0
%40 = select i1 %38, i1 true, i1 %39
br i1 %40, label %44, label %41
41: ; preds = %36
%42 = call i32 @free(i8* %21)
%43 = load i32, i32* @ENOENT
br label %48
44: ; preds = %36
br i1 %39, label %45, label %47
45: ; preds = %44
%46 = call i32 @free(i8* %21)
br label %48
47: ; preds = %44
store i8* %21, i8** %1
br label %48
48: ; preds = %34, %41, %45, %47, %15
%49 = phi i32 [ %16, %15 ], [ %25, %45 ], [ 0, %47 ], [ %43, %41 ], [ %35, %34 ]
ret i32 %49
}