#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 {
  char *pw_name;
};

/* Variables and functions */
#define ERANGE 128
int /*<<< orphan*/ __nss_configure_lookup(char *, char *);
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ endpwent();
int errno;
int /*<<< orphan*/ free(char *);
int getpwent_r(struct passwd *, char *, size_t, struct passwd **);
char *malloc(size_t);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ setpwent();
char *strerror(int);
int main(int argc, char **argv) {
  char *buf;
  size_t size = 2;
  int result;
  struct passwd passwd, *pwd = NULL;
  __nss_configure_lookup("passwd", "directories");
  buf = malloc(size);
  setpwent();
  do {
    pwd = NULL;
    result = getpwent_r(&passwd, buf, size, &pwd);
    printf("%d: ", result);
    switch (result) {
    case 0:
      printf("%s\n", pwd->pw_name);
      break;
    case ERANGE:
      printf("%d is not enough, error = %s\n", size, strerror(errno));
      size++;
      free(buf);
      buf = malloc(size);
      result = 0;
      break;
    default:
      printf("failed (%s)\n", strerror(result));
      _exit(1);
      break;
    }
  } while (result == 0);
  endpwent();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x2,-0x20(%rbp)
movq   $0x0,-0x38(%rbp)
lea    0xe43(%rip),%rdi
lea    0xe43(%rip),%rsi
call   1050 <__nss_configure_lookup@plt>
mov    -0x20(%rbp),%rdi
call   1090 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1080 <setpwent@plt>
movq   $0x0,-0x38(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    -0x30(%rbp),%rdi
lea    -0x38(%rbp),%rcx
call   1060 <getpwent_r@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xe0c(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x3c(%rbp)
test   %eax,%eax
je     1234 <main+0xa4>
jmp    1221 <main+0x91>
mov    -0x3c(%rbp),%eax
sub    $0x80,%eax
je     124e <main+0xbe>
jmp    12a7 <main+0x117>
mov    -0x38(%rbp),%rax
mov    (%rax),%rsi
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    12ca <main+0x13a>
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x48(%rbp),%rsi
mov    %rax,%rdx
lea    0xda6(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1090 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
jmp    12ca <main+0x13a>
mov    -0x24(%rbp),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xd7d(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x1,%edi
call   1040 <_exit@plt>
jmp    12cf <main+0x13f>
cmpl   $0x0,-0x24(%rbp)
je     11dd <main+0x4d>
mov    $0x0,%al
call   10a0 <endpwent@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe5b(%rip),%rdi
lea    0xe5b(%rip),%rsi
call   1050 <__nss_configure_lookup@plt>
mov    $0x2,%r14d
mov    $0x2,%edi
call   10a0 <malloc@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1090 <setpwent@plt>
lea    0x10(%rsp),%r15
lea    0x8(%rsp),%r12
lea    0xe37(%rip),%r13
jmp    11ed <main+0x5d>
xchg   %ax,%ax
mov    0x8(%rsp),%rax
mov    (%rax),%rdi
call   1060 <puts@plt>
movq   $0x0,0x8(%rsp)
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
mov    %r12,%rcx
call   1070 <getpwent_r@plt>
mov    %eax,%ebp
mov    %r13,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <printf@plt>
test   %ebp,%ebp
je     11e0 <main+0x50>
cmp    $0x80,%ebp
jne    125c <main+0xcc>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xde2(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1080 <printf@plt>
add    $0x1,%r14
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   10a0 <malloc@plt>
mov    %rax,%rbx
jmp    11ed <main+0x5d>
mov    %ebp,%edi
call   10b0 <strerror@plt>
lea    0xdcc(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    $0x1,%edi
call   1040 <_exit@plt>

LLVM IR (-O0):
%struct.passwd = type { i8* }
@.str = [7 x i8] c"passwd\00"
@.str.1 = [12 x i8] c"directories\00"
@.str.2 = [5 x i8] c"%d: \00"
@.str.3 = [4 x i8] c"%s\0A\00"
@.str.4 = [30 x i8] c"%d is not enough, error = %s\0A\00"
@errno = global i32 0
@.str.5 = [13 x i8] c"failed (%s)\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca %struct.passwd
%10 = alloca %struct.passwd*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 2, i64* %7
store %struct.passwd* null, %struct.passwd** %10
%11 = call i32 @__nss_configure_lookup(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%12 = load i64, i64* %7
%13 = call i8* @malloc(i64 %12)
store i8* %13, i8** %6
%14 = call i32 (...) @setpwent()
br label %15
15: ; preds = %44, %2
store %struct.passwd* null, %struct.passwd** %10
%16 = load i8*, i8** %6
%17 = load i64, i64* %7
%18 = call i32 @getpwent_r(%struct.passwd* %9, i8* %16, i64 %17, %struct.passwd** %10)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %19)
%21 = load i32, i32* %8
switch i32 %21, label %38 [
i32 0, label %22
i32 128, label %27
]
22: ; preds = %15
%23 = load %struct.passwd*, %struct.passwd** %10
%24 = getelementptr inbounds %struct.passwd, %struct.passwd* %23, i32 0, i32 0
%25 = load i8*, i8** %24
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %25)
br label %43
27: ; preds = %15
%28 = load i64, i64* %7
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i64 %28, i8* %30)
%32 = load i64, i64* %7
%33 = add i64 %32, 1
store i64 %33, i64* %7
%34 = load i8*, i8** %6
%35 = call i32 @free(i8* %34)
%36 = load i64, i64* %7
%37 = call i8* @malloc(i64 %36)
store i8* %37, i8** %6
store i32 0, i32* %8
br label %43
38: ; preds = %15
%39 = load i32, i32* %8
%40 = call i8* @strerror(i32 %39)
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i8* %40)
%42 = call i32 @_exit(i32 1)
unreachable
43: ; preds = %27, %22
br label %44
44: ; preds = %43
%45 = load i32, i32* %8
%46 = icmp eq i32 %45, 0
br i1 %46, label %15, label %47
47: ; preds = %44
%48 = call i32 (...) @endpwent()
ret i32 0
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@.str = [7 x i8] c"passwd\00"
@.str.1 = [12 x i8] c"directories\00"
@.str.2 = [5 x i8] c"%d: \00"
@.str.4 = [30 x i8] c"%d is not enough, error = %s\0A\00"
@errno = global i32 0
@.str.5 = [13 x i8] c"failed (%s)\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.passwd
%4 = alloca %struct.passwd*
%5 = bitcast %struct.passwd* %3 to i8*
%6 = bitcast %struct.passwd** %4 to i8*
%7 = tail call i32 @__nss_configure_lookup(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call dereferenceable_or_null(2) i8* @malloc(i64 2)
%9 = tail call i32 (...) @setpwent()
br label %10
10: ; preds = %21, %2
%11 = phi i8* [ %27, %21 ], [ %8, %2 ]
%12 = phi i64 [ %25, %21 ], [ 2, %2 ]
br label %13
13: ; preds = %10, %16
store %struct.passwd* null, %struct.passwd** %4
%14 = call i32 @getpwent_r(%struct.passwd* %3, i8* %11, i64 %12, %struct.passwd** %4)
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %14)
switch i32 %14, label %28 [
i32 0, label %16
i32 128, label %21
]
16: ; preds = %13
%17 = load %struct.passwd*, %struct.passwd** %4
%18 = getelementptr inbounds %struct.passwd, %struct.passwd* %17, i64 0, i32 0
%19 = load i8*, i8** %18
%20 = call i32 @puts(i8* dereferenceable(1) %19)
br label %13
21: ; preds = %13
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i64 %12, i8* %23)
%25 = add i64 %12, 1
%26 = call i32 @free(i8* %11)
%27 = call i8* @malloc(i64 %25)
br label %10
28: ; preds = %13
%29 = call i8* @strerror(i32 %14)
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i8* %29)
%31 = call i32 @_exit(i32 1)
unreachable
}