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

/* Variables and functions */
int /*<<< orphan*/ endgrent();
struct group *getgrent();
scalar_t__ strcmp(char const *, int /*<<< orphan*/);
gid_t get_group_by_name(const char *name) {
  struct group *grp;
  gid_t res = 0;
  while ((0 == res) && (NULL != (grp = getgrent()))) {
    if (0 == strcmp(name, grp->gr_name)) {
      res = grp->gr_gid;
    }
  }
  endgrent();
  return res;
}


x86 assembly (clang, -O0):
<get_group_by_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x18(%rbp)
xor    %eax,%eax
xor    %ecx,%ecx
cmp    -0x18(%rbp),%rcx
mov    %al,-0x19(%rbp)
jne    116e <get_group_by_name+0x3e>
mov    $0x0,%al
call   1040 <getgrent@plt>
mov    %rax,%rcx
mov    %rcx,-0x10(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    117e <get_group_by_name+0x4e>
jmp    11ac <get_group_by_name+0x7c>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%esi
call   1050 <strcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    11a7 <get_group_by_name+0x77>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    1144 <get_group_by_name+0x14>
mov    $0x0,%al
call   1030 <endgrent@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_group_by_name>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
jmp    1148 <get_group_by_name+0x18>
nopl   0x0(%rax)
mov    (%rbx),%rbx
test   %rbx,%rbx
jne    1172 <get_group_by_name+0x42>
xor    %eax,%eax
call   1040 <getgrent@plt>
test   %rax,%rax
je     1170 <get_group_by_name+0x40>
mov    %rax,%rbx
mov    0x8(%rax),%esi
mov    %r14,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
je     1140 <get_group_by_name+0x10>
xor    %ebx,%ebx
test   %rbx,%rbx
je     1148 <get_group_by_name+0x18>
jmp    1172 <get_group_by_name+0x42>
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <endgrent@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
%struct.group = type { i64, i32 }
define i64 @get_group_by_name(i8* %0) {
br label %2
2: ; preds = %1, %13
%3 = tail call %struct.group* (...) @getgrent()
%4 = icmp eq %struct.group* %3, null
br i1 %4, label %16, label %5
5: ; preds = %2
%6 = getelementptr inbounds %struct.group, %struct.group* %3, i64 0, i32 1
%7 = load i32, i32* %6
%8 = tail call i64 @strcmp(i8* %0, i32 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %5
%11 = getelementptr inbounds %struct.group, %struct.group* %3, i64 0, i32 0
%12 = load i64, i64* %11
br label %13
13: ; preds = %10, %5
%14 = phi i64 [ %12, %10 ], [ 0, %5 ]
%15 = icmp eq i64 %14, 0
br i1 %15, label %2, label %16
16: ; preds = %13, %2
%17 = phi i64 [ %14, %13 ], [ 0, %2 ]
%18 = tail call i32 (...) @endgrent()
ret i64 %17
}