#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 strlen(char *);
int file_openfile(char *filename, int rwmode, FILE **diskfile) {
  char mode[4];
  char tempname[1024], *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) {
        if (strlen(cptr) + strlen(filename + 1) > 1023)
          return (104);
        strcpy(tempname, cptr);
        strcat(tempname, filename + 1);
      } else {
        if (strlen(filename) > 1023)
          return (104);
        strcpy(tempname, filename);
      }
    } else {
      cptr = filename + 1;
      while (*cptr && (*cptr != '/')) {
        user[ii] = *cptr;
        cptr++;
        ii++;
      }
      user[ii] = '\0';
      pwd = getpwnam(user);
      if (strlen(pwd->pw_dir) + strlen(cptr) > 1023)
        return (104);
      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    $0x4b0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x49c(%rbp)
cmpl   $0x1,-0x14(%rbp)
jne    119f <file_openfile+0x3f>
lea    -0x24(%rbp),%rdi
lea    0xe6b(%rip),%rsi
call   1040 <strcpy@plt>
jmp    11af <file_openfile+0x4f>
lea    -0x24(%rbp),%rdi
lea    0xe5a(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7e,%eax
jne    13ce <file_openfile+0x26e>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x2f,%eax
jne    1292 <file_openfile+0x132>
lea    0xe30(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x438(%rbp)
cmpq   $0x0,-0x438(%rbp)
je     125d <file_openfile+0xfd>
mov    -0x438(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x4a0(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x4a0(%rbp),%eax
add    %ecx,%eax
cmp    $0x3ff,%eax
jle    1231 <file_openfile+0xd1>
movl   $0x68,-0x4(%rbp)
jmp    1406 <file_openfile+0x2a6>
lea    -0x430(%rbp),%rdi
mov    -0x438(%rbp),%rsi
call   1040 <strcpy@plt>
lea    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x1,%rsi
call   1080 <strcat@plt>
jmp    128d <file_openfile+0x12d>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x3ff,%eax
jle    127d <file_openfile+0x11d>
movl   $0x68,-0x4(%rbp)
jmp    1406 <file_openfile+0x2a6>
lea    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
jmp    13af <file_openfile+0x24f>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x438(%rbp)
mov    -0x438(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x4a1(%rbp)
je     12d2 <file_openfile+0x172>
mov    -0x438(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
setne  %al
mov    %al,-0x4a1(%rbp)
mov    -0x4a1(%rbp),%al
test   $0x1,%al
jne    12e5 <file_openfile+0x185>
jmp    1322 <file_openfile+0x1c2>
mov    -0x438(%rbp),%rax
mov    (%rax),%cl
movslq -0x49c(%rbp),%rax
mov    %cl,-0x490(%rbp,%rax,1)
mov    -0x438(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x438(%rbp)
mov    -0x49c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x49c(%rbp)
jmp    12a1 <file_openfile+0x141>
movslq -0x49c(%rbp),%rax
movb   $0x0,-0x490(%rbp,%rax,1)
lea    -0x490(%rbp),%rdi
call   1060 <getpwnam@plt>
mov    %rax,-0x498(%rbp)
mov    -0x498(%rbp),%rax
mov    (%rax),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x4a8(%rbp)
mov    -0x438(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x4a8(%rbp),%eax
add    %ecx,%eax
cmp    $0x3ff,%eax
jle    1386 <file_openfile+0x226>
movl   $0x68,-0x4(%rbp)
jmp    1406 <file_openfile+0x2a6>
lea    -0x430(%rbp),%rdi
mov    -0x498(%rbp),%rax
mov    (%rax),%rsi
call   1040 <strcpy@plt>
lea    -0x430(%rbp),%rdi
mov    -0x438(%rbp),%rsi
call   1080 <strcat@plt>
lea    -0x430(%rbp),%rdi
lea    -0x24(%rbp),%rsi
call   1070 <fopen@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
jmp    13e5 <file_openfile+0x285>
mov    -0x10(%rbp),%rdi
lea    -0x24(%rbp),%rsi
call   1070 <fopen@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jne    13ff <file_openfile+0x29f>
movl   $0x68,-0x4(%rbp)
jmp    1406 <file_openfile+0x2a6>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x4b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_openfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x468,%rsp
mov    %rdx,%r14
mov    %rdi,%r13
cmp    $0x1,%esi
lea    0xe7f(%rip),%rax
lea    0xe7c(%rip),%rsi
cmove  %rax,%rsi
lea    0xc(%rsp),%rdi
call   1040 <strcpy@plt>
cmpb   $0x7e,0x0(%r13)
jne    126c <file_openfile+0x10c>
lea    0x1(%r13),%r15
mov    0x1(%r13),%al
cmp    $0x2f,%al
jne    11f9 <file_openfile+0x99>
lea    0xe53(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     129b <file_openfile+0x13b>
mov    %rax,%r12
mov    %rax,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
add    %ebx,%ecx
mov    $0x68,%eax
cmp    $0x3ff,%ecx
jg     1289 <file_openfile+0x129>
lea    0x60(%rsp),%r13
mov    %r13,%rdi
mov    %r12,%rsi
jmp    125c <file_openfile+0xfc>
xor    %ebx,%ebx
test   %al,%al
je     1216 <file_openfile+0xb6>
nop
cmp    $0x2f,%al
je     1216 <file_openfile+0xb6>
mov    %al,0x10(%rsp,%rbx,1)
movzbl 0x2(%r13,%rbx,1),%eax
add    $0x1,%rbx
test   %al,%al
jne    1200 <file_openfile+0xa0>
mov    %ebx,%eax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0x10(%rsp),%rdi
call   1060 <getpwnam@plt>
mov    %rax,%r12
mov    (%rax),%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
add    %rbx,%r15
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
add    %ebp,%ecx
mov    $0x68,%eax
cmp    $0x3ff,%ecx
jg     1289 <file_openfile+0x129>
lea    0x60(%rsp),%r13
mov    (%r12),%rsi
mov    %r13,%rdi
call   1040 <strcpy@plt>
mov    %r13,%rdi
mov    %r15,%rsi
call   1080 <strcat@plt>
lea    0xc(%rsp),%rsi
mov    %r13,%rdi
call   1070 <fopen@plt>
mov    %rax,(%r14)
xor    %ecx,%ecx
test   %rax,%rax
mov    $0x68,%eax
cmovne %ecx,%eax
add    $0x468,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    $0x68,%eax
cmp    $0x3ff,%ecx
jg     1289 <file_openfile+0x129>
lea    0x60(%rsp),%r15
mov    %r15,%rdi
mov    %r13,%rsi
call   1040 <strcpy@plt>
mov    %r15,%r13
jmp    126c <file_openfile+0x10c>

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 [1024 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 %118
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 %64
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 %54
37: ; preds = %33
%38 = load i8*, i8** %10
%39 = call i32 @strlen(i8* %38)
%40 = load i8*, i8** %5
%41 = getelementptr inbounds i8, i8* %40, i64 1
%42 = call i32 @strlen(i8* %41)
%43 = add nsw i32 %39, %42
%44 = icmp sgt i32 %43, 1023
br i1 %44, label %45, label %46
45: ; preds = %37
store i32 104, i32* %4
br label %129
46: ; preds = %37
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%48 = load i8*, i8** %10
%49 = call i32 @strcpy(i8* %47, i8* %48)
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%51 = load i8*, i8** %5
%52 = getelementptr inbounds i8, i8* %51, i64 1
%53 = call i32 @strcat(i8* %50, i8* %52)
br label %63
54: ; preds = %33
%55 = load i8*, i8** %5
%56 = call i32 @strlen(i8* %55)
%57 = icmp sgt i32 %56, 1023
br i1 %57, label %58, label %59
58: ; preds = %54
store i32 104, i32* %4
br label %129
59: ; preds = %54
%60 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%61 = load i8*, i8** %5
%62 = call i32 @strcpy(i8* %60, i8* %61)
br label %63
63: ; preds = %59, %46
br label %113
64: ; preds = %27
%65 = load i8*, i8** %5
%66 = getelementptr inbounds i8, i8* %65, i64 1
store i8* %66, i8** %10
br label %67
67: ; preds = %79, %64
%68 = load i8*, i8** %10
%69 = load i8, i8* %68
%70 = sext i8 %69 to i32
%71 = icmp ne i32 %70, 0
br i1 %71, label %72, label %77
72: ; preds = %67
%73 = load i8*, i8** %10
%74 = load i8, i8* %73
%75 = sext i8 %74 to i32
%76 = icmp ne i32 %75, 47
br label %77
77: ; preds = %72, %67
%78 = phi i1 [ false, %67 ], [ %76, %72 ]
br i1 %78, label %79, label %89
79: ; preds = %77
%80 = load i8*, i8** %10
%81 = load i8, i8* %80
%82 = load i32, i32* %13
%83 = sext i32 %82 to i64
%84 = getelementptr inbounds [80 x i8], [80 x i8]* %11, i64 0, i64 %83
store i8 %81, i8* %84
%85 = load i8*, i8** %10
%86 = getelementptr inbounds i8, i8* %85, i32 1
store i8* %86, i8** %10
%87 = load i32, i32* %13
%88 = add nsw i32 %87, 1
store i32 %88, i32* %13
br label %67
89: ; preds = %77
%90 = load i32, i32* %13
%91 = sext i32 %90 to i64
%92 = getelementptr inbounds [80 x i8], [80 x i8]* %11, i64 0, i64 %91
store i8 0, i8* %92
%93 = getelementptr inbounds [80 x i8], [80 x i8]* %11, i64 0, i64 0
%94 = call %struct.passwd* @getpwnam(i8* %93)
store %struct.passwd* %94, %struct.passwd** %12
%95 = load %struct.passwd*, %struct.passwd** %12
%96 = getelementptr inbounds %struct.passwd, %struct.passwd* %95, i32 0, i32 0
%97 = load i8*, i8** %96
%98 = call i32 @strlen(i8* %97)
%99 = load i8*, i8** %10
%100 = call i32 @strlen(i8* %99)
%101 = add nsw i32 %98, %100
%102 = icmp sgt i32 %101, 1023
br i1 %102, label %103, label %104
103: ; preds = %89
store i32 104, i32* %4
br label %129
104: ; preds = %89
%105 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%106 = load %struct.passwd*, %struct.passwd** %12
%107 = getelementptr inbounds %struct.passwd, %struct.passwd* %106, i32 0, i32 0
%108 = load i8*, i8** %107
%109 = call i32 @strcpy(i8* %105, i8* %108)
%110 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%111 = load i8*, i8** %10
%112 = call i32 @strcat(i8* %110, i8* %111)
br label %113
113: ; preds = %104, %63
%114 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%115 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 0
%116 = call i32* @fopen(i8* %114, i8* %115)
%117 = load i32**, i32*** %7
store i32* %116, i32** %117
br label %123
118: ; preds = %22
%119 = load i8*, i8** %5
%120 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 0
%121 = call i32* @fopen(i8* %119, i8* %120)
%122 = load i32**, i32*** %7
store i32* %121, i32** %122
br label %123
123: ; preds = %118, %113
%124 = load i32**, i32*** %7
%125 = load i32*, i32** %124
%126 = icmp ne i32* %125, null
br i1 %126, label %128, label %127
127: ; preds = %123
store i32 104, i32* %4
br label %129
128: ; preds = %123
store i32 0, i32* %4
br label %129
129: ; preds = %128, %127, %103, %58, %45
%130 = load i32, i32* %4
ret i32 %130
}

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 [1024 x i8]
%6 = alloca [80 x i8]
%7 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [1024 x i8], [1024 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 %58
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 %35
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 %30, label %22
22: ; preds = %19
%23 = call i32 @strlen(i8* %20)
%24 = call i32 @strlen(i8* %16)
%25 = add nsw i32 %24, %23
%26 = icmp sgt i32 %25, 1023
br i1 %26, label %63, label %27
27: ; preds = %22
%28 = call i32 @strcpy(i8* %8, i8* %20)
%29 = call i32 @strcat(i8* %8, i8* %16)
br label %58
30: ; preds = %19
%31 = call i32 @strlen(i8* %0)
%32 = icmp sgt i32 %31, 1023
br i1 %32, label %63, label %33
33: ; preds = %30
%34 = call i32 @strcpy(i8* %8, i8* %0)
br label %58
35: ; preds = %15, %39
%36 = phi i64 [ %42, %39 ], [ 0, %15 ]
%37 = phi i8 [ %43, %39 ], [ %17, %15 ]
%38 = phi i8* [ %41, %39 ], [ %16, %15 ]
switch i8 %37, label %39 [
i8 0, label %44
i8 47, label %44
]
39: ; preds = %35
%40 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 %36
store i8 %37, i8* %40
%41 = getelementptr inbounds i8, i8* %38, i64 1
%42 = add nuw i64 %36, 1
%43 = load i8, i8* %41
br label %35
44: ; preds = %35, %35
%45 = and i64 %36, 4294967295
%46 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 %45
store i8 0, i8* %46
%47 = call %struct.passwd* @getpwnam(i8* %9)
%48 = getelementptr inbounds %struct.passwd, %struct.passwd* %47, i64 0, i32 0
%49 = load i8*, i8** %48
%50 = call i32 @strlen(i8* %49)
%51 = call i32 @strlen(i8* %38)
%52 = add nsw i32 %51, %50
%53 = icmp sgt i32 %52, 1023
br i1 %53, label %63, label %54
54: ; preds = %44
%55 = load i8*, i8** %48
%56 = call i32 @strcpy(i8* %8, i8* %55)
%57 = call i32 @strcat(i8* %8, i8* %38)
br label %58
58: ; preds = %3, %54, %33, %27
%59 = phi i8* [ %8, %27 ], [ %8, %33 ], [ %8, %54 ], [ %0, %3 ]
%60 = call i32* @fopen(i8* %59, i8* %7)
store i32* %60, i32** %2
%61 = icmp eq i32* %60, null
%62 = select i1 %61, i32 104, i32 0
br label %63
63: ; preds = %58, %44, %30, %22
%64 = phi i32 [ 104, %22 ], [ 104, %30 ], [ 104, %44 ], [ %62, %58 ]
ret i32 %64
}