#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 /*<<< orphan*/ pw_name;
};
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 *);
scalar_t__ strcmp(int /*<<< orphan*/, char const *);
int __getpwnam_r(const char *name, 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 (strcmp(result_buf->pw_name, name) == 0) {
      retval = 0;
      break;
    }
  }
  save_errno = errno;
  (void)fclose(pwfile);
  __set_errno(save_errno);
  return retval;
}


x86 assembly (clang, -O0):
<__getpwnam_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    0xe8d(%rip),%rdi
lea    0xe92(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x38(%rbp)
movl   $0xffffffff,-0x3c(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11a1 <__getpwnam_r+0x51>
movl   $0xffffffff,-0x4(%rbp)
jmp    121b <__getpwnam_r+0xcb>
jmp    11a6 <__getpwnam_r+0x56>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%r8
call   1070 <fgetpwent_r@plt>
cmp    $0x0,%rax
je     11ce <__getpwnam_r+0x7e>
jmp    11f8 <__getpwnam_r+0xa8>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    11f3 <__getpwnam_r+0xa3>
movl   $0x0,-0x3c(%rbp)
jmp    11f8 <__getpwnam_r+0xa8>
jmp    11a6 <__getpwnam_r+0x56>
mov    0x2de1(%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):
<__getpwnam_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    0xe8f(%rip),%rdi
lea    0xe94(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11bc <__getpwnam_r+0x6c>
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   1070 <fgetpwent_r@plt>
test   %rax,%rax
jne    11c3 <__getpwnam_r+0x73>
mov    (%rbx),%edi
mov    %rbp,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    1190 <__getpwnam_r+0x40>
xor    %ebx,%ebx
jmp    11c8 <__getpwnam_r+0x78>
mov    $0xffffffff,%ebx
jmp    11e0 <__getpwnam_r+0x90>
mov    $0xffffffff,%ebx
mov    0x2e11(%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 { i32 }
@.str = [12 x i8] c"/etc/passwd\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i32 0
define i32 @__getpwnam_r(i8* %0, %struct.passwd* %1, i8* %2, i64 %3, %struct.passwd** %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.passwd*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca %struct.passwd**
%12 = alloca i32*
%13 = alloca i32
%14 = alloca i32
store i8* %0, i8** %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 %45
19: ; preds = %5
br label %20
20: ; preds = %19, %37
%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 %38
29: ; preds = %20
%30 = load %struct.passwd*, %struct.passwd** %8
%31 = getelementptr inbounds %struct.passwd, %struct.passwd* %30, i32 0, i32 0
%32 = load i32, i32* %31
%33 = load i8*, i8** %7
%34 = call i64 @strcmp(i32 %32, i8* %33)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %29
store i32 0, i32* %13
br label %38
37: ; preds = %29
br label %20
38: ; preds = %36, %28
%39 = load i32, i32* @errno
store i32 %39, i32* %14
%40 = load i32*, i32** %12
%41 = call i32 @fclose(i32* %40)
%42 = load i32, i32* %14
%43 = call i32 @__set_errno(i32 %42)
%44 = load i32, i32* %13
store i32 %44, i32* %6
br label %45
45: ; preds = %38, %18
%46 = load i32, i32* %6
ret i32 %46
}

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