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

/* Variables and functions */
char *calloc(int, int);
int /*<<< orphan*/ geteuid();
struct passwd *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
char *get_username() {
  struct passwd *pw;
  uid_t uid;
  uid = geteuid();
  pw = getpwuid(uid);
  if (pw) {
    char *out = calloc(sizeof(pw->pw_name), 1);
    strcpy(out, pw->pw_name);
    return out;
  } else {
    return NULL;
  }
}


x86 assembly (clang, -O0):
<get_username>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1050 <geteuid@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <getpwuid@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1198 <get_username+0x58>
mov    $0x4,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11a0 <get_username+0x60>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_username>:
push   %r14
push   %rbx
push   %rax
xor    %r14d,%r14d
xor    %eax,%eax
call   1050 <geteuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
test   %rax,%rax
je     1179 <get_username+0x39>
mov    %rax,%rbx
mov    $0x4,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%r14
mov    (%rbx),%esi
mov    %rax,%rdi
call   1030 <strcpy@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.passwd = type { i32 }
define i8* @get_username() {
%1 = alloca i8*
%2 = alloca %struct.passwd*
%3 = alloca i32
%4 = alloca i8*
%5 = call i32 (...) @geteuid()
store i32 %5, i32* %3
%6 = load i32, i32* %3
%7 = call %struct.passwd* @getpwuid(i32 %6)
store %struct.passwd* %7, %struct.passwd** %2
%8 = load %struct.passwd*, %struct.passwd** %2
%9 = icmp ne %struct.passwd* %8, null
br i1 %9, label %10, label %18
10: ; preds = %0
%11 = call i8* @calloc(i32 4, i32 1)
store i8* %11, i8** %4
%12 = load i8*, i8** %4
%13 = load %struct.passwd*, %struct.passwd** %2
%14 = getelementptr inbounds %struct.passwd, %struct.passwd* %13, i32 0, i32 0
%15 = load i32, i32* %14
%16 = call i32 @strcpy(i8* %12, i32 %15)
%17 = load i8*, i8** %4
store i8* %17, i8** %1
br label %19
18: ; preds = %0
store i8* null, i8** %1
br label %19
19: ; preds = %18, %10
%20 = load i8*, i8** %1
ret i8* %20
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
define i8* @get_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 %9, label %4
4: ; preds = %0
%5 = tail call dereferenceable_or_null(4) i8* @calloc(i32 4, i32 1)
%6 = getelementptr inbounds %struct.passwd, %struct.passwd* %2, i64 0, i32 0
%7 = load i32, i32* %6
%8 = tail call i32 @strcpy(i8* %5, i32 %7)
br label %9
9: ; preds = %0, %4
%10 = phi i8* [ %5, %4 ], [ null, %0 ]
ret i8* %10
}