#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 stat {
  int st_mode;
};
typedef int /*<<< orphan*/ storage;

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int PATH_MAX;
int /*<<< orphan*/ close(int);
int open(char *, int, ...);
int read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
scalar_t__ stat(char *, struct stat *);
char *strrchr(char *, char);
int /*<<< orphan*/ unlink(char *);
int write(int, char *, int);
int movefile(char *filename, char *directory) {
  char newname[PATH_MAX];
  char storage[1024];
  int source, dest;
  int readcount;
  char *cp;
  struct stat statbuf;
  if (stat(filename, &statbuf) < 0)
    statbuf.st_mode = 0640;
  statbuf.st_mode &= 07777;
  cp = strrchr(filename, '/');
  if (cp)
    sprintf(newname, "%s%s", directory, cp);
  else
    sprintf(newname, "%s/%s", directory, filename);
  source = open(filename, O_RDONLY);
  if (source >= 0) {
    dest = open(newname, O_WRONLY | O_CREAT | O_TRUNC, statbuf.st_mode);
    if (dest >= 0) {
      while ((readcount = read(source, storage, sizeof(storage))) > 0)
        if (write(dest, storage, readcount) < readcount) {
          close(dest);
          close(source);
          return 0;
        }
      close(dest);
      close(source);
      unlink(filename);
      return 1;
    } else {
      close(source);
      return 0;
    }
  } else
    return 0;
}


x86 assembly (clang, -O0):
<movefile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x460,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x460(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x450(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jge    11e6 <movefile+0x66>
movl   $0x1a0,-0x450(%rbp)
mov    -0x450(%rbp),%eax
and    $0xfff,%eax
mov    %eax,-0x450(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x448(%rbp)
cmpq   $0x0,-0x448(%rbp)
je     123d <movefile+0xbd>
mov    -0x460(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x448(%rbp),%rcx
lea    0xdcd(%rip),%rsi
call   10a0 <sprintf@plt>
jmp    1258 <movefile+0xd8>
mov    -0x460(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xdb2(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2d6d(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x434(%rbp)
cmpl   $0x0,-0x434(%rbp)
jl     1397 <movefile+0x217>
mov    -0x460(%rbp),%rdi
mov    0x2d2b(%rip),%rax
mov    (%rax),%esi
mov    0x2d62(%rip),%rax
or     (%rax),%esi
mov    0x2d51(%rip),%rax
or     (%rax),%esi
mov    -0x450(%rbp),%edx
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x438(%rbp)
cmpl   $0x0,-0x438(%rbp)
jl     1376 <movefile+0x1f6>
jmp    12c6 <movefile+0x146>
mov    -0x434(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %eax,-0x43c(%rbp)
cmp    $0x0,%eax
jle    1341 <movefile+0x1c1>
mov    -0x438(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    -0x43c(%rbp),%edx
call   1040 <write@plt>
cmp    -0x43c(%rbp),%eax
jge    133c <movefile+0x1bc>
mov    -0x438(%rbp),%edi
call   1060 <close@plt>
mov    -0x434(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x454(%rbp)
jmp    13a8 <movefile+0x228>
jmp    12c6 <movefile+0x146>
mov    -0x438(%rbp),%edi
call   1060 <close@plt>
mov    -0x434(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x454(%rbp)
jmp    13a8 <movefile+0x228>
mov    -0x434(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x454(%rbp)
jmp    13a8 <movefile+0x228>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x454(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<movefile>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
lea    -0x30(%rbp),%rsi
call   1080 <stat@plt>
mov    $0xfff,%ecx
and    -0x30(%rbp),%ecx
test   %rax,%rax
mov    $0x1a0,%eax
cmovns %ecx,%eax
mov    %eax,-0x30(%rbp)
mov    %r14,%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     11f7 <movefile+0x77>
lea    0xe14(%rip),%rsi
mov    %r12,%rdi
mov    %rbx,%rdx
mov    %rax,%rcx
jmp    1207 <movefile+0x87>
lea    0xe07(%rip),%rsi
mov    %r12,%rdi
mov    %rbx,%rdx
mov    %r14,%rcx
call   10a0 <sprintf@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
xor    %r15d,%r15d
mov    %r14,%rdi
xor    %eax,%eax
call   1090 <open@plt>
test   %eax,%eax
js     12c7 <movefile+0x147>
mov    %eax,%r13d
mov    0x2d84(%rip),%rax
mov    0x2dbd(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2daa(%rip),%rax
or     (%rax),%esi
mov    -0x30(%rbp),%edx
xor    %r15d,%r15d
mov    %r12,%rdi
xor    %eax,%eax
call   1090 <open@plt>
test   %eax,%eax
js     129f <movefile+0x11f>
mov    %eax,%ebx
mov    %r14,-0x38(%rbp)
lea    -0x440(%rbp),%r12
nopl   0x0(%rax)
mov    %r13d,%edi
mov    %r12,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
test   %eax,%eax
jle    12a9 <movefile+0x129>
mov    %eax,%r14d
mov    %ebx,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    %r14d,%eax
jge    1270 <movefile+0xf0>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %r13d,%edi
call   1060 <close@plt>
jmp    12c7 <movefile+0x147>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %r13d,%edi
call   1060 <close@plt>
mov    -0x38(%rbp),%rdi
call   1030 <unlink@plt>
mov    $0x1,%r15d
mov    %r15d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [5 x i8] c"%s%s\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
define i32 @movefile(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca [1024 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca %struct.stat
%14 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%15 = load i32, i32* @PATH_MAX
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %6
%18 = alloca i8, i64 %16
store i64 %16, i64* %7
%19 = load i8*, i8** %4
%20 = call i64 @stat(i8* %19, %struct.stat* %13)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %2
%23 = getelementptr inbounds %struct.stat, %struct.stat* %13, i32 0, i32 0
store i32 416, i32* %23
br label %24
24: ; preds = %22, %2
%25 = getelementptr inbounds %struct.stat, %struct.stat* %13, i32 0, i32 0
%26 = load i32, i32* %25
%27 = and i32 %26, 4095
store i32 %27, i32* %25
%28 = load i8*, i8** %4
%29 = call i8* @strrchr(i8* %28, i8 signext 47)
store i8* %29, i8** %12
%30 = load i8*, i8** %12
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %36
32: ; preds = %24
%33 = load i8*, i8** %5
%34 = load i8*, i8** %12
%35 = call i32 @sprintf(i8* %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %33, i8* %34)
br label %40
36: ; preds = %24
%37 = load i8*, i8** %5
%38 = load i8*, i8** %4
%39 = call i32 @sprintf(i8* %18, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %37, i8* %38)
br label %40
40: ; preds = %36, %32
%41 = load i8*, i8** %4
%42 = load i32, i32* @O_RDONLY
%43 = call i32 (i8*, i32, ...) @open(i8* %41, i32 %42)
store i32 %43, i32* %9
%44 = load i32, i32* %9
%45 = icmp sge i32 %44, 0
br i1 %45, label %46, label %86
46: ; preds = %40
%47 = load i32, i32* @O_WRONLY
%48 = load i32, i32* @O_CREAT
%49 = or i32 %47, %48
%50 = load i32, i32* @O_TRUNC
%51 = or i32 %49, %50
%52 = getelementptr inbounds %struct.stat, %struct.stat* %13, i32 0, i32 0
%53 = load i32, i32* %52
%54 = call i32 (i8*, i32, ...) @open(i8* %18, i32 %51, i32 %53)
store i32 %54, i32* %10
%55 = load i32, i32* %10
%56 = icmp sge i32 %55, 0
br i1 %56, label %57, label %83
57: ; preds = %46
br label %58
58: ; preds = %75, %57
%59 = load i32, i32* %9
%60 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%61 = call i32 @read(i32 %59, i8* %60, i32 1024)
store i32 %61, i32* %11
%62 = icmp sgt i32 %61, 0
br i1 %62, label %63, label %76
63: ; preds = %58
%64 = load i32, i32* %10
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%66 = load i32, i32* %11
%67 = call i32 @write(i32 %64, i8* %65, i32 %66)
%68 = load i32, i32* %11
%69 = icmp slt i32 %67, %68
br i1 %69, label %70, label %75
70: ; preds = %63
%71 = load i32, i32* %10
%72 = call i32 @close(i32 %71)
%73 = load i32, i32* %9
%74 = call i32 @close(i32 %73)
store i32 0, i32* %3
store i32 1, i32* %14
br label %87
75: ; preds = %63
br label %58
76: ; preds = %58
%77 = load i32, i32* %10
%78 = call i32 @close(i32 %77)
%79 = load i32, i32* %9
%80 = call i32 @close(i32 %79)
%81 = load i8*, i8** %4
%82 = call i32 @unlink(i8* %81)
store i32 1, i32* %3
store i32 1, i32* %14
br label %87
83: ; preds = %46
%84 = load i32, i32* %9
%85 = call i32 @close(i32 %84)
store i32 0, i32* %3
store i32 1, i32* %14
br label %87
86: ; preds = %40
store i32 0, i32* %3
store i32 1, i32* %14
br label %87
87: ; preds = %86, %83, %76, %70
%88 = load i8*, i8** %6
%89 = load i32, i32* %3
ret i32 %89
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [5 x i8] c"%s%s\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
define i32 @movefile(i8* %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = alloca %struct.stat
%5 = load i32, i32* @PATH_MAX
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%9 = bitcast %struct.stat* %4 to i8*
%10 = call i64 @stat(i8* %0, %struct.stat* %4)
%11 = icmp slt i64 %10, 0
%12 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%13 = load i32, i32* %12
%14 = and i32 %13, 4095
%15 = select i1 %11, i32 416, i32 %14
%16 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
store i32 %15, i32* %16
%17 = call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%18 = icmp eq i8* %17, null
br i1 %18, label %21, label %19
19: ; preds = %2
%20 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %17)
br label %23
21: ; preds = %2
%22 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %1, i8* %0)
br label %23
23: ; preds = %21, %19
%24 = load i32, i32* @O_RDONLY
%25 = call i32 (i8*, i32, ...) @open(i8* %0, i32 %24)
%26 = icmp sgt i32 %25, -1
br i1 %26, label %27, label %51
27: ; preds = %23
%28 = load i32, i32* @O_WRONLY
%29 = load i32, i32* @O_CREAT
%30 = or i32 %29, %28
%31 = load i32, i32* @O_TRUNC
%32 = or i32 %30, %31
%33 = load i32, i32* %16
%34 = call i32 (i8*, i32, ...) @open(i8* %7, i32 %32, i32 %33)
%35 = icmp sgt i32 %34, -1
br i1 %35, label %36, label %49
36: ; preds = %27, %39
%37 = call i32 @read(i32 %25, i8* %8, i32 1024)
%38 = icmp sgt i32 %37, 0
br i1 %38, label %39, label %45
39: ; preds = %36
%40 = call i32 @write(i32 %34, i8* %8, i32 %37)
%41 = icmp slt i32 %40, %37
br i1 %41, label %42, label %36
42: ; preds = %39
%43 = call i32 @close(i32 %34)
%44 = call i32 @close(i32 %25)
br label %51
45: ; preds = %36
%46 = call i32 @close(i32 %34)
%47 = call i32 @close(i32 %25)
%48 = call i32 @unlink(i8* %0)
br label %51
49: ; preds = %27
%50 = call i32 @close(i32 %25)
br label %51
51: ; preds = %23, %49, %45, %42
%52 = phi i32 [ 0, %42 ], [ 1, %45 ], [ 0, %49 ], [ 0, %23 ]
ret i32 %52
}