#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;
};
struct flock {
  scalar_t__ l_len;
  scalar_t__ l_start;
  int /*<<< orphan*/ l_whence;
  int /*<<< orphan*/ l_type;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ EINTR;
int EIO;
int ERANGE;
int /*<<< orphan*/ F_RDLCK;
int /*<<< orphan*/ F_SETLKW;
int /*<<< orphan*/ SEEK_SET;
int __pwnextent(int /*<<< orphan*/ *, struct passwd *, char *);
scalar_t__ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fcntl(int /*<<< orphan*/, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ memset(struct flock *, int /*<<< orphan*/, int);
scalar_t__ strcmp(int /*<<< orphan*/, char const *);
int getpwnam_r(const char *username, 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;
  };
  struct flock lock;
  memset(&lock, 0, sizeof(struct flock));
  lock.l_type = F_RDLCK;
  lock.l_whence = SEEK_SET;
  lock.l_start = 0;
  lock.l_len = 0;
  int status;
  do {
    status = fcntl(fileno(fp), F_SETLKW, &lock);
  } while (status != 0 && errno == EINTR);
  while (__pwnextent(fp, pwd, buffer) != -1) {
    if (strcmp(pwd->pw_name, username) == 0) {
      *result = pwd;
      fclose(fp);
      return 0;
    };
  };
  fclose(fp);
  *result = NULL;
  return 0;
}


x86 assembly (clang, -O0):
<getpwnam_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%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    11ab <getpwnam_r+0x3b>
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12e2 <getpwnam_r+0x172>
lea    0xe4e(%rip),%rdi
lea    0xe53(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11de <getpwnam_r+0x6e>
mov    0x2dfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12e2 <getpwnam_r+0x172>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    0x2db3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
mov    -0x38(%rbp),%rdi
call   1080 <fileno@plt>
mov    %eax,%edi
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
lea    -0x50(%rbp),%rdx
call   1030 <fcntl@plt>
mov    %eax,-0x54(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x54(%rbp)
mov    %al,-0x55(%rbp)
je     125f <getpwnam_r+0xef>
mov    0x2d7c(%rip),%rax
mov    (%rax),%rax
mov    0x2d62(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x55(%rbp)
mov    -0x55(%rbp),%al
test   $0x1,%al
jne    1216 <getpwnam_r+0xa6>
jmp    126f <getpwnam_r+0xff>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <__pwnextent@plt>
cmp    $0xffffffff,%eax
je     12c7 <getpwnam_r+0x157>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    12c2 <getpwnam_r+0x152>
mov    -0x18(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e2 <getpwnam_r+0x172>
jmp    126f <getpwnam_r+0xff>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rax
movq   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getpwnam_r>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
cmp    $0xfff,%rcx
ja     1195 <getpwnam_r+0x25>
mov    0x2e52(%rip),%rax
mov    (%rax),%eax
jmp    12ad <getpwnam_r+0x13d>
mov    %r8,%r14
mov    %rdx,%r15
mov    %rsi,%r13
mov    %rdi,%rbp
lea    0xe58(%rip),%rdi
lea    0xe5d(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1291 <getpwnam_r+0x121>
mov    %rax,%rbx
mov    %rbp,0x8(%rsp)
mov    %r15,0x10(%rsp)
mov    %r14,0x18(%rsp)
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x34(%rsp)
mov    0x2dc9(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
mov    0x2ddc(%rip),%r14
mov    0x2db5(%rip),%r12
mov    0x2d9e(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1080 <fileno@plt>
mov    (%r14),%esi
mov    %eax,%edi
mov    %rbp,%rdx
call   1030 <fcntl@plt>
test   %eax,%eax
je     1242 <getpwnam_r+0xd2>
mov    (%r15),%rax
cmp    %rax,(%r12)
je     1220 <getpwnam_r+0xb0>
mov    0x18(%rsp),%r14
mov    0x10(%rsp),%rbp
mov    0x8(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1050 <__pwnextent@plt>
cmp    $0xffffffff,%eax
je     129c <getpwnam_r+0x12c>
mov    0x0(%r13),%edi
mov    %r15,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    1260 <getpwnam_r+0xf0>
mov    %r13,(%r14)
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    12ab <getpwnam_r+0x13b>
mov    0x2d38(%rip),%rax
mov    (%rax),%eax
jmp    12ad <getpwnam_r+0x13d>
mov    %rbx,%rdi
call   1040 <fclose@plt>
movq   $0x0,(%r14)
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.passwd = type { i32 }
%struct.flock = type { i64, i64, i32, i32 }
@ERANGE = global i32 0
@.str = [12 x i8] c"/etc/passwd\00"
@.str.1 = [2 x i8] c"r\00"
@EIO = global i32 0
@F_RDLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLKW = global i32 0
@errno = global i64 0
@EINTR = global i64 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 %struct.flock
%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 = load i64, i64* %10
%16 = icmp ult i64 %15, 4096
br i1 %16, label %17, label %19
17: ; preds = %5
%18 = load i32, i32* @ERANGE
store i32 %18, i32* %6
br label %71
19: ; preds = %5
%20 = 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* %20, i32** %12
%21 = load i32*, i32** %12
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = load i32, i32* @EIO
store i32 %24, i32* %6
br label %71
25: ; preds = %19
%26 = call i32 @memset(%struct.flock* %13, i32 0, i32 24)
%27 = load i32, i32* @F_RDLCK
%28 = getelementptr inbounds %struct.flock, %struct.flock* %13, i32 0, i32 3
store i32 %27, i32* %28
%29 = load i32, i32* @SEEK_SET
%30 = getelementptr inbounds %struct.flock, %struct.flock* %13, i32 0, i32 2
store i32 %29, i32* %30
%31 = getelementptr inbounds %struct.flock, %struct.flock* %13, i32 0, i32 1
store i64 0, i64* %31
%32 = getelementptr inbounds %struct.flock, %struct.flock* %13, i32 0, i32 0
store i64 0, i64* %32
br label %33
33: ; preds = %45, %25
%34 = load i32*, i32** %12
%35 = call i32 @fileno(i32* %34)
%36 = load i32, i32* @F_SETLKW
%37 = call i32 @fcntl(i32 %35, i32 %36, %struct.flock* %13)
store i32 %37, i32* %14
br label %38
38: ; preds = %33
%39 = load i32, i32* %14
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %38
%42 = load i64, i64* @errno
%43 = load i64, i64* @EINTR
%44 = icmp eq i64 %42, %43
br label %45
45: ; preds = %41, %38
%46 = phi i1 [ false, %38 ], [ %44, %41 ]
br i1 %46, label %33, label %47
47: ; preds = %45
br label %48
48: ; preds = %66, %47
%49 = load i32*, i32** %12
%50 = load %struct.passwd*, %struct.passwd** %8
%51 = load i8*, i8** %9
%52 = call i32 @__pwnextent(i32* %49, %struct.passwd* %50, i8* %51)
%53 = icmp ne i32 %52, -1
br i1 %53, label %54, label %67
54: ; preds = %48
%55 = load %struct.passwd*, %struct.passwd** %8
%56 = getelementptr inbounds %struct.passwd, %struct.passwd* %55, i32 0, i32 0
%57 = load i32, i32* %56
%58 = load i8*, i8** %7
%59 = call i64 @strcmp(i32 %57, i8* %58)
%60 = icmp eq i64 %59, 0
br i1 %60, label %61, label %66
61: ; preds = %54
%62 = load %struct.passwd*, %struct.passwd** %8
%63 = load %struct.passwd**, %struct.passwd*** %11
store %struct.passwd* %62, %struct.passwd** %63
%64 = load i32*, i32** %12
%65 = call i32 @fclose(i32* %64)
store i32 0, i32* %6
br label %71
66: ; preds = %54
br label %48
67: ; preds = %48
%68 = load i32*, i32** %12
%69 = call i32 @fclose(i32* %68)
%70 = load %struct.passwd**, %struct.passwd*** %11
store %struct.passwd* null, %struct.passwd** %70
store i32 0, i32* %6
br label %71
71: ; preds = %67, %61, %23, %17
%72 = load i32, i32* %6
ret i32 %72
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
%struct.flock = type { i64, i64, i32, i32 }
@ERANGE = global i32 0
@.str = [12 x i8] c"/etc/passwd\00"
@.str.1 = [2 x i8] c"r\00"
@EIO = global i32 0
@F_RDLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLKW = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @getpwnam_r(i8* %0, %struct.passwd* %1, i8* %2, i64 %3, %struct.passwd** nocapture writeonly %4) {
%6 = alloca %struct.flock
%7 = icmp ult i64 %3, 4096
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = load i32, i32* @ERANGE
br label %46
10: ; preds = %5
%11 = 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))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = load i32, i32* @EIO
br label %46
15: ; preds = %10
%16 = bitcast %struct.flock* %6 to i8*
%17 = call i32 @memset(%struct.flock* %6, i32 0, i32 24)
%18 = load i32, i32* @F_RDLCK
%19 = getelementptr inbounds %struct.flock, %struct.flock* %6, i64 0, i32 3
store i32 %18, i32* %19
%20 = load i32, i32* @SEEK_SET
%21 = getelementptr inbounds %struct.flock, %struct.flock* %6, i64 0, i32 2
store i32 %20, i32* %21
%22 = bitcast %struct.flock* %6 to i8*
br label %23
23: ; preds = %23, %15
%24 = call i32 @fileno(i32* %11)
%25 = load i32, i32* @F_SETLKW
%26 = call i32 @fcntl(i32 %24, i32 %25, %struct.flock* %6)
%27 = icmp ne i32 %26, 0
%28 = load i64, i64* @errno
%29 = load i64, i64* @EINTR
%30 = icmp eq i64 %28, %29
%31 = select i1 %27, i1 %30, i1 false
br i1 %31, label %23, label %32
32: ; preds = %23
%33 = getelementptr inbounds %struct.passwd, %struct.passwd* %1, i64 0, i32 0
br label %34
34: ; preds = %32, %37
%35 = call i32 @__pwnextent(i32* %11, %struct.passwd* %1, i8* %2)
%36 = icmp eq i32 %35, -1
br i1 %36, label %43, label %37
37: ; preds = %34
%38 = load i32, i32* %33
%39 = call i64 @strcmp(i32 %38, i8* %0)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %34
41: ; preds = %37
store %struct.passwd* %1, %struct.passwd** %4
%42 = call i32 @fclose(i32* %11)
br label %45
43: ; preds = %34
%44 = call i32 @fclose(i32* %11)
store %struct.passwd* null, %struct.passwd** %4
br label %45
45: ; preds = %43, %41
br label %46
46: ; preds = %13, %45, %8
%47 = phi i32 [ %9, %8 ], [ %14, %13 ], [ 0, %45 ]
ret i32 %47
}