#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 /*<<< orphan*/ _SC_GETPW_R_SIZE_MAX;
scalar_t__ getpwuid_r(int /*<<< orphan*/, struct passwd *, char *, int,
                      struct passwd **);
char *malloc(int);
int sysconf(int /*<<< orphan*/);
char *get_name(uid_t u_id) {
  int bufsize;
  if ((bufsize = sysconf(_SC_GETPW_R_SIZE_MAX)) == -1) {
    bufsize = 1024;
  }
  char *buffer = malloc(bufsize);
  struct passwd pwd, *result = NULL;
  if (getpwuid_r(u_id, &pwd, buffer, bufsize, &result) != 0 || !result) {
    return NULL;
  }
  return pwd.pw_name;
}


x86 assembly (clang, -O0):
<get_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    0x2ea6(%rip),%rax
mov    (%rax),%edi
call   1050 <sysconf@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
jne    115c <get_name+0x2c>
movl   $0x400,-0x10(%rbp)
mov    -0x10(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%ecx
lea    -0x20(%rbp),%rsi
lea    -0x28(%rbp),%r8
call   1030 <getpwuid_r@plt>
cmp    $0x0,%rax
jne    119c <get_name+0x6c>
cmpq   $0x0,-0x28(%rbp)
jne    11a9 <get_name+0x79>
movq   $0x0,-0x8(%rbp)
jmp    11b1 <get_name+0x81>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_name>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
mov    0x2ea9(%rip),%rax
mov    (%rax),%edi
call   1050 <sysconf@plt>
cmp    $0xffffffff,%eax
mov    $0x400,%ebp
cmovne %eax,%ebp
mov    %ebp,%edi
call   1040 <malloc@plt>
movq   $0x0,0x8(%rsp)
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%r8
mov    %ebx,%edi
mov    %rax,%rdx
mov    %ebp,%ecx
call   1030 <getpwuid_r@plt>
mov    0x8(%rsp),%rdx
test   %rdx,%rdx
je     1186 <get_name+0x56>
mov    0x10(%rsp),%rdx
xor    %ecx,%ecx
test   %rax,%rax
cmove  %rdx,%rcx
mov    %rcx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.passwd = type { i8* }
@_SC_GETPW_R_SIZE_MAX = global i32 0
define i8* @get_name(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.passwd
%7 = alloca %struct.passwd*
store i32 %0, i32* %3
%8 = load i32, i32* @_SC_GETPW_R_SIZE_MAX
%9 = call i32 @sysconf(i32 %8)
store i32 %9, i32* %4
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 1024, i32* %4
br label %12
12: ; preds = %11, %1
%13 = load i32, i32* %4
%14 = call i8* @malloc(i32 %13)
store i8* %14, i8** %5
store %struct.passwd* null, %struct.passwd** %7
%15 = load i32, i32* %3
%16 = load i8*, i8** %5
%17 = load i32, i32* %4
%18 = call i64 @getpwuid_r(i32 %15, %struct.passwd* %6, i8* %16, i32 %17, %struct.passwd** %7)
%19 = icmp ne i64 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %12
%21 = load %struct.passwd*, %struct.passwd** %7
%22 = icmp ne %struct.passwd* %21, null
br i1 %22, label %24, label %23
23: ; preds = %20, %12
store i8* null, i8** %2
br label %27
24: ; preds = %20
%25 = getelementptr inbounds %struct.passwd, %struct.passwd* %6, i32 0, i32 0
%26 = load i8*, i8** %25
store i8* %26, i8** %2
br label %27
27: ; preds = %24, %23
%28 = load i8*, i8** %2
ret i8* %28
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@_SC_GETPW_R_SIZE_MAX = global i32 0
define i8* @get_name(i32 %0) {
%2 = alloca %struct.passwd
%3 = alloca %struct.passwd*
%4 = load i32, i32* @_SC_GETPW_R_SIZE_MAX
%5 = tail call i32 @sysconf(i32 %4)
%6 = icmp eq i32 %5, -1
%7 = select i1 %6, i32 1024, i32 %5
%8 = tail call i8* @malloc(i32 %7)
%9 = bitcast %struct.passwd* %2 to i8*
%10 = bitcast %struct.passwd** %3 to i8*
store %struct.passwd* null, %struct.passwd** %3
%11 = call i64 @getpwuid_r(i32 %0, %struct.passwd* %2, i8* %8, i32 %7, %struct.passwd** %3)
%12 = icmp eq i64 %11, 0
%13 = load %struct.passwd*, %struct.passwd** %3
%14 = icmp ne %struct.passwd* %13, null
%15 = select i1 %12, i1 %14, i1 false
%16 = getelementptr inbounds %struct.passwd, %struct.passwd* %2, i64 0, i32 0
%17 = load i8*, i8** %16
%18 = select i1 %15, i8* %17, i8* null
ret i8* %18
}