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

/* Variables and functions */
struct passwd *getpwuid(int);
int getuid();
char *malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, int);
char *strdup(int /*<<< orphan*/);
char *get_username(void) {
  uid_t uid = getuid();
  struct passwd *pwd = getpwuid(uid);
  char *res;
  if (pwd)
    return strdup(pwd->pw_name);
  res = malloc(20);
  sprintf(res, "%u", uid);
  return res;
}


x86 assembly (clang, -O0):
<get_username>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <getpwuid@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     118d <get_username+0x3d>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11b6 <get_username+0x66>
mov    $0x14,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe57(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-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    %eax,%eax
call   1040 <getuid@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1030 <getpwuid@plt>
test   %rax,%rax
je     1177 <get_username+0x27>
mov    (%rax),%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1070 <strdup@plt>
mov    $0x14,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
lea    0xe75(%rip),%rsi
mov    %rax,%rdi
mov    %ebx,%edx
call   1060 <sprintf@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.passwd = type { i32 }
@.str = [3 x i8] c"%u\00"
define i8* @get_username() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca %struct.passwd*
%4 = alloca i8*
%5 = call i32 (...) @getuid()
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = call %struct.passwd* @getpwuid(i32 %6)
store %struct.passwd* %7, %struct.passwd** %3
%8 = load %struct.passwd*, %struct.passwd** %3
%9 = icmp ne %struct.passwd* %8, null
br i1 %9, label %10, label %15
10: ; preds = %0
%11 = load %struct.passwd*, %struct.passwd** %3
%12 = getelementptr inbounds %struct.passwd, %struct.passwd* %11, i32 0, i32 0
%13 = load i32, i32* %12
%14 = call i8* @strdup(i32 %13)
store i8* %14, i8** %1
br label %21
15: ; preds = %0
%16 = call i8* @malloc(i32 20)
store i8* %16, i8** %4
%17 = load i8*, i8** %4
%18 = load i32, i32* %2
%19 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = load i8*, i8** %4
store i8* %20, i8** %1
br label %21
21: ; preds = %15, %10
%22 = load i8*, i8** %1
ret i8* %22
}

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