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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
struct group *getgrgid(int);
int /*<<< orphan*/ stderr;
char *getgroupname(gid_t gid) {
  struct group *grp;
  if ((grp = getgrgid(gid)) == NULL) {
    fprintf(stderr, "Failed to get group name from gid: %-8d\n", gid);
    exit(EXIT_FAILURE);
  } else
    return (grp->gr_name);
}


x86 assembly (clang, -O0):
<getgroupname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <getgrgid@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1177 <getgroupname+0x47>
mov    0x2e88(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xe9c(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e68(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getgroupname>:
push   %rbx
mov    %edi,%ebx
call   1030 <getgrgid@plt>
test   %rax,%rax
je     1142 <getgroupname+0x12>
mov    (%rax),%rax
pop    %rbx
ret
mov    0x2e97(%rip),%rax
mov    (%rax),%edi
lea    0xeae(%rip),%rsi
mov    %ebx,%edx
call   1040 <fprintf@plt>
mov    0x2e78(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
%struct.group = type { i8* }
@stderr = global i32 0
@.str = [41 x i8] c"Failed to get group name from gid: %-8d\0A\00"
@EXIT_FAILURE = global i32 0
define i8* @getgroupname(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.group*
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = call %struct.group* @getgrgid(i32 %4)
store %struct.group* %5, %struct.group** %3
%6 = icmp eq %struct.group* %5, null
br i1 %6, label %7, label %13
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = load i32, i32* %2
%10 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @exit(i32 %11)
unreachable
13: ; preds = %1
%14 = load %struct.group*, %struct.group** %3
%15 = getelementptr inbounds %struct.group, %struct.group* %14, i32 0, i32 0
%16 = load i8*, i8** %15
ret i8* %16
}

LLVM IR (-O2):
%struct.group = type { i8* }
@stderr = global i32 0
@.str = [41 x i8] c"Failed to get group name from gid: %-8d\0A\00"
@EXIT_FAILURE = global i32 0
define i8* @getgroupname(i32 %0) {
%2 = tail call %struct.group* @getgrgid(i32 %0)
%3 = icmp eq %struct.group* %2, null
br i1 %3, label %4, label %9
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %1
%10 = getelementptr inbounds %struct.group, %struct.group* %2, i64 0, i32 0
%11 = load i8*, i8** %10
ret i8* %11
}