#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ parsePasswdLine(char *, struct passwd *);
int /*<<< orphan*/ strcmp(int /*<<< orphan*/, char const *);
int getpwnam_r(const char *name, struct passwd *pwd, char *buf, size_t buflen,
               struct passwd **result) {
  FILE *passwdFile = fopen("/etc/passwd", "r");
  if (!passwdFile) {
    *result = NULL;
    return -1;
  }
  char *line = buf;
  while (1) {
    if (!fgets(line, buflen, passwdFile)) {
      break;
    }
    parsePasswdLine(line, pwd);
    if (!strcmp(pwd->pw_name, name)) {
      *result = pwd;
      fclose(passwdFile);
      return 0;
    }
  }
  *result = NULL;
  fclose(passwdFile);
  return -1;
}


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   1070 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11a5 <getpwnam_r+0x55>
mov    -0x30(%rbp),%rax
movq   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1231 <getpwnam_r+0xe1>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1040 <fgets@plt>
cmp    $0x0,%eax
jne    11cc <getpwnam_r+0x7c>
jmp    1216 <getpwnam_r+0xc6>
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <parsePasswdLine@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1211 <getpwnam_r+0xc1>
mov    -0x18(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    1231 <getpwnam_r+0xe1>
jmp    11ad <getpwnam_r+0x5d>
mov    -0x30(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-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,%rbx
mov    %rsi,%r12
mov    %rdi,%r13
lea    0xe8f(%rip),%rdi
lea    0xe94(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11cc <getpwnam_r+0x7c>
mov    %rax,%rbp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %rbp,%rdx
call   1040 <fgets@plt>
test   %eax,%eax
je     11d5 <getpwnam_r+0x85>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1050 <parsePasswdLine@plt>
mov    (%r12),%edi
mov    %r13,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
jne    1190 <getpwnam_r+0x40>
mov    %r12,(%r14)
mov    %rbp,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    11e9 <getpwnam_r+0x99>
movq   $0x0,(%r14)
jmp    11e4 <getpwnam_r+0x94>
movq   $0x0,(%r14)
mov    %rbp,%rdi
call   1030 <fclose@plt>
mov    $0xffffffff,%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"
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 i8*
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
%14 = 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* %14, i32** %12
%15 = load i32*, i32** %12
%16 = icmp ne i32* %15, null
br i1 %16, label %19, label %17
17: ; preds = %5
%18 = load %struct.passwd**, %struct.passwd*** %11
store %struct.passwd* null, %struct.passwd** %18
store i32 -1, i32* %6
br label %48
19: ; preds = %5
%20 = load i8*, i8** %9
store i8* %20, i8** %13
br label %21
21: ; preds = %19, %43
%22 = load i8*, i8** %13
%23 = load i64, i64* %10
%24 = load i32*, i32** %12
%25 = call i32 @fgets(i8* %22, i64 %23, i32* %24)
%26 = icmp ne i32 %25, 0
br i1 %26, label %28, label %27
27: ; preds = %21
br label %44
28: ; preds = %21
%29 = load i8*, i8** %13
%30 = load %struct.passwd*, %struct.passwd** %8
%31 = call i32 @parsePasswdLine(i8* %29, %struct.passwd* %30)
%32 = load %struct.passwd*, %struct.passwd** %8
%33 = getelementptr inbounds %struct.passwd, %struct.passwd* %32, i32 0, i32 0
%34 = load i32, i32* %33
%35 = load i8*, i8** %7
%36 = call i32 @strcmp(i32 %34, i8* %35)
%37 = icmp ne i32 %36, 0
br i1 %37, label %43, label %38
38: ; preds = %28
%39 = load %struct.passwd*, %struct.passwd** %8
%40 = load %struct.passwd**, %struct.passwd*** %11
store %struct.passwd* %39, %struct.passwd** %40
%41 = load i32*, i32** %12
%42 = call i32 @fclose(i32* %41)
store i32 0, i32* %6
br label %48
43: ; preds = %28
br label %21
44: ; preds = %27
%45 = load %struct.passwd**, %struct.passwd*** %11
store %struct.passwd* null, %struct.passwd** %45
%46 = load i32*, i32** %12
%47 = call i32 @fclose(i32* %46)
store i32 -1, i32* %6
br label %48
48: ; preds = %44, %38, %17
%49 = load i32, i32* %6
ret i32 %49
}

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