#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 dummy;
};

/* Variables and functions */
scalar_t__ ERANGE;
size_t ULONG_MAX;
int /*<<< orphan*/ _SC_GETPW_R_SIZE_MAX;
scalar_t__ errno;
size_t get_max_value(size_t, size_t, size_t);
int /*<<< orphan*/ getpwuid_r(int /*<<< orphan*/, struct passwd *, char *,
                              size_t, struct passwd **);
void *malloc(size_t);
char *realloc(char *, size_t);
size_t sysconf(int /*<<< orphan*/);
struct passwd *get_passwd_entry(uid_t uid) {
  struct passwd *passwd_entry;
  char *buf;
  size_t buflen;
  passwd_entry = malloc(sizeof(struct passwd));
  buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
  if (buflen == -1) {
    buflen = 1024;
  }
  buf = malloc(buflen);
  while (1) {
    getpwuid_r(uid, passwd_entry, buf, buflen, &passwd_entry);
    if (errno == ERANGE && buflen < ULONG_MAX) {
      buflen = get_max_value(buflen, buflen * 2, ULONG_MAX);
      buf = realloc(buf, buflen);
    } else {
      break;
    }
  }
  return passwd_entry;
}


x86 assembly (clang, -O0):
<get_passwd_entry>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x4,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    0x2e78(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    118e <get_passwd_entry+0x3e>
movq   $0x400,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    -0x10(%rbp),%r8
call   1030 <getpwuid_r@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%rax
mov    0x2e1c(%rip),%rcx
cmp    (%rcx),%rax
jne    1216 <get_passwd_entry+0xc6>
mov    -0x20(%rbp),%rax
mov    0x2de8(%rip),%rcx
cmp    (%rcx),%rax
jae    1216 <get_passwd_entry+0xc6>
mov    -0x20(%rbp),%rdi
mov    -0x20(%rbp),%rsi
shl    $0x1,%rsi
mov    0x2dcc(%rip),%rax
mov    (%rax),%rdx
call   1040 <get_max_value@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,-0x18(%rbp)
jmp    121b <get_passwd_entry+0xcb>
jmp    1220 <get_passwd_entry+0xd0>
jmp    119b <get_passwd_entry+0x4b>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_passwd_entry>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r14d
mov    $0x4,%edi
call   1050 <malloc@plt>
mov    %rax,%r15
mov    %rax,(%rsp)
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
cmp    $0xffffffffffffffff,%rax
mov    $0x400,%ebx
cmovne %rax,%rbx
mov    %rbx,%rdi
call   1050 <malloc@plt>
mov    %rax,%rbp
mov    %rsp,%r8
mov    %r14d,%edi
mov    %r15,%rsi
mov    %rax,%rdx
mov    %rbx,%rcx
call   1030 <getpwuid_r@plt>
mov    0x2e20(%rip),%r12
mov    (%r12),%rax
mov    0x2e25(%rip),%r13
cmp    0x0(%r13),%rax
jne    1214 <get_passwd_entry+0xc4>
mov    0x2df8(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    (%r15),%rdx
cmp    %rdx,%rbx
jae    1214 <get_passwd_entry+0xc4>
lea    (%rbx,%rbx,1),%rsi
mov    %rbx,%rdi
call   1040 <get_max_value@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
mov    %rax,%rsi
call   1060 <realloc@plt>
mov    %rax,%rbp
mov    (%rsp),%rsi
mov    %r14d,%edi
mov    %rax,%rdx
mov    %rbx,%rcx
mov    %rsp,%r8
call   1030 <getpwuid_r@plt>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     11d0 <get_passwd_entry+0x80>
mov    (%rsp),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.passwd = type { i32 }
@_SC_GETPW_R_SIZE_MAX = global i32 0
@errno = global i64 0
@ERANGE = global i64 0
@ULONG_MAX = global i64 0
define %struct.passwd* @get_passwd_entry(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.passwd*
%4 = alloca i8*
%5 = alloca i64
store i32 %0, i32* %2
%6 = call i8* @malloc(i64 4)
%7 = bitcast i8* %6 to %struct.passwd*
store %struct.passwd* %7, %struct.passwd** %3
%8 = load i32, i32* @_SC_GETPW_R_SIZE_MAX
%9 = call i64 @sysconf(i32 %8)
store i64 %9, i64* %5
%10 = load i64, i64* %5
%11 = icmp eq i64 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %1
store i64 1024, i64* %5
br label %13
13: ; preds = %12, %1
%14 = load i64, i64* %5
%15 = call i8* @malloc(i64 %14)
store i8* %15, i8** %4
br label %16
16: ; preds = %13, %39
%17 = load i32, i32* %2
%18 = load %struct.passwd*, %struct.passwd** %3
%19 = load i8*, i8** %4
%20 = load i64, i64* %5
%21 = call i32 @getpwuid_r(i32 %17, %struct.passwd* %18, i8* %19, i64 %20, %struct.passwd** %3)
%22 = load i64, i64* @errno
%23 = load i64, i64* @ERANGE
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %38
25: ; preds = %16
%26 = load i64, i64* %5
%27 = load i64, i64* @ULONG_MAX
%28 = icmp ult i64 %26, %27
br i1 %28, label %29, label %38
29: ; preds = %25
%30 = load i64, i64* %5
%31 = load i64, i64* %5
%32 = mul i64 %31, 2
%33 = load i64, i64* @ULONG_MAX
%34 = call i64 @get_max_value(i64 %30, i64 %32, i64 %33)
store i64 %34, i64* %5
%35 = load i8*, i8** %4
%36 = load i64, i64* %5
%37 = call i8* @realloc(i8* %35, i64 %36)
store i8* %37, i8** %4
br label %39
38: ; preds = %25, %16
br label %40
39: ; preds = %29
br label %16
40: ; preds = %38
%41 = load %struct.passwd*, %struct.passwd** %3
ret %struct.passwd* %41
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
@_SC_GETPW_R_SIZE_MAX = global i32 0
@errno = global i64 0
@ERANGE = global i64 0
@ULONG_MAX = global i64 0
define %struct.passwd* @get_passwd_entry(i32 %0) {
%2 = alloca %struct.passwd*
%3 = bitcast %struct.passwd** %2 to i8*
%4 = tail call dereferenceable_or_null(4) i8* @malloc(i64 4)
%5 = bitcast %struct.passwd** %2 to i8**
store i8* %4, i8** %5
%6 = load i32, i32* @_SC_GETPW_R_SIZE_MAX
%7 = tail call i64 @sysconf(i32 %6)
%8 = icmp eq i64 %7, -1
%9 = select i1 %8, i64 1024, i64 %7
%10 = tail call i8* @malloc(i64 %9)
%11 = bitcast i8* %4 to %struct.passwd*
%12 = call i32 @getpwuid_r(i32 %0, %struct.passwd* %11, i8* %10, i64 %9, %struct.passwd** %2)
%13 = load i64, i64* @errno
%14 = load i64, i64* @ERANGE
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %30
16: ; preds = %1, %21
%17 = phi i64 [ %23, %21 ], [ %9, %1 ]
%18 = phi i8* [ %24, %21 ], [ %10, %1 ]
%19 = load i64, i64* @ULONG_MAX
%20 = icmp ult i64 %17, %19
br i1 %20, label %21, label %30
21: ; preds = %16
%22 = shl i64 %17, 1
%23 = call i64 @get_max_value(i64 %17, i64 %22, i64 %19)
%24 = call i8* @realloc(i8* %18, i64 %23)
%25 = load %struct.passwd*, %struct.passwd** %2
%26 = call i32 @getpwuid_r(i32 %0, %struct.passwd* %25, i8* %24, i64 %23, %struct.passwd** %2)
%27 = load i64, i64* @errno
%28 = load i64, i64* @ERANGE
%29 = icmp eq i64 %27, %28
br i1 %29, label %16, label %30
30: ; preds = %16, %21, %1
%31 = load %struct.passwd*, %struct.passwd** %2
ret %struct.passwd* %31
}