#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 /*<<< orphan*/ __set_errno(int);
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgetpwent_r(int /*<<< orphan*/ *, struct passwd *, char *, size_t,
                       struct passwd **);
int /*<<< orphan*/ *fopen(char *, char *);
int __getpwuid_r(uid_t uid, struct passwd *result_buf, char *buffer,
                 size_t buflen, struct passwd **result) {
  FILE *pwfile = fopen("/etc/passwd", "r");
  int retval = -1;
  int save_errno;
  if (pwfile == NULL)
    return -1;
  while (1) {
    if (fgetpwent_r(pwfile, result_buf, buffer, buflen, result) != 0)
      break;
    if (result_buf->pw_uid == uid) {
      retval = 0;
      break;
    }
  }
  save_errno = errno;
  (void)fclose(pwfile);
  __set_errno(save_errno);
  return retval;
}


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)
lea    0xe9d(%rip),%rdi
lea    0xea2(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x38(%rbp)
movl   $0xffffffff,-0x3c(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1191 <__getpwuid_r+0x51>
movl   $0xffffffff,-0x4(%rbp)
jmp    1203 <__getpwuid_r+0xc3>
jmp    1196 <__getpwuid_r+0x56>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%r8
call   1060 <fgetpwent_r@plt>
cmp    $0x0,%rax
je     11be <__getpwuid_r+0x7e>
jmp    11e0 <__getpwuid_r+0xa0>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
cmp    -0x10(%rbp),%rax
jne    11db <__getpwuid_r+0x9b>
movl   $0x0,-0x3c(%rbp)
jmp    11e0 <__getpwuid_r+0xa0>
jmp    1196 <__getpwuid_r+0x56>
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x40(%rbp),%edi
call   1030 <__set_errno@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__getpwuid_r>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r14
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0xe9f(%rip),%rdi
lea    0xea4(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11a2 <__getpwuid_r+0x62>
mov    %rax,%r13
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
mov    %r15,%rcx
mov    %r14,%r8
call   1060 <fgetpwent_r@plt>
test   %rax,%rax
jne    11a9 <__getpwuid_r+0x69>
cmp    %rbp,(%rbx)
jne    1180 <__getpwuid_r+0x40>
xor    %ebx,%ebx
jmp    11ae <__getpwuid_r+0x6e>
mov    $0xffffffff,%ebx
jmp    11c6 <__getpwuid_r+0x86>
mov    $0xffffffff,%ebx
mov    0x2e2b(%rip),%rax
mov    (%rax),%ebp
mov    %r13,%rdi
call   1040 <fclose@plt>
mov    %ebp,%edi
call   1030 <__set_errno@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.passwd = type { i64 }
@.str = [12 x i8] c"/etc/passwd\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i32 0
define i32 @__getpwuid_r(i64 %0, %struct.passwd* %1, i8* %2, i64 %3, %struct.passwd** %4) {
%6 = 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))
%7 = icmp eq i32* %6, null
br i1 %7, label %21, label %8
8: ; preds = %5
%9 = getelementptr inbounds %struct.passwd, %struct.passwd* %1, i64 0, i32 0
br label %10
10: ; preds = %8, %13
%11 = tail call i64 @fgetpwent_r(i32* %6, %struct.passwd* %1, i8* %2, i64 %3, %struct.passwd** %4)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = load i64, i64* %9
%15 = icmp eq i64 %14, %0
br i1 %15, label %16, label %10
16: ; preds = %13, %10
%17 = phi i32 [ -1, %10 ], [ 0, %13 ]
%18 = load i32, i32* @errno
%19 = tail call i32 @fclose(i32* %6)
%20 = tail call i32 @__set_errno(i32 %18)
br label %21
21: ; preds = %5, %16
%22 = phi i32 [ %17, %16 ], [ -1, %5 ]
ret i32 %22
}