#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 {
  char *pw_name;
};

/* Variables and functions */
int ENOMEM;
int ENOSYS;
int /*<<< orphan*/ _SC_GETPW_R_SIZE_MAX;
int /*<<< orphan*/ free(char *);
char *getenv(char *);
int /*<<< orphan*/ geteuid();
int getpwuid_r(int /*<<< orphan*/, struct passwd *, char *, long,
               struct passwd **);
char *malloc(long);
long sysconf(int /*<<< orphan*/);
int zsnprintf(char *, size_t, char *, char *);
int get_current_username(char *out, size_t out_len) {
  int ret;
  char *buf = NULL;
  struct passwd pwd, *p;
  long buf_len;
  uid_t euid;
  euid = geteuid();
  buf_len = sysconf(_SC_GETPW_R_SIZE_MAX);
  if (buf_len == -1)
    buf_len = 1048576;
  buf = malloc(buf_len);
  if (!buf) {
    ret = -ENOMEM;
    goto done;
  }
  ret = getpwuid_r(euid, &pwd, buf, buf_len, &p);
  if (ret) {
    char *user_env;
    user_env = getenv("USER");
    if (!user_env) {
      ret = -ENOSYS;
      goto done;
    }
    ret = zsnprintf(out, out_len, "%s", user_env);
  } else {
    ret = zsnprintf(out, out_len, "%s", pwd.pw_name);
  }
done:
  free(buf);
  return ret;
}


x86 assembly (clang, -O0):
<get_current_username>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    $0x0,%al
call   1060 <geteuid@plt>
mov    %eax,-0x3c(%rbp)
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0xffffffffffffffff,-0x38(%rbp)
jne    11b7 <get_current_username+0x47>
movq   $0x100000,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e2 <get_current_username+0x72>
xor    %eax,%eax
mov    0x2df8(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x14(%rbp)
jmp    1275 <get_current_username+0x105>
mov    -0x3c(%rbp),%edi
mov    -0x20(%rbp),%rdx
mov    -0x38(%rbp),%rcx
lea    -0x28(%rbp),%rsi
lea    -0x30(%rbp),%r8
call   1050 <getpwuid_r@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1255 <get_current_username+0xe5>
lea    0xdf2(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1235 <get_current_username+0xc5>
xor    %eax,%eax
mov    0x2dbd(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x14(%rbp)
jmp    1275 <get_current_username+0x105>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x48(%rbp),%rcx
lea    0xdbd(%rip),%rdx
call   1080 <zsnprintf@plt>
mov    %eax,-0x14(%rbp)
jmp    1270 <get_current_username+0x100>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rcx
lea    0xd9d(%rip),%rdx
call   1080 <zsnprintf@plt>
mov    %eax,-0x14(%rbp)
jmp    1275 <get_current_username+0x105>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
mov    -0x14(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_current_username>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <geteuid@plt>
mov    %eax,%r12d
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
cmp    $0xffffffffffffffff,%rax
mov    $0x100000,%ebx
cmovne %rax,%rbx
mov    %rbx,%rdi
call   1070 <malloc@plt>
mov    %rax,%r13
test   %rax,%rax
je     11fa <get_current_username+0x8a>
lea    0x8(%rsp),%rsi
lea    0x10(%rsp),%r8
mov    %r12d,%edi
mov    %r13,%rdx
mov    %rbx,%rcx
call   1050 <getpwuid_r@plt>
test   %eax,%eax
je     1203 <get_current_username+0x93>
lea    0xe22(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     121e <get_current_username+0xae>
lea    0xe16(%rip),%rdx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rax,%rcx
jmp    1215 <get_current_username+0xa5>
mov    0x2dcf(%rip),%rax
jmp    1227 <get_current_username+0xb7>
mov    0x8(%rsp),%rcx
lea    0xdf6(%rip),%rdx
mov    %r15,%rdi
mov    %r14,%rsi
call   1080 <zsnprintf@plt>
mov    %eax,%ebp
jmp    1229 <get_current_username+0xb9>
mov    0x2dc3(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    %r13,%rdi
call   1040 <free@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.passwd = type { i8* }
@_SC_GETPW_R_SIZE_MAX = global i32 0
@ENOMEM = global i32 0
@.str = [5 x i8] c"USER\00"
@ENOSYS = global i32 0
@.str.1 = [3 x i8] c"%s\00"
define i32 @get_current_username(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca %struct.passwd
%8 = alloca %struct.passwd*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
store i8* null, i8** %6
%12 = call i32 (...) @geteuid()
store i32 %12, i32* %10
%13 = load i32, i32* @_SC_GETPW_R_SIZE_MAX
%14 = call i64 @sysconf(i32 %13)
store i64 %14, i64* %9
%15 = load i64, i64* %9
%16 = icmp eq i64 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %2
store i64 1048576, i64* %9
br label %18
18: ; preds = %17, %2
%19 = load i64, i64* %9
%20 = call i8* @malloc(i64 %19)
store i8* %20, i8** %6
%21 = load i8*, i8** %6
%22 = icmp ne i8* %21, null
br i1 %22, label %26, label %23
23: ; preds = %18
%24 = load i32, i32* @ENOMEM
%25 = sub nsw i32 0, %24
store i32 %25, i32* %5
br label %52
26: ; preds = %18
%27 = load i32, i32* %10
%28 = load i8*, i8** %6
%29 = load i64, i64* %9
%30 = call i32 @getpwuid_r(i32 %27, %struct.passwd* %7, i8* %28, i64 %29, %struct.passwd** %8)
store i32 %30, i32* %5
%31 = load i32, i32* %5
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %45
33: ; preds = %26
%34 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %34, i8** %11
%35 = load i8*, i8** %11
%36 = icmp ne i8* %35, null
br i1 %36, label %40, label %37
37: ; preds = %33
%38 = load i32, i32* @ENOSYS
%39 = sub nsw i32 0, %38
store i32 %39, i32* %5
br label %52
40: ; preds = %33
%41 = load i8*, i8** %3
%42 = load i64, i64* %4
%43 = load i8*, i8** %11
%44 = call i32 @zsnprintf(i8* %41, i64 %42, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %43)
store i32 %44, i32* %5
br label %51
45: ; preds = %26
%46 = load i8*, i8** %3
%47 = load i64, i64* %4
%48 = getelementptr inbounds %struct.passwd, %struct.passwd* %7, i32 0, i32 0
%49 = load i8*, i8** %48
%50 = call i32 @zsnprintf(i8* %46, i64 %47, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %49)
store i32 %50, i32* %5
br label %51
51: ; preds = %45, %40
br label %52
52: ; preds = %51, %37, %23
%53 = load i8*, i8** %6
%54 = call i32 @free(i8* %53)
%55 = load i32, i32* %5
ret i32 %55
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@_SC_GETPW_R_SIZE_MAX = global i32 0
@ENOMEM = global i32 0
@.str = [5 x i8] c"USER\00"
@ENOSYS = global i32 0
@.str.1 = [3 x i8] c"%s\00"
define i32 @get_current_username(i8* %0, i64 %1) {
%3 = alloca %struct.passwd
%4 = alloca %struct.passwd*
%5 = bitcast %struct.passwd* %3 to i8*
%6 = bitcast %struct.passwd** %4 to i8*
%7 = tail call i32 (...) @geteuid()
%8 = load i32, i32* @_SC_GETPW_R_SIZE_MAX
%9 = tail call i64 @sysconf(i32 %8)
%10 = icmp eq i64 %9, -1
%11 = select i1 %10, i64 1048576, i64 %9
%12 = tail call i8* @malloc(i64 %11)
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i32, i32* @ENOMEM
%16 = sub nsw i32 0, %15
br label %32
17: ; preds = %2
%18 = call i32 @getpwuid_r(i32 %7, %struct.passwd* %3, i8* %12, i64 %11, %struct.passwd** %4)
%19 = icmp eq i32 %18, 0
br i1 %19, label %28, label %20
20: ; preds = %17
%21 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load i32, i32* @ENOSYS
%25 = sub nsw i32 0, %24
br label %32
26: ; preds = %20
%27 = call i32 @zsnprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %21)
br label %32
28: ; preds = %17
%29 = getelementptr inbounds %struct.passwd, %struct.passwd* %3, i64 0, i32 0
%30 = load i8*, i8** %29
%31 = call i32 @zsnprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %30)
br label %32
32: ; preds = %26, %23, %28, %14
%33 = phi i32 [ %31, %28 ], [ %16, %14 ], [ %27, %26 ], [ %25, %23 ]
%34 = call i32 @free(i8* %12)
ret i32 %33
}