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

/* Variables and functions */
scalar_t__ copy_file(char *, char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ rename(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
scalar_t__ stat(char *, struct stat *);
char *strerror(int /*<<< orphan*/);
int gmarc_main(int argc, char **argv) {
  char *file;
  char orig[200];
  struct stat sb;
  if (argc != 2) {
    printf("usage: gmarc <file>\n");
    return 1;
  }
  file = argv[1];
  sprintf(orig, "%s.orig", file);
  if (stat(orig, &sb) == 0) {
    char origorig[80];
    printf("warning: %s already exists\n", orig);
    sprintf(origorig, "%s2", orig);
    if (rename(orig, origorig) < 0) {
      printf("rename %s to %s failed (%s)\n", orig, origorig, strerror(errno));
      return 1;
    }
  }
  if (rename(file, orig) < 0) {
    printf("rename %s to %s failed (%s)\n", file, orig, strerror(errno));
    return 1;
  }
  if (copy_file(orig, file) < 0) {
    printf("copy_file failed (%s)\n", strerror(errno));
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<gmarc_main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1196 <gmarc_main+0x36>
lea    0xe7d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1334 <gmarc_main+0x1d4>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
lea    -0xe0(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe61(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0xe0(%rbp),%rdi
lea    -0xe8(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jne    127c <gmarc_main+0x11c>
lea    -0xe0(%rbp),%rsi
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x140(%rbp),%rdi
lea    -0xe0(%rbp),%rdx
lea    0xe39(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0xe0(%rbp),%rdi
lea    -0x140(%rbp),%rsi
call   1060 <rename@plt>
cmp    $0x0,%rax
jge    1277 <gmarc_main+0x117>
lea    -0xe0(%rbp),%rax
mov    %rax,-0x150(%rbp)
lea    -0x140(%rbp),%rax
mov    %rax,-0x148(%rbp)
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x150(%rbp),%rsi
mov    -0x148(%rbp),%rdx
mov    %rax,%rcx
lea    0xdd9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1334 <gmarc_main+0x1d4>
jmp    127c <gmarc_main+0x11c>
mov    -0x18(%rbp),%rdi
lea    -0xe0(%rbp),%rsi
call   1060 <rename@plt>
cmp    $0x0,%rax
jge    12e8 <gmarc_main+0x188>
mov    -0x18(%rbp),%rax
mov    %rax,-0x160(%rbp)
lea    -0xe0(%rbp),%rax
mov    %rax,-0x158(%rbp)
mov    0x2d2a(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x160(%rbp),%rsi
mov    -0x158(%rbp),%rdx
mov    %rax,%rcx
lea    0xd68(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1334 <gmarc_main+0x1d4>
lea    -0xe0(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <copy_file@plt>
cmp    $0x0,%rax
jge    132d <gmarc_main+0x1cd>
mov    0x2cd7(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xd40(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1334 <gmarc_main+0x1d4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x160,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gmarc_main>:
push   %r15
push   %r14
push   %rbx
sub    $0x130,%rsp
cmp    $0x2,%edi
jne    123c <gmarc_main+0xcc>
mov    0x8(%rsi),%r14
lea    0xe70(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %r14,%rdx
call   1080 <sprintf@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1060 <stat@plt>
test   %rax,%rax
jne    11f2 <gmarc_main+0x82>
lea    0xe4f(%rip),%rdi
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe55(%rip),%rsi
lea    0xe0(%rsp),%r15
mov    %r15,%rdi
mov    %rbx,%rdx
call   1080 <sprintf@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1070 <rename@plt>
test   %rax,%rax
js     1269 <gmarc_main+0xf9>
lea    0x10(%rsp),%rsi
mov    %r14,%rdi
call   1070 <rename@plt>
test   %rax,%rax
js     124a <gmarc_main+0xda>
lea    0x10(%rsp),%rdi
mov    %r14,%rsi
call   1030 <copy_file@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jns    129a <gmarc_main+0x12a>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe15(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1295 <gmarc_main+0x125>
lea    0xe19(%rip),%rdi
call   1040 <puts@plt>
jmp    1295 <gmarc_main+0x125>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdc9(%rip),%rdi
lea    0x10(%rsp),%rdx
mov    %r14,%rsi
jmp    128b <gmarc_main+0x11b>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdaa(%rip),%rdi
lea    0x10(%rsp),%rsi
lea    0xe0(%rsp),%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%eax
add    $0x130,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [21 x i8] c"usage: gmarc <file>\0A\00"
@.str.1 = [8 x i8] c"%s.orig\00"
@.str.2 = [28 x i8] c"warning: %s already exists\0A\00"
@.str.3 = [4 x i8] c"%s2\00"
@.str.4 = [29 x i8] c"rename %s to %s failed (%s)\0A\00"
@errno = global i32 0
@.str.5 = [23 x i8] c"copy_file failed (%s)\0A\00"
define i32 @gmarc_main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca [200 x i8]
%8 = alloca %struct.stat
%9 = alloca [80 x i8]
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %62
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
store i8* %17, i8** %6
%18 = getelementptr inbounds [200 x i8], [200 x i8]* %7, i64 0, i64 0
%19 = load i8*, i8** %6
%20 = call i32 @sprintf(i8* %18, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = getelementptr inbounds [200 x i8], [200 x i8]* %7, i64 0, i64 0
%22 = call i64 @stat(i8* %21, %struct.stat* %8)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %41
24: ; preds = %14
%25 = getelementptr inbounds [200 x i8], [200 x i8]* %7, i64 0, i64 0
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %25)
%27 = getelementptr inbounds [80 x i8], [80 x i8]* %9, i64 0, i64 0
%28 = getelementptr inbounds [200 x i8], [200 x i8]* %7, i64 0, i64 0
%29 = call i32 @sprintf(i8* %27, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %28)
%30 = getelementptr inbounds [200 x i8], [200 x i8]* %7, i64 0, i64 0
%31 = getelementptr inbounds [80 x i8], [80 x i8]* %9, i64 0, i64 0
%32 = call i64 @rename(i8* %30, i8* %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %24
%35 = getelementptr inbounds [200 x i8], [200 x i8]* %7, i64 0, i64 0
%36 = getelementptr inbounds [80 x i8], [80 x i8]* %9, i64 0, i64 0
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %35, i8* %36, i8* %38)
store i32 1, i32* %3
br label %62
40: ; preds = %24
br label %41
41: ; preds = %40, %14
%42 = load i8*, i8** %6
%43 = getelementptr inbounds [200 x i8], [200 x i8]* %7, i64 0, i64 0
%44 = call i64 @rename(i8* %42, i8* %43)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %52
46: ; preds = %41
%47 = load i8*, i8** %6
%48 = getelementptr inbounds [200 x i8], [200 x i8]* %7, i64 0, i64 0
%49 = load i32, i32* @errno
%50 = call i8* @strerror(i32 %49)
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %47, i8* %48, i8* %50)
store i32 1, i32* %3
br label %62
52: ; preds = %41
%53 = getelementptr inbounds [200 x i8], [200 x i8]* %7, i64 0, i64 0
%54 = load i8*, i8** %6
%55 = call i64 @copy_file(i8* %53, i8* %54)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %61
57: ; preds = %52
%58 = load i32, i32* @errno
%59 = call i8* @strerror(i32 %58)
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* %59)
store i32 1, i32* %3
br label %62
61: ; preds = %52
store i32 0, i32* %3
br label %62
62: ; preds = %61, %57, %46, %34, %12
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str.1 = [8 x i8] c"%s.orig\00"
@.str.2 = [28 x i8] c"warning: %s already exists\0A\00"
@.str.3 = [4 x i8] c"%s2\00"
@.str.4 = [29 x i8] c"rename %s to %s failed (%s)\0A\00"
@errno = global i32 0
@.str.5 = [23 x i8] c"copy_file failed (%s)\0A\00"
@str = [20 x i8] c"usage: gmarc <file>\00"
define i32 @gmarc_main(i32 %0, i8** nocapture %1) {
%3 = alloca [200 x i8]
%4 = alloca %struct.stat
%5 = alloca [80 x i8]
%6 = getelementptr inbounds [200 x i8], [200 x i8]* %3, i64 0, i64 0
%7 = bitcast %struct.stat* %4 to i8*
%8 = icmp eq i32 %0, 2
br i1 %8, label %11, label %9
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %42
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = call i64 @stat(i8* %6, %struct.stat* %4)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %28
17: ; preds = %11
%18 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %6)
%20 = call i32 @sprintf(i8* %18, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %6)
%21 = call i64 @rename(i8* %6, i8* %18)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %17
%24 = load i32, i32* @errno
%25 = call i8* @strerror(i32 %24)
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %6, i8* %18, i8* %25)
br label %42
27: ; preds = %17
br label %28
28: ; preds = %27, %11
%29 = call i64 @rename(i8* %13, i8* %6)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %28
%32 = load i32, i32* @errno
%33 = call i8* @strerror(i32 %32)
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %13, i8* %6, i8* %33)
br label %42
35: ; preds = %28
%36 = call i64 @copy_file(i8* %6, i8* %13)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %35
%39 = load i32, i32* @errno
%40 = call i8* @strerror(i32 %39)
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* %40)
br label %42
42: ; preds = %23, %35, %38, %31, %9
%43 = phi i32 [ 1, %9 ], [ 1, %31 ], [ 1, %38 ], [ 1, %23 ], [ 0, %35 ]
ret i32 %43
}