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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ g_error(char *);
struct group *getgrgid(int /*<<< orphan*/);
char *get_groupname(gid_t gid) {
  struct group *grp;
  errno = 0;
  grp = getgrgid(gid);
  if (grp == NULL) {
    if (errno == 0) {
      return "unknown groupname";
    } else {
      g_error("groupname lookup failed");
      exit(EXIT_FAILURE);
    }
  }
  return grp->gr_name;
}


x86 assembly (clang, -O0):
<get_groupname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    0x2e9e(%rip),%rax
movq   $0x0,(%rax)
mov    -0xc(%rbp),%edi
call   1030 <getgrgid@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    119b <get_groupname+0x6b>
mov    0x2e79(%rip),%rax
cmpq   $0x0,(%rax)
jne    1181 <get_groupname+0x51>
lea    0xe88(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11a6 <get_groupname+0x76>
lea    0xe8a(%rip),%rdi
call   1040 <g_error@plt>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_groupname>:
push   %rbx
mov    0x2ea8(%rip),%rbx
movq   $0x0,(%rbx)
call   1030 <getgrgid@plt>
test   %rax,%rax
je     114e <get_groupname+0x1e>
mov    (%rax),%rax
pop    %rbx
ret
cmpq   $0x0,(%rbx)
jne    115d <get_groupname+0x2d>
lea    0xea5(%rip),%rax
pop    %rbx
ret
lea    0xeae(%rip),%rdi
call   1040 <g_error@plt>
mov    0x2e68(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
%struct.group = type { i8* }
@errno = global i64 0
@.str = [18 x i8] c"unknown groupname\00"
@.str.1 = [24 x i8] c"groupname lookup failed\00"
@EXIT_FAILURE = global i32 0
define i8* @get_groupname(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.group*
store i32 %0, i32* %3
store i64 0, i64* @errno
%5 = load i32, i32* %3
%6 = call %struct.group* @getgrgid(i32 %5)
store %struct.group* %6, %struct.group** %4
%7 = load %struct.group*, %struct.group** %4
%8 = icmp eq %struct.group* %7, null
br i1 %8, label %9, label %17
9: ; preds = %1
%10 = load i64, i64* @errno
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %9
store i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8** %2
br label %21
13: ; preds = %9
%14 = call i32 @g_error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %1
%18 = load %struct.group*, %struct.group** %4
%19 = getelementptr inbounds %struct.group, %struct.group* %18, i32 0, i32 0
%20 = load i8*, i8** %19
store i8* %20, i8** %2
br label %21
21: ; preds = %17, %12
%22 = load i8*, i8** %2
ret i8* %22
}

LLVM IR (-O2):
%struct.group = type { i8* }
@errno = global i64 0
@.str = [18 x i8] c"unknown groupname\00"
@.str.1 = [24 x i8] c"groupname lookup failed\00"
@EXIT_FAILURE = global i32 0
define i8* @get_groupname(i32 %0) {
store i64 0, i64* @errno
%2 = tail call %struct.group* @getgrgid(i32 %0)
%3 = icmp eq %struct.group* %2, null
br i1 %3, label %4, label %11
4: ; preds = %1
%5 = load i64, i64* @errno
%6 = icmp eq i64 %5, 0
br i1 %6, label %14, label %7
7: ; preds = %4
%8 = tail call i32 @g_error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %1
%12 = getelementptr inbounds %struct.group, %struct.group* %2, i64 0, i32 0
%13 = load i8*, i8** %12
br label %14
14: ; preds = %4, %11
%15 = phi i8* [ %13, %11 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %4 ]
ret i8* %15
}