#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 {
  char const *gr_gid;
};
typedef char const *gid_t;

/* Variables and functions */
int /*<<< orphan*/ errno;
int getgrnam_r(char const *, struct group *, char *, size_t, struct group **);
int /*<<< orphan*/ log_debug(char *, char const *);
int /*<<< orphan*/ log_error(char *, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ log_warn(char *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int resolve_group_gid(const char *groupname, gid_t *gid) {
  struct group gr;
  struct group *result;
  char buf[10000];
  size_t bufsize = 10000;
  int rc;
  if (groupname == NULL) {
    log_warn("resolve_group_gid: groupname is NULL");
    return -1;
  }
  log_debug("resolve_group_gid for %s", groupname);
  rc = getgrnam_r(groupname, &gr, buf, bufsize, &result);
  if (rc == 0 && result != NULL) {
    *gid = gr.gr_gid;
    log_debug("resolve_group_gid: gid=%d", gr.gr_gid);
    return 0;
  } else {
    log_error("resolve_group_gid: failed to resolve POSIX gid for %s: %s",
              groupname, strerror(errno));
    return -2;
  }
}


x86 assembly (clang, -O0):
<resolve_group_gid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2760,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x2710,-0x2748(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1191 <resolve_group_gid+0x41>
lea    0xe80(%rip),%rdi
call   1050 <log_warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123a <resolve_group_gid+0xea>
mov    -0x10(%rbp),%rsi
lea    0xe89(%rip),%rdi
call   1040 <log_debug@plt>
mov    -0x10(%rbp),%rdi
lea    -0x2740(%rbp),%rdx
mov    -0x2748(%rbp),%rcx
lea    -0x20(%rbp),%rsi
lea    -0x28(%rbp),%r8
call   1060 <getgrnam_r@plt>
mov    %eax,-0x274c(%rbp)
cmpl   $0x0,-0x274c(%rbp)
jne    1205 <resolve_group_gid+0xb5>
cmpq   $0x0,-0x28(%rbp)
je     1205 <resolve_group_gid+0xb5>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rsi
lea    0xe4a(%rip),%rdi
call   1040 <log_debug@plt>
movl   $0x0,-0x4(%rbp)
jmp    123a <resolve_group_gid+0xea>
mov    -0x10(%rbp),%rax
mov    %rax,-0x2758(%rbp)
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x2758(%rbp),%rsi
mov    %eax,%edx
lea    0xe2a(%rip),%rdi
call   1030 <log_error@plt>
movl   $0xfffffffe,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2760,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve_group_gid>:
push   %r14
push   %rbx
sub    $0x2728,%rsp
test   %rdi,%rdi
je     11b1 <resolve_group_gid+0x61>
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xeb9(%rip),%rdi
mov    %rbx,%rsi
call   1040 <log_debug@plt>
lea    0x8(%rsp),%rsi
lea    0x10(%rsp),%rdx
mov    %rsp,%r8
mov    $0x2710,%ecx
mov    %rbx,%rdi
call   1060 <getgrnam_r@plt>
test   %eax,%eax
jne    11c4 <resolve_group_gid+0x74>
cmpq   $0x0,(%rsp)
je     11c4 <resolve_group_gid+0x74>
mov    0x8(%rsp),%rsi
mov    %rsi,(%r14)
lea    0xe96(%rip),%rdi
call   1040 <log_debug@plt>
xor    %eax,%eax
jmp    11e8 <resolve_group_gid+0x98>
lea    0xe48(%rip),%rdi
call   1050 <log_warn@plt>
mov    $0xffffffff,%eax
jmp    11e8 <resolve_group_gid+0x98>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe7f(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <log_error@plt>
mov    $0xfffffffe,%eax
add    $0x2728,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.group = type { i8* }
@.str = [37 x i8] c"resolve_group_gid: groupname is NULL\00"
@.str.1 = [25 x i8] c"resolve_group_gid for %s\00"
@.str.2 = [26 x i8] c"resolve_group_gid: gid=%d\00"
@.str.3 = [58 x i8] c"resolve_group_gid: failed to resolve POSIX gid for %s: %s\00"
@errno = global i32 0
define i32 @resolve_group_gid(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca %struct.group
%7 = alloca %struct.group*
%8 = alloca [10000 x i8]
%9 = alloca i64
%10 = alloca i32
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i64 10000, i64* %9
%11 = load i8*, i8** %4
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @log_warn(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %39
15: ; preds = %2
%16 = load i8*, i8** %4
%17 = call i32 @log_debug(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = load i8*, i8** %4
%19 = getelementptr inbounds [10000 x i8], [10000 x i8]* %8, i64 0, i64 0
%20 = load i64, i64* %9
%21 = call i32 @getgrnam_r(i8* %18, %struct.group* %6, i8* %19, i64 %20, %struct.group** %7)
store i32 %21, i32* %10
%22 = load i32, i32* %10
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %34
24: ; preds = %15
%25 = load %struct.group*, %struct.group** %7
%26 = icmp ne %struct.group* %25, null
br i1 %26, label %27, label %34
27: ; preds = %24
%28 = getelementptr inbounds %struct.group, %struct.group* %6, i32 0, i32 0
%29 = load i8*, i8** %28
%30 = load i8**, i8*** %5
store i8* %29, i8** %30
%31 = getelementptr inbounds %struct.group, %struct.group* %6, i32 0, i32 0
%32 = load i8*, i8** %31
%33 = call i32 @log_debug(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %32)
store i32 0, i32* %3
br label %39
34: ; preds = %24, %15
%35 = load i8*, i8** %4
%36 = load i32, i32* @errno
%37 = call i32 @strerror(i32 %36)
%38 = call i32 @log_error(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.3, i64 0, i64 0), i8* %35, i32 %37)
store i32 -2, i32* %3
br label %39
39: ; preds = %34, %27, %13
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
%struct.group = type { i8* }
@.str = [37 x i8] c"resolve_group_gid: groupname is NULL\00"
@.str.1 = [25 x i8] c"resolve_group_gid for %s\00"
@.str.2 = [26 x i8] c"resolve_group_gid: gid=%d\00"
@.str.3 = [58 x i8] c"resolve_group_gid: failed to resolve POSIX gid for %s: %s\00"
@errno = global i32 0
define i32 @resolve_group_gid(i8* %0, i8** nocapture writeonly %1) {
%3 = alloca %struct.group
%4 = alloca %struct.group*
%5 = alloca [10000 x i8]
%6 = bitcast %struct.group* %3 to i8*
%7 = bitcast %struct.group** %4 to i8*
%8 = getelementptr inbounds [10000 x i8], [10000 x i8]* %5, i64 0, i64 0
%9 = icmp eq i8* %0, null
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @log_warn(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
br label %27
12: ; preds = %2
%13 = tail call i32 @log_debug(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%14 = call i32 @getgrnam_r(i8* %0, %struct.group* %3, i8* %8, i64 10000, %struct.group** %4)
%15 = icmp eq i32 %14, 0
%16 = load %struct.group*, %struct.group** %4
%17 = icmp ne %struct.group* %16, null
%18 = select i1 %15, i1 %17, i1 false
br i1 %18, label %19, label %23
19: ; preds = %12
%20 = getelementptr inbounds %struct.group, %struct.group* %3, i64 0, i32 0
%21 = load i8*, i8** %20
store i8* %21, i8** %1
%22 = call i32 @log_debug(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %21)
br label %27
23: ; preds = %12
%24 = load i32, i32* @errno
%25 = call i32 @strerror(i32 %24)
%26 = call i32 @log_error(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.3, i64 0, i64 0), i8* %0, i32 %25)
br label %27
27: ; preds = %23, %19, %10
%28 = phi i32 [ -1, %10 ], [ 0, %19 ], [ -2, %23 ]
ret i32 %28
}