#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;
};

/* Variables and functions */
int /*<<< orphan*/ endgrent();
scalar_t__ errno;
struct group *getgrent();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print_entry(int /*<<< orphan*/, unsigned long);
int /*<<< orphan*/ setgrent();
void list_all_groups(void) {
  setgrent();
  errno = 0;
  struct group *g;
  while ((g = getgrent())) {
    print_entry(g->gr_name, (unsigned long int)g->gr_gid);
    errno = 0;
  }
  if (errno)
    perror("getgrent");
  endgrent();
}


x86 assembly (clang, -O0):
<list_all_groups>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1050 <setgrent@plt>
mov    0x2e7a(%rip),%rax
movq   $0x0,(%rax)
mov    $0x0,%al
call   1040 <getgrent@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
je     11a8 <list_all_groups+0x58>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
call   1070 <print_entry@plt>
mov    0x2e44(%rip),%rax
movq   $0x0,(%rax)
jmp    116d <list_all_groups+0x1d>
mov    0x2e31(%rip),%rax
cmpq   $0x0,(%rax)
je     11c5 <list_all_groups+0x75>
lea    0xe40(%rip),%rdi
call   1060 <perror@plt>
mov    $0x0,%al
call   1030 <endgrent@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<list_all_groups>:
push   %rbx
xor    %eax,%eax
call   1050 <setgrent@plt>
mov    0x2e81(%rip),%rbx
nop
movq   $0x0,(%rbx)
xor    %eax,%eax
call   1040 <getgrent@plt>
test   %rax,%rax
je     1180 <list_all_groups+0x30>
mov    0x8(%rax),%edi
mov    (%rax),%rsi
call   1070 <print_entry@plt>
jmp    1160 <list_all_groups+0x10>
cmpq   $0x0,(%rbx)
jne    118e <list_all_groups+0x3e>
xor    %eax,%eax
pop    %rbx
jmp    1030 <endgrent@plt>
lea    0xe6b(%rip),%rdi
call   1060 <perror@plt>
jmp    1186 <list_all_groups+0x36>

LLVM IR (-O0):
%struct.group = type { i64, i32 }
@errno = global i64 0
@.str = [9 x i8] c"getgrent\00"
define void @list_all_groups() {
%1 = alloca %struct.group*
%2 = call i32 (...) @setgrent()
store i64 0, i64* @errno
br label %3
3: ; preds = %6, %0
%4 = call %struct.group* (...) @getgrent()
store %struct.group* %4, %struct.group** %1
%5 = icmp ne %struct.group* %4, null
br i1 %5, label %6, label %14
6: ; preds = %3
%7 = load %struct.group*, %struct.group** %1
%8 = getelementptr inbounds %struct.group, %struct.group* %7, i32 0, i32 1
%9 = load i32, i32* %8
%10 = load %struct.group*, %struct.group** %1
%11 = getelementptr inbounds %struct.group, %struct.group* %10, i32 0, i32 0
%12 = load i64, i64* %11
%13 = call i32 @print_entry(i32 %9, i64 %12)
store i64 0, i64* @errno
br label %3
14: ; preds = %3
%15 = load i64, i64* @errno
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %14
%20 = call i32 (...) @endgrent()
ret void
}

LLVM IR (-O2):
%struct.group = type { i64, i32 }
@errno = global i64 0
@.str = [9 x i8] c"getgrent\00"
define void @list_all_groups() {
%1 = tail call i32 (...) @setgrent()
store i64 0, i64* @errno
%2 = tail call %struct.group* (...) @getgrent()
%3 = icmp eq %struct.group* %2, null
br i1 %3, label %13, label %4
4: ; preds = %0, %4
%5 = phi %struct.group* [ %11, %4 ], [ %2, %0 ]
%6 = getelementptr inbounds %struct.group, %struct.group* %5, i64 0, i32 1
%7 = load i32, i32* %6
%8 = getelementptr inbounds %struct.group, %struct.group* %5, i64 0, i32 0
%9 = load i64, i64* %8
%10 = tail call i32 @print_entry(i32 %7, i64 %9)
store i64 0, i64* @errno
%11 = tail call %struct.group* (...) @getgrent()
%12 = icmp eq %struct.group* %11, null
br i1 %12, label %13, label %4
13: ; preds = %4, %0
%14 = load i64, i64* @errno
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %13
%19 = tail call i32 (...) @endgrent()
ret void
}