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

/* Variables and functions */
int /*<<< orphan*/ EACCES;
int /*<<< orphan*/ errno;
int open(char *, int, int);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ username_sane(char *);
int open_userfile(char *user, int flags) {
  if (!username_sane(user)) {
    errno = EACCES;
    return -1;
  }
  char path[6 + 20 + 1] = "users/";
  strcpy(path + 6, user);
  return open(path, flags, 0700);
}


x86 assembly (clang, -O0):
<open_userfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <username_sane@plt>
cmp    $0x0,%eax
jne    116f <open_userfile+0x3f>
mov    0x2e90(%rip),%rax
mov    (%rax),%ecx
mov    0x2e77(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11c9 <open_userfile+0x99>
mov    0xe8a(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe87(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe84(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe81(%rip),%ax
mov    %ax,-0x18(%rbp)
mov    0xe79(%rip),%al
mov    %al,-0x16(%rbp)
lea    -0x30(%rbp),%rdi
add    $0x6,%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x30(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    $0x1c0,%edx
call   1050 <open@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_userfile>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
call   1040 <username_sane@plt>
test   %eax,%eax
je     1179 <open_userfile+0x49>
movups 0xec0(%rip),%xmm0
movups %xmm0,0xb(%rsp)
movaps 0xea9(%rip),%xmm0
movaps %xmm0,(%rsp)
lea    0x6(%rsp),%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %rsp,%rdi
mov    %ebp,%esi
mov    $0x1c0,%edx
call   1050 <open@plt>
jmp    1190 <open_userfile+0x60>
mov    0x2e68(%rip),%rax
mov    (%rax),%eax
mov    0x2e4f(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@EACCES = global i32 0
@errno = global i32 0
@__const.open_userfile.path = [27 x i8] c"users/\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
define i32 @open_userfile(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [27 x i8]
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i8*, i8** %4
%8 = call i32 @username_sane(i8* %7)
%9 = icmp ne i32 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = load i32, i32* @EACCES
store i32 %11, i32* @errno
store i32 -1, i32* %3
br label %21
12: ; preds = %2
%13 = bitcast [27 x i8]* %6 to i8*
%14 = getelementptr inbounds [27 x i8], [27 x i8]* %6, i64 0, i64 0
%15 = getelementptr inbounds i8, i8* %14, i64 6
%16 = load i8*, i8** %4
%17 = call i32 @strcpy(i8* %15, i8* %16)
%18 = getelementptr inbounds [27 x i8], [27 x i8]* %6, i64 0, i64 0
%19 = load i32, i32* %5
%20 = call i32 @open(i8* %18, i32 %19, i32 448)
store i32 %20, i32* %3
br label %21
21: ; preds = %12, %10
%22 = load i32, i32* %3
ret i32 %22
}

LLVM IR (-O2):
@EACCES = global i32 0
@errno = global i32 0
@__const.open_userfile.path = [27 x i8] c"users/\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
define i32 @open_userfile(i8* %0, i32 %1) {
%3 = alloca [27 x i8]
%4 = tail call i32 @username_sane(i8* %0)
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = load i32, i32* @EACCES
store i32 %7, i32* @errno
br label %13
8: ; preds = %2
%9 = getelementptr inbounds [27 x i8], [27 x i8]* %3, i64 0, i64 0
%10 = getelementptr inbounds [27 x i8], [27 x i8]* %3, i64 0, i64 6
%11 = call i32 @strcpy(i8* %10, i8* %0)
%12 = call i32 @open(i8* %9, i32 %1, i32 448)
br label %13
13: ; preds = %8, %6
%14 = phi i32 [ %12, %8 ], [ -1, %6 ]
ret i32 %14
}