#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*/ SEEK_END;
int atoi(char *);
int checkCorrectElemSize(int);
int /*<<< orphan*/ error(int, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ printUsage();
int /*<<< orphan*/ sprintf(char *, char *, char const *, ...);
int swapBytes(int /*<<< orphan*/ *, int, int);
int main(int argc, char **argv) {
  const char *filePath;
  int fileSize;
  int modulo;
  int swapStatus = 0;
  FILE *file;
  int elemSize = 4;
  char message[1024];
  int i;
  if (argc < 2) {
    printUsage();
    return 1;
  }
  elemSize = atoi(argv[1]);
  if (checkCorrectElemSize(elemSize) == -1) {
    error(2, "element size must be 2, 4 or 8");
  }
  for (i = 2; i < argc; i++) {
    filePath = argv[i];
    file = fopen(filePath, "rb+");
    if (file == NULL) {
      sprintf(message, "failed to open file '%s'", filePath);
      error(7, message);
    }
    fileSize = fseek(file, 0, SEEK_END);
    fileSize = ftell(file);
    modulo = fileSize % elemSize;
    if (modulo != 0) {
      sprintf(message,
              "file '%s': file size (%d) modulo element size (%d) should be "
              "zero, but was %d",
              filePath, fileSize, elemSize, modulo);
      error(3, message);
    }
    swapStatus = swapBytes(file, fileSize, elemSize);
    if (swapStatus != 0) {
      sprintf(message, "file '%s': I/O error", filePath);
      error(swapStatus, message);
    }
    fclose(file);
  }
  return swapStatus;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x4,-0x34(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11e4 <main+0x44>
mov    $0x0,%al
call   1070 <printUsage@plt>
movl   $0x1,-0x4(%rbp)
jmp    135e <main+0x1be>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
call   1040 <checkCorrectElemSize@plt>
cmp    $0xffffffff,%eax
jne    1216 <main+0x76>
mov    $0x2,%edi
lea    0xdef(%rip),%rsi
call   1080 <error@plt>
movl   $0x2,-0x444(%rbp)
mov    -0x444(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1358 <main+0x1b8>
mov    -0x10(%rbp),%rax
movslq -0x444(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xdd2(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    128b <main+0xeb>
lea    -0x440(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xdb0(%rip),%rsi
mov    $0x0,%al
call   10c0 <sprintf@plt>
lea    -0x440(%rbp),%rsi
mov    $0x7,%edi
call   1080 <error@plt>
mov    -0x30(%rbp),%rdi
mov    0x2d52(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <fseek@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <ftell@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cltd
idivl  -0x34(%rbp)
mov    %edx,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     12f7 <main+0x157>
lea    -0x440(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
mov    -0x34(%rbp),%r8d
mov    -0x20(%rbp),%r9d
lea    0xd5d(%rip),%rsi
mov    $0x0,%al
call   10c0 <sprintf@plt>
lea    -0x440(%rbp),%rsi
mov    $0x3,%edi
call   1080 <error@plt>
mov    -0x30(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x34(%rbp),%edx
call   1090 <swapBytes@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     133b <main+0x19b>
lea    -0x440(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xd65(%rip),%rsi
mov    $0x0,%al
call   10c0 <sprintf@plt>
mov    -0x24(%rbp),%edi
lea    -0x440(%rbp),%rsi
call   1080 <error@plt>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x444(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x444(%rbp)
jmp    1220 <main+0x80>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
cmp    $0x1,%edi
jg     11c8 <main+0x28>
xor    %eax,%eax
call   1070 <printUsage@plt>
mov    $0x1,%r14d
jmp    1316 <main+0x176>
mov    %rsi,%rbx
mov    %edi,%r15d
mov    0x8(%rsi),%rdi
call   10b0 <atoi@plt>
mov    %eax,0xc(%rsp)
mov    %eax,%edi
call   1040 <checkCorrectElemSize@plt>
cmp    $0xffffffff,%eax
jne    11f8 <main+0x58>
lea    0xe12(%rip),%rsi
mov    $0x2,%edi
call   1080 <error@plt>
xor    %r14d,%r14d
cmp    $0x3,%r15d
jb     1316 <main+0x176>
mov    %r15d,%eax
mov    %rax,0x10(%rsp)
mov    $0x2,%r12d
mov    %rbx,0x18(%rsp)
jmp    123c <main+0x9c>
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
call   1030 <fclose@plt>
add    $0x1,%r12
cmp    %r12,0x10(%rsp)
mov    0x18(%rsp),%rbx
je     1316 <main+0x176>
mov    (%rbx,%r12,8),%r15
mov    %r15,%rdi
lea    0xdd5(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%r13
test   %rax,%rax
jne    127d <main+0xdd>
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
lea    0xdbd(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   10c0 <sprintf@plt>
mov    $0x7,%edi
mov    %rbx,%rsi
call   1080 <error@plt>
mov    0x2d64(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
xor    %esi,%esi
call   1060 <fseek@plt>
mov    %r13,%rdi
call   1050 <ftell@plt>
mov    %eax,%r14d
cltd
mov    0xc(%rsp),%ebx
idiv   %ebx
test   %edx,%edx
je     12d5 <main+0x135>
mov    %edx,%r9d
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
lea    0xd84(%rip),%rsi
mov    %r15,%rdx
mov    %r14d,%ecx
mov    %ebx,%r8d
xor    %eax,%eax
call   10c0 <sprintf@plt>
mov    $0x3,%edi
mov    %rbp,%rsi
call   1080 <error@plt>
mov    %r13,%rdi
mov    %r14d,%esi
mov    %ebx,%edx
call   1090 <swapBytes@plt>
mov    %eax,%r14d
test   %eax,%eax
je     1220 <main+0x80>
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
lea    0xd8e(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   10c0 <sprintf@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1080 <error@plt>
jmp    1220 <main+0x80>
mov    %r14d,%eax
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [31 x i8] c"element size must be 2, 4 or 8\00"
@.str.1 = [4 x i8] c"rb+\00"
@.str.2 = [25 x i8] c"failed to open file '%s'\00"
@SEEK_END = global i32 0
@.str.3 = [78 x i8] c"file '%s': file size (%d) modulo element size (%d) should be zero, but was %d\00"
@.str.4 = [21 x i8] c"file '%s': I/O error\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32
%12 = alloca [1024 x i8]
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %9
store i32 4, i32* %11
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 2
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 (...) @printUsage()
store i32 1, i32* %3
br label %91
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i32 @atoi(i8* %21)
store i32 %22, i32* %11
%23 = load i32, i32* %11
%24 = call i32 @checkCorrectElemSize(i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %18
%27 = call i32 @error(i32 2, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %28
28: ; preds = %26, %18
store i32 2, i32* %13
br label %29
29: ; preds = %86, %28
%30 = load i32, i32* %13
%31 = load i32, i32* %4
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %89
33: ; preds = %29
%34 = load i8**, i8*** %5
%35 = load i32, i32* %13
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8*, i8** %34, i64 %36
%38 = load i8*, i8** %37
store i8* %38, i8** %6
%39 = load i8*, i8** %6
%40 = call i32* @fopen(i8* %39, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i32* %40, i32** %10
%41 = load i32*, i32** %10
%42 = icmp eq i32* %41, null
br i1 %42, label %43, label %49
43: ; preds = %33
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%45 = load i8*, i8** %6
%46 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %44, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %45)
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%48 = call i32 @error(i32 7, i8* %47)
br label %49
49: ; preds = %43, %33
%50 = load i32*, i32** %10
%51 = load i32, i32* @SEEK_END
%52 = call i32 @fseek(i32* %50, i32 0, i32 %51)
store i32 %52, i32* %7
%53 = load i32*, i32** %10
%54 = call i32 @ftell(i32* %53)
store i32 %54, i32* %7
%55 = load i32, i32* %7
%56 = load i32, i32* %11
%57 = srem i32 %55, %56
store i32 %57, i32* %8
%58 = load i32, i32* %8
%59 = icmp ne i32 %58, 0
br i1 %59, label %60, label %69
60: ; preds = %49
%61 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%62 = load i8*, i8** %6
%63 = load i32, i32* %7
%64 = load i32, i32* %11
%65 = load i32, i32* %8
%66 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %61, i8* getelementptr inbounds ([78 x i8], [78 x i8]* @.str.3, i64 0, i64 0), i8* %62, i32 %63, i32 %64, i32 %65)
%67 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%68 = call i32 @error(i32 3, i8* %67)
br label %69
69: ; preds = %60, %49
%70 = load i32*, i32** %10
%71 = load i32, i32* %7
%72 = load i32, i32* %11
%73 = call i32 @swapBytes(i32* %70, i32 %71, i32 %72)
store i32 %73, i32* %9
%74 = load i32, i32* %9
%75 = icmp ne i32 %74, 0
br i1 %75, label %76, label %83
76: ; preds = %69
%77 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%78 = load i8*, i8** %6
%79 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %77, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* %78)
%80 = load i32, i32* %9
%81 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%82 = call i32 @error(i32 %80, i8* %81)
br label %83
83: ; preds = %76, %69
%84 = load i32*, i32** %10
%85 = call i32 @fclose(i32* %84)
br label %86
86: ; preds = %83
%87 = load i32, i32* %13
%88 = add nsw i32 %87, 1
store i32 %88, i32* %13
br label %29
89: ; preds = %29
%90 = load i32, i32* %9
store i32 %90, i32* %3
br label %91
91: ; preds = %89, %16
%92 = load i32, i32* %3
ret i32 %92
}

LLVM IR (-O2):
@.str = [31 x i8] c"element size must be 2, 4 or 8\00"
@.str.1 = [4 x i8] c"rb+\00"
@.str.2 = [25 x i8] c"failed to open file '%s'\00"
@SEEK_END = global i32 0
@.str.3 = [78 x i8] c"file '%s': file size (%d) modulo element size (%d) should be zero, but was %d\00"
@.str.4 = [21 x i8] c"file '%s': I/O error\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 (...) @printUsage()
br label %48
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 @atoi(i8* %10)
%12 = tail call i32 @checkCorrectElemSize(i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = tail call i32 @error(i32 2, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %8
%17 = icmp ugt i32 %0, 2
br i1 %17, label %18, label %48
18: ; preds = %16
%19 = zext i32 %0 to i64
br label %20
20: ; preds = %18, %44
%21 = phi i64 [ 2, %18 ], [ %46, %44 ]
%22 = getelementptr inbounds i8*, i8** %1, i64 %21
%23 = load i8*, i8** %22
%24 = call i32* @fopen(i8* %23, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %29
26: ; preds = %20
%27 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%28 = call i32 @error(i32 7, i8* %4)
br label %29
29: ; preds = %26, %20
%30 = load i32, i32* @SEEK_END
%31 = call i32 @fseek(i32* %24, i32 0, i32 %30)
%32 = call i32 @ftell(i32* %24)
%33 = srem i32 %32, %11
%34 = icmp eq i32 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %29
%36 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([78 x i8], [78 x i8]* @.str.3, i64 0, i64 0), i8* %23, i32 %32, i32 %11, i32 %33)
%37 = call i32 @error(i32 3, i8* %4)
br label %38
38: ; preds = %35, %29
%39 = call i32 @swapBytes(i32* %24, i32 %32, i32 %11)
%40 = icmp eq i32 %39, 0
br i1 %40, label %44, label %41
41: ; preds = %38
%42 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* %23)
%43 = call i32 @error(i32 %39, i8* %4)
br label %44
44: ; preds = %41, %38
%45 = call i32 @fclose(i32* %24)
%46 = add nuw nsw i64 %21, 1
%47 = icmp eq i64 %46, %19
br i1 %47, label %48, label %20
48: ; preds = %44, %16, %6
%49 = phi i32 [ 1, %6 ], [ 0, %16 ], [ %39, %44 ]
ret i32 %49
}