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

/* Variables and functions */
char *getenv(char *);
char *getname(scalar_t__);
scalar_t__ getuid();
int /*<<< orphan*/ printf(char *, int);
char *username(void) {
  char *np;
  uid_t uid;
  if ((np = getenv("USER")) != NULL)
    return np;
  if ((np = getname(uid = getuid())) != NULL)
    return np;
  printf(("Cannot associate a name with uid %d\n"), (int)uid);
  return NULL;
}


x86 assembly (clang, -O0):
<username>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xeb1(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     116f <username+0x2f>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b7 <username+0x77>
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %rax,%rdi
mov    %rdi,-0x18(%rbp)
call   1060 <getname@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     119d <username+0x5d>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b7 <username+0x77>
mov    -0x18(%rbp),%rax
mov    %eax,%esi
lea    0xe5b(%rip),%rdi
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<username>:
push   %rbx
lea    0xeb8(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1154 <username+0x14>
pop    %rbx
ret
xor    %eax,%eax
call   1040 <getuid@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <getname@plt>
test   %rax,%rax
jne    1152 <username+0x12>
lea    0xe93(%rip),%rdi
mov    %ebx,%esi
call   1050 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [5 x i8] c"USER\00"
@.str.1 = [37 x i8] c"Cannot associate a name with uid %d\0A\00"
define i8* @username() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i64
%4 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %4, i8** %2
%5 = icmp ne i8* %4, null
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = load i8*, i8** %2
store i8* %7, i8** %1
br label %18
8: ; preds = %0
%9 = call i64 (...) @getuid()
store i64 %9, i64* %3
%10 = call i8* @getname(i64 %9)
store i8* %10, i8** %2
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = load i8*, i8** %2
store i8* %13, i8** %1
br label %18
14: ; preds = %8
%15 = load i64, i64* %3
%16 = trunc i64 %15 to i32
%17 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %16)
store i8* null, i8** %1
br label %18
18: ; preds = %14, %12, %6
%19 = load i8*, i8** %1
ret i8* %19
}

LLVM IR (-O2):
@.str = [5 x i8] c"USER\00"
@.str.1 = [37 x i8] c"Cannot associate a name with uid %d\0A\00"
define i8* @username() {
%1 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i8* %1, null
br i1 %2, label %3, label %10
3: ; preds = %0
%4 = tail call i64 (...) @getuid()
%5 = tail call i8* @getname(i64 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = trunc i64 %4 to i32
%9 = tail call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %8)
br label %10
10: ; preds = %3, %0, %7
%11 = phi i8* [ null, %7 ], [ %1, %0 ], [ %5, %3 ]
ret i8* %11
}