#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 passwd {
  scalar_t__ pw_name;
};

/* Variables and functions */
int /*<<< orphan*/ free(char *);
struct passwd *getpwuid(int);
char *strdup(scalar_t__);
char *pwname(int uid) {
  static int last_uid = -1;
  static char *last_name = NULL;
  struct passwd *pw;
  if (uid != last_uid) {
    free(last_name);
    pw = getpwuid(uid);
    if (pw && pw->pw_name)
      last_name = strdup(pw->pw_name);
    else
      last_name = NULL;
    last_uid = uid;
  }
  return last_name;
}


x86 assembly (clang, -O0):
<pwname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
cmp    0x2ef4(%rip),%eax
je     11a7 <pwname+0x77>
mov    0x2ef7(%rip),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%edi
call   1040 <getpwuid@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1193 <pwname+0x63>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     1193 <pwname+0x63>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strdup@plt>
mov    %rax,0x2eba(%rip)
jmp    119e <pwname+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):
<pwname>:
push   %rbx
mov    0x2f10(%rip),%rax
cmp    %edi,0x2efa(%rip)
je     1174 <pwname+0x44>
mov    %edi,%ebx
mov    %rax,%rdi
call   1030 <free@plt>
mov    %ebx,%edi
call   1040 <getpwuid@plt>
test   %rax,%rax
je     1165 <pwname+0x35>
mov    (%rax),%rdi
test   %rdi,%rdi
je     1165 <pwname+0x35>
call   1050 <strdup@plt>
jmp    1167 <pwname+0x37>
xor    %eax,%eax
mov    %rax,0x2eda(%rip)
mov    %ebx,0x2ec4(%rip)
pop    %rbx
ret

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

LLVM IR (-O2):
%struct.passwd = type { i64 }
@pwname.last_uid = internal unnamed_addr global i32 -1
@pwname.last_name = internal unnamed_addr global i8* null
define i8* @pwname(i32 %0) {
%2 = load i32, i32* @pwname.last_uid
%3 = icmp eq i32 %2, %0
%4 = load i8*, i8** @pwname.last_name
br i1 %3, label %17, label %5
5: ; preds = %1
%6 = tail call i32 @free(i8* %4)
%7 = tail call %struct.passwd* @getpwuid(i32 %0)
%8 = icmp eq %struct.passwd* %7, null
br i1 %8, label %15, label %9
9: ; preds = %5
%10 = getelementptr inbounds %struct.passwd, %struct.passwd* %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** @pwname.last_name
store i32 %0, i32* @pwname.last_uid
br label %17
17: ; preds = %1, %15
%18 = phi i8* [ %16, %15 ], [ %4, %1 ]
ret i8* %18
}