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

/* Variables and functions */
scalar_t__ cuserid(int /*<<< orphan*/ *);
int geteuid();
scalar_t__ getlogin();
struct passwd *getpwnam(char *);
int getuid();
int /*<<< orphan*/ printf(char *, int, int);
unsigned long who_am_i() {
  struct passwd *pw;
  static unsigned long i_am = 0L;
  char *name;
  if (i_am > 0L)
    return i_am;
  printf("\ngetuid = %d, geteuid = %d.\n", getuid(), geteuid());
  name = (char *)getlogin();
  if (name == NULL || name[0] == '\0')
    name = (char *)cuserid(NULL);
  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,0x2ee8(%rip)
jbe    1186 <who_am_i+0x26>
mov    0x2edb(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1201 <who_am_i+0xa1>
mov    $0x0,%al
call   1030 <getuid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1050 <geteuid@plt>
mov    -0x1c(%rbp),%esi
mov    %eax,%edx
lea    0xe5d(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1070 <getlogin@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11ce <who_am_i+0x6e>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11db <who_am_i+0x7b>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <cuserid@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <getpwnam@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,0x2e62(%rip)
mov    0x2e5b(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<who_am_i>:
push   %rbx
mov    0x2ef0(%rip),%rax
test   %rax,%rax
je     116f <who_am_i+0xf>
pop    %rbx
ret
xor    %eax,%eax
call   1030 <getuid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <geteuid@plt>
lea    0xe7a(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <printf@plt>
xor    %eax,%eax
call   1070 <getlogin@plt>
test   %rax,%rax
je     11a0 <who_am_i+0x40>
cmpb   $0x0,(%rax)
jne    11a7 <who_am_i+0x47>
xor    %edi,%edi
call   1080 <cuserid@plt>
mov    %rax,%rdi
call   1060 <getpwnam@plt>
mov    (%rax),%rax
mov    %rax,0x2e9f(%rip)
pop    %rbx
ret

LLVM IR (-O0):
%struct.passwd = type { i64 }
@who_am_i.i_am = internal global i64 0
@.str = [29 x i8] c"\0Agetuid = %d, geteuid = %d.\0A\00"
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 %32
8: ; preds = %0
%9 = call i32 (...) @getuid()
%10 = call i32 (...) @geteuid()
%11 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %9, i32 %10)
%12 = call i64 (...) @getlogin()
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %3
%14 = load i8*, i8** %3
%15 = icmp eq i8* %14, null
br i1 %15, label %22, label %16
16: ; preds = %8
%17 = load i8*, i8** %3
%18 = getelementptr inbounds i8, i8* %17, i64 0
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %16, %8
%23 = call i64 @cuserid(i32* null)
%24 = inttoptr i64 %23 to i8*
store i8* %24, i8** %3
br label %25
25: ; preds = %22, %16
%26 = load i8*, i8** %3
%27 = call %struct.passwd* @getpwnam(i8* %26)
store %struct.passwd* %27, %struct.passwd** %2
%28 = load %struct.passwd*, %struct.passwd** %2
%29 = getelementptr inbounds %struct.passwd, %struct.passwd* %28, i32 0, i32 0
%30 = load i64, i64* %29
store i64 %30, i64* @who_am_i.i_am
%31 = load i64, i64* @who_am_i.i_am
store i64 %31, i64* %1
br label %32
32: ; preds = %25, %6
%33 = load i64, i64* %1
ret i64 %33
}

LLVM IR (-O2):
%struct.passwd = type { i64 }
@who_am_i.i_am = internal unnamed_addr global i64 0
@.str = [29 x i8] c"\0Agetuid = %d, geteuid = %d.\0A\00"
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 %21
3: ; preds = %0
%4 = tail call i32 (...) @getuid()
%5 = tail call i32 (...) @geteuid()
%6 = tail call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %4, i32 %5)
%7 = tail call i64 (...) @getlogin()
%8 = inttoptr i64 %7 to i8*
%9 = icmp eq i64 %7, 0
br i1 %9, label %13, label %10
10: ; preds = %3
%11 = load i8, i8* %8
%12 = icmp eq i8 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10, %3
%14 = tail call i64 @cuserid(i32* null)
%15 = inttoptr i64 %14 to i8*
br label %16
16: ; preds = %13, %10
%17 = phi i8* [ %15, %13 ], [ %8, %10 ]
%18 = tail call %struct.passwd* @getpwnam(i8* %17)
%19 = getelementptr inbounds %struct.passwd, %struct.passwd* %18, i64 0, i32 0
%20 = load i64, i64* %19
store i64 %20, i64* @who_am_i.i_am
br label %21
21: ; preds = %0, %16
%22 = phi i64 [ %20, %16 ], [ %1, %0 ]
ret i64 %22
}