#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;
  int /*<<< orphan*/ gr_name;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ bb_error_msg_and_die(char *, char const *);
int /*<<< orphan*/ bb_perror_msg_and_die(char *);
int /*<<< orphan*/ *bb_xfopen(char *, char *);
scalar_t__ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
struct group *fgetgrent(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcmp(char const *, int /*<<< orphan*/);
long my_getgrnam(const char *name) {
  struct group *mygroup;
  FILE *stream;
  stream = bb_xfopen("etc/group", "r");
  while (1) {
    errno = 0;
    mygroup = fgetgrent(stream);
    if (mygroup == NULL)
      bb_error_msg_and_die("unknown group name: %s", name);
    if (errno)
      bb_perror_msg_and_die("fgetgrent");
    if (!strcmp(name, mygroup->gr_name))
      break;
  }
  fclose(stream);
  return mygroup->gr_gid;
}


x86 assembly (clang, -O0):
<my_getgrnam>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe8d(%rip),%rdi
lea    0xe90(%rip),%rsi
call   1070 <bb_xfopen@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e56(%rip),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
call   1080 <fgetgrent@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b9 <my_getgrnam+0x59>
mov    -0x8(%rbp),%rsi
lea    0xe58(%rip),%rdi
call   1060 <bb_error_msg_and_die@plt>
mov    0x2e20(%rip),%rax
cmpq   $0x0,(%rax)
je     11d6 <my_getgrnam+0x76>
lea    0xe52(%rip),%rdi
call   1050 <bb_perror_msg_and_die@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%esi
call   1040 <strcmp@plt>
cmp    $0x0,%eax
jne    11f4 <my_getgrnam+0x94>
jmp    11f9 <my_getgrnam+0x99>
jmp    1183 <my_getgrnam+0x23>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_getgrnam>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r13
lea    0xe8b(%rip),%rdi
lea    0xe8e(%rip),%rsi
call   1070 <bb_xfopen@plt>
mov    %rax,%r14
mov    0x2e55(%rip),%rbp
lea    0xe7a(%rip),%r15
lea    0xe8a(%rip),%r12
jmp    11af <my_getgrnam+0x4f>
nopl   0x0(%rax,%rax,1)
mov    0x8(%rbx),%esi
mov    %r13,%rdi
call   1040 <strcmp@plt>
test   %eax,%eax
je     11ec <my_getgrnam+0x8c>
movq   $0x0,0x0(%rbp)
mov    %r14,%rdi
call   1080 <fgetgrent@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11d0 <my_getgrnam+0x70>
cmpq   $0x0,0x0(%rbp)
jne    11e2 <my_getgrnam+0x82>
jmp    11a0 <my_getgrnam+0x40>
mov    %r15,%rdi
mov    %r13,%rsi
call   1060 <bb_error_msg_and_die@plt>
cmpq   $0x0,0x0(%rbp)
je     11a0 <my_getgrnam+0x40>
mov    %r12,%rdi
call   1050 <bb_perror_msg_and_die@plt>
jmp    11a0 <my_getgrnam+0x40>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    (%rbx),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.group = type { i64, i32 }
@.str = [10 x i8] c"etc/group\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i64 0
@.str.2 = [23 x i8] c"unknown group name: %s\00"
@.str.3 = [10 x i8] c"fgetgrent\00"
define i64 @my_getgrnam(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.group*
%4 = alloca i32*
store i8* %0, i8** %2
%5 = call i32* @bb_xfopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %4
br label %6
6: ; preds = %1, %27
store i64 0, i64* @errno
%7 = load i32*, i32** %4
%8 = call %struct.group* @fgetgrent(i32* %7)
store %struct.group* %8, %struct.group** %3
%9 = load %struct.group*, %struct.group** %3
%10 = icmp eq %struct.group* %9, null
br i1 %10, label %11, label %14
11: ; preds = %6
%12 = load i8*, i8** %2
%13 = call i32 @bb_error_msg_and_die(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %12)
br label %14
14: ; preds = %11, %6
%15 = load i64, i64* @errno
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 @bb_perror_msg_and_die(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
br label %19
19: ; preds = %17, %14
%20 = load i8*, i8** %2
%21 = load %struct.group*, %struct.group** %3
%22 = getelementptr inbounds %struct.group, %struct.group* %21, i32 0, i32 1
%23 = load i32, i32* %22
%24 = call i32 @strcmp(i8* %20, i32 %23)
%25 = icmp ne i32 %24, 0
br i1 %25, label %27, label %26
26: ; preds = %19
br label %28
27: ; preds = %19
br label %6
28: ; preds = %26
%29 = load i32*, i32** %4
%30 = call i32 @fclose(i32* %29)
%31 = load %struct.group*, %struct.group** %3
%32 = getelementptr inbounds %struct.group, %struct.group* %31, i32 0, i32 0
%33 = load i64, i64* %32
ret i64 %33
}

LLVM IR (-O2):
%struct.group = type { i64, i32 }
@.str = [10 x i8] c"etc/group\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i64 0
@.str.2 = [23 x i8] c"unknown group name: %s\00"
@.str.3 = [10 x i8] c"fgetgrent\00"
define i64 @my_getgrnam(i8* %0) {
%2 = tail call i32* @bb_xfopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %3
3: ; preds = %13, %1
store i64 0, i64* @errno
%4 = tail call %struct.group* @fgetgrent(i32* %2)
%5 = icmp eq %struct.group* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @bb_error_msg_and_die(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %8
8: ; preds = %6, %3
%9 = load i64, i64* @errno
%10 = icmp eq i64 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %8
%12 = tail call i32 @bb_perror_msg_and_die(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
br label %13
13: ; preds = %11, %8
%14 = getelementptr inbounds %struct.group, %struct.group* %4, i64 0, i32 1
%15 = load i32, i32* %14
%16 = tail call i32 @strcmp(i8* %0, i32 %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %3
18: ; preds = %13
%19 = tail call i32 @fclose(i32* %2)
%20 = getelementptr inbounds %struct.group, %struct.group* %4, i64 0, i32 0
%21 = load i64, i64* %20
ret i64 %21
}