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

/* Variables and functions */
int ENOENT;
int ENOMEM;
int ERANGE;
int /*<<< orphan*/ _SC_GETPW_R_SIZE_MAX;
int /*<<< orphan*/ free(char *);
int getpwnam_r(char const *, struct passwd *, char *, size_t, struct passwd **);
scalar_t__ malloc(int);
int sysconf(int /*<<< orphan*/);
int getPW(const char *user, char **pwbuf) {
  struct passwd *pwbufp = NULL;
  size_t currBufferSize =
      (sysconf(_SC_GETPW_R_SIZE_MAX) > 2048 ? sysconf(_SC_GETPW_R_SIZE_MAX)
                                            : 2048);
  *pwbuf = NULL;
  char *buf = (char *)malloc(sizeof(char) * currBufferSize);
  if (!buf) {
    return ENOMEM;
  }
  int error;
  for (;;) {
    error = getpwnam_r(user, (struct passwd *)buf, buf + sizeof(struct passwd),
                       currBufferSize - sizeof(struct passwd), &pwbufp);
    if (error != ERANGE) {
      break;
    }
    free(buf);
    currBufferSize *= 2;
    buf = (char *)malloc(sizeof(char) * currBufferSize);
    if (!buf) {
      return ENOMEM;
    }
  }
  if (!pwbufp && !error) {
    free(buf);
    return ENOENT;
  } else if (error) {
    free(buf);
    return error;
  }
  *pwbuf = buf;
  return 0;
}


x86 assembly (clang, -O0):
<getPW>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2e89(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
cmp    $0x800,%eax
jle    1187 <getPW+0x47>
mov    0x2e70(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    %eax,-0x38(%rbp)
jmp    1194 <getPW+0x54>
mov    $0x800,%eax
mov    %eax,-0x38(%rbp)
jmp    1194 <getPW+0x54>
mov    -0x38(%rbp),%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x28(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11d7 <getPW+0x97>
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12c4 <getPW+0x184>
jmp    11dc <getPW+0x9c>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x30(%rbp),%rdx
add    $0x4,%rdx
mov    -0x28(%rbp),%rcx
sub    $0x4,%rcx
lea    -0x20(%rbp),%r8
call   1050 <getpwnam_r@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    0x2dd6(%rip),%rcx
cmp    (%rcx),%eax
je     1217 <getPW+0xd7>
jmp    1260 <getPW+0x120>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    125b <getPW+0x11b>
mov    0x2d7f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12c4 <getPW+0x184>
jmp    11dc <getPW+0x9c>
cmpq   $0x0,-0x20(%rbp)
jne    128f <getPW+0x14f>
cmpl   $0x0,-0x34(%rbp)
jne    128f <getPW+0x14f>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    0x2d3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12c4 <getPW+0x184>
cmpl   $0x0,-0x34(%rbp)
je     12ad <getPW+0x16d>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12c4 <getPW+0x184>
jmp    12b2 <getPW+0x172>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getPW>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbp
mov    %rdi,%r15
movq   $0x0,0x8(%rsp)
mov    0x2e84(%rip),%r14
mov    (%r14),%edi
call   1060 <sysconf@plt>
mov    $0x800,%ebx
cmp    $0x801,%eax
jl     1182 <getPW+0x42>
mov    (%r14),%edi
call   1060 <sysconf@plt>
mov    %eax,%ebx
movq   $0x0,0x0(%rbp)
mov    %ebx,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     11e6 <getPW+0xa6>
mov    %rax,%r12
mov    %rbp,0x10(%rsp)
movslq %ebx,%rbx
lea    0x8(%rsp),%r13
mov    0x2e33(%rip),%r14
nopl   (%rax)
lea    0x4(%r12),%rdx
lea    -0x4(%rbx),%rcx
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r13,%r8
call   1050 <getpwnam_r@plt>
cmp    (%r14),%eax
jne    1200 <getPW+0xc0>
mov    %r12,%rdi
call   1030 <free@plt>
add    %rbx,%rbx
mov    %ebx,%edi
call   1040 <malloc@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11b0 <getPW+0x70>
mov    0x2de3(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebp
cmpq   $0x0,0x8(%rsp)
jne    121f <getPW+0xdf>
test   %ebp,%ebp
jne    121f <getPW+0xdf>
mov    %r12,%rdi
call   1030 <free@plt>
mov    0x2da3(%rip),%rax
jmp    11ed <getPW+0xad>
test   %ebp,%ebp
je     122d <getPW+0xed>
mov    %r12,%rdi
call   1030 <free@plt>
jmp    11ef <getPW+0xaf>
mov    0x10(%rsp),%rax
mov    %r12,(%rax)
xor    %ebp,%ebp
jmp    11ef <getPW+0xaf>

LLVM IR (-O0):
%struct.passwd = type { i32 }
@_SC_GETPW_R_SIZE_MAX = global i32 0
@ENOMEM = global i32 0
@ERANGE = global i32 0
@ENOENT = global i32 0
define i32 @getPW(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca %struct.passwd*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %4
store i8** %1, i8*** %5
store %struct.passwd* null, %struct.passwd** %6
%10 = load i32, i32* @_SC_GETPW_R_SIZE_MAX
%11 = call i32 @sysconf(i32 %10)
%12 = icmp sgt i32 %11, 2048
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @_SC_GETPW_R_SIZE_MAX
%15 = call i32 @sysconf(i32 %14)
br label %17
16: ; preds = %2
br label %17
17: ; preds = %16, %13
%18 = phi i32 [ %15, %13 ], [ 2048, %16 ]
%19 = sext i32 %18 to i64
store i64 %19, i64* %7
%20 = load i8**, i8*** %5
store i8* null, i8** %20
%21 = load i64, i64* %7
%22 = mul i64 1, %21
%23 = trunc i64 %22 to i32
%24 = call i64 @malloc(i32 %23)
%25 = inttoptr i64 %24 to i8*
store i8* %25, i8** %8
%26 = load i8*, i8** %8
%27 = icmp ne i8* %26, null
br i1 %27, label %30, label %28
28: ; preds = %17
%29 = load i32, i32* @ENOMEM
store i32 %29, i32* %3
br label %80
30: ; preds = %17
br label %31
31: ; preds = %58, %30
%32 = load i8*, i8** %4
%33 = load i8*, i8** %8
%34 = bitcast i8* %33 to %struct.passwd*
%35 = load i8*, i8** %8
%36 = getelementptr inbounds i8, i8* %35, i64 4
%37 = load i64, i64* %7
%38 = sub i64 %37, 4
%39 = call i32 @getpwnam_r(i8* %32, %struct.passwd* %34, i8* %36, i64 %38, %struct.passwd** %6)
store i32 %39, i32* %9
%40 = load i32, i32* %9
%41 = load i32, i32* @ERANGE
%42 = icmp ne i32 %40, %41
br i1 %42, label %43, label %44
43: ; preds = %31
br label %59
44: ; preds = %31
%45 = load i8*, i8** %8
%46 = call i32 @free(i8* %45)
%47 = load i64, i64* %7
%48 = mul i64 %47, 2
store i64 %48, i64* %7
%49 = load i64, i64* %7
%50 = mul i64 1, %49
%51 = trunc i64 %50 to i32
%52 = call i64 @malloc(i32 %51)
%53 = inttoptr i64 %52 to i8*
store i8* %53, i8** %8
%54 = load i8*, i8** %8
%55 = icmp ne i8* %54, null
br i1 %55, label %58, label %56
56: ; preds = %44
%57 = load i32, i32* @ENOMEM
store i32 %57, i32* %3
br label %80
58: ; preds = %44
br label %31
59: ; preds = %43
%60 = load %struct.passwd*, %struct.passwd** %6
%61 = icmp ne %struct.passwd* %60, null
br i1 %61, label %69, label %62
62: ; preds = %59
%63 = load i32, i32* %9
%64 = icmp ne i32 %63, 0
br i1 %64, label %69, label %65
65: ; preds = %62
%66 = load i8*, i8** %8
%67 = call i32 @free(i8* %66)
%68 = load i32, i32* @ENOENT
store i32 %68, i32* %3
br label %80
69: ; preds = %62, %59
%70 = load i32, i32* %9
%71 = icmp ne i32 %70, 0
br i1 %71, label %72, label %76
72: ; preds = %69
%73 = load i8*, i8** %8
%74 = call i32 @free(i8* %73)
%75 = load i32, i32* %9
store i32 %75, i32* %3
br label %80
76: ; preds = %69
br label %77
77: ; preds = %76
%78 = load i8*, i8** %8
%79 = load i8**, i8*** %5
store i8* %78, i8** %79
store i32 0, i32* %3
br label %80
80: ; preds = %77, %72, %65, %56, %28
%81 = load i32, i32* %3
ret i32 %81
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
@_SC_GETPW_R_SIZE_MAX = global i32 0
@ENOMEM = global i32 0
@ERANGE = global i32 0
@ENOENT = global i32 0
define i32 @getPW(i8* %0, i8** nocapture writeonly %1) {
%3 = alloca %struct.passwd*
%4 = bitcast %struct.passwd** %3 to i8*
store %struct.passwd* null, %struct.passwd** %3
%5 = load i32, i32* @_SC_GETPW_R_SIZE_MAX
%6 = tail call i32 @sysconf(i32 %5)
%7 = icmp sgt i32 %6, 2048
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i32, i32* @_SC_GETPW_R_SIZE_MAX
%10 = tail call i32 @sysconf(i32 %9)
br label %11
11: ; preds = %2, %8
%12 = phi i32 [ %10, %8 ], [ 2048, %2 ]
store i8* null, i8** %1
%13 = tail call i64 @malloc(i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = load i32, i32* @ENOMEM
br label %50
17: ; preds = %11
%18 = sext i32 %12 to i64
br label %19
19: ; preds = %29, %17
%20 = phi i64 [ %18, %17 ], [ %31, %29 ]
%21 = phi i64 [ %13, %17 ], [ %33, %29 ]
%22 = inttoptr i64 %21 to i8*
%23 = inttoptr i64 %21 to %struct.passwd*
%24 = getelementptr inbounds i8, i8* %22, i64 4
%25 = add i64 %20, -4
%26 = call i32 @getpwnam_r(i8* %0, %struct.passwd* %23, i8* %24, i64 %25, %struct.passwd** %3)
%27 = load i32, i32* @ERANGE
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %37
29: ; preds = %19
%30 = call i32 @free(i8* %22)
%31 = shl i64 %20, 1
%32 = trunc i64 %31 to i32
%33 = call i64 @malloc(i32 %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %19
35: ; preds = %29
%36 = load i32, i32* @ENOMEM
br label %50
37: ; preds = %19
%38 = inttoptr i64 %21 to i8*
%39 = load %struct.passwd*, %struct.passwd** %3
%40 = icmp ne %struct.passwd* %39, null
%41 = icmp ne i32 %26, 0
%42 = select i1 %40, i1 true, i1 %41
br i1 %42, label %46, label %43
43: ; preds = %37
%44 = call i32 @free(i8* %38)
%45 = load i32, i32* @ENOENT
br label %50
46: ; preds = %37
br i1 %41, label %47, label %49
47: ; preds = %46
%48 = call i32 @free(i8* %38)
br label %50
49: ; preds = %46
store i8* %38, i8** %1
br label %50
50: ; preds = %35, %43, %47, %49, %15
%51 = phi i32 [ %16, %15 ], [ %26, %47 ], [ 0, %49 ], [ %45, %43 ], [ %36, %35 ]
ret i32 %51
}