#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 int /*<<< orphan*/ file;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ eprintf(char *);
int /*<<< orphan*/ *fopen(char *, char *);
int snprintf(char *, int, char *, char const *);
FILE *spw_get_file(const char *user) {
  FILE *fp = NULL;
  char file[PATH_MAX];
  int r;
  r = snprintf(file, sizeof(file), "/etc/tcb/%s/shadow", user);
  if (r < 0 || (size_t)r >= sizeof(file))
    eprintf("snprintf:");
  fp = fopen(file, "r+");
  if (!fp)
    fp = fopen("/etc/shadow", "r+");
  return fp;
}


x86 assembly (clang, -O0):
<spw_get_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    0x2ea5(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x38(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x30(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    %eax,%esi
mov    -0x8(%rbp),%rcx
lea    0xe7f(%rip),%rdx
call   1030 <snprintf@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     11a4 <spw_get_file+0x74>
mov    -0x38(%rbp),%rcx
movslq -0x24(%rbp),%rax
cmp    %rcx,%rax
jb     11b0 <spw_get_file+0x80>
lea    0xe68(%rip),%rdi
call   1040 <eprintf@plt>
mov    -0x30(%rbp),%rdi
lea    0xe62(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11e6 <spw_get_file+0xb6>
lea    0xe4a(%rip),%rdi
lea    0xe40(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spw_get_file>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
mov    %rdi,%rcx
mov    0x2eaf(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%rbx
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xea4(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
call   1030 <snprintf@plt>
test   %eax,%eax
js     1172 <spw_get_file+0x42>
cltq
cmp    %r14,%rax
jb     117e <spw_get_file+0x4e>
lea    0xe9a(%rip),%rdi
call   1040 <eprintf@plt>
lea    0xe98(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
jne    11a5 <spw_get_file+0x75>
lea    0xe87(%rip),%rdi
lea    0xe7d(%rip),%rsi
call   1050 <fopen@plt>
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [19 x i8] c"/etc/tcb/%s/shadow\00"
@.str.1 = [10 x i8] c"snprintf:\00"
@.str.2 = [3 x i8] c"r+\00"
@.str.3 = [12 x i8] c"/etc/shadow\00"
define i32* @spw_get_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
store i8* %0, i8** %2
store i32* null, i32** %3
%7 = load i32, i32* @PATH_MAX
%8 = zext i32 %7 to i64
%9 = call i8* @llvm.stacksave()
store i8* %9, i8** %4
%10 = alloca i8, i64 %8
store i64 %8, i64* %5
%11 = trunc i64 %8 to i32
%12 = load i8*, i8** %2
%13 = call i32 @snprintf(i8* %10, i32 %11, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %12)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %1
%17 = load i32, i32* %6
%18 = sext i32 %17 to i64
%19 = icmp uge i64 %18, %8
br i1 %19, label %20, label %22
20: ; preds = %16, %1
%21 = call i32 @eprintf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %16
%23 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %23, i32** %3
%24 = load i32*, i32** %3
%25 = icmp ne i32* %24, null
br i1 %25, label %28, label %26
26: ; preds = %22
%27 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %27, i32** %3
br label %28
28: ; preds = %26, %22
%29 = load i32*, i32** %3
%30 = load i8*, i8** %4
ret i32* %29
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [19 x i8] c"/etc/tcb/%s/shadow\00"
@.str.1 = [10 x i8] c"snprintf:\00"
@.str.2 = [3 x i8] c"r+\00"
@.str.3 = [12 x i8] c"/etc/shadow\00"
define noalias i32* @spw_get_file(i8* %0) {
%2 = load i32, i32* @PATH_MAX
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = call i32 @snprintf(i8* %4, i32 %2, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %0)
%6 = icmp slt i32 %5, 0
%7 = sext i32 %5 to i64
%8 = icmp uge i64 %7, %3
%9 = or i1 %6, %8
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @eprintf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %1, %10
%13 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15, %12
%18 = phi i32* [ %13, %12 ], [ %16, %15 ]
ret i32* %18
}