#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 {
  long gr_gid;
};
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ _SC_GETGR_R_SIZE_MAX;
int /*<<< orphan*/ free(char *);
int getgrnam_r(char const *, struct group *, char *, size_t, struct group **);
char *malloc(size_t);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ snip_log_fatal(int, char *, ...);
int /*<<< orphan*/ strerror(int);
int sysconf(int /*<<< orphan*/);
long get_gid_for_group_name(const char *group_name) {
  struct group group;
  struct group *result;
  char *buffer;
  ssize_t buffer_size;
  int s;
  buffer_size = sysconf(_SC_GETGR_R_SIZE_MAX);
  if (buffer_size == -1)
    buffer_size = 16384;
  buffer = malloc((size_t)buffer_size);
  memset(buffer, '\0', buffer_size);
  if (buffer == NULL) {
    snip_log_fatal(66, "Unable to allocate buffer.");
    return -1;
  }
  s = getgrnam_r(group_name, &group, buffer, (size_t)buffer_size, &result);
  if (result == NULL) {
    if (s == 0) {
      return -1;
    } else {
      snip_log_fatal(66, "Failed to resolve username '%s': (%d) %s", group_name,
                     strerror(s));
      return -1;
    }
  }
  long gid = group.gr_gid;
  free(buffer);
  return gid;
}


x86 assembly (clang, -O0):
<get_gid_for_group_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    119e <get_gid_for_group_name+0x2e>
movl   $0x4000,-0x2c(%rbp)
movslq -0x2c(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%edx
xor    %esi,%esi
call   1040 <memset@plt>
cmpq   $0x0,-0x28(%rbp)
jne    11e4 <get_gid_for_group_name+0x74>
mov    $0x42,%edi
lea    0xe30(%rip),%rsi
mov    $0x0,%al
call   1050 <snip_log_fatal@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1271 <get_gid_for_group_name+0x101>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rdx
movslq -0x2c(%rbp),%rcx
lea    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%r8
call   1080 <getgrnam_r@plt>
mov    %eax,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1258 <get_gid_for_group_name+0xe8>
cmpl   $0x0,-0x30(%rbp)
jne    1222 <get_gid_for_group_name+0xb2>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1271 <get_gid_for_group_name+0x101>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%edi
call   1090 <strerror@plt>
mov    -0x40(%rbp),%rdx
mov    %eax,%ecx
mov    $0x42,%edi
lea    0xdd7(%rip),%rsi
mov    $0x0,%al
call   1050 <snip_log_fatal@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1271 <get_gid_for_group_name+0x101>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_gid_for_group_name>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x2e73(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
cmp    $0xffffffff,%eax
mov    $0x4000,%ecx
cmovne %eax,%ecx
movslq %ecx,%rbx
mov    %rbx,%rdi
call   1060 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1040 <memset@plt>
test   %r12,%r12
je     11e5 <get_gid_for_group_name+0x75>
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%r8
mov    %r14,%rdi
mov    %r12,%rdx
mov    %rbx,%rcx
call   1080 <getgrnam_r@plt>
cmpq   $0x0,0x8(%rsp)
je     1201 <get_gid_for_group_name+0x91>
mov    0x10(%rsp),%r15
mov    %r12,%rdi
call   1030 <free@plt>
jmp    122b <get_gid_for_group_name+0xbb>
lea    0xe14(%rip),%rsi
mov    $0x42,%edi
xor    %eax,%eax
call   1050 <snip_log_fatal@plt>
mov    $0xffffffffffffffff,%r15
jmp    122b <get_gid_for_group_name+0xbb>
mov    $0xffffffffffffffff,%r15
test   %eax,%eax
je     122b <get_gid_for_group_name+0xbb>
mov    %eax,%edi
call   1090 <strerror@plt>
lea    0xe01(%rip),%rsi
mov    $0x42,%edi
mov    %r14,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <snip_log_fatal@plt>
mov    %r15,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.group = type { i64 }
@_SC_GETGR_R_SIZE_MAX = global i32 0
@.str = [27 x i8] c"Unable to allocate buffer.\00"
@.str.1 = [41 x i8] c"Failed to resolve username '%s': (%d) %s\00"
define i64 @get_gid_for_group_name(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca %struct.group
%5 = alloca %struct.group*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
store i8* %0, i8** %3
%10 = load i32, i32* @_SC_GETGR_R_SIZE_MAX
%11 = call i32 @sysconf(i32 %10)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %1
store i32 16384, i32* %7
br label %15
15: ; preds = %14, %1
%16 = load i32, i32* %7
%17 = sext i32 %16 to i64
%18 = call i8* @malloc(i64 %17)
store i8* %18, i8** %6
%19 = load i8*, i8** %6
%20 = load i32, i32* %7
%21 = call i32 @memset(i8* %19, i8 signext 0, i32 %20)
%22 = load i8*, i8** %6
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %26
24: ; preds = %15
%25 = call i32 (i32, i8*, ...) @snip_log_fatal(i32 66, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i64 -1, i64* %2
br label %49
26: ; preds = %15
%27 = load i8*, i8** %3
%28 = load i8*, i8** %6
%29 = load i32, i32* %7
%30 = sext i32 %29 to i64
%31 = call i32 @getgrnam_r(i8* %27, %struct.group* %4, i8* %28, i64 %30, %struct.group** %5)
store i32 %31, i32* %8
%32 = load %struct.group*, %struct.group** %5
%33 = icmp eq %struct.group* %32, null
br i1 %33, label %34, label %43
34: ; preds = %26
%35 = load i32, i32* %8
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %34
store i64 -1, i64* %2
br label %49
38: ; preds = %34
%39 = load i8*, i8** %3
%40 = load i32, i32* %8
%41 = call i32 @strerror(i32 %40)
%42 = call i32 (i32, i8*, ...) @snip_log_fatal(i32 66, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %39, i32 %41)
store i64 -1, i64* %2
br label %49
43: ; preds = %26
%44 = getelementptr inbounds %struct.group, %struct.group* %4, i32 0, i32 0
%45 = load i64, i64* %44
store i64 %45, i64* %9
%46 = load i8*, i8** %6
%47 = call i32 @free(i8* %46)
%48 = load i64, i64* %9
store i64 %48, i64* %2
br label %49
49: ; preds = %43, %38, %37, %24
%50 = load i64, i64* %2
ret i64 %50
}

LLVM IR (-O2):
%struct.group = type { i64 }
@_SC_GETGR_R_SIZE_MAX = global i32 0
@.str = [27 x i8] c"Unable to allocate buffer.\00"
@.str.1 = [41 x i8] c"Failed to resolve username '%s': (%d) %s\00"
define i64 @get_gid_for_group_name(i8* %0) {
%2 = alloca %struct.group
%3 = alloca %struct.group*
%4 = bitcast %struct.group* %2 to i8*
%5 = bitcast %struct.group** %3 to i8*
%6 = load i32, i32* @_SC_GETGR_R_SIZE_MAX
%7 = tail call i32 @sysconf(i32 %6)
%8 = icmp eq i32 %7, -1
%9 = select i1 %8, i32 16384, i32 %7
%10 = sext i32 %9 to i64
%11 = tail call i8* @malloc(i64 %10)
%12 = tail call i32 @memset(i8* %11, i8 signext 0, i32 %9)
%13 = icmp eq i8* %11, null
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = tail call i32 (i32, i8*, ...) @snip_log_fatal(i32 66, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %29
16: ; preds = %1
%17 = call i32 @getgrnam_r(i8* %0, %struct.group* %2, i8* %11, i64 %10, %struct.group** %3)
%18 = load %struct.group*, %struct.group** %3
%19 = icmp eq %struct.group* %18, null
br i1 %19, label %20, label %25
20: ; preds = %16
%21 = icmp eq i32 %17, 0
br i1 %21, label %29, label %22
22: ; preds = %20
%23 = call i32 @strerror(i32 %17)
%24 = call i32 (i32, i8*, ...) @snip_log_fatal(i32 66, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %23)
br label %29
25: ; preds = %16
%26 = getelementptr inbounds %struct.group, %struct.group* %2, i64 0, i32 0
%27 = load i64, i64* %26
%28 = call i32 @free(i8* %11)
br label %29
29: ; preds = %20, %25, %22, %14
%30 = phi i64 [ -1, %14 ], [ -1, %22 ], [ %27, %25 ], [ -1, %20 ]
ret i64 %30
}