#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*/ geteuid();
struct passwd *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/, size_t);
int al_get_username(char *buf, size_t len) {
  struct passwd *pw = getpwuid(geteuid());
  if (pw == NULL) {
    return -1;
  }
  strncpy(buf, pw->pw_name, len);
  buf[len - 1] = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<al_get_username>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x0,%al
call   1050 <geteuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1169 <al_get_username+0x39>
movl   $0xffffffff,-0x4(%rbp)
jmp    1193 <al_get_username+0x63>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<al_get_username>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%rbp
xor    %r14d,%r14d
xor    %eax,%eax
call   1050 <geteuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
test   %rax,%rax
je     1164 <al_get_username+0x34>
mov    (%rax),%esi
mov    %rbp,%rdi
mov    %rbx,%rdx
call   1030 <strncpy@plt>
movb   $0x0,-0x1(%rbx,%rbp,1)
jmp    116a <al_get_username+0x3a>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.passwd = type { i32 }
define i32 @al_get_username(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca %struct.passwd*
store i8* %0, i8** %4
store i64 %1, i64* %5
%7 = call i32 (...) @geteuid()
%8 = call %struct.passwd* @getpwuid(i32 %7)
store %struct.passwd* %8, %struct.passwd** %6
%9 = load %struct.passwd*, %struct.passwd** %6
%10 = icmp eq %struct.passwd* %9, null
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -1, i32* %3
br label %23
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = load %struct.passwd*, %struct.passwd** %6
%15 = getelementptr inbounds %struct.passwd, %struct.passwd* %14, i32 0, i32 0
%16 = load i32, i32* %15
%17 = load i64, i64* %5
%18 = call i32 @strncpy(i8* %13, i32 %16, i64 %17)
%19 = load i8*, i8** %4
%20 = load i64, i64* %5
%21 = sub i64 %20, 1
%22 = getelementptr inbounds i8, i8* %19, i64 %21
store i8 0, i8* %22
store i32 0, i32* %3
br label %23
23: ; preds = %12, %11
%24 = load i32, i32* %3
ret i32 %24
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
define i32 @al_get_username(i8* %0, i64 %1) {
%3 = tail call i32 (...) @geteuid()
%4 = tail call %struct.passwd* @getpwuid(i32 %3)
%5 = icmp eq %struct.passwd* %4, null
br i1 %5, label %12, label %6
6: ; preds = %2
%7 = getelementptr inbounds %struct.passwd, %struct.passwd* %4, i64 0, i32 0
%8 = load i32, i32* %7
%9 = tail call i32 @strncpy(i8* %0, i32 %8, i64 %1)
%10 = add i64 %1, -1
%11 = getelementptr inbounds i8, i8* %0, i64 %10
store i8 0, i8* %11
br label %12
12: ; preds = %2, %6
%13 = phi i32 [ 0, %6 ], [ -1, %2 ]
ret i32 %13
}