#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 */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, unsigned int);
struct passwd *getpwuid(unsigned int);
int /*<<< orphan*/ stderr;
char *strdup(int /*<<< orphan*/);
char *get_current_username(unsigned int uid) {
  struct passwd *passwd_entry;
  if ((passwd_entry = getpwuid(uid)) == NULL) {
    fprintf(stderr, "Could not look up user record for %d\n", uid);
    return NULL;
  }
  return (strdup(passwd_entry->pw_name));
}


x86 assembly (clang, -O0):
<get_current_username>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <getpwuid@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1176 <get_current_username+0x46>
mov    0x2e88(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
lea    0xe9c(%rip),%rsi
call   1040 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1185 <get_current_username+0x55>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1050 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_current_username>:
push   %rbx
mov    %edi,%ebx
call   1030 <getpwuid@plt>
test   %rax,%rax
je     1145 <get_current_username+0x15>
mov    (%rax),%edi
pop    %rbx
jmp    1050 <strdup@plt>
mov    0x2e94(%rip),%rax
mov    (%rax),%edi
lea    0xeab(%rip),%rsi
mov    %ebx,%edx
call   1040 <fprintf@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.passwd = type { i32 }
@stderr = global i32 0
@.str = [38 x i8] c"Could not look up user record for %d\0A\00"
define i8* @get_current_username(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.passwd*
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = call %struct.passwd* @getpwuid(i32 %5)
store %struct.passwd* %6, %struct.passwd** %4
%7 = icmp eq %struct.passwd* %6, null
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = load i32, i32* %3
%11 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %10)
store i8* null, i8** %2
br label %17
12: ; preds = %1
%13 = load %struct.passwd*, %struct.passwd** %4
%14 = getelementptr inbounds %struct.passwd, %struct.passwd* %13, i32 0, i32 0
%15 = load i32, i32* %14
%16 = call i8* @strdup(i32 %15)
store i8* %16, i8** %2
br label %17
17: ; preds = %12, %8
%18 = load i8*, i8** %2
ret i8* %18
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
@stderr = global i32 0
@.str = [38 x i8] c"Could not look up user record for %d\0A\00"
define i8* @get_current_username(i32 %0) {
%2 = tail call %struct.passwd* @getpwuid(i32 %0)
%3 = icmp eq %struct.passwd* %2, null
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %11
7: ; preds = %1
%8 = getelementptr inbounds %struct.passwd, %struct.passwd* %2, i64 0, i32 0
%9 = load i32, i32* %8
%10 = tail call i8* @strdup(i32 %9)
br label %11
11: ; preds = %7, %4
%12 = phi i8* [ null, %4 ], [ %10, %7 ]
ret i8* %12
}