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

/* Variables and functions */
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ rename(char *, char *);
scalar_t__ stat(char *, struct stat *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
int main(int argc, char *argv[]) {
  struct stat st;
  int dst_exists = 0, dst_isdir = 0;
  int i, err;
  char buf[512];
  if (argc < 3) {
    printf("mv: file1 file.. dest dir\n");
    printf("mv: file1 file2\n");
    return 1;
  }
  if (stat(argv[argc - 1], &st) == 0) {
    dst_exists = 1;
    if (st.st_mode & 0040000)
      dst_isdir = 1;
  }
  if (argc > 3 && (!dst_exists || !dst_isdir)) {
    printf("mv: for multiple files, dest %s should be a directory\n",
           argv[argc - 1]);
    return 1;
  }
  for (i = 1; i < argc; i++) {
    if (stat(argv[i], &st) < 0) {
      printf("mv: cannot stat %s\n", argv[i]);
      err++;
      continue;
    }
    strncpy(buf, argv[argc - 1], sizeof(buf) - 1);
    if (dst_isdir) {
      strncat(buf, "/", sizeof(buf) - strlen(buf) - 1);
      strncat(buf, argv[i], sizeof(buf) - strlen(buf) - 1);
    }
    if (rename(argv[i], buf) < 0)
      err++;
  }
  return err;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11b9 <main+0x59>
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe75(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13a3 <main+0x243>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jne    1201 <main+0xa1>
movl   $0x1,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
and    $0x4000,%eax
cmp    $0x0,%eax
je     11fc <main+0x9c>
movl   $0x1,-0x20(%rbp)
jmp    1201 <main+0xa1>
cmpl   $0x3,-0x8(%rbp)
jle    124a <main+0xea>
cmpl   $0x0,-0x1c(%rbp)
je     121f <main+0xbf>
cmpl   $0x0,-0x20(%rbp)
jne    124a <main+0xea>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13a3 <main+0x243>
movl   $0x1,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    139d <main+0x23d>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jge    12a4 <main+0x144>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xdd4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    138f <main+0x22f>
lea    -0x230(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rsi
mov    $0x1ff,%edx
call   1030 <strncpy@plt>
cmpl   $0x0,-0x20(%rbp)
je     135f <main+0x1ff>
lea    -0x230(%rbp),%rax
mov    %rax,-0x248(%rbp)
lea    -0x230(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x248(%rbp),%rdi
movslq %eax,%rcx
mov    $0x200,%eax
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,%edx
lea    0xd6e(%rip),%rsi
call   1060 <strncat@plt>
lea    -0x230(%rbp),%rax
mov    %rax,-0x240(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x238(%rbp)
lea    -0x230(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x240(%rbp),%rdi
mov    -0x238(%rbp),%rsi
movslq %eax,%rcx
mov    $0x200,%eax
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,%edx
call   1060 <strncat@plt>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    -0x230(%rbp),%rsi
call   1080 <rename@plt>
cmp    $0x0,%rax
jge    138a <main+0x22a>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    138f <main+0x22f>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1251 <main+0xf1>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x228,%rsp
cmp    $0x2,%edi
jg     11a0 <main+0x30>
lea    0xec0(%rip),%rdi
call   1040 <puts@plt>
lea    0xece(%rip),%rdi
call   1040 <puts@plt>
jmp    11f4 <main+0x84>
mov    %rsi,%rbx
mov    %edi,%ebp
lea    -0x1(%rbp),%eax
mov    %rax,0x10(%rsp)
mov    (%rsi,%rax,8),%rdi
lea    0x8(%rsp),%rsi
call   1080 <stat@plt>
test   %rax,%rax
sete   %al
setne  %r15b
mov    $0x4000,%edx
and    0x8(%rsp),%edx
sete   %cl
cmp    $0x3,%ebp
je     120d <main+0x9d>
shr    $0xe,%edx
and    %dl,%al
jne    120d <main+0x9d>
mov    0x10(%rsp),%rax
mov    (%rbx,%rax,8),%rsi
lea    0xe13(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x228,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
or     %cl,%r15b
mov    %ebp,%r13d
mov    $0x1,%r14d
mov    %r13,0x18(%rsp)
mov    %r15b,0x7(%rsp)
jmp    124b <main+0xdb>
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%rbx,%r14,8),%rdi
mov    %r12,%rsi
call   1090 <rename@plt>
shr    $0x3f,%rax
add    %eax,%ebp
add    $0x1,%r14
cmp    %r14,%r13
je     11f9 <main+0x89>
mov    (%rbx,%r14,8),%rdi
lea    0x8(%rsp),%rsi
call   1080 <stat@plt>
test   %rax,%rax
js     12d0 <main+0x160>
mov    0x10(%rsp),%rax
mov    (%rbx,%rax,8),%rsi
lea    0x20(%rsp),%r12
mov    %r12,%rdi
mov    $0x1ff,%edx
call   1030 <strncpy@plt>
test   %r15b,%r15b
jne    1230 <main+0xc0>
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    $0x1ff,%edx
sub    %eax,%edx
mov    %r12,%rdi
lea    0xdb4(%rip),%rsi
call   1070 <strncat@plt>
mov    (%rbx,%r14,8),%r13
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    $0x1ff,%edx
sub    %eax,%edx
mov    %r12,%rdi
mov    %r13,%rsi
movzbl 0x7(%rsp),%r15d
mov    0x18(%rsp),%r13
call   1070 <strncat@plt>
jmp    1230 <main+0xc0>
nopw   0x0(%rax,%rax,1)
mov    (%rbx,%r14,8),%rsi
lea    0xd5c(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%eax
jmp    1240 <main+0xd0>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [27 x i8] c"mv: file1 file.. dest dir\0A\00"
@.str.1 = [17 x i8] c"mv: file1 file2\0A\00"
@.str.2 = [55 x i8] c"mv: for multiple files, dest %s should be a directory\0A\00"
@.str.3 = [20 x i8] c"mv: cannot stat %s\0A\00"
@.str.4 = [2 x i8] c"/\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [512 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
store i32 0, i32* %8
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 3
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %123
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = load i32, i32* %4
%20 = sub nsw i32 %19, 1
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8*, i8** %18, i64 %21
%23 = load i8*, i8** %22
%24 = call i64 @stat(i8* %23, %struct.stat* %6)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %17
store i32 1, i32* %7
%27 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%28 = load i32, i32* %27
%29 = and i32 %28, 16384
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %26
store i32 1, i32* %8
br label %32
32: ; preds = %31, %26
br label %33
33: ; preds = %32, %17
%34 = load i32, i32* %4
%35 = icmp sgt i32 %34, 3
br i1 %35, label %36, label %50
36: ; preds = %33
%37 = load i32, i32* %7
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = load i32, i32* %8
%41 = icmp ne i32 %40, 0
br i1 %41, label %50, label %42
42: ; preds = %39, %36
%43 = load i8**, i8*** %5
%44 = load i32, i32* %4
%45 = sub nsw i32 %44, 1
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8*, i8** %43, i64 %46
%48 = load i8*, i8** %47
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0), i8* %48)
store i32 1, i32* %3
br label %123
50: ; preds = %39, %33
store i32 1, i32* %9
br label %51
51: ; preds = %118, %50
%52 = load i32, i32* %9
%53 = load i32, i32* %4
%54 = icmp slt i32 %52, %53
br i1 %54, label %55, label %121
55: ; preds = %51
%56 = load i8**, i8*** %5
%57 = load i32, i32* %9
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8*, i8** %56, i64 %58
%60 = load i8*, i8** %59
%61 = call i64 @stat(i8* %60, %struct.stat* %6)
%62 = icmp slt i64 %61, 0
br i1 %62, label %63, label %72
63: ; preds = %55
%64 = load i8**, i8*** %5
%65 = load i32, i32* %9
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i8*, i8** %64, i64 %66
%68 = load i8*, i8** %67
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %68)
%70 = load i32, i32* %10
%71 = add nsw i32 %70, 1
store i32 %71, i32* %10
br label %118
72: ; preds = %55
%73 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%74 = load i8**, i8*** %5
%75 = load i32, i32* %4
%76 = sub nsw i32 %75, 1
%77 = sext i32 %76 to i64
%78 = getelementptr inbounds i8*, i8** %74, i64 %77
%79 = load i8*, i8** %78
%80 = call i32 @strncpy(i8* %73, i8* %79, i32 511)
%81 = load i32, i32* %8
%82 = icmp ne i32 %81, 0
br i1 %82, label %83, label %105
83: ; preds = %72
%84 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%85 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%86 = call i32 @strlen(i8* %85)
%87 = sext i32 %86 to i64
%88 = sub i64 512, %87
%89 = sub i64 %88, 1
%90 = trunc i64 %89 to i32
%91 = call i32 @strncat(i8* %84, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 %90)
%92 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%93 = load i8**, i8*** %5
%94 = load i32, i32* %9
%95 = sext i32 %94 to i64
%96 = getelementptr inbounds i8*, i8** %93, i64 %95
%97 = load i8*, i8** %96
%98 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%99 = call i32 @strlen(i8* %98)
%100 = sext i32 %99 to i64
%101 = sub i64 512, %100
%102 = sub i64 %101, 1
%103 = trunc i64 %102 to i32
%104 = call i32 @strncat(i8* %92, i8* %97, i32 %103)
br label %105
105: ; preds = %83, %72
%106 = load i8**, i8*** %5
%107 = load i32, i32* %9
%108 = sext i32 %107 to i64
%109 = getelementptr inbounds i8*, i8** %106, i64 %108
%110 = load i8*, i8** %109
%111 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%112 = call i64 @rename(i8* %110, i8* %111)
%113 = icmp slt i64 %112, 0
br i1 %113, label %114, label %117
114: ; preds = %105
%115 = load i32, i32* %10
%116 = add nsw i32 %115, 1
store i32 %116, i32* %10
br label %117
117: ; preds = %114, %105
br label %118
118: ; preds = %117, %63
%119 = load i32, i32* %9
%120 = add nsw i32 %119, 1
store i32 %120, i32* %9
br label %51
121: ; preds = %51
%122 = load i32, i32* %10
store i32 %122, i32* %3
br label %123
123: ; preds = %121, %42, %14
%124 = load i32, i32* %3
ret i32 %124
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str.2 = [55 x i8] c"mv: for multiple files, dest %s should be a directory\0A\00"
@.str.3 = [20 x i8] c"mv: cannot stat %s\0A\00"
@.str.4 = [2 x i8] c"/\00"
@str = [26 x i8] c"mv: file1 file.. dest dir\00"
@str.5 = [16 x i8] c"mv: file1 file2\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = alloca [512 x i8]
%5 = bitcast %struct.stat* %3 to i8*
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%7 = icmp slt i32 %0, 3
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.5, i64 0, i64 0))
br label %64
11: ; preds = %2
%12 = add nsw i32 %0, -1
%13 = zext i32 %12 to i64
%14 = getelementptr inbounds i8*, i8** %1, i64 %13
%15 = load i8*, i8** %14
%16 = call i64 @stat(i8* %15, %struct.stat* %3)
%17 = icmp eq i64 %16, 0
%18 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%19 = load i32, i32* %18
%20 = and i32 %19, 16384
%21 = icmp eq i32 %20, 0
%22 = xor i1 %21, true
%23 = xor i1 %17, true
%24 = select i1 %23, i1 true, i1 %21
%25 = select i1 %17, i1 %22, i1 false
%26 = icmp eq i32 %0, 3
%27 = select i1 %26, i1 true, i1 %25
br i1 %27, label %28, label %30
28: ; preds = %11
%29 = zext i32 %0 to i64
br label %33
30: ; preds = %11
%31 = load i8*, i8** %14
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0), i8* %31)
br label %64
33: ; preds = %28, %59
%34 = phi i64 [ 1, %28 ], [ %62, %59 ]
%35 = phi i32 [ undef, %28 ], [ %61, %59 ]
%36 = getelementptr inbounds i8*, i8** %1, i64 %34
%37 = load i8*, i8** %36
%38 = call i64 @stat(i8* %37, %struct.stat* %3)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %33
%41 = load i8*, i8** %36
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %41)
br label %59
43: ; preds = %33
%44 = load i8*, i8** %14
%45 = call i32 @strncpy(i8* %6, i8* %44, i32 511)
br i1 %24, label %54, label %46
46: ; preds = %43
%47 = call i32 @strlen(i8* %6)
%48 = sub i32 511, %47
%49 = call i32 @strncat(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 %48)
%50 = load i8*, i8** %36
%51 = call i32 @strlen(i8* %6)
%52 = sub i32 511, %51
%53 = call i32 @strncat(i8* %6, i8* %50, i32 %52)
br label %54
54: ; preds = %46, %43
%55 = load i8*, i8** %36
%56 = call i64 @rename(i8* %55, i8* %6)
%57 = lshr i64 %56, 63
%58 = trunc i64 %57 to i32
br label %59
59: ; preds = %54, %40
%60 = phi i32 [ %58, %54 ], [ 1, %40 ]
%61 = add nsw i32 %35, %60
%62 = add nuw nsw i64 %34, 1
%63 = icmp eq i64 %62, %29
br i1 %63, label %64, label %33
64: ; preds = %59, %30, %8
%65 = phi i32 [ 1, %8 ], [ 1, %30 ], [ %61, %59 ]
ret i32 %65
}