#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*/ *);
struct passwd *fgetpwent(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
char *getenv(char *);
scalar_t__ strcmp(int /*<<< orphan*/, char const *);
struct passwd *getpwnam(const char *name) {
  const char *path = getenv("MOCK_PASSWD");
  if (!path)
    return NULL;
  FILE *stream = fopen(path, "r");
  if (!stream)
    return NULL;
  struct passwd *entry;
  while ((entry = fgetpwent(stream))) {
    if (strcmp(entry->pw_name, name) == 0) {
      fclose(stream);
      return entry;
    }
  }
  fclose(stream);
  return NULL;
}


x86 assembly (clang, -O0):
<getpwnam>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe9d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1184 <getpwnam+0x34>
movq   $0x0,-0x8(%rbp)
jmp    1211 <getpwnam+0xc1>
mov    -0x18(%rbp),%rdi
lea    0xe7d(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b0 <getpwnam+0x60>
movq   $0x0,-0x8(%rbp)
jmp    1211 <getpwnam+0xc1>
jmp    11b5 <getpwnam+0x65>
mov    -0x20(%rbp),%rdi
call   1060 <fgetpwent@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1200 <getpwnam+0xb0>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    11fb <getpwnam+0xab>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1211 <getpwnam+0xc1>
jmp    11b5 <getpwnam+0x65>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getpwnam>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
lea    0xea1(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11a1 <getpwnam+0x51>
lea    0xe9c(%rip),%rsi
mov    %rax,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11a1 <getpwnam+0x51>
mov    %rax,%r15
mov    %r15,%rdi
call   1060 <fgetpwent@plt>
test   %rax,%rax
je     11a5 <getpwnam+0x55>
mov    %rax,%rbx
mov    (%rax),%edi
mov    %r14,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    1180 <getpwnam+0x30>
jmp    11a7 <getpwnam+0x57>
xor    %ebx,%ebx
jmp    11af <getpwnam+0x5f>
xor    %ebx,%ebx
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.passwd = type { i32 }
@.str = [12 x i8] c"MOCK_PASSWD\00"
@.str.1 = [2 x i8] c"r\00"
define %struct.passwd* @getpwnam(i8* %0) {
%2 = alloca %struct.passwd*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca %struct.passwd*
store i8* %0, i8** %3
%7 = call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
br i1 %9, label %11, label %10
10: ; preds = %1
store %struct.passwd* null, %struct.passwd** %2
br label %37
11: ; preds = %1
%12 = load i8*, i8** %4
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %5
%14 = load i32*, i32** %5
%15 = icmp ne i32* %14, null
br i1 %15, label %17, label %16
16: ; preds = %11
store %struct.passwd* null, %struct.passwd** %2
br label %37
17: ; preds = %11
br label %18
18: ; preds = %33, %17
%19 = load i32*, i32** %5
%20 = call %struct.passwd* @fgetpwent(i32* %19)
store %struct.passwd* %20, %struct.passwd** %6
%21 = icmp ne %struct.passwd* %20, null
br i1 %21, label %22, label %34
22: ; preds = %18
%23 = load %struct.passwd*, %struct.passwd** %6
%24 = getelementptr inbounds %struct.passwd, %struct.passwd* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = load i8*, i8** %3
%27 = call i64 @strcmp(i32 %25, i8* %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %22
%30 = load i32*, i32** %5
%31 = call i32 @fclose(i32* %30)
%32 = load %struct.passwd*, %struct.passwd** %6
store %struct.passwd* %32, %struct.passwd** %2
br label %37
33: ; preds = %22
br label %18
34: ; preds = %18
%35 = load i32*, i32** %5
%36 = call i32 @fclose(i32* %35)
store %struct.passwd* null, %struct.passwd** %2
br label %37
37: ; preds = %34, %29, %16, %10
%38 = load %struct.passwd*, %struct.passwd** %2
ret %struct.passwd* %38
}

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