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

/* Variables and functions */
int MAXPATHLEN;
int /*<<< orphan*/ R_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char const *, char const *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char const *);
void appendShellConfig(const char *basedir, const char *target, const char *fmt,
                       const char *dir) {
  char fname[MAXPATHLEN];
  snprintf(fname, sizeof(fname), "%s/%s", basedir, target);
  if (access(fname, R_OK) < 0)
    return;
  FILE *f = fopen(fname, "a");
  if (!f)
    return;
  fprintf(f, fmt, dir);
  fclose(f);
}


x86 assembly (clang, -O0):
<appendShellConfig>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    0x2e79(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x48(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x30(%rbp)
mov    %eax,%esi
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xe5b(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x48(%rbp),%rdi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
call   1060 <access@plt>
cmp    $0x0,%rax
jge    11d2 <appendShellConfig+0x82>
movl   $0x1,-0x34(%rbp)
jmp    121e <appendShellConfig+0xce>
mov    -0x48(%rbp),%rdi
lea    0xe29(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11fd <appendShellConfig+0xad>
movl   $0x1,-0x34(%rbp)
jmp    121e <appendShellConfig+0xce>
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <fprintf@plt>
mov    -0x40(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<appendShellConfig>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r8
mov    %rdi,%rcx
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe76(%rip),%rdx
mov    %rbx,%rdi
call   1040 <snprintf@plt>
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <access@plt>
test   %rax,%rax
js     11d5 <appendShellConfig+0x85>
lea    0xe57(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11d5 <appendShellConfig+0x85>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@MAXPATHLEN = global i32 0
@.str = [6 x i8] c"%s/%s\00"
@R_OK = global i32 0
@.str.1 = [2 x i8] c"a\00"
define void @appendShellConfig(i8* %0, i8* %1, i8* %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i8* %3, i8** %8
%13 = load i32, i32* @MAXPATHLEN
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %9
%16 = alloca i8, i64 %14
store i64 %14, i64* %10
%17 = trunc i64 %14 to i32
%18 = load i8*, i8** %5
%19 = load i8*, i8** %6
%20 = call i32 @snprintf(i8* %16, i32 %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %18, i8* %19)
%21 = load i32, i32* @R_OK
%22 = call i64 @access(i8* %16, i32 %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %4
store i32 1, i32* %11
br label %37
25: ; preds = %4
%26 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %26, i32** %12
%27 = load i32*, i32** %12
%28 = icmp ne i32* %27, null
br i1 %28, label %30, label %29
29: ; preds = %25
store i32 1, i32* %11
br label %37
30: ; preds = %25
%31 = load i32*, i32** %12
%32 = load i8*, i8** %7
%33 = load i8*, i8** %8
%34 = call i32 @fprintf(i32* %31, i8* %32, i8* %33)
%35 = load i32*, i32** %12
%36 = call i32 @fclose(i32* %35)
store i32 0, i32* %11
br label %37
37: ; preds = %30, %29, %24
%38 = load i8*, i8** %9
%39 = load i32, i32* %11
switch i32 %39, label %41 [
i32 0, label %40
i32 1, label %40
]
40: ; preds = %37, %37
ret void
41: ; preds = %37
unreachable
}

LLVM IR (-O2):
@MAXPATHLEN = global i32 0
@.str = [6 x i8] c"%s/%s\00"
@R_OK = global i32 0
@.str.1 = [2 x i8] c"a\00"
define void @appendShellConfig(i8* %0, i8* %1, i8* nocapture %2, i8* %3) {
%5 = load i32, i32* @MAXPATHLEN
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = call i32 @snprintf(i8* %7, i32 %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%9 = load i32, i32* @R_OK
%10 = call i64 @access(i8* %7, i32 %9)
%11 = icmp slt i64 %10, 0
br i1 %11, label %18, label %12
12: ; preds = %4
%13 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %18, label %15
15: ; preds = %12
%16 = call i32 @fprintf(i32* %13, i8* %2, i8* %3)
%17 = call i32 @fclose(i32* %13)
br label %18
18: ; preds = %15, %12, %4
ret void
}