#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 */
typedef scalar_t__ uid_t;
struct passwd {
  scalar_t__ pw_uid;
};
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 *);
struct passwd *getpwuid(uid_t uid) {
  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 (entry->pw_uid == uid) {
      fclose(stream);
      return entry;
    }
  }
  fclose(stream);
  return NULL;
}


x86 assembly (clang, -O0):
<getpwuid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xead(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1174 <getpwuid+0x34>
movq   $0x0,-0x8(%rbp)
jmp    11f9 <getpwuid+0xb9>
mov    -0x18(%rbp),%rdi
lea    0xe8d(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a0 <getpwuid+0x60>
movq   $0x0,-0x8(%rbp)
jmp    11f9 <getpwuid+0xb9>
jmp    11a5 <getpwuid+0x65>
mov    -0x20(%rbp),%rdi
call   1050 <fgetpwent@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     11e8 <getpwuid+0xa8>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
cmp    -0x10(%rbp),%rax
jne    11e3 <getpwuid+0xa3>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11f9 <getpwuid+0xb9>
jmp    11a5 <getpwuid+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):
<getpwuid>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
lea    0xeb1(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1187 <getpwuid+0x47>
lea    0xeac(%rip),%rsi
mov    %rax,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     1187 <getpwuid+0x47>
mov    %rax,%r15
mov    %r15,%rdi
call   1050 <fgetpwent@plt>
test   %rax,%rax
je     118b <getpwuid+0x4b>
mov    %rax,%rbx
cmp    %r14,(%rax)
jne    1170 <getpwuid+0x30>
jmp    118d <getpwuid+0x4d>
xor    %ebx,%ebx
jmp    1195 <getpwuid+0x55>
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 { i64 }
@.str = [12 x i8] c"MOCK_PASSWD\00"
@.str.1 = [2 x i8] c"r\00"
define %struct.passwd* @getpwuid(i64 %0) {
%2 = alloca %struct.passwd*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca %struct.passwd*
store i64 %0, i64* %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 %36
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 %36
17: ; preds = %11
br label %18
18: ; preds = %32, %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 %33
22: ; preds = %18
%23 = load %struct.passwd*, %struct.passwd** %6
%24 = getelementptr inbounds %struct.passwd, %struct.passwd* %23, i32 0, i32 0
%25 = load i64, i64* %24
%26 = load i64, i64* %3
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %32
28: ; preds = %22
%29 = load i32*, i32** %5
%30 = call i32 @fclose(i32* %29)
%31 = load %struct.passwd*, %struct.passwd** %6
store %struct.passwd* %31, %struct.passwd** %2
br label %36
32: ; preds = %22
br label %18
33: ; preds = %18
%34 = load i32*, i32** %5
%35 = call i32 @fclose(i32* %34)
store %struct.passwd* null, %struct.passwd** %2
br label %36
36: ; preds = %33, %28, %16, %10
%37 = load %struct.passwd*, %struct.passwd** %2
ret %struct.passwd* %37
}

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