#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 {
  int /*<<< orphan*/ pw_name;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int ENOENT;
int getpwuid_r(int /*<<< orphan*/, struct passwd *, char *, int,
               struct passwd **);
int zsnprintf(char *, size_t, char *, int /*<<< orphan*/);
int get_user_name(uid_t uid, char *username, size_t username_len) {
  char buf[8192];
  struct passwd pwd, *res;
  int ret;
  ret = getpwuid_r(uid, &pwd, buf, sizeof(buf), &res);
  if (res == NULL) {
    if (!ret)
      return -ENOENT;
    return ret;
  }
  return zsnprintf(username, username_len, "%s", pwd.pw_name);
}


x86 assembly (clang, -O0):
<get_user_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x2020(%rbp),%rdx
lea    -0x2028(%rbp),%rsi
mov    $0x2000,%ecx
lea    -0x2030(%rbp),%r8
call   1030 <getpwuid_r@plt>
mov    %eax,-0x2034(%rbp)
cmpq   $0x0,-0x2030(%rbp)
jne    119a <get_user_name+0x7a>
cmpl   $0x0,-0x2034(%rbp)
jne    118c <get_user_name+0x6c>
xor    %eax,%eax
mov    0x2e56(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11b7 <get_user_name+0x97>
mov    -0x2034(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b7 <get_user_name+0x97>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x2028(%rbp),%ecx
lea    0xe51(%rip),%rdx
call   1040 <zsnprintf@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_user_name>:
push   %r14
push   %rbx
sub    $0x2018,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rsp,%rsi
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%r8
mov    $0x2000,%ecx
call   1030 <getpwuid_r@plt>
cmpq   $0x0,0x8(%rsp)
je     1166 <get_user_name+0x46>
mov    (%rsp),%ecx
lea    0xea7(%rip),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <zsnprintf@plt>
jmp    1175 <get_user_name+0x55>
test   %eax,%eax
jne    1175 <get_user_name+0x55>
mov    0x2e67(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x2018,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.passwd = type { i32 }
@ENOENT = global i32 0
@.str = [3 x i8] c"%s\00"
define i32 @get_user_name(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca [8192 x i8]
%9 = alloca %struct.passwd
%10 = alloca %struct.passwd*
%11 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%12 = load i32, i32* %5
%13 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%14 = call i32 @getpwuid_r(i32 %12, %struct.passwd* %9, i8* %13, i32 8192, %struct.passwd** %10)
store i32 %14, i32* %11
%15 = load %struct.passwd*, %struct.passwd** %10
%16 = icmp eq %struct.passwd* %15, null
br i1 %16, label %17, label %25
17: ; preds = %3
%18 = load i32, i32* %11
%19 = icmp ne i32 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %17
%21 = load i32, i32* @ENOENT
%22 = sub nsw i32 0, %21
store i32 %22, i32* %4
br label %31
23: ; preds = %17
%24 = load i32, i32* %11
store i32 %24, i32* %4
br label %31
25: ; preds = %3
%26 = load i8*, i8** %6
%27 = load i64, i64* %7
%28 = getelementptr inbounds %struct.passwd, %struct.passwd* %9, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @zsnprintf(i8* %26, i64 %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %29)
store i32 %30, i32* %4
br label %31
31: ; preds = %25, %23, %20
%32 = load i32, i32* %4
ret i32 %32
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
@ENOENT = global i32 0
@.str = [3 x i8] c"%s\00"
define i32 @get_user_name(i32 %0, i8* %1, i64 %2) {
%4 = alloca [8192 x i8]
%5 = alloca %struct.passwd
%6 = alloca %struct.passwd*
%7 = getelementptr inbounds [8192 x i8], [8192 x i8]* %4, i64 0, i64 0
%8 = bitcast %struct.passwd* %5 to i8*
%9 = bitcast %struct.passwd** %6 to i8*
%10 = call i32 @getpwuid_r(i32 %0, %struct.passwd* %5, i8* %7, i32 8192, %struct.passwd** %6)
%11 = load %struct.passwd*, %struct.passwd** %6
%12 = icmp eq %struct.passwd* %11, null
br i1 %12, label %13, label %18
13: ; preds = %3
%14 = icmp eq i32 %10, 0
br i1 %14, label %15, label %22
15: ; preds = %13
%16 = load i32, i32* @ENOENT
%17 = sub nsw i32 0, %16
br label %22
18: ; preds = %3
%19 = getelementptr inbounds %struct.passwd, %struct.passwd* %5, i64 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @zsnprintf(i8* %1, i64 %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %13, %18, %15
%23 = phi i32 [ %17, %15 ], [ %21, %18 ], [ %10, %13 ]
ret i32 %23
}