#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;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ chmod(char *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fopen(char *, char *);
int /*<<< orphan*/ fputc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ mkdir(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int main() {
  char orig_path[10 + 20];
  char *temp;
  FILE *f;
  int i, n;
  temp = orig_path;
  strcpy(temp, "/tmp/");
  temp = temp + 5;
  memset(temp, 'a', sizeof(char) * (10 + 15));
  orig_path[10 + 19] = '\0';
  mkdir(orig_path, O_RDONLY);
  chmod(orig_path, 0700);
  f = (FILE *)fopen("pathfile", "r+");
  n = strlen(orig_path);
  for (i = 0; i < n; i++)
    fputc(orig_path[i], f);
  fputc('\0', f);
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xe5e(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x38(%rbp),%rax
add    $0x5,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    $0x61,%esi
mov    $0x19,%edx
call   1070 <memset@plt>
movb   $0x0,-0x13(%rbp)
lea    -0x30(%rbp),%rdi
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
call   1040 <mkdir@plt>
lea    -0x30(%rbp),%rdi
mov    $0x1c0,%esi
call   1090 <chmod@plt>
lea    0xe15(%rip),%rdi
lea    0xe17(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x40(%rbp)
lea    -0x30(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x48(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x48(%rbp),%eax
jge    1242 <main+0xc2>
movslq -0x44(%rbp),%rax
mov    -0x30(%rbp,%rax,1),%al
mov    -0x40(%rbp),%rsi
movsbl %al,%edi
call   1080 <fputc@plt>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1214 <main+0x94>
mov    -0x40(%rbp),%rsi
xor    %edi,%edi
call   1080 <fputc@plt>
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
lea    0xe70(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
call   1030 <strcpy@plt>
lea    0x5(%rsp),%rdi
mov    $0x61,%esi
mov    $0x19,%edx
call   1070 <memset@plt>
movb   $0x0,0x1d(%rsp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1040 <mkdir@plt>
mov    %r14,%rdi
mov    $0x1c0,%esi
call   1090 <chmod@plt>
lea    0xe2d(%rip),%rdi
lea    0xe2f(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1060 <strlen@plt>
test   %eax,%eax
jle    1215 <main+0x95>
mov    %eax,%r14d
xor    %ebx,%ebx
nopl   0x0(%rax)
movsbl (%rsp,%rbx,1),%edi
mov    %r15,%rsi
call   1080 <fputc@plt>
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1200 <main+0x80>
xor    %edi,%edi
mov    %r15,%rsi
call   1080 <fputc@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [6 x i8] c"/tmp/\00"
@O_RDONLY = global i32 0
@.str.1 = [9 x i8] c"pathfile\00"
@.str.2 = [3 x i8] c"r+\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [30 x i8]
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = getelementptr inbounds [30 x i8], [30 x i8]* %2, i64 0, i64 0
store i8* %7, i8** %3
%8 = load i8*, i8** %3
%9 = call i32 @strcpy(i8* %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%10 = load i8*, i8** %3
%11 = getelementptr inbounds i8, i8* %10, i64 5
store i8* %11, i8** %3
%12 = load i8*, i8** %3
%13 = call i32 @memset(i8* %12, i8 signext 97, i32 25)
%14 = getelementptr inbounds [30 x i8], [30 x i8]* %2, i64 0, i64 29
store i8 0, i8* %14
%15 = getelementptr inbounds [30 x i8], [30 x i8]* %2, i64 0, i64 0
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @mkdir(i8* %15, i32 %16)
%18 = getelementptr inbounds [30 x i8], [30 x i8]* %2, i64 0, i64 0
%19 = call i32 @chmod(i8* %18, i32 448)
%20 = call i64 @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%21 = inttoptr i64 %20 to i32*
store i32* %21, i32** %4
%22 = getelementptr inbounds [30 x i8], [30 x i8]* %2, i64 0, i64 0
%23 = call i32 @strlen(i8* %22)
store i32 %23, i32* %6
store i32 0, i32* %5
br label %24
24: ; preds = %35, %0
%25 = load i32, i32* %5
%26 = load i32, i32* %6
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %38
28: ; preds = %24
%29 = load i32, i32* %5
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [30 x i8], [30 x i8]* %2, i64 0, i64 %30
%32 = load i8, i8* %31
%33 = load i32*, i32** %4
%34 = call i32 @fputc(i8 signext %32, i32* %33)
br label %35
35: ; preds = %28
%36 = load i32, i32* %5
%37 = add nsw i32 %36, 1
store i32 %37, i32* %5
br label %24
38: ; preds = %24
%39 = load i32*, i32** %4
%40 = call i32 @fputc(i8 signext 0, i32* %39)
%41 = load i32*, i32** %4
%42 = call i32 @fclose(i32* %41)
ret i32 0
}

LLVM IR (-O2):
@.str = [6 x i8] c"/tmp/\00"
@O_RDONLY = global i32 0
@.str.1 = [9 x i8] c"pathfile\00"
@.str.2 = [3 x i8] c"r+\00"
define i32 @main() {
%1 = alloca [30 x i8]
%2 = getelementptr inbounds [30 x i8], [30 x i8]* %1, i64 0, i64 0
%3 = call i32 @strcpy(i8* %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%4 = getelementptr inbounds [30 x i8], [30 x i8]* %1, i64 0, i64 5
%5 = call i32 @memset(i8* %4, i8 signext 97, i32 25)
%6 = getelementptr inbounds [30 x i8], [30 x i8]* %1, i64 0, i64 29
store i8 0, i8* %6
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @mkdir(i8* %2, i32 %7)
%9 = call i32 @chmod(i8* %2, i32 448)
%10 = call i64 @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%11 = inttoptr i64 %10 to i32*
%12 = call i32 @strlen(i8* %2)
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %23
14: ; preds = %0
%15 = zext i32 %12 to i64
br label %16
16: ; preds = %14, %16
%17 = phi i64 [ 0, %14 ], [ %21, %16 ]
%18 = getelementptr inbounds [30 x i8], [30 x i8]* %1, i64 0, i64 %17
%19 = load i8, i8* %18
%20 = call i32 @fputc(i8 signext %19, i32* %11)
%21 = add nuw nsw i64 %17, 1
%22 = icmp eq i64 %21, %15
br i1 %22, label %23, label %16
23: ; preds = %16, %0
%24 = call i32 @fputc(i8 signext 0, i32* %11)
%25 = call i32 @fclose(i32* %11)
ret i32 0
}