#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 */
int /*<<< orphan*/ free(char *);
struct group *getgrgid(scalar_t__);
char *strdup(char *);
char *xgetgrgid(gid_t gid) {
  static char *cache[32];
  static gid_t gids[32];
  static unsigned int used, next;
  struct group *gr;
  int i;
  for (i = 0; i < used; i++) {
    if (gids[i] == gid) {
      return cache[i];
    }
  }
  gr = getgrgid(gid);
  if (NULL != cache[next]) {
    free(cache[next]);
    cache[next] = NULL;
  }
  if (NULL != gr) {
    cache[next] = strdup(gr->gr_name);
  }
  gids[next] = gid;
  next++;
  if (32 == next) {
    next = 0;
  }
  if (used < 32) {
    used++;
  }
  return gr ? gr->gr_name : NULL;
}


x86 assembly (clang, -O0):
<xgetgrgid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    0x3104(%rip),%eax
jae    1196 <xgetgrgid+0x66>
movslq -0x1c(%rbp),%rcx
lea    0x2ff3(%rip),%rax
mov    (%rax,%rcx,8),%rax
cmp    -0x10(%rbp),%rax
jne    1183 <xgetgrgid+0x53>
movslq -0x1c(%rbp),%rcx
lea    0x2eda(%rip),%rax
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x8(%rbp)
jmp    12a6 <xgetgrgid+0x176>
jmp    1188 <xgetgrgid+0x58>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1143 <xgetgrgid+0x13>
mov    -0x10(%rbp),%rdi
call   1040 <getgrgid@plt>
mov    %rax,-0x18(%rbp)
mov    0x30ab(%rip),%eax
mov    %eax,%edx
xor    %eax,%eax
lea    0x2e9c(%rip),%rcx
cmp    (%rcx,%rdx,8),%rax
je     11ed <xgetgrgid+0xbd>
mov    0x3090(%rip),%eax
mov    %eax,%ecx
lea    0x2e83(%rip),%rax
mov    (%rax,%rcx,8),%rdi
call   1030 <free@plt>
mov    0x3078(%rip),%eax
mov    %eax,%ecx
lea    0x2e6b(%rip),%rax
movq   $0x0,(%rax,%rcx,8)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
je     121b <xgetgrgid+0xeb>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strdup@plt>
mov    %rax,%rdx
mov    0x3046(%rip),%eax
mov    %eax,%ecx
lea    0x2e39(%rip),%rax
mov    %rdx,(%rax,%rcx,8)
mov    -0x10(%rbp),%rdx
mov    0x302f(%rip),%eax
mov    %eax,%ecx
lea    0x2f22(%rip),%rax
mov    %rdx,(%rax,%rcx,8)
mov    0x301c(%rip),%eax
add    $0x1,%eax
mov    %eax,0x3013(%rip)
mov    $0x20,%eax
cmp    0x3008(%rip),%eax
jne    125c <xgetgrgid+0x12c>
movl   $0x0,0x2ff8(%rip)
cmpl   $0x20,0x2fed(%rip)
jae    1278 <xgetgrgid+0x148>
mov    0x2fe1(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2fd8(%rip)
cmpq   $0x0,-0x18(%rbp)
je     1293 <xgetgrgid+0x163>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    129e <xgetgrgid+0x16e>
xor    %eax,%eax
mov    %rax,-0x28(%rbp)
jmp    129e <xgetgrgid+0x16e>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xgetgrgid>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x310f(%rip),%eax
test   %rax,%rax
je     1173 <xgetgrgid+0x43>
shl    $0x3,%rax
xor    %ecx,%ecx
lea    0x2ffd(%rip),%rdx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmp    %rbx,(%rcx,%rdx,1)
je     11ed <xgetgrgid+0xbd>
add    $0x8,%rcx
cmp    %rcx,%rax
jne    1160 <xgetgrgid+0x30>
mov    %rbx,%rdi
call   1040 <getgrgid@plt>
mov    %rax,%r14
mov    0x30cf(%rip),%r15d
lea    0x2ec4(%rip),%r12
mov    (%r12,%r15,8),%rdi
test   %rdi,%rdi
je     11a2 <xgetgrgid+0x72>
call   1030 <free@plt>
movq   $0x0,(%r12,%r15,8)
test   %r14,%r14
je     11b3 <xgetgrgid+0x83>
mov    (%r14),%rdi
call   1050 <strdup@plt>
mov    %rax,(%r12,%r15,8)
lea    0x1(%r15),%eax
xor    %ecx,%ecx
cmp    $0x20,%eax
cmovne %eax,%ecx
lea    0x2f8a(%rip),%rax
mov    %rbx,(%rax,%r15,8)
mov    %ecx,0x3084(%rip)
mov    0x307a(%rip),%eax
cmp    $0x1f,%eax
ja     11e4 <xgetgrgid+0xb4>
add    $0x1,%eax
mov    %eax,0x306c(%rip)
test   %r14,%r14
jne    11f7 <xgetgrgid+0xc7>
xor    %eax,%eax
jmp    11fa <xgetgrgid+0xca>
lea    0x2e5c(%rip),%r14
add    %rcx,%r14
mov    (%r14),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.group = type { i8* }
@xgetgrgid.cache = internal global [32 x i8*] zeroinitializer
@xgetgrgid.gids = internal global [32 x i64] zeroinitializer
@xgetgrgid.used = internal global i32 0
@xgetgrgid.next = internal global i32 0
define i8* @xgetgrgid(i64 %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca %struct.group*
%5 = alloca i32
store i64 %0, i64* %3
store i32 0, i32* %5
br label %6
6: ; preds = %23, %1
%7 = load i32, i32* %5
%8 = load i32, i32* @xgetgrgid.used
%9 = icmp ult i32 %7, %8
br i1 %9, label %10, label %26
10: ; preds = %6
%11 = load i32, i32* %5
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [32 x i64], [32 x i64]* @xgetgrgid.gids, i64 0, i64 %12
%14 = load i64, i64* %13
%15 = load i64, i64* %3
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %22
17: ; preds = %10
%18 = load i32, i32* %5
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [32 x i8*], [32 x i8*]* @xgetgrgid.cache, i64 0, i64 %19
%21 = load i8*, i8** %20
store i8* %21, i8** %2
br label %80
22: ; preds = %10
br label %23
23: ; preds = %22
%24 = load i32, i32* %5
%25 = add nsw i32 %24, 1
store i32 %25, i32* %5
br label %6
26: ; preds = %6
%27 = load i64, i64* %3
%28 = call %struct.group* @getgrgid(i64 %27)
store %struct.group* %28, %struct.group** %4
%29 = load i32, i32* @xgetgrgid.next
%30 = zext i32 %29 to i64
%31 = getelementptr inbounds [32 x i8*], [32 x i8*]* @xgetgrgid.cache, i64 0, i64 %30
%32 = load i8*, i8** %31
%33 = icmp ne i8* null, %32
br i1 %33, label %34, label %43
34: ; preds = %26
%35 = load i32, i32* @xgetgrgid.next
%36 = zext i32 %35 to i64
%37 = getelementptr inbounds [32 x i8*], [32 x i8*]* @xgetgrgid.cache, i64 0, i64 %36
%38 = load i8*, i8** %37
%39 = call i32 @free(i8* %38)
%40 = load i32, i32* @xgetgrgid.next
%41 = zext i32 %40 to i64
%42 = getelementptr inbounds [32 x i8*], [32 x i8*]* @xgetgrgid.cache, i64 0, i64 %41
store i8* null, i8** %42
br label %43
43: ; preds = %34, %26
%44 = load %struct.group*, %struct.group** %4
%45 = icmp ne %struct.group* null, %44
br i1 %45, label %46, label %54
46: ; preds = %43
%47 = load %struct.group*, %struct.group** %4
%48 = getelementptr inbounds %struct.group, %struct.group* %47, i32 0, i32 0
%49 = load i8*, i8** %48
%50 = call i8* @strdup(i8* %49)
%51 = load i32, i32* @xgetgrgid.next
%52 = zext i32 %51 to i64
%53 = getelementptr inbounds [32 x i8*], [32 x i8*]* @xgetgrgid.cache, i64 0, i64 %52
store i8* %50, i8** %53
br label %54
54: ; preds = %46, %43
%55 = load i64, i64* %3
%56 = load i32, i32* @xgetgrgid.next
%57 = zext i32 %56 to i64
%58 = getelementptr inbounds [32 x i64], [32 x i64]* @xgetgrgid.gids, i64 0, i64 %57
store i64 %55, i64* %58
%59 = load i32, i32* @xgetgrgid.next
%60 = add i32 %59, 1
store i32 %60, i32* @xgetgrgid.next
%61 = load i32, i32* @xgetgrgid.next
%62 = icmp eq i32 32, %61
br i1 %62, label %63, label %64
63: ; preds = %54
store i32 0, i32* @xgetgrgid.next
br label %64
64: ; preds = %63, %54
%65 = load i32, i32* @xgetgrgid.used
%66 = icmp ult i32 %65, 32
br i1 %66, label %67, label %70
67: ; preds = %64
%68 = load i32, i32* @xgetgrgid.used
%69 = add i32 %68, 1
store i32 %69, i32* @xgetgrgid.used
br label %70
70: ; preds = %67, %64
%71 = load %struct.group*, %struct.group** %4
%72 = icmp ne %struct.group* %71, null
br i1 %72, label %73, label %77
73: ; preds = %70
%74 = load %struct.group*, %struct.group** %4
%75 = getelementptr inbounds %struct.group, %struct.group* %74, i32 0, i32 0
%76 = load i8*, i8** %75
br label %78
77: ; preds = %70
br label %78
78: ; preds = %77, %73
%79 = phi i8* [ %76, %73 ], [ null, %77 ]
store i8* %79, i8** %2
br label %80
80: ; preds = %78, %17
%81 = load i8*, i8** %2
ret i8* %81
}

LLVM IR (-O2):
%struct.group = type { i8* }
@xgetgrgid.cache = internal unnamed_addr global [32 x i8*] zeroinitializer
@xgetgrgid.gids = internal unnamed_addr global [32 x i64] zeroinitializer
@xgetgrgid.used = internal unnamed_addr global i32 0
@xgetgrgid.next = internal unnamed_addr global i32 0
define i8* @xgetgrgid(i64 %0) {
%2 = load i32, i32* @xgetgrgid.used
%3 = icmp eq i32 %2, 0
br i1 %3, label %17, label %4
4: ; preds = %1
%5 = zext i32 %2 to i64
br label %6
6: ; preds = %4, %14
%7 = phi i64 [ 0, %4 ], [ %15, %14 ]
%8 = getelementptr inbounds [32 x i64], [32 x i64]* @xgetgrgid.gids, i64 0, i64 %7
%9 = load i64, i64* %8
%10 = icmp eq i64 %9, %0
br i1 %10, label %11, label %14
11: ; preds = %6
%12 = and i64 %7, 4294967295
%13 = getelementptr inbounds [32 x i8*], [32 x i8*]* @xgetgrgid.cache, i64 0, i64 %12
br label %44
14: ; preds = %6
%15 = add nuw nsw i64 %7, 1
%16 = icmp eq i64 %15, %5
br i1 %16, label %17, label %6
17: ; preds = %14, %1
%18 = tail call %struct.group* @getgrgid(i64 %0)
%19 = load i32, i32* @xgetgrgid.next
%20 = zext i32 %19 to i64
%21 = getelementptr inbounds [32 x i8*], [32 x i8*]* @xgetgrgid.cache, i64 0, i64 %20
%22 = load i8*, i8** %21
%23 = icmp eq i8* %22, null
br i1 %23, label %26, label %24
24: ; preds = %17
%25 = tail call i32 @free(i8* %22)
store i8* null, i8** %21
br label %26
26: ; preds = %24, %17
%27 = icmp eq %struct.group* %18, null
br i1 %27, label %32, label %28
28: ; preds = %26
%29 = getelementptr inbounds %struct.group, %struct.group* %18, i64 0, i32 0
%30 = load i8*, i8** %29
%31 = tail call i8* @strdup(i8* %30)
store i8* %31, i8** %21
br label %32
32: ; preds = %28, %26
%33 = getelementptr inbounds [32 x i64], [32 x i64]* @xgetgrgid.gids, i64 0, i64 %20
store i64 %0, i64* %33
%34 = add i32 %19, 1
%35 = icmp eq i32 %34, 32
%36 = select i1 %35, i32 0, i32 %34
store i32 %36, i32* @xgetgrgid.next
%37 = load i32, i32* @xgetgrgid.used
%38 = icmp ult i32 %37, 32
br i1 %38, label %39, label %41
39: ; preds = %32
%40 = add nuw nsw i32 %37, 1
store i32 %40, i32* @xgetgrgid.used
br label %41
41: ; preds = %39, %32
br i1 %27, label %47, label %42
42: ; preds = %41
%43 = getelementptr inbounds %struct.group, %struct.group* %18, i64 0, i32 0
br label %44
44: ; preds = %11, %42
%45 = phi i8** [ %43, %42 ], [ %13, %11 ]
%46 = load i8*, i8** %45
br label %47
47: ; preds = %44, %41
%48 = phi i8* [ null, %41 ], [ %46, %44 ]
ret i8* %48
}