#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*/ *);
int /*<<< orphan*/ fgets(char *, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ parsePasswdLine(char *, struct passwd *);
int getpwuid_r(uid_t uid, 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 (pwd->pw_uid == uid) {
      *result = pwd;
      fclose(passwdFile);
      return 0;
    }
  }
  *result = NULL;
  fclose(passwdFile);
  return -1;
}


x86 assembly (clang, -O0):
<getpwuid_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    0xe9d(%rip),%rdi
lea    0xea2(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1195 <getpwuid_r+0x55>
mov    -0x30(%rbp),%rax
movq   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    121a <getpwuid_r+0xda>
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    11bc <getpwuid_r+0x7c>
jmp    11ff <getpwuid_r+0xbf>
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <parsePasswdLine@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
cmp    -0x10(%rbp),%rax
jne    11fa <getpwuid_r+0xba>
mov    -0x18(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    121a <getpwuid_r+0xda>
jmp    119d <getpwuid_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):
<getpwuid_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    0xe9f(%rip),%rdi
lea    0xea4(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11b2 <getpwuid_r+0x72>
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     11bb <getpwuid_r+0x7b>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1050 <parsePasswdLine@plt>
cmp    %r13,(%r12)
jne    1180 <getpwuid_r+0x40>
mov    %r12,(%r14)
mov    %rbp,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    11cf <getpwuid_r+0x8f>
movq   $0x0,(%r14)
jmp    11ca <getpwuid_r+0x8a>
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 { i64 }
@.str = [12 x i8] c"/etc/passwd\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @getpwuid_r(i64 %0, %struct.passwd* %1, i8* %2, i64 %3, %struct.passwd** %4) {
%6 = alloca i32
%7 = alloca i64
%8 = alloca %struct.passwd*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca %struct.passwd**
%12 = alloca i32*
%13 = alloca i8*
store i64 %0, i64* %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 %47
19: ; preds = %5
%20 = load i8*, i8** %9
store i8* %20, i8** %13
br label %21
21: ; preds = %19, %42
%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 %43
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 i64, i64* %33
%35 = load i64, i64* %7
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %42
37: ; preds = %28
%38 = load %struct.passwd*, %struct.passwd** %8
%39 = load %struct.passwd**, %struct.passwd*** %11
store %struct.passwd* %38, %struct.passwd** %39
%40 = load i32*, i32** %12
%41 = call i32 @fclose(i32* %40)
store i32 0, i32* %6
br label %47
42: ; preds = %28
br label %21
43: ; preds = %27
%44 = load %struct.passwd**, %struct.passwd*** %11
store %struct.passwd* null, %struct.passwd** %44
%45 = load i32*, i32** %12
%46 = call i32 @fclose(i32* %45)
store i32 -1, i32* %6
br label %47
47: ; preds = %43, %37, %17
%48 = load i32, i32* %6
ret i32 %48
}

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