#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*/ Log(char *, int, int);
int /*<<< orphan*/ LogErrno(char *);
int /*<<< orphan*/ _SC_GETPW_R_SIZE_MAX;
int /*<<< orphan*/ explicit_bzero(char *, long);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ getpwuid_r(int /*<<< orphan*/, struct passwd *, char *,
                              size_t, struct passwd **);
int /*<<< orphan*/ getuid();
char *malloc(size_t);
size_t strlen(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/, size_t);
long sysconf(int /*<<< orphan*/);
int GetUserName(char *username_buf, size_t username_buflen) {
  struct passwd *pwd = NULL;
  struct passwd pwd_storage;
  char *pwd_buf;
  long pwd_bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
  if (pwd_bufsize < 0) {
    pwd_bufsize = 1 << 20;
  }
  pwd_buf = malloc((size_t)pwd_bufsize);
  if (!pwd_buf) {
    LogErrno("malloc(pwd_bufsize)");
    return 0;
  }
  if (0 < 0) {
    LogErrno("mlock");
  }
  getpwuid_r(getuid(), &pwd_storage, pwd_buf, (size_t)pwd_bufsize, &pwd);
  if (!pwd) {
    LogErrno("getpwuid_r");
    free(pwd_buf);
    return 0;
  }
  if (strlen(pwd->pw_name) >= username_buflen) {
    Log("Username too long: got %d, want < %d", (int)strlen(pwd->pw_name),
        (int)username_buflen);
    free(pwd_buf);
    return 0;
  }
  strncpy(username_buf, pwd->pw_name, username_buflen);
  username_buf[username_buflen - 1] = 0;
  explicit_bzero(pwd_buf, pwd_bufsize);
  free(pwd_buf);
  return 1;
}


x86 assembly (clang, -O0):
<GetUserName>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
call   10b0 <sysconf@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jge    11dd <GetUserName+0x3d>
movq   $0x100000,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    120d <GetUserName+0x6d>
lea    0xe04(%rip),%rdi
call   1080 <LogErrno@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e0 <GetUserName+0x140>
mov    $0x0,%al
call   1070 <getuid@plt>
mov    %eax,%edi
mov    -0x30(%rbp),%rdx
mov    -0x38(%rbp),%rcx
lea    -0x28(%rbp),%rsi
lea    -0x20(%rbp),%r8
call   1060 <getpwuid_r@plt>
cmpq   $0x0,-0x20(%rbp)
jne    1257 <GetUserName+0xb7>
lea    0xdd7(%rip),%rdi
call   1080 <LogErrno@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e0 <GetUserName+0x140>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1050 <strlen@plt>
cmp    -0x18(%rbp),%rax
jb     12a0 <GetUserName+0x100>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1050 <strlen@plt>
mov    %eax,%esi
mov    -0x18(%rbp),%rax
mov    %eax,%edx
lea    0xd99(%rip),%rdi
call   10a0 <Log@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e0 <GetUserName+0x140>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rdx
call   1040 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   10c0 <explicit_bzero@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GetUserName>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
movq   $0x0,(%rsp)
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
call   10b0 <sysconf@plt>
test   %rax,%rax
mov    $0x100000,%ebx
cmovns %rax,%rbx
mov    %rbx,%rdi
call   1090 <malloc@plt>
test   %rax,%rax
je     1248 <GetUserName+0xa8>
mov    %rax,%r12
xor    %ebp,%ebp
xor    %eax,%eax
call   1070 <getuid@plt>
lea    0x8(%rsp),%rsi
mov    %rsp,%r8
mov    %eax,%edi
mov    %r12,%rdx
mov    %rbx,%rcx
call   1060 <getpwuid_r@plt>
mov    (%rsp),%rax
test   %rax,%rax
je     1256 <GetUserName+0xb6>
mov    (%rax),%edi
call   1050 <strlen@plt>
mov    (%rsp),%rcx
mov    (%rcx),%esi
cmp    %r14,%rax
jae    126c <GetUserName+0xcc>
mov    %r15,%rdi
mov    %r14,%rdx
call   1040 <strncpy@plt>
movb   $0x0,-0x1(%r14,%r15,1)
mov    %r12,%rdi
mov    %rbx,%rsi
call   10c0 <explicit_bzero@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    $0x1,%ebp
jmp    128e <GetUserName+0xee>
lea    0xdb1(%rip),%rdi
call   1080 <LogErrno@plt>
jmp    128c <GetUserName+0xec>
lea    0xdb7(%rip),%rdi
call   1080 <LogErrno@plt>
mov    %r12,%rdi
call   1030 <free@plt>
jmp    128e <GetUserName+0xee>
mov    %esi,%edi
call   1050 <strlen@plt>
lea    0xda5(%rip),%rdi
mov    %eax,%esi
mov    %r14d,%edx
call   10a0 <Log@plt>
mov    %r12,%rdi
call   1030 <free@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.passwd = type { i32 }
@_SC_GETPW_R_SIZE_MAX = global i32 0
@.str = [20 x i8] c"malloc(pwd_bufsize)\00"
@.str.1 = [11 x i8] c"getpwuid_r\00"
@.str.2 = [37 x i8] c"Username too long: got %d, want < %d\00"
define i32 @GetUserName(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca %struct.passwd*
%7 = alloca %struct.passwd
%8 = alloca i8*
%9 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
store %struct.passwd* null, %struct.passwd** %6
%10 = load i32, i32* @_SC_GETPW_R_SIZE_MAX
%11 = call i64 @sysconf(i32 %10)
store i64 %11, i64* %9
%12 = load i64, i64* %9
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %2
store i64 1048576, i64* %9
br label %15
15: ; preds = %14, %2
%16 = load i64, i64* %9
%17 = call i8* @malloc(i64 %16)
store i8* %17, i8** %8
%18 = load i8*, i8** %8
%19 = icmp ne i8* %18, null
br i1 %19, label %22, label %20
20: ; preds = %15
%21 = call i32 @LogErrno(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %67
22: ; preds = %15
%23 = call i32 (...) @getuid()
%24 = load i8*, i8** %8
%25 = load i64, i64* %9
%26 = call i32 @getpwuid_r(i32 %23, %struct.passwd* %7, i8* %24, i64 %25, %struct.passwd** %6)
%27 = load %struct.passwd*, %struct.passwd** %6
%28 = icmp ne %struct.passwd* %27, null
br i1 %28, label %33, label %29
29: ; preds = %22
%30 = call i32 @LogErrno(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%31 = load i8*, i8** %8
%32 = call i32 @free(i8* %31)
store i32 0, i32* %3
br label %67
33: ; preds = %22
%34 = load %struct.passwd*, %struct.passwd** %6
%35 = getelementptr inbounds %struct.passwd, %struct.passwd* %34, i32 0, i32 0
%36 = load i32, i32* %35
%37 = call i64 @strlen(i32 %36)
%38 = load i64, i64* %5
%39 = icmp uge i64 %37, %38
br i1 %39, label %40, label %51
40: ; preds = %33
%41 = load %struct.passwd*, %struct.passwd** %6
%42 = getelementptr inbounds %struct.passwd, %struct.passwd* %41, i32 0, i32 0
%43 = load i32, i32* %42
%44 = call i64 @strlen(i32 %43)
%45 = trunc i64 %44 to i32
%46 = load i64, i64* %5
%47 = trunc i64 %46 to i32
%48 = call i32 @Log(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %45, i32 %47)
%49 = load i8*, i8** %8
%50 = call i32 @free(i8* %49)
store i32 0, i32* %3
br label %67
51: ; preds = %33
%52 = load i8*, i8** %4
%53 = load %struct.passwd*, %struct.passwd** %6
%54 = getelementptr inbounds %struct.passwd, %struct.passwd* %53, i32 0, i32 0
%55 = load i32, i32* %54
%56 = load i64, i64* %5
%57 = call i32 @strncpy(i8* %52, i32 %55, i64 %56)
%58 = load i8*, i8** %4
%59 = load i64, i64* %5
%60 = sub i64 %59, 1
%61 = getelementptr inbounds i8, i8* %58, i64 %60
store i8 0, i8* %61
%62 = load i8*, i8** %8
%63 = load i64, i64* %9
%64 = call i32 @explicit_bzero(i8* %62, i64 %63)
%65 = load i8*, i8** %8
%66 = call i32 @free(i8* %65)
store i32 1, i32* %3
br label %67
67: ; preds = %51, %40, %29, %20
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
@_SC_GETPW_R_SIZE_MAX = global i32 0
@.str = [20 x i8] c"malloc(pwd_bufsize)\00"
@.str.1 = [11 x i8] c"getpwuid_r\00"
@.str.2 = [37 x i8] c"Username too long: got %d, want < %d\00"
define i32 @GetUserName(i8* %0, i64 %1) {
%3 = alloca %struct.passwd*
%4 = alloca %struct.passwd
%5 = bitcast %struct.passwd** %3 to i8*
store %struct.passwd* null, %struct.passwd** %3
%6 = bitcast %struct.passwd* %4 to i8*
%7 = load i32, i32* @_SC_GETPW_R_SIZE_MAX
%8 = tail call i64 @sysconf(i32 %7)
%9 = icmp slt i64 %8, 0
%10 = select i1 %9, i64 1048576, i64 %8
%11 = tail call i8* @malloc(i64 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = tail call i32 @LogErrno(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %43
15: ; preds = %2
%16 = tail call i32 (...) @getuid()
%17 = call i32 @getpwuid_r(i32 %16, %struct.passwd* %4, i8* %11, i64 %10, %struct.passwd** %3)
%18 = load %struct.passwd*, %struct.passwd** %3
%19 = icmp eq %struct.passwd* %18, null
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = call i32 @LogErrno(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @free(i8* %11)
br label %43
23: ; preds = %15
%24 = getelementptr inbounds %struct.passwd, %struct.passwd* %18, i64 0, i32 0
%25 = load i32, i32* %24
%26 = call i64 @strlen(i32 %25)
%27 = icmp ult i64 %26, %1
%28 = load %struct.passwd*, %struct.passwd** %3
%29 = getelementptr inbounds %struct.passwd, %struct.passwd* %28, i64 0, i32 0
%30 = load i32, i32* %29
br i1 %27, label %37, label %31
31: ; preds = %23
%32 = call i64 @strlen(i32 %30)
%33 = trunc i64 %32 to i32
%34 = trunc i64 %1 to i32
%35 = call i32 @Log(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %33, i32 %34)
%36 = call i32 @free(i8* %11)
br label %43
37: ; preds = %23
%38 = call i32 @strncpy(i8* %0, i32 %30, i64 %1)
%39 = add i64 %1, -1
%40 = getelementptr inbounds i8, i8* %0, i64 %39
store i8 0, i8* %40
%41 = call i32 @explicit_bzero(i8* %11, i64 %10)
%42 = call i32 @free(i8* %11)
br label %43
43: ; preds = %37, %31, %20, %13
%44 = phi i32 [ 0, %31 ], [ 1, %37 ], [ 0, %20 ], [ 0, %13 ]
ret i32 %44
}