#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 spwd {
  int /*<<< orphan*/ sp_namp;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ _PATH_SHADOW;
int /*<<< orphan*/ __set_errno(int);
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgetspent_r(int /*<<< orphan*/ *, struct spwd *, char *, size_t,
                       struct spwd **);
int /*<<< orphan*/ *fopen(int /*<<< orphan*/, char *);
scalar_t__ strcmp(int /*<<< orphan*/, char const *);
int __getspnam_r(const char *name, struct spwd *result_buf, char *buffer,
                 size_t buflen, struct spwd **result) {
  FILE *spfile = fopen(_PATH_SHADOW, "r");
  int retval = -1;
  int save_errno;
  if (spfile == NULL)
    return -1;
  while (1) {
    if (fgetspent_r(spfile, result_buf, buffer, buflen, result) != 0)
      break;
    if (strcmp(result_buf->sp_namp, name) == 0) {
      retval = 0;
      break;
    }
  }
  save_errno = errno;
  (void)fclose(spfile);
  __set_errno(save_errno);
  return retval;
}


x86 assembly (clang, -O0):
<__getspnam_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)
mov    0x2e6d(%rip),%rax
mov    (%rax),%edi
lea    0xe84(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x38(%rbp)
movl   $0xffffffff,-0x3c(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11a3 <__getspnam_r+0x53>
movl   $0xffffffff,-0x4(%rbp)
jmp    121d <__getspnam_r+0xcd>
jmp    11a8 <__getspnam_r+0x58>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%r8
call   1040 <fgetspent_r@plt>
cmp    $0x0,%rax
je     11d0 <__getspnam_r+0x80>
jmp    11fa <__getspnam_r+0xaa>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    11f5 <__getspnam_r+0xa5>
movl   $0x0,-0x3c(%rbp)
jmp    11fa <__getspnam_r+0xaa>
jmp    11a8 <__getspnam_r+0x58>
mov    0x2dd7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
call   1050 <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):
<__getspnam_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
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
lea    0xe86(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11bc <__getspnam_r+0x6c>
mov    %rax,%r13
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
mov    %r15,%rcx
mov    %r14,%r8
call   1040 <fgetspent_r@plt>
test   %rax,%rax
jne    11c3 <__getspnam_r+0x73>
mov    (%rbx),%edi
mov    %rbp,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    1190 <__getspnam_r+0x40>
xor    %ebx,%ebx
jmp    11c8 <__getspnam_r+0x78>
mov    $0xffffffff,%ebx
jmp    11e0 <__getspnam_r+0x90>
mov    $0xffffffff,%ebx
mov    0x2e09(%rip),%rax
mov    (%rax),%ebp
mov    %r13,%rdi
call   1050 <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.spwd = type { i32 }
@_PATH_SHADOW = global i32 0
@.str = [2 x i8] c"r\00"
@errno = global i32 0
define i32 @__getspnam_r(i8* %0, %struct.spwd* %1, i8* %2, i64 %3, %struct.spwd** %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.spwd*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca %struct.spwd**
%12 = alloca i32*
%13 = alloca i32
%14 = alloca i32
store i8* %0, i8** %7
store %struct.spwd* %1, %struct.spwd** %8
store i8* %2, i8** %9
store i64 %3, i64* %10
store %struct.spwd** %4, %struct.spwd*** %11
%15 = load i32, i32* @_PATH_SHADOW
%16 = call i32* @fopen(i32 %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %12
store i32 -1, i32* %13
%17 = load i32*, i32** %12
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %20
19: ; preds = %5
store i32 -1, i32* %6
br label %46
20: ; preds = %5
br label %21
21: ; preds = %20, %38
%22 = load i32*, i32** %12
%23 = load %struct.spwd*, %struct.spwd** %8
%24 = load i8*, i8** %9
%25 = load i64, i64* %10
%26 = load %struct.spwd**, %struct.spwd*** %11
%27 = call i64 @fgetspent_r(i32* %22, %struct.spwd* %23, i8* %24, i64 %25, %struct.spwd** %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %21
br label %39
30: ; preds = %21
%31 = load %struct.spwd*, %struct.spwd** %8
%32 = getelementptr inbounds %struct.spwd, %struct.spwd* %31, i32 0, i32 0
%33 = load i32, i32* %32
%34 = load i8*, i8** %7
%35 = call i64 @strcmp(i32 %33, i8* %34)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %30
store i32 0, i32* %13
br label %39
38: ; preds = %30
br label %21
39: ; preds = %37, %29
%40 = load i32, i32* @errno
store i32 %40, i32* %14
%41 = load i32*, i32** %12
%42 = call i32 @fclose(i32* %41)
%43 = load i32, i32* %14
%44 = call i32 @__set_errno(i32 %43)
%45 = load i32, i32* %13
store i32 %45, i32* %6
br label %46
46: ; preds = %39, %19
%47 = load i32, i32* %6
ret i32 %47
}

LLVM IR (-O2):
%struct.spwd = type { i32 }
@_PATH_SHADOW = global i32 0
@.str = [2 x i8] c"r\00"
@errno = global i32 0
define i32 @__getspnam_r(i8* %0, %struct.spwd* %1, i8* %2, i64 %3, %struct.spwd** %4) {
%6 = load i32, i32* @_PATH_SHADOW
%7 = tail call i32* @fopen(i32 %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %23, label %9
9: ; preds = %5
%10 = getelementptr inbounds %struct.spwd, %struct.spwd* %1, i64 0, i32 0
br label %11
11: ; preds = %9, %14
%12 = tail call i64 @fgetspent_r(i32* %7, %struct.spwd* %1, i8* %2, i64 %3, %struct.spwd** %4)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i32, i32* %10
%16 = tail call i64 @strcmp(i32 %15, i8* %0)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %11
18: ; preds = %14, %11
%19 = phi i32 [ -1, %11 ], [ 0, %14 ]
%20 = load i32, i32* @errno
%21 = tail call i32 @fclose(i32* %7)
%22 = tail call i32 @__set_errno(i32 %20)
br label %23
23: ; preds = %5, %18
%24 = phi i32 [ %19, %18 ], [ -1, %5 ]
ret i32 %24
}