#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 */
typedef scalar_t__ uid_t;
struct passwd {
  char *pw_name;
};

/* Variables and functions */
struct passwd *getpwuid(scalar_t__);
const char *getUserName(uid_t uid) {
  static const char *cachedName;
  static uid_t cachedUid;
  if (uid == cachedUid && cachedName) {
    return cachedName;
  } else {
    cachedUid = uid;
    struct passwd *user = getpwuid(uid);
    cachedName = user ? user->pw_name : NULL;
    return cachedName;
  }
}


x86 assembly (clang, -O0):
<getUserName>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmp    0x2f11(%rip),%rax
jne    114b <getUserName+0x3b>
cmpq   $0x0,0x2efb(%rip)
je     114b <getUserName+0x3b>
mov    0x2eee(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    119f <getUserName+0x8f>
mov    -0x10(%rbp),%rax
mov    %rax,0x2ee2(%rip)
mov    -0x10(%rbp),%rdi
call   1030 <getpwuid@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     117e <getUserName+0x6e>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    1189 <getUserName+0x79>
xor    %eax,%eax
mov    %rax,-0x20(%rbp)
jmp    1189 <getUserName+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):
<getUserName>:
push   %rax
cmp    %rdi,0x2f20(%rip)
jne    1128 <getUserName+0x18>
mov    0x2f0f(%rip),%rax
test   %rax,%rax
je     1128 <getUserName+0x18>
pop    %rcx
ret
mov    %rdi,0x2f09(%rip)
call   1030 <getpwuid@plt>
test   %rax,%rax
je     113e <getUserName+0x2e>
mov    (%rax),%rax
jmp    1140 <getUserName+0x30>
xor    %eax,%eax
mov    %rax,0x2ee9(%rip)
pop    %rcx
ret

LLVM IR (-O0):
%struct.passwd = type { i8* }
@getUserName.cachedName = internal global i8* null
@getUserName.cachedUid = internal global i64 0
define i8* @getUserName(i64 %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca %struct.passwd*
store i64 %0, i64* %3
%5 = load i64, i64* %3
%6 = load i64, i64* @getUserName.cachedUid
%7 = icmp eq i64 %5, %6
br i1 %7, label %8, label %13
8: ; preds = %1
%9 = load i8*, i8** @getUserName.cachedName
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = load i8*, i8** @getUserName.cachedName
store i8* %12, i8** %2
br label %27
13: ; preds = %8, %1
%14 = load i64, i64* %3
store i64 %14, i64* @getUserName.cachedUid
%15 = load i64, i64* %3
%16 = call %struct.passwd* @getpwuid(i64 %15)
store %struct.passwd* %16, %struct.passwd** %4
%17 = load %struct.passwd*, %struct.passwd** %4
%18 = icmp ne %struct.passwd* %17, null
br i1 %18, label %19, label %23
19: ; preds = %13
%20 = load %struct.passwd*, %struct.passwd** %4
%21 = getelementptr inbounds %struct.passwd, %struct.passwd* %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** @getUserName.cachedName
%26 = load i8*, i8** @getUserName.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.passwd = type { i8* }
@getUserName.cachedName = internal unnamed_addr global i8* null
@getUserName.cachedUid = internal unnamed_addr global i64 0
define i8* @getUserName(i64 %0) {
%2 = load i64, i64* @getUserName.cachedUid
%3 = icmp eq i64 %2, %0
%4 = load i8*, i8** @getUserName.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* @getUserName.cachedUid
%8 = tail call %struct.passwd* @getpwuid(i64 %0)
%9 = icmp eq %struct.passwd* %8, null
br i1 %9, label %13, label %10
10: ; preds = %7
%11 = getelementptr inbounds %struct.passwd, %struct.passwd* %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** @getUserName.cachedName
br label %15
15: ; preds = %1, %13
%16 = phi i8* [ %14, %13 ], [ %4, %1 ]
ret i8* %16
}