#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 */
char *getenv(char *);
struct passwd *getpwuid(scalar_t__);
scalar_t__ getuid();
char *strdup(char *);
char *get_username(void) {
  uid_t uid = getuid();
  char *username = NULL;
  if (uid == 0)
    username = getenv("USER");
  if (username == NULL) {
    struct passwd *pw;
    pw = getpwuid(uid);
    if (pw == NULL)
      return NULL;
    username = pw->pw_name;
  }
  return strdup(username);
}


x86 assembly (clang, -O0):
<get_username>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1050 <getuid@plt>
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1176 <get_username+0x36>
lea    0xe93(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b1 <get_username+0x71>
mov    -0x10(%rbp),%rdi
call   1040 <getpwuid@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a6 <get_username+0x66>
movq   $0x0,-0x8(%rbp)
jmp    11be <get_username+0x7e>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_username>:
push   %rbx
xor    %eax,%eax
call   1050 <getuid@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1162 <get_username+0x22>
mov    %rbx,%rdi
call   1040 <getpwuid@plt>
test   %rax,%rax
je     117c <get_username+0x3c>
mov    (%rax),%rdi
jmp    1176 <get_username+0x36>
lea    0xe97(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
test   %rax,%rax
je     1150 <get_username+0x10>
pop    %rbx
jmp    1060 <strdup@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.passwd = type { i8* }
@.str = [5 x i8] c"USER\00"
define i8* @get_username() {
%1 = alloca i8*
%2 = alloca i64
%3 = alloca i8*
%4 = alloca %struct.passwd*
%5 = call i64 (...) @getuid()
store i64 %5, i64* %2
store i8* null, i8** %3
%6 = load i64, i64* %2
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %9, i8** %3
br label %10
10: ; preds = %8, %0
%11 = load i8*, i8** %3
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %23
13: ; preds = %10
%14 = load i64, i64* %2
%15 = call %struct.passwd* @getpwuid(i64 %14)
store %struct.passwd* %15, %struct.passwd** %4
%16 = load %struct.passwd*, %struct.passwd** %4
%17 = icmp eq %struct.passwd* %16, null
br i1 %17, label %18, label %19
18: ; preds = %13
store i8* null, i8** %1
br label %26
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
store i8* %22, i8** %3
br label %23
23: ; preds = %19, %10
%24 = load i8*, i8** %3
%25 = call i8* @strdup(i8* %24)
store i8* %25, i8** %1
br label %26
26: ; preds = %23, %18
%27 = load i8*, i8** %1
ret i8* %27
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@.str = [5 x i8] c"USER\00"
define noalias i8* @get_username() {
%1 = tail call i64 (...) @getuid()
%2 = icmp eq i64 %1, 0
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %12
6: ; preds = %0, %3
%7 = tail call %struct.passwd* @getpwuid(i64 %1)
%8 = icmp eq %struct.passwd* %7, null
br i1 %8, label %15, label %9
9: ; preds = %6
%10 = getelementptr inbounds %struct.passwd, %struct.passwd* %7, i64 0, i32 0
%11 = load i8*, i8** %10
br label %12
12: ; preds = %9, %3
%13 = phi i8* [ %11, %9 ], [ %4, %3 ]
%14 = tail call i8* @strdup(i8* %13)
br label %15
15: ; preds = %6, %12
%16 = phi i8* [ %14, %12 ], [ null, %6 ]
ret i8* %16
}