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

/* Variables and functions */
int /*<<< orphan*/ free(char *);
struct group *getgrgid(int);
char *strdup(scalar_t__);
char *grname(int gid) {
  static int last_gid = -1;
  static char *last_name = NULL;
  struct group *gr;
  if (gid != last_gid) {
    free(last_name);
    gr = getgrgid(gid);
    if (gr && gr->gr_name)
      last_name = strdup(gr->gr_name);
    else
      last_name = NULL;
    last_gid = gid;
  }
  return last_name;
}


x86 assembly (clang, -O0):
<grname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
cmp    0x2ef4(%rip),%eax
je     11a7 <grname+0x77>
mov    0x2ef7(%rip),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%edi
call   1040 <getgrgid@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1193 <grname+0x63>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     1193 <grname+0x63>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strdup@plt>
mov    %rax,0x2eba(%rip)
jmp    119e <grname+0x6e>
movq   $0x0,0x2eaa(%rip)
mov    -0x4(%rbp),%eax
mov    %eax,0x2e91(%rip)
mov    0x2e9a(%rip),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<grname>:
push   %rbx
mov    0x2f10(%rip),%rax
cmp    %edi,0x2efa(%rip)
je     1174 <grname+0x44>
mov    %edi,%ebx
mov    %rax,%rdi
call   1030 <free@plt>
mov    %ebx,%edi
call   1040 <getgrgid@plt>
test   %rax,%rax
je     1165 <grname+0x35>
mov    (%rax),%rdi
test   %rdi,%rdi
je     1165 <grname+0x35>
call   1050 <strdup@plt>
jmp    1167 <grname+0x37>
xor    %eax,%eax
mov    %rax,0x2eda(%rip)
mov    %ebx,0x2ec4(%rip)
pop    %rbx
ret

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

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