#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 {
  long st_mtime;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int creat(char *, int);
int /*<<< orphan*/ error(char *, ...);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int stat(char *, struct stat *);
int /*<<< orphan*/ strcat(char *, char *);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  if (argc != 3)
    error("Usage: sv source dest-dir");
  char buf[1024];
  int sourcefd, destfd, count;
  char destfile[1024];
  long mtimesrc, mtimedest = 0;
  strcat(destfile, argv[2]);
  strcat(destfile, "/");
  strcat(destfile, argv[1]);
  struct stat stbufsrc, stbufdest;
  if (stat(argv[1], &stbufsrc) == -1)
    error("sv: can't acess %s", argv[1]);
  mtimesrc = stbufsrc.st_mtime;
  if (stat(destfile, &stbufdest) != -1)
    mtimedest = stbufdest.st_mtime;
  if (mtimesrc > mtimedest) {
    if ((sourcefd = open(argv[1], O_RDONLY)) == -1)
      error("sv: failed to open %s", argv[1]);
    if ((destfd = creat(destfile, 0600)) == -1)
      error("sv: failed to open %s for writing", destfile);
    while ((count = read(sourcefd, buf, 1024)) > 0)
      if ((write(destfd, buf, count)) != count)
        error("sv: write error on file %s", destfile);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11a1 <main+0x31>
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1060 <error@plt>
movq   $0x0,-0x830(%rbp)
lea    -0x820(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
call   1080 <strcat@plt>
lea    -0x820(%rbp),%rdi
lea    0xe4c(%rip),%rsi
call   1080 <strcat@plt>
lea    -0x820(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1080 <strcat@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x838(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
jne    121a <main+0xaa>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1060 <error@plt>
mov    -0x838(%rbp),%rax
mov    %rax,-0x828(%rbp)
lea    -0x820(%rbp),%rdi
lea    -0x840(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
je     1252 <main+0xe2>
mov    -0x840(%rbp),%rax
mov    %rax,-0x830(%rbp)
mov    -0x828(%rbp),%rax
cmp    -0x830(%rbp),%rax
jle    1344 <main+0x1d4>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d73(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x414(%rbp)
cmp    $0xffffffff,%eax
jne    12a1 <main+0x131>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd95(%rip),%rdi
mov    $0x0,%al
call   1060 <error@plt>
lea    -0x820(%rbp),%rdi
mov    $0x180,%esi
call   1090 <creat@plt>
mov    %eax,-0x418(%rbp)
cmp    $0xffffffff,%eax
jne    12d6 <main+0x166>
lea    -0x820(%rbp),%rsi
lea    0xd76(%rip),%rdi
mov    $0x0,%al
call   1060 <error@plt>
jmp    12db <main+0x16b>
mov    -0x414(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,-0x41c(%rbp)
cmp    $0x0,%eax
jle    133f <main+0x1cf>
mov    -0x418(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x41c(%rbp),%edx
call   1030 <write@plt>
cmp    -0x41c(%rbp),%eax
je     133a <main+0x1ca>
lea    -0x820(%rbp),%rsi
lea    0xd34(%rip),%rdi
mov    $0x0,%al
call   1060 <error@plt>
jmp    12db <main+0x16b>
jmp    1344 <main+0x1d4>
xor    %eax,%eax
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
je     1197 <main+0x27>
lea    0xe70(%rip),%rdi
xor    %eax,%eax
call   1060 <error@plt>
mov    0x10(%rbx),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   1080 <strcat@plt>
lea    0xe6b(%rip),%rsi
mov    %rbp,%rdi
call   1080 <strcat@plt>
mov    0x8(%rbx),%rsi
mov    %rbp,%rdi
call   1080 <strcat@plt>
mov    0x8(%rbx),%rdi
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
jne    11e8 <main+0x78>
mov    0x8(%rbx),%rsi
lea    0xe3b(%rip),%rdi
xor    %eax,%eax
call   1060 <error@plt>
mov    0x8(%rsp),%rbp
lea    0x10(%rsp),%rdi
mov    %rsp,%rsi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
je     120d <main+0x9d>
mov    (%rsp),%rax
cmp    %rax,%rbp
jg     1218 <main+0xa8>
jmp    12d6 <main+0x166>
xor    %eax,%eax
cmp    %rax,%rbp
jle    12d6 <main+0x166>
mov    0x8(%rbx),%rdi
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
jne    1244 <main+0xd4>
mov    0x8(%rbx),%rsi
lea    0xdf2(%rip),%rdi
xor    %eax,%eax
call   1060 <error@plt>
lea    0x10(%rsp),%rdi
mov    $0x180,%esi
call   1090 <creat@plt>
mov    %eax,%r13d
cmp    $0xffffffff,%eax
jne    126e <main+0xfe>
lea    0xde3(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1060 <error@plt>
lea    0x410(%rsp),%rsi
mov    %r14d,%edi
mov    $0x400,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    12d6 <main+0x166>
mov    %eax,%ebx
lea    0x410(%rsp),%rbp
lea    0xdcf(%rip),%r15
lea    0x10(%rsp),%r12
jmp    12b6 <main+0x146>
nop
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    12d6 <main+0x166>
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
je     12a0 <main+0x130>
mov    %r15,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1060 <error@plt>
jmp    12a0 <main+0x130>
xor    %eax,%eax
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [26 x i8] c"Usage: sv source dest-dir\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [19 x i8] c"sv: can't acess %s\00"
@O_RDONLY = global i32 0
@.str.3 = [22 x i8] c"sv: failed to open %s\00"
@.str.4 = [34 x i8] c"sv: failed to open %s for writing\00"
@.str.5 = [27 x i8] c"sv: write error on file %s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1024 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [1024 x i8]
%11 = alloca i64
%12 = alloca i64
%13 = alloca %struct.stat
%14 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 3
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %2
store i64 0, i64* %12
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 2
%23 = load i8*, i8** %22
%24 = call i32 @strcat(i8* %20, i8* %23)
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%26 = call i32 @strcat(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = call i32 @strcat(i8* %27, i8* %30)
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = call i32 @stat(i8* %34, %struct.stat* %13)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %42
37: ; preds = %19
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 1
%40 = load i8*, i8** %39
%41 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %40)
br label %42
42: ; preds = %37, %19
%43 = getelementptr inbounds %struct.stat, %struct.stat* %13, i32 0, i32 0
%44 = load i64, i64* %43
store i64 %44, i64* %11
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%46 = call i32 @stat(i8* %45, %struct.stat* %14)
%47 = icmp ne i32 %46, -1
br i1 %47, label %48, label %51
48: ; preds = %42
%49 = getelementptr inbounds %struct.stat, %struct.stat* %14, i32 0, i32 0
%50 = load i64, i64* %49
store i64 %50, i64* %12
br label %51
51: ; preds = %48, %42
%52 = load i64, i64* %11
%53 = load i64, i64* %12
%54 = icmp sgt i64 %52, %53
br i1 %54, label %55, label %92
55: ; preds = %51
%56 = load i8**, i8*** %5
%57 = getelementptr inbounds i8*, i8** %56, i64 1
%58 = load i8*, i8** %57
%59 = load i32, i32* @O_RDONLY
%60 = call i32 @open(i8* %58, i32 %59)
store i32 %60, i32* %7
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %67
62: ; preds = %55
%63 = load i8**, i8*** %5
%64 = getelementptr inbounds i8*, i8** %63, i64 1
%65 = load i8*, i8** %64
%66 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %65)
br label %67
67: ; preds = %62, %55
%68 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%69 = call i32 @creat(i8* %68, i32 384)
store i32 %69, i32* %8
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %74
71: ; preds = %67
%72 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%73 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i8* %72)
br label %74
74: ; preds = %71, %67
br label %75
75: ; preds = %90, %74
%76 = load i32, i32* %7
%77 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%78 = call i32 @read(i32 %76, i8* %77, i32 1024)
store i32 %78, i32* %9
%79 = icmp sgt i32 %78, 0
br i1 %79, label %80, label %91
80: ; preds = %75
%81 = load i32, i32* %8
%82 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%83 = load i32, i32* %9
%84 = call i32 @write(i32 %81, i8* %82, i32 %83)
%85 = load i32, i32* %9
%86 = icmp ne i32 %84, %85
br i1 %86, label %87, label %90
87: ; preds = %80
%88 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%89 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i8* %88)
br label %90
90: ; preds = %87, %80
br label %75
91: ; preds = %75
br label %92
92: ; preds = %91, %51
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [26 x i8] c"Usage: sv source dest-dir\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [19 x i8] c"sv: can't acess %s\00"
@O_RDONLY = global i32 0
@.str.3 = [22 x i8] c"sv: failed to open %s\00"
@.str.4 = [34 x i8] c"sv: failed to open %s for writing\00"
@.str.5 = [27 x i8] c"sv: write error on file %s\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = alloca [1024 x i8]
%5 = alloca %struct.stat
%6 = alloca %struct.stat
%7 = icmp eq i32 %0, 3
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = tail call i32 (i8*, ...) @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = call i32 @strcat(i8* %12, i8* %14)
%16 = call i32 @strcat(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = getelementptr inbounds i8*, i8** %1, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @strcat(i8* %12, i8* %18)
%20 = bitcast %struct.stat* %5 to i8*
%21 = bitcast %struct.stat* %6 to i8*
%22 = load i8*, i8** %17
%23 = call i32 @stat(i8* %22, %struct.stat* %5)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %10
%26 = load i8*, i8** %17
%27 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %26)
br label %28
28: ; preds = %25, %10
%29 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%30 = load i64, i64* %29
%31 = call i32 @stat(i8* %12, %struct.stat* %6)
%32 = icmp eq i32 %31, -1
%33 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 0
%34 = load i64, i64* %33
%35 = select i1 %32, i64 0, i64 %34
%36 = icmp sgt i64 %30, %35
br i1 %36, label %37, label %62
37: ; preds = %28
%38 = load i8*, i8** %17
%39 = load i32, i32* @O_RDONLY
%40 = call i32 @open(i8* %38, i32 %39)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %45
42: ; preds = %37
%43 = load i8*, i8** %17
%44 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %43)
br label %45
45: ; preds = %42, %37
%46 = call i32 @creat(i8* %12, i32 384)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %45
%49 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i8* %12)
br label %50
50: ; preds = %48, %45
%51 = call i32 @read(i32 %40, i8* %11, i32 1024)
%52 = icmp sgt i32 %51, 0
br i1 %52, label %53, label %62
53: ; preds = %50, %59
%54 = phi i32 [ %60, %59 ], [ %51, %50 ]
%55 = call i32 @write(i32 %46, i8* %11, i32 %54)
%56 = icmp eq i32 %55, %54
br i1 %56, label %59, label %57
57: ; preds = %53
%58 = call i32 (i8*, ...) @error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i8* %12)
br label %59
59: ; preds = %57, %53
%60 = call i32 @read(i32 %40, i8* %11, i32 1024)
%61 = icmp sgt i32 %60, 0
br i1 %61, label %53, label %62
62: ; preds = %59, %50, %28
ret i32 0
}