#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 gr_gid;
};
typedef int gid_t;

/* Variables and functions */
int /*<<< orphan*/ _SC_GETGR_R_SIZE_MAX;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ getgrnam_r(char const *, struct group *, char *, long,
                              struct group **);
char *malloc(long);
long sysconf(int /*<<< orphan*/);
gid_t get_gid_by_name(const char *group_name) {
  if (!group_name)
    return -1;
  long buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
  if (buflen == -1)
    return -1;
  char *buf = malloc(buflen);
  struct group grbuf, *grbufp;
  getgrnam_r(group_name, &grbuf, buf, buflen, &grbufp);
  free(buf);
  return grbufp ? grbufp->gr_gid : -1;
}


x86 assembly (clang, -O0):
<get_gid_by_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1163 <get_gid_by_name+0x23>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e7 <get_gid_by_name+0xa7>
mov    0x2e8e(%rip),%rax
mov    (%rax),%edi
call   1050 <sysconf@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0xffffffffffffffff,-0x18(%rbp)
jne    118c <get_gid_by_name+0x4c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e7 <get_gid_by_name+0xa7>
mov    -0x18(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    -0x28(%rbp),%rsi
lea    -0x30(%rbp),%r8
call   1060 <getgrnam_r@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x30(%rbp)
je     11d4 <get_gid_by_name+0x94>
mov    -0x30(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
jmp    11e1 <get_gid_by_name+0xa1>
mov    $0xffffffff,%eax
mov    %eax,-0x34(%rbp)
jmp    11e1 <get_gid_by_name+0xa1>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_gid_by_name>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    $0xffffffff,%r14d
test   %rdi,%rdi
je     11aa <get_gid_by_name+0x6a>
mov    %rdi,%rbx
mov    0x2e9b(%rip),%rax
mov    (%rax),%edi
call   1050 <sysconf@plt>
cmp    $0xffffffffffffffff,%rax
je     11aa <get_gid_by_name+0x6a>
mov    %rax,%rbp
mov    %rax,%rdi
call   1040 <malloc@plt>
mov    %rax,%r14
lea    0x8(%rsp),%rsi
mov    %rsp,%r8
mov    %rbx,%rdi
mov    %rax,%rdx
mov    %rbp,%rcx
call   1060 <getgrnam_r@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    (%rsp),%rax
test   %rax,%rax
je     11a4 <get_gid_by_name+0x64>
mov    (%rax),%r14d
jmp    11aa <get_gid_by_name+0x6a>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.group = type { i32 }
@_SC_GETGR_R_SIZE_MAX = global i32 0
define i32 @get_gid_by_name(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca %struct.group
%7 = alloca %struct.group*
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = icmp ne i8* %8, null
br i1 %9, label %11, label %10
10: ; preds = %1
store i32 -1, i32* %2
br label %35
11: ; preds = %1
%12 = load i32, i32* @_SC_GETGR_R_SIZE_MAX
%13 = call i64 @sysconf(i32 %12)
store i64 %13, i64* %4
%14 = load i64, i64* %4
%15 = icmp eq i64 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %11
store i32 -1, i32* %2
br label %35
17: ; preds = %11
%18 = load i64, i64* %4
%19 = call i8* @malloc(i64 %18)
store i8* %19, i8** %5
%20 = load i8*, i8** %3
%21 = load i8*, i8** %5
%22 = load i64, i64* %4
%23 = call i32 @getgrnam_r(i8* %20, %struct.group* %6, i8* %21, i64 %22, %struct.group** %7)
%24 = load i8*, i8** %5
%25 = call i32 @free(i8* %24)
%26 = load %struct.group*, %struct.group** %7
%27 = icmp ne %struct.group* %26, null
br i1 %27, label %28, label %32
28: ; preds = %17
%29 = load %struct.group*, %struct.group** %7
%30 = getelementptr inbounds %struct.group, %struct.group* %29, i32 0, i32 0
%31 = load i32, i32* %30
br label %33
32: ; preds = %17
br label %33
33: ; preds = %32, %28
%34 = phi i32 [ %31, %28 ], [ -1, %32 ]
store i32 %34, i32* %2
br label %35
35: ; preds = %33, %16, %10
%36 = load i32, i32* %2
ret i32 %36
}

LLVM IR (-O2):
%struct.group = type { i32 }
@_SC_GETGR_R_SIZE_MAX = global i32 0
define i32 @get_gid_by_name(i8* %0) {
%2 = alloca %struct.group
%3 = alloca %struct.group*
%4 = icmp eq i8* %0, null
br i1 %4, label %22, label %5
5: ; preds = %1
%6 = load i32, i32* @_SC_GETGR_R_SIZE_MAX
%7 = tail call i64 @sysconf(i32 %6)
%8 = icmp eq i64 %7, -1
br i1 %8, label %22, label %9
9: ; preds = %5
%10 = tail call i8* @malloc(i64 %7)
%11 = bitcast %struct.group* %2 to i8*
%12 = bitcast %struct.group** %3 to i8*
%13 = call i32 @getgrnam_r(i8* %0, %struct.group* %2, i8* %10, i64 %7, %struct.group** %3)
%14 = call i32 @free(i8* %10)
%15 = load %struct.group*, %struct.group** %3
%16 = icmp eq %struct.group* %15, null
br i1 %16, label %20, label %17
17: ; preds = %9
%18 = getelementptr inbounds %struct.group, %struct.group* %15, i64 0, i32 0
%19 = load i32, i32* %18
br label %20
20: ; preds = %9, %17
%21 = phi i32 [ %19, %17 ], [ -1, %9 ]
br label %22
22: ; preds = %20, %5, %1
%23 = phi i32 [ -1, %1 ], [ %21, %20 ], [ -1, %5 ]
ret i32 %23
}