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

/* Variables and functions */
struct group *getgrgid(scalar_t__);
const char *getGroupName(gid_t gid) {
  static const char *cachedName;
  static gid_t cachedGid;
  if (gid == cachedGid && cachedName) {
    return cachedName;
  } else {
    cachedGid = gid;
    struct group *group = getgrgid(gid);
    cachedName = group ? group->gr_name : NULL;
    return cachedName;
  }
}


x86 assembly (clang, -O0):
<getGroupName>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmp    0x2f11(%rip),%rax
jne    114b <getGroupName+0x3b>
cmpq   $0x0,0x2efb(%rip)
je     114b <getGroupName+0x3b>
mov    0x2eee(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    119f <getGroupName+0x8f>
mov    -0x10(%rbp),%rax
mov    %rax,0x2ee2(%rip)
mov    -0x10(%rbp),%rdi
call   1030 <getgrgid@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     117e <getGroupName+0x6e>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    1189 <getGroupName+0x79>
xor    %eax,%eax
mov    %rax,-0x20(%rbp)
jmp    1189 <getGroupName+0x79>
mov    -0x20(%rbp),%rax
mov    %rax,0x2e9c(%rip)
mov    0x2e95(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getGroupName>:
push   %rax
cmp    %rdi,0x2f20(%rip)
jne    1128 <getGroupName+0x18>
mov    0x2f0f(%rip),%rax
test   %rax,%rax
je     1128 <getGroupName+0x18>
pop    %rcx
ret
mov    %rdi,0x2f09(%rip)
call   1030 <getgrgid@plt>
test   %rax,%rax
je     113e <getGroupName+0x2e>
mov    (%rax),%rax
jmp    1140 <getGroupName+0x30>
xor    %eax,%eax
mov    %rax,0x2ee9(%rip)
pop    %rcx
ret

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

LLVM IR (-O2):
%struct.group = type { i8* }
@getGroupName.cachedName = internal unnamed_addr global i8* null
@getGroupName.cachedGid = internal unnamed_addr global i64 0
define i8* @getGroupName(i64 %0) {
%2 = load i64, i64* @getGroupName.cachedGid
%3 = icmp eq i64 %2, %0
%4 = load i8*, i8** @getGroupName.cachedName
%5 = icmp ne i8* %4, null
%6 = select i1 %3, i1 %5, i1 false
br i1 %6, label %15, label %7
7: ; preds = %1
store i64 %0, i64* @getGroupName.cachedGid
%8 = tail call %struct.group* @getgrgid(i64 %0)
%9 = icmp eq %struct.group* %8, null
br i1 %9, label %13, label %10
10: ; preds = %7
%11 = getelementptr inbounds %struct.group, %struct.group* %8, i64 0, i32 0
%12 = load i8*, i8** %11
br label %13
13: ; preds = %7, %10
%14 = phi i8* [ %12, %10 ], [ null, %7 ]
store i8* %14, i8** @getGroupName.cachedName
br label %15
15: ; preds = %1, %13
%16 = phi i8* [ %14, %13 ], [ %4, %1 ]
ret i8* %16
}