#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 {
  int /*<<< orphan*/ pw_name;
};

/* Variables and functions */
char const *bprintf(char *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *);
int geteuid();
struct passwd *getpwuid(int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
const char *username(void) {
  struct passwd *pw = getpwuid(geteuid());
  if (pw == NULL) {
    fprintf(stderr, "getpwuid '%d': %s\n", geteuid(), strerror(errno));
    return NULL;
  }
  return bprintf("%s", pw->pw_name);
}


x86 assembly (clang, -O0):
<username>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1050 <geteuid@plt>
mov    %eax,%edi
call   1030 <getpwuid@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11bb <username+0x6b>
mov    0x2e64(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1050 <geteuid@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%edx
mov    %rax,%rcx
lea    0xe57(%rip),%rsi
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11d1 <username+0x81>
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
lea    0xe4b(%rip),%rdi
call   1040 <bprintf@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<username>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1050 <geteuid@plt>
mov    %eax,%edi
call   1030 <getpwuid@plt>
test   %rax,%rax
je     117a <username+0x2a>
mov    (%rax),%esi
lea    0xea4(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <bprintf@plt>
mov    0x2e5f(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1050 <geteuid@plt>
mov    %eax,%ebp
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe5f(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
mov    %rax,%rcx
call   1060 <fprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.passwd = type { i32 }
@stderr = global i32 0
@.str = [19 x i8] c"getpwuid '%d': %s\0A\00"
@errno = global i32 0
@.str.1 = [3 x i8] c"%s\00"
define i8* @username() {
%1 = alloca i8*
%2 = alloca %struct.passwd*
%3 = call i32 (...) @geteuid()
%4 = call %struct.passwd* @getpwuid(i32 %3)
store %struct.passwd* %4, %struct.passwd** %2
%5 = load %struct.passwd*, %struct.passwd** %2
%6 = icmp eq %struct.passwd* %5, null
br i1 %6, label %7, label %13
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = call i32 (...) @geteuid()
%10 = load i32, i32* @errno
%11 = call i8* @strerror(i32 %10)
%12 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %9, i8* %11)
store i8* null, i8** %1
br label %18
13: ; preds = %0
%14 = load %struct.passwd*, %struct.passwd** %2
%15 = getelementptr inbounds %struct.passwd, %struct.passwd* %14, i32 0, i32 0
%16 = load i32, i32* %15
%17 = call i8* @bprintf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %16)
store i8* %17, i8** %1
br label %18
18: ; preds = %13, %7
%19 = load i8*, i8** %1
ret i8* %19
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
@stderr = global i32 0
@.str = [19 x i8] c"getpwuid '%d': %s\0A\00"
@errno = global i32 0
@.str.1 = [3 x i8] c"%s\00"
define i8* @username() {
%1 = tail call i32 (...) @geteuid()
%2 = tail call %struct.passwd* @getpwuid(i32 %1)
%3 = icmp eq %struct.passwd* %2, null
br i1 %3, label %4, label %10
4: ; preds = %0
%5 = load i32, i32* @stderr
%6 = tail call i32 (...) @geteuid()
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %6, i8* %8)
br label %14
10: ; preds = %0
%11 = getelementptr inbounds %struct.passwd, %struct.passwd* %2, i64 0, i32 0
%12 = load i32, i32* %11
%13 = tail call i8* @bprintf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %12)
br label %14
14: ; preds = %10, %4
%15 = phi i8* [ null, %4 ], [ %13, %10 ]
ret i8* %15
}