#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct passwd {
  scalar_t__ pw_uid;
};
struct TYPE_2__ {
  char *pw_name;
};

/* Variables and functions */
char *getlogin();
struct passwd *getpwnam(char *);
TYPE_1__ *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ getuid();
unsigned long who_am_i(void) {
  struct passwd *pw;
  static unsigned long i_am = 0L;
  char *name;
  if (i_am > 0L)
    return i_am;
  name = getlogin();
  if (name == NULL || name[0] == '\0')
    name = getpwuid(getuid())->pw_name;
  pw = getpwnam(name);
  i_am = (unsigned long)pw->pw_uid;
  return i_am;
}


x86 assembly (clang, -O0):
<who_am_i>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
cmpq   $0x0,0x2ef8(%rip)
jbe    1166 <who_am_i+0x26>
mov    0x2eeb(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11c7 <who_am_i+0x87>
mov    $0x0,%al
call   1060 <getlogin@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     118c <who_am_i+0x4c>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11a1 <who_am_i+0x61>
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %eax,%edi
call   1030 <getpwuid@plt>
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <getpwnam@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,0x2e8c(%rip)
mov    0x2e85(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<who_am_i>:
push   %rax
mov    0x2f00(%rip),%rax
test   %rax,%rax
je     114f <who_am_i+0xf>
pop    %rcx
ret
xor    %eax,%eax
call   1060 <getlogin@plt>
test   %rax,%rax
je     1160 <who_am_i+0x20>
cmpb   $0x0,(%rax)
jne    1171 <who_am_i+0x31>
xor    %eax,%eax
call   1040 <getuid@plt>
mov    %eax,%edi
call   1030 <getpwuid@plt>
mov    (%rax),%rax
mov    %rax,%rdi
call   1050 <getpwnam@plt>
mov    (%rax),%rax
mov    %rax,0x2ec5(%rip)
pop    %rcx
ret

LLVM IR (-O0):
%struct.passwd = type { i64 }
%struct.TYPE_2__ = type { i8* }
@who_am_i.i_am = internal global i64 0
define i64 @who_am_i() {
%1 = alloca i64
%2 = alloca %struct.passwd*
%3 = alloca i8*
%4 = load i64, i64* @who_am_i.i_am
%5 = icmp ugt i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = load i64, i64* @who_am_i.i_am
store i64 %7, i64* %1
br label %30
8: ; preds = %0
%9 = call i8* (...) @getlogin()
store i8* %9, i8** %3
%10 = load i8*, i8** %3
%11 = icmp eq i8* %10, null
br i1 %11, label %18, label %12
12: ; preds = %8
%13 = load i8*, i8** %3
%14 = getelementptr inbounds i8, i8* %13, i64 0
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %12, %8
%19 = call i32 (...) @getuid()
%20 = call %struct.TYPE_2__* @getpwuid(i32 %19)
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i32 0, i32 0
%22 = load i8*, i8** %21
store i8* %22, i8** %3
br label %23
23: ; preds = %18, %12
%24 = load i8*, i8** %3
%25 = call %struct.passwd* @getpwnam(i8* %24)
store %struct.passwd* %25, %struct.passwd** %2
%26 = load %struct.passwd*, %struct.passwd** %2
%27 = getelementptr inbounds %struct.passwd, %struct.passwd* %26, i32 0, i32 0
%28 = load i64, i64* %27
store i64 %28, i64* @who_am_i.i_am
%29 = load i64, i64* @who_am_i.i_am
store i64 %29, i64* %1
br label %30
30: ; preds = %23, %6
%31 = load i64, i64* %1
ret i64 %31
}

LLVM IR (-O2):
%struct.TYPE_2__ = type { i8* }
%struct.passwd = type { i64 }
@who_am_i.i_am = internal unnamed_addr global i64 0
define i64 @who_am_i() {
%1 = load i64, i64* @who_am_i.i_am
%2 = icmp eq i64 %1, 0
br i1 %2, label %3, label %19
3: ; preds = %0
%4 = tail call i8* (...) @getlogin()
%5 = icmp eq i8* %4, null
br i1 %5, label %9, label %6
6: ; preds = %3
%7 = load i8, i8* %4
%8 = icmp eq i8 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %6, %3
%10 = tail call i32 (...) @getuid()
%11 = tail call %struct.TYPE_2__* @getpwuid(i32 %10)
%12 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %11, i64 0, i32 0
%13 = load i8*, i8** %12
br label %14
14: ; preds = %9, %6
%15 = phi i8* [ %13, %9 ], [ %4, %6 ]
%16 = tail call %struct.passwd* @getpwnam(i8* %15)
%17 = getelementptr inbounds %struct.passwd, %struct.passwd* %16, i64 0, i32 0
%18 = load i64, i64* %17
store i64 %18, i64* @who_am_i.i_am
br label %19
19: ; preds = %0, %14
%20 = phi i64 [ %18, %14 ], [ %1, %0 ]
ret i64 %20
}