#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 scalar_t__ uid_t;
struct passwd {
  scalar_t__ pw_uid;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EIO;
int ERANGE;
int __pwnextent(int /*<<< orphan*/ *, struct passwd *, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int getpwuid_r(uid_t uid, struct passwd *pwd, char *buffer, size_t bufsiz,
               struct passwd **result) {
  if (bufsiz < 0x1000) {
    return ERANGE;
  };
  FILE *fp = fopen("/etc/passwd", "r");
  if (fp == NULL) {
    return EIO;
  };
  while (__pwnextent(fp, pwd, buffer) != -1) {
    if (pwd->pw_uid == uid) {
      *result = pwd;
      fclose(fp);
      return 0;
    };
  };
  fclose(fp);
  *result = NULL;
  return 0;
}


x86 assembly (clang, -O0):
<getpwuid_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
cmpq   $0x1000,-0x28(%rbp)
jae    116b <getpwuid_r+0x3b>
mov    0x2e87(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    120e <getpwuid_r+0xde>
lea    0xe8e(%rip),%rdi
lea    0xe93(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    119e <getpwuid_r+0x6e>
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    120e <getpwuid_r+0xde>
jmp    11a3 <getpwuid_r+0x73>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <__pwnextent@plt>
cmp    $0xffffffff,%eax
je     11f3 <getpwuid_r+0xc3>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
cmp    -0x10(%rbp),%rax
jne    11ee <getpwuid_r+0xbe>
mov    -0x18(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    120e <getpwuid_r+0xde>
jmp    11a3 <getpwuid_r+0x73>
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x30(%rbp),%rax
movq   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getpwuid_r>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
cmp    $0xfff,%rcx
ja     114d <getpwuid_r+0x1d>
mov    0x2e9f(%rip),%rax
mov    (%rax),%eax
jmp    11c1 <getpwuid_r+0x91>
mov    %r8,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r12
lea    0xea0(%rip),%rdi
lea    0xea5(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11a5 <getpwuid_r+0x75>
mov    %rax,%r13
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1040 <__pwnextent@plt>
cmp    $0xffffffff,%eax
je     11b0 <getpwuid_r+0x80>
cmp    %r12,(%rbx)
jne    1180 <getpwuid_r+0x50>
mov    %rbx,(%r14)
mov    %r13,%rdi
call   1030 <fclose@plt>
jmp    11bf <getpwuid_r+0x8f>
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
jmp    11c1 <getpwuid_r+0x91>
mov    %r13,%rdi
call   1030 <fclose@plt>
movq   $0x0,(%r14)
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.passwd = type { i64 }
@ERANGE = global i32 0
@.str = [12 x i8] c"/etc/passwd\00"
@.str.1 = [2 x i8] c"r\00"
@EIO = global i32 0
define i32 @getpwuid_r(i64 %0, %struct.passwd* %1, i8* %2, i64 %3, %struct.passwd** %4) {
%6 = alloca i32
%7 = alloca i64
%8 = alloca %struct.passwd*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca %struct.passwd**
%12 = alloca i32*
store i64 %0, i64* %7
store %struct.passwd* %1, %struct.passwd** %8
store i8* %2, i8** %9
store i64 %3, i64* %10
store %struct.passwd** %4, %struct.passwd*** %11
%13 = load i64, i64* %10
%14 = icmp ult i64 %13, 4096
br i1 %14, label %15, label %17
15: ; preds = %5
%16 = load i32, i32* @ERANGE
store i32 %16, i32* %6
br label %46
17: ; preds = %5
%18 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %12
%19 = load i32*, i32** %12
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = load i32, i32* @EIO
store i32 %22, i32* %6
br label %46
23: ; preds = %17
br label %24
24: ; preds = %41, %23
%25 = load i32*, i32** %12
%26 = load %struct.passwd*, %struct.passwd** %8
%27 = load i8*, i8** %9
%28 = call i32 @__pwnextent(i32* %25, %struct.passwd* %26, i8* %27)
%29 = icmp ne i32 %28, -1
br i1 %29, label %30, label %42
30: ; preds = %24
%31 = load %struct.passwd*, %struct.passwd** %8
%32 = getelementptr inbounds %struct.passwd, %struct.passwd* %31, i32 0, i32 0
%33 = load i64, i64* %32
%34 = load i64, i64* %7
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %41
36: ; preds = %30
%37 = load %struct.passwd*, %struct.passwd** %8
%38 = load %struct.passwd**, %struct.passwd*** %11
store %struct.passwd* %37, %struct.passwd** %38
%39 = load i32*, i32** %12
%40 = call i32 @fclose(i32* %39)
store i32 0, i32* %6
br label %46
41: ; preds = %30
br label %24
42: ; preds = %24
%43 = load i32*, i32** %12
%44 = call i32 @fclose(i32* %43)
%45 = load %struct.passwd**, %struct.passwd*** %11
store %struct.passwd* null, %struct.passwd** %45
store i32 0, i32* %6
br label %46
46: ; preds = %42, %36, %21, %15
%47 = load i32, i32* %6
ret i32 %47
}

LLVM IR (-O2):
%struct.passwd = type { i64 }
@ERANGE = global i32 0
@.str = [12 x i8] c"/etc/passwd\00"
@.str.1 = [2 x i8] c"r\00"
@EIO = global i32 0
define i32 @getpwuid_r(i64 %0, %struct.passwd* %1, i8* %2, i64 %3, %struct.passwd** nocapture writeonly %4) {
%6 = icmp ult i64 %3, 4096
br i1 %6, label %7, label %9
7: ; preds = %5
%8 = load i32, i32* @ERANGE
br label %26
9: ; preds = %5
%10 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %14, label %12
12: ; preds = %9
%13 = getelementptr inbounds %struct.passwd, %struct.passwd* %1, i64 0, i32 0
br label %16
14: ; preds = %9
%15 = load i32, i32* @EIO
br label %26
16: ; preds = %12, %19
%17 = tail call i32 @__pwnextent(i32* %10, %struct.passwd* %1, i8* %2)
%18 = icmp eq i32 %17, -1
br i1 %18, label %24, label %19
19: ; preds = %16
%20 = load i64, i64* %13
%21 = icmp eq i64 %20, %0
br i1 %21, label %22, label %16
22: ; preds = %19
store %struct.passwd* %1, %struct.passwd** %4
%23 = tail call i32 @fclose(i32* %10)
br label %26
24: ; preds = %16
%25 = tail call i32 @fclose(i32* %10)
store %struct.passwd* null, %struct.passwd** %4
br label %26
26: ; preds = %14, %22, %24, %7
%27 = phi i32 [ %8, %7 ], [ %15, %14 ], [ 0, %22 ], [ 0, %24 ]
ret i32 %27
}