#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ *fopen(char *, char *);
char *getenv(char *);
struct passwd *getpwnam(char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int file_openfile(char *filename, int rwmode, FILE **diskfile) {
  char mode[4];
  char tempname[512], *cptr, user[80];
  struct passwd *pwd;
  int ii = 0;
  if (rwmode == 1) {
    strcpy(mode, "r+b");
  } else {
    strcpy(mode, "rb");
  }
  if (*filename == '~') {
    if (filename[1] == '/') {
      cptr = getenv("HOME");
      if (cptr) {
        strcpy(tempname, cptr);
        strcat(tempname, filename + 1);
      } else {
        strcpy(tempname, filename);
      }
    } else {
      cptr = filename + 1;
      while (*cptr && (*cptr != '/')) {
        user[ii] = *cptr;
        cptr++;
        ii++;
      }
      user[ii] = '\0';
      pwd = getpwnam(user);
      strcpy(tempname, pwd->pw_dir);
      strcat(tempname, cptr);
    }
    *diskfile = fopen(tempname, mode);
  } else {
    *diskfile = fopen(filename, mode);
  }
  if (!(*diskfile)) {
    return (104);
  }
  return (0);
}


x86 assembly (clang, -O0):
<file_openfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2a0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x29c(%rbp)
cmpl   $0x1,-0x14(%rbp)
jne    118f <file_openfile+0x3f>
lea    -0x24(%rbp),%rdi
lea    0xe7b(%rip),%rsi
call   1040 <strcpy@plt>
jmp    119f <file_openfile+0x4f>
lea    -0x24(%rbp),%rdi
lea    0xe6a(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7e,%eax
jne    131c <file_openfile+0x1cc>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x2f,%eax
jne    1222 <file_openfile+0xd2>
lea    0xe40(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x238(%rbp)
cmpq   $0x0,-0x238(%rbp)
je     120d <file_openfile+0xbd>
lea    -0x230(%rbp),%rdi
mov    -0x238(%rbp),%rsi
call   1040 <strcpy@plt>
lea    -0x230(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x1,%rsi
call   1070 <strcat@plt>
jmp    121d <file_openfile+0xcd>
lea    -0x230(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
jmp    12fd <file_openfile+0x1ad>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x238(%rbp)
mov    -0x238(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x29d(%rbp)
je     1262 <file_openfile+0x112>
mov    -0x238(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
setne  %al
mov    %al,-0x29d(%rbp)
mov    -0x29d(%rbp),%al
test   $0x1,%al
jne    1275 <file_openfile+0x125>
jmp    12b2 <file_openfile+0x162>
mov    -0x238(%rbp),%rax
mov    (%rax),%cl
movslq -0x29c(%rbp),%rax
mov    %cl,-0x290(%rbp,%rax,1)
mov    -0x238(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x238(%rbp)
mov    -0x29c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x29c(%rbp)
jmp    1231 <file_openfile+0xe1>
movslq -0x29c(%rbp),%rax
movb   $0x0,-0x290(%rbp,%rax,1)
lea    -0x290(%rbp),%rdi
call   1050 <getpwnam@plt>
mov    %rax,-0x298(%rbp)
lea    -0x230(%rbp),%rdi
mov    -0x298(%rbp),%rax
mov    (%rax),%rsi
call   1040 <strcpy@plt>
lea    -0x230(%rbp),%rdi
mov    -0x238(%rbp),%rsi
call   1070 <strcat@plt>
lea    -0x230(%rbp),%rdi
lea    -0x24(%rbp),%rsi
call   1060 <fopen@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
jmp    1333 <file_openfile+0x1e3>
mov    -0x10(%rbp),%rdi
lea    -0x24(%rbp),%rsi
call   1060 <fopen@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jne    134d <file_openfile+0x1fd>
movl   $0x68,-0x4(%rbp)
jmp    1354 <file_openfile+0x204>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2a0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_openfile>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x268,%rsp
mov    %rdx,%r14
mov    %rdi,%r12
cmp    $0x1,%esi
lea    0xe92(%rip),%rax
lea    0xe8f(%rip),%rsi
cmove  %rax,%rsi
lea    0xc(%rsp),%rdi
call   1040 <strcpy@plt>
cmpb   $0x7e,(%r12)
jne    122a <file_openfile+0xda>
lea    0x1(%r12),%r15
mov    0x1(%r12),%al
cmp    $0x2f,%al
jne    11bf <file_openfile+0x6f>
lea    0xe64(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1217 <file_openfile+0xc7>
lea    0x60(%rsp),%r12
mov    %r12,%rdi
mov    %rax,%rsi
call   1040 <strcpy@plt>
jmp    120a <file_openfile+0xba>
xor    %ebx,%ebx
test   %al,%al
je     11e6 <file_openfile+0x96>
cs nopw 0x0(%rax,%rax,1)
nop
cmp    $0x2f,%al
je     11e6 <file_openfile+0x96>
mov    %al,0x10(%rsp,%rbx,1)
movzbl 0x2(%r12,%rbx,1),%eax
add    $0x1,%rbx
test   %al,%al
jne    11d0 <file_openfile+0x80>
lea    0x60(%rsp),%r12
mov    %ebx,%eax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0x10(%rsp),%rdi
call   1050 <getpwnam@plt>
mov    (%rax),%rsi
mov    %r12,%rdi
call   1040 <strcpy@plt>
add    %rbx,%r15
mov    %r12,%rdi
mov    %r15,%rsi
call   1070 <strcat@plt>
jmp    122a <file_openfile+0xda>
lea    0x60(%rsp),%r15
mov    %r15,%rdi
mov    %r12,%rsi
call   1040 <strcpy@plt>
mov    %r15,%r12
lea    0xc(%rsp),%rsi
mov    %r12,%rdi
call   1060 <fopen@plt>
mov    %rax,(%r14)
xor    %ecx,%ecx
test   %rax,%rax
mov    $0x68,%eax
cmovne %ecx,%eax
add    $0x268,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.passwd = type { i8* }
@.str = [4 x i8] c"r+b\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [5 x i8] c"HOME\00"
define i32 @file_openfile(i8* %0, i32 %1, i32** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32**
%8 = alloca [4 x i8]
%9 = alloca [512 x i8]
%10 = alloca i8*
%11 = alloca [80 x i8]
%12 = alloca %struct.passwd*
%13 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32** %2, i32*** %7
store i32 0, i32* %13
%14 = load i32, i32* %6
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %19
16: ; preds = %3
%17 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 0
%18 = call i32 @strcpy(i8* %17, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %22
19: ; preds = %3
%20 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 0
%21 = call i32 @strcpy(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %19, %16
%23 = load i8*, i8** %5
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 126
br i1 %26, label %27, label %94
27: ; preds = %22
%28 = load i8*, i8** %5
%29 = getelementptr inbounds i8, i8* %28, i64 1
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 47
br i1 %32, label %33, label %50
33: ; preds = %27
%34 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i8* %34, i8** %10
%35 = load i8*, i8** %10
%36 = icmp ne i8* %35, null
br i1 %36, label %37, label %45
37: ; preds = %33
%38 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%39 = load i8*, i8** %10
%40 = call i32 @strcpy(i8* %38, i8* %39)
%41 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%42 = load i8*, i8** %5
%43 = getelementptr inbounds i8, i8* %42, i64 1
%44 = call i32 @strcat(i8* %41, i8* %43)
br label %49
45: ; preds = %33
%46 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%47 = load i8*, i8** %5
%48 = call i32 @strcpy(i8* %46, i8* %47)
br label %49
49: ; preds = %45, %37
br label %89
50: ; preds = %27
%51 = load i8*, i8** %5
%52 = getelementptr inbounds i8, i8* %51, i64 1
store i8* %52, i8** %10
br label %53
53: ; preds = %65, %50
%54 = load i8*, i8** %10
%55 = load i8, i8* %54
%56 = sext i8 %55 to i32
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %63
58: ; preds = %53
%59 = load i8*, i8** %10
%60 = load i8, i8* %59
%61 = sext i8 %60 to i32
%62 = icmp ne i32 %61, 47
br label %63
63: ; preds = %58, %53
%64 = phi i1 [ false, %53 ], [ %62, %58 ]
br i1 %64, label %65, label %75
65: ; preds = %63
%66 = load i8*, i8** %10
%67 = load i8, i8* %66
%68 = load i32, i32* %13
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds [80 x i8], [80 x i8]* %11, i64 0, i64 %69
store i8 %67, i8* %70
%71 = load i8*, i8** %10
%72 = getelementptr inbounds i8, i8* %71, i32 1
store i8* %72, i8** %10
%73 = load i32, i32* %13
%74 = add nsw i32 %73, 1
store i32 %74, i32* %13
br label %53
75: ; preds = %63
%76 = load i32, i32* %13
%77 = sext i32 %76 to i64
%78 = getelementptr inbounds [80 x i8], [80 x i8]* %11, i64 0, i64 %77
store i8 0, i8* %78
%79 = getelementptr inbounds [80 x i8], [80 x i8]* %11, i64 0, i64 0
%80 = call %struct.passwd* @getpwnam(i8* %79)
store %struct.passwd* %80, %struct.passwd** %12
%81 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%82 = load %struct.passwd*, %struct.passwd** %12
%83 = getelementptr inbounds %struct.passwd, %struct.passwd* %82, i32 0, i32 0
%84 = load i8*, i8** %83
%85 = call i32 @strcpy(i8* %81, i8* %84)
%86 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%87 = load i8*, i8** %10
%88 = call i32 @strcat(i8* %86, i8* %87)
br label %89
89: ; preds = %75, %49
%90 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%91 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 0
%92 = call i32* @fopen(i8* %90, i8* %91)
%93 = load i32**, i32*** %7
store i32* %92, i32** %93
br label %99
94: ; preds = %22
%95 = load i8*, i8** %5
%96 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 0
%97 = call i32* @fopen(i8* %95, i8* %96)
%98 = load i32**, i32*** %7
store i32* %97, i32** %98
br label %99
99: ; preds = %94, %89
%100 = load i32**, i32*** %7
%101 = load i32*, i32** %100
%102 = icmp ne i32* %101, null
br i1 %102, label %104, label %103
103: ; preds = %99
store i32 104, i32* %4
br label %105
104: ; preds = %99
store i32 0, i32* %4
br label %105
105: ; preds = %104, %103
%106 = load i32, i32* %4
ret i32 %106
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
@.str = [4 x i8] c"r+b\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [5 x i8] c"HOME\00"
define i32 @file_openfile(i8* %0, i32 %1, i32** nocapture writeonly %2) {
%4 = alloca [4 x i8]
%5 = alloca [512 x i8]
%6 = alloca [80 x i8]
%7 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%9 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%10 = icmp eq i32 %1, 1
%11 = select i1 %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0)
%12 = call i32 @strcpy(i8* %7, i8* %11)
%13 = load i8, i8* %0
%14 = icmp eq i8 %13, 126
br i1 %14, label %15, label %44
15: ; preds = %3
%16 = getelementptr inbounds i8, i8* %0, i64 1
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 47
br i1 %18, label %19, label %27
19: ; preds = %15
%20 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%21 = icmp eq i8* %20, null
br i1 %21, label %25, label %22
22: ; preds = %19
%23 = call i32 @strcpy(i8* %8, i8* %20)
%24 = call i32 @strcat(i8* %8, i8* %16)
br label %44
25: ; preds = %19
%26 = call i32 @strcpy(i8* %8, i8* %0)
br label %44
27: ; preds = %15, %31
%28 = phi i64 [ %34, %31 ], [ 0, %15 ]
%29 = phi i8 [ %35, %31 ], [ %17, %15 ]
%30 = phi i8* [ %33, %31 ], [ %16, %15 ]
switch i8 %29, label %31 [
i8 0, label %36
i8 47, label %36
]
31: ; preds = %27
%32 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 %28
store i8 %29, i8* %32
%33 = getelementptr inbounds i8, i8* %30, i64 1
%34 = add nuw i64 %28, 1
%35 = load i8, i8* %33
br label %27
36: ; preds = %27, %27
%37 = and i64 %28, 4294967295
%38 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 %37
store i8 0, i8* %38
%39 = call %struct.passwd* @getpwnam(i8* %9)
%40 = getelementptr inbounds %struct.passwd, %struct.passwd* %39, i64 0, i32 0
%41 = load i8*, i8** %40
%42 = call i32 @strcpy(i8* %8, i8* %41)
%43 = call i32 @strcat(i8* %8, i8* %30)
br label %44
44: ; preds = %3, %36, %25, %22
%45 = phi i8* [ %8, %22 ], [ %8, %25 ], [ %8, %36 ], [ %0, %3 ]
%46 = call i32* @fopen(i8* %45, i8* %7)
store i32* %46, i32** %2
%47 = icmp eq i32* %46, null
%48 = select i1 %47, i32 104, i32 0
ret i32 %48
}