#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 /*<<< orphan*/ gr_gid;
};
typedef int /*<<< orphan*/ gid_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int ENOENT;
int getgrnam_r(char const *, struct group *, char *, int, struct group **);
int get_group_id(const char *groupname, gid_t *gid) {
  char buf[8192];
  struct group grp, *res;
  int ret;
  ret = getgrnam_r(groupname, &grp, buf, sizeof(buf), &res);
  if (res == NULL) {
    if (!ret)
      return -ENOENT;
    return ret;
  }
  *gid = grp.gr_gid;
  return 0;
}


x86 assembly (clang, -O0):
<get_group_id>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x2020(%rbp),%rdx
lea    -0x2028(%rbp),%rsi
mov    $0x2000,%ecx
lea    -0x2030(%rbp),%r8
call   1030 <getgrnam_r@plt>
mov    %eax,-0x2034(%rbp)
cmpq   $0x0,-0x2030(%rbp)
jne    1188 <get_group_id+0x78>
cmpl   $0x0,-0x2034(%rbp)
jne    117a <get_group_id+0x6a>
xor    %eax,%eax
mov    0x2e68(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    119b <get_group_id+0x8b>
mov    -0x2034(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    119b <get_group_id+0x8b>
mov    -0x2028(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_group_id>:
push   %rbx
sub    $0x2010,%rsp
mov    %rsi,%rbx
mov    %rsp,%rsi
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%r8
mov    $0x2000,%ecx
call   1030 <getgrnam_r@plt>
cmpq   $0x0,0x8(%rsp)
je     1143 <get_group_id+0x33>
mov    (%rsp),%eax
mov    %eax,(%rbx)
xor    %eax,%eax
jmp    1152 <get_group_id+0x42>
test   %eax,%eax
jne    1152 <get_group_id+0x42>
mov    0x2e8a(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x2010,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.group = type { i32 }
@ENOENT = global i32 0
define i32 @get_group_id(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca [8192 x i8]
%7 = alloca %struct.group
%8 = alloca %struct.group*
%9 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%10 = load i8*, i8** %4
%11 = getelementptr inbounds [8192 x i8], [8192 x i8]* %6, i64 0, i64 0
%12 = call i32 @getgrnam_r(i8* %10, %struct.group* %7, i8* %11, i32 8192, %struct.group** %8)
store i32 %12, i32* %9
%13 = load %struct.group*, %struct.group** %8
%14 = icmp eq %struct.group* %13, null
br i1 %14, label %15, label %23
15: ; preds = %2
%16 = load i32, i32* %9
%17 = icmp ne i32 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %15
%19 = load i32, i32* @ENOENT
%20 = sub nsw i32 0, %19
store i32 %20, i32* %3
br label %27
21: ; preds = %15
%22 = load i32, i32* %9
store i32 %22, i32* %3
br label %27
23: ; preds = %2
%24 = getelementptr inbounds %struct.group, %struct.group* %7, i32 0, i32 0
%25 = load i32, i32* %24
%26 = load i32*, i32** %5
store i32 %25, i32* %26
store i32 0, i32* %3
br label %27
27: ; preds = %23, %21, %18
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
%struct.group = type { i32 }
@ENOENT = global i32 0
define i32 @get_group_id(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca [8192 x i8]
%4 = alloca %struct.group
%5 = alloca %struct.group*
%6 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 0
%7 = bitcast %struct.group* %4 to i8*
%8 = bitcast %struct.group** %5 to i8*
%9 = call i32 @getgrnam_r(i8* %0, %struct.group* %4, i8* %6, i32 8192, %struct.group** %5)
%10 = load %struct.group*, %struct.group** %5
%11 = icmp eq %struct.group* %10, null
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = icmp eq i32 %9, 0
br i1 %13, label %14, label %20
14: ; preds = %12
%15 = load i32, i32* @ENOENT
%16 = sub nsw i32 0, %15
br label %20
17: ; preds = %2
%18 = getelementptr inbounds %struct.group, %struct.group* %4, i64 0, i32 0
%19 = load i32, i32* %18
store i32 %19, i32* %1
br label %20
20: ; preds = %12, %17, %14
%21 = phi i32 [ %16, %14 ], [ 0, %17 ], [ %9, %12 ]
ret i32 %21
}