#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 {
  char *pw_dir;
};
typedef int /*<<< orphan*/ pwbuffer;
typedef int /*<<< orphan*/ buffer;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ F_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ getpwnam_r(char const *, struct passwd *, char *, int,
                              struct passwd **);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char const *);
FILE *open_file(const char *user) {
  char buffer[512];
  char pwbuffer[1024];
  struct passwd pw;
  struct passwd *pwp;
  snprintf(buffer, sizeof(buffer), "%s%s", "/etc/pam_totp/", user);
  if (access(buffer, F_OK) == 0) {
    return fopen(buffer, "r");
  }
  getpwnam_r(user, &pw, pwbuffer, sizeof(pwbuffer), &pwp);
  if (!pwp)
    return NULL;
  snprintf(buffer, sizeof(buffer), "%s%s", pw.pw_dir, "/.pam_totp");
  if (access(buffer, F_OK) == 0) {
    return fopen(buffer, "r");
  }
  printf("Could not find a file\n");
  return NULL;
}


x86 assembly (clang, -O0):
<open_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x620,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x210(%rbp),%rdi
mov    -0x10(%rbp),%r8
mov    $0x200,%esi
lea    0xe8a(%rip),%rdx
lea    0xe88(%rip),%rcx
call   1040 <snprintf@plt>
lea    -0x210(%rbp),%rdi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
call   1060 <access@plt>
cmp    $0x0,%rax
jne    11bd <open_file+0x6d>
lea    -0x210(%rbp),%rdi
lea    0xe65(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x8(%rbp)
jmp    1270 <open_file+0x120>
mov    -0x10(%rbp),%rdi
lea    -0x610(%rbp),%rdx
lea    -0x618(%rbp),%rsi
mov    $0x400,%ecx
lea    -0x620(%rbp),%r8
call   1050 <getpwnam_r@plt>
cmpq   $0x0,-0x620(%rbp)
jne    11fb <open_file+0xab>
movq   $0x0,-0x8(%rbp)
jmp    1270 <open_file+0x120>
lea    -0x210(%rbp),%rdi
mov    -0x618(%rbp),%rcx
mov    $0x200,%esi
lea    0xdeb(%rip),%rdx
lea    0xdfa(%rip),%r8
call   1040 <snprintf@plt>
lea    -0x210(%rbp),%rdi
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
call   1060 <access@plt>
cmp    $0x0,%rax
jne    125c <open_file+0x10c>
lea    -0x210(%rbp),%rdi
lea    0xdc6(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x8(%rbp)
jmp    1270 <open_file+0x120>
lea    0xdbe(%rip),%rdi
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x620,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_file>:
push   %r15
push   %r14
push   %rbx
sub    $0x610,%rsp
mov    %rdi,%rbx
lea    0xe9a(%rip),%rdx
lea    0xe98(%rip),%rcx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x200,%esi
mov    %rbx,%r8
call   1040 <snprintf@plt>
mov    0x2e4f(%rip),%r15
mov    (%r15),%esi
mov    %r14,%rdi
call   1060 <access@plt>
test   %rax,%rax
je     1202 <open_file+0xb2>
lea    0x8(%rsp),%rsi
lea    0x210(%rsp),%rdx
mov    %rsp,%r8
mov    %rbx,%rdi
mov    $0x400,%ecx
call   1050 <getpwnam_r@plt>
cmpq   $0x0,(%rsp)
je     11fe <open_file+0xae>
mov    0x8(%rsp),%rcx
lea    0xe37(%rip),%rdx
lea    0xe46(%rip),%r8
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x200,%esi
call   1040 <snprintf@plt>
mov    (%r15),%esi
mov    %rbx,%rdi
call   1060 <access@plt>
test   %rax,%rax
je     1202 <open_file+0xb2>
lea    0xe28(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    1213 <open_file+0xc3>
lea    0xe0b(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1070 <fopen@plt>
add    $0x610,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.passwd = type { i8* }
@.str = [5 x i8] c"%s%s\00"
@.str.1 = [15 x i8] c"/etc/pam_totp/\00"
@F_OK = global i32 0
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [11 x i8] c"/.pam_totp\00"
@.str.4 = [23 x i8] c"Could not find a file\0A\00"
define i32* @open_file(i8* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca [512 x i8]
%5 = alloca [1024 x i8]
%6 = alloca %struct.passwd
%7 = alloca %struct.passwd*
store i8* %0, i8** %3
%8 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%9 = load i8*, i8** %3
%10 = call i32 @snprintf(i8* %8, i32 512, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%11 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%12 = load i32, i32* @F_OK
%13 = call i64 @access(i8* %11, i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %1
%16 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %17, i32** %2
br label %39
18: ; preds = %1
%19 = load i8*, i8** %3
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%21 = call i32 @getpwnam_r(i8* %19, %struct.passwd* %6, i8* %20, i32 1024, %struct.passwd** %7)
%22 = load %struct.passwd*, %struct.passwd** %7
%23 = icmp ne %struct.passwd* %22, null
br i1 %23, label %25, label %24
24: ; preds = %18
store i32* null, i32** %2
br label %39
25: ; preds = %18
%26 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%27 = getelementptr inbounds %struct.passwd, %struct.passwd* %6, i32 0, i32 0
%28 = load i8*, i8** %27
%29 = call i32 @snprintf(i8* %26, i32 512, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %28, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%30 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%31 = load i32, i32* @F_OK
%32 = call i64 @access(i8* %30, i32 %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %25
%35 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%36 = call i32* @fopen(i8* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %36, i32** %2
br label %39
37: ; preds = %25
%38 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
store i32* null, i32** %2
br label %39
39: ; preds = %37, %34, %24, %15
%40 = load i32*, i32** %2
ret i32* %40
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@.str = [5 x i8] c"%s%s\00"
@.str.1 = [15 x i8] c"/etc/pam_totp/\00"
@F_OK = global i32 0
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [11 x i8] c"/.pam_totp\00"
@str = [22 x i8] c"Could not find a file\00"
define noalias i32* @open_file(i8* %0) {
%2 = alloca [512 x i8]
%3 = alloca [1024 x i8]
%4 = alloca %struct.passwd
%5 = alloca %struct.passwd*
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%8 = bitcast %struct.passwd* %4 to i8*
%9 = bitcast %struct.passwd** %5 to i8*
%10 = call i32 @snprintf(i8* %6, i32 512, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%11 = load i32, i32* @F_OK
%12 = call i64 @access(i8* %6, i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %31
16: ; preds = %1
%17 = call i32 @getpwnam_r(i8* %0, %struct.passwd* %4, i8* %7, i32 1024, %struct.passwd** %5)
%18 = load %struct.passwd*, %struct.passwd** %5
%19 = icmp eq %struct.passwd* %18, null
br i1 %19, label %31, label %20
20: ; preds = %16
%21 = getelementptr inbounds %struct.passwd, %struct.passwd* %4, i64 0, i32 0
%22 = load i8*, i8** %21
%23 = call i32 @snprintf(i8* %6, i32 512, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %22, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%24 = load i32, i32* @F_OK
%25 = call i64 @access(i8* %6, i32 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %20
%28 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %31
29: ; preds = %20
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %31
31: ; preds = %16, %29, %27, %14
%32 = phi i32* [ %15, %14 ], [ %28, %27 ], [ null, %29 ], [ null, %16 ]
ret i32* %32
}