#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_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ swapn(unsigned char *, int, int);
int main(int iargc, char *argv[]) {
  int i, n, wdt;
  char *p;
  unsigned char *b;
  struct stat st;
  FILE *in;
  int icnt, icnte, irest, msize = 1000000;
  if (iargc < 3) {
    printf("Usage: endi nwidth file1 [ file2 ... filen]\n");
    return 1;
  }
  for (i = 2; i < iargc; i++) {
    p = argv[i];
    if (stat(p, &st) != 0) {
      printf("File %s does no exists\n", p);
      continue;
    }
    wdt = atoi(argv[1]);
    msize = msize / wdt * wdt;
    if (st.st_size <= msize)
      msize = st.st_size;
    if ((b = (unsigned char *)malloc(msize)) == NULL) {
      printf(" Can not allocate memory\n");
    }
    if ((in = fopen(p, "r+")) == NULL) {
      fprintf(stderr, "File %s does nor exist.\n", p);
      return 1;
    }
    icnt = 0;
    do {
      icnte = icnt + msize;
      n = msize / wdt;
      fseek(in, icnt, SEEK_SET);
      fread(b, msize, 1, in);
      swapn(b, wdt, n);
      fseek(in, icnt, SEEK_SET);
      fwrite(b, msize, 1, in);
      irest = st.st_size - icnte;
      if (irest < msize && irest > 0)
        msize = irest;
      icnt = icnte;
    } while (icnte != st.st_size);
    fclose(in);
    free(b);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xf4240,-0x50(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    1201 <main+0x41>
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13c4 <main+0x204>
movl   $0x2,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    13bd <main+0x1fd>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
je     1252 <main+0x92>
mov    -0x28(%rbp),%rsi
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    13af <main+0x1ef>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x50(%rbp),%eax
cltd
idivl  -0x1c(%rbp)
imul   -0x1c(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x50(%rbp),%eax
jg     1282 <main+0xc2>
mov    -0x38(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0x50(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    12a6 <main+0xe6>
lea    0xda6(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
lea    0xdae(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    12e9 <main+0x129>
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xd8a(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13c4 <main+0x204>
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
add    -0x50(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x50(%rbp),%eax
cltd
idivl  -0x1c(%rbp)
mov    %eax,-0x18(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x44(%rbp),%esi
mov    0x2cc7(%rip),%rax
mov    (%rax),%edx
call   10a0 <fseek@plt>
mov    -0x30(%rbp),%rdi
mov    -0x50(%rbp),%esi
mov    -0x40(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
mov    -0x30(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%edx
call   10d0 <swapn@plt>
mov    -0x40(%rbp),%rdi
mov    -0x44(%rbp),%esi
mov    0x2c8e(%rip),%rax
mov    (%rax),%edx
call   10a0 <fseek@plt>
mov    -0x30(%rbp),%rdi
mov    -0x50(%rbp),%esi
mov    -0x40(%rbp),%rcx
mov    $0x1,%edx
call   10e0 <fwrite@plt>
mov    -0x38(%rbp),%eax
sub    -0x48(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
cmp    -0x50(%rbp),%eax
jge    138b <main+0x1cb>
cmpl   $0x0,-0x4c(%rbp)
jle    138b <main+0x1cb>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0x48(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x48(%rbp),%eax
cmp    -0x38(%rbp),%eax
jne    12f0 <main+0x130>
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1208 <main+0x48>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
cmp    $0x3,%edi
jge    11f4 <main+0x24>
lea    0xe63(%rip),%rdi
call   1040 <puts@plt>
jmp    139d <main+0x1cd>
mov    %rsi,%r15
mov    %edi,%r13d
mov    $0xf4240,%ebp
mov    $0x2,%ebx
lea    0x8(%rsp),%r12
mov    %rsi,0x28(%rsp)
mov    %r13,0x20(%rsp)
jmp    123e <main+0x6e>
cs nopw 0x0(%rax,%rax,1)
nop
lea    0xdd9(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
add    $0x1,%rbx
cmp    %r13,%rbx
je     1381 <main+0x1b1>
mov    (%r15,%rbx,8),%r14
mov    %r14,%rdi
mov    %r12,%rsi
call   1090 <stat@plt>
test   %rax,%rax
jne    1220 <main+0x50>
mov    %rbx,0x30(%rsp)
mov    0x8(%r15),%rdi
call   10d0 <atoi@plt>
mov    %eax,%ebx
mov    %ebp,%eax
cltd
idiv   %ebx
sub    %edx,%ebp
mov    0x8(%rsp),%eax
cmp    %ebp,%eax
cmovle %eax,%ebp
mov    %ebp,%edi
call   10a0 <malloc@plt>
mov    %rax,%r12
test   %rax,%rax
jne    128d <main+0xbd>
lea    0xdac(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
lea    0xd81(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     1385 <main+0x1b5>
xor    %r14d,%r14d
mov    %ebx,0x10(%rsp)
mov    %rax,0x18(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%ebx
lea    (%rbx,%r14,1),%r15d
mov    %ebp,%eax
cltd
mov    0x10(%rsp),%r13d
idiv   %r13d
mov    %eax,0x14(%rsp)
mov    0x2cfc(%rip),%rax
mov    (%rax),%edx
mov    0x18(%rsp),%rbp
mov    %rbp,%rdi
mov    %r14d,%esi
call   10b0 <fseek@plt>
mov    %r12,%rdi
mov    %ebx,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   1050 <fread@plt>
mov    %r12,%rdi
mov    %r13d,%esi
mov    0x14(%rsp),%edx
call   10e0 <swapn@plt>
mov    0x2cc2(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
mov    %r14d,%esi
call   10b0 <fseek@plt>
mov    %r12,%rdi
mov    %ebx,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   10f0 <fwrite@plt>
mov    0x8(%rsp),%eax
mov    %eax,%ecx
sub    %r15d,%ecx
test   %ecx,%ecx
mov    %ebx,%ebp
cmovg  %ecx,%ebp
cmp    %ebx,%ecx
cmovge %ebx,%ebp
mov    %r15d,%r14d
cmp    %r15d,%eax
jne    12c0 <main+0xf0>
mov    0x18(%rsp),%rdi
call   1060 <fclose@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    0x28(%rsp),%r15
mov    0x20(%rsp),%r13
mov    0x30(%rsp),%rbx
lea    0x8(%rsp),%r12
jmp    1231 <main+0x61>
xor    %eax,%eax
jmp    13a2 <main+0x1d2>
mov    0x2c54(%rip),%rax
mov    (%rax),%edi
lea    0xc86(%rip),%rsi
mov    %r14,%rdx
call   1080 <fprintf@plt>
mov    $0x1,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [45 x i8] c"Usage: endi nwidth file1 [ file2 ... filen]\0A\00"
@.str.1 = [24 x i8] c"File %s does no exists\0A\00"
@.str.2 = [26 x i8] c" Can not allocate memory\0A\00"
@.str.3 = [3 x i8] c"r+\00"
@stderr = global i32 0
@.str.4 = [25 x i8] c"File %s does nor exist.\0A\00"
@SEEK_SET = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca %struct.stat
%12 = alloca i32*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1000000, i32* %16
%17 = load i32, i32* %4
%18 = icmp slt i32 %17, 3
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %126
21: ; preds = %2
store i32 2, i32* %6
br label %22
22: ; preds = %122, %21
%23 = load i32, i32* %6
%24 = load i32, i32* %4
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %125
26: ; preds = %22
%27 = load i8**, i8*** %5
%28 = load i32, i32* %6
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8*, i8** %27, i64 %29
%31 = load i8*, i8** %30
store i8* %31, i8** %9
%32 = load i8*, i8** %9
%33 = call i64 @stat(i8* %32, %struct.stat* %11)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %26
%36 = load i8*, i8** %9
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %36)
br label %122
38: ; preds = %26
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 1
%41 = load i8*, i8** %40
%42 = call i32 @atoi(i8* %41)
store i32 %42, i32* %8
%43 = load i32, i32* %16
%44 = load i32, i32* %8
%45 = sdiv i32 %43, %44
%46 = load i32, i32* %8
%47 = mul nsw i32 %45, %46
store i32 %47, i32* %16
%48 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%49 = load i32, i32* %48
%50 = load i32, i32* %16
%51 = icmp sle i32 %49, %50
br i1 %51, label %52, label %55
52: ; preds = %38
%53 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%54 = load i32, i32* %53
store i32 %54, i32* %16
br label %55
55: ; preds = %52, %38
%56 = load i32, i32* %16
%57 = call i64 @malloc(i32 %56)
%58 = inttoptr i64 %57 to i8*
store i8* %58, i8** %10
%59 = icmp eq i8* %58, null
br i1 %59, label %60, label %62
60: ; preds = %55
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %62
62: ; preds = %60, %55
%63 = load i8*, i8** %9
%64 = call i32* @fopen(i8* %63, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %64, i32** %12
%65 = icmp eq i32* %64, null
br i1 %65, label %66, label %70
66: ; preds = %62
%67 = load i32, i32* @stderr
%68 = load i8*, i8** %9
%69 = call i32 @fprintf(i32 %67, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %68)
store i32 1, i32* %3
br label %126
70: ; preds = %62
store i32 0, i32* %13
br label %71
71: ; preds = %112, %70
%72 = load i32, i32* %13
%73 = load i32, i32* %16
%74 = add nsw i32 %72, %73
store i32 %74, i32* %14
%75 = load i32, i32* %16
%76 = load i32, i32* %8
%77 = sdiv i32 %75, %76
store i32 %77, i32* %7
%78 = load i32*, i32** %12
%79 = load i32, i32* %13
%80 = load i32, i32* @SEEK_SET
%81 = call i32 @fseek(i32* %78, i32 %79, i32 %80)
%82 = load i8*, i8** %10
%83 = load i32, i32* %16
%84 = load i32*, i32** %12
%85 = call i32 @fread(i8* %82, i32 %83, i32 1, i32* %84)
%86 = load i8*, i8** %10
%87 = load i32, i32* %8
%88 = load i32, i32* %7
%89 = call i32 @swapn(i8* %86, i32 %87, i32 %88)
%90 = load i32*, i32** %12
%91 = load i32, i32* %13
%92 = load i32, i32* @SEEK_SET
%93 = call i32 @fseek(i32* %90, i32 %91, i32 %92)
%94 = load i8*, i8** %10
%95 = load i32, i32* %16
%96 = load i32*, i32** %12
%97 = call i32 @fwrite(i8* %94, i32 %95, i32 1, i32* %96)
%98 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%99 = load i32, i32* %98
%100 = load i32, i32* %14
%101 = sub nsw i32 %99, %100
store i32 %101, i32* %15
%102 = load i32, i32* %15
%103 = load i32, i32* %16
%104 = icmp slt i32 %102, %103
br i1 %104, label %105, label %110
105: ; preds = %71
%106 = load i32, i32* %15
%107 = icmp sgt i32 %106, 0
br i1 %107, label %108, label %110
108: ; preds = %105
%109 = load i32, i32* %15
store i32 %109, i32* %16
br label %110
110: ; preds = %108, %105, %71
%111 = load i32, i32* %14
store i32 %111, i32* %13
br label %112
112: ; preds = %110
%113 = load i32, i32* %14
%114 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%115 = load i32, i32* %114
%116 = icmp ne i32 %113, %115
br i1 %116, label %71, label %117
117: ; preds = %112
%118 = load i32*, i32** %12
%119 = call i32 @fclose(i32* %118)
%120 = load i8*, i8** %10
%121 = call i32 @free(i8* %120)
br label %122
122: ; preds = %117, %35
%123 = load i32, i32* %6
%124 = add nsw i32 %123, 1
store i32 %124, i32* %6
br label %22
125: ; preds = %22
store i32 0, i32* %3
br label %126
126: ; preds = %125, %66, %19
%127 = load i32, i32* %3
ret i32 %127
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str.1 = [24 x i8] c"File %s does no exists\0A\00"
@.str.3 = [3 x i8] c"r+\00"
@stderr = global i32 0
@.str.4 = [25 x i8] c"File %s does nor exist.\0A\00"
@SEEK_SET = global i32 0
@str = [25 x i8] c" Can not allocate memory\00"
@str.5 = [44 x i8] c"Usage: endi nwidth file1 [ file2 ... filen]\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp slt i32 %0, 3
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%9 = zext i32 %0 to i64
br label %12
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str.5, i64 0, i64 0))
br label %66
12: ; preds = %6, %62
%13 = phi i64 [ 2, %6 ], [ %64, %62 ]
%14 = phi i32 [ 1000000, %6 ], [ %63, %62 ]
%15 = getelementptr inbounds i8*, i8** %1, i64 %13
%16 = load i8*, i8** %15
%17 = call i64 @stat(i8* %16, %struct.stat* %3)
%18 = icmp eq i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %12
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %16)
br label %62
21: ; preds = %12
%22 = load i8*, i8** %7
%23 = call i32 @atoi(i8* %22)
%24 = srem i32 %14, %23
%25 = sub i32 %14, %24
%26 = load i32, i32* %8
%27 = icmp sgt i32 %26, %25
%28 = select i1 %27, i32 %25, i32 %26
%29 = call i64 @malloc(i32 %28)
%30 = inttoptr i64 %29 to i8*
%31 = icmp eq i64 %29, 0
br i1 %31, label %32, label %34
32: ; preds = %21
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
br label %34
34: ; preds = %32, %21
%35 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%36 = icmp eq i32* %35, null
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = load i32, i32* @stderr
%39 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %16)
br label %66
40: ; preds = %34, %40
%41 = phi i32 [ %43, %40 ], [ 0, %34 ]
%42 = phi i32 [ %57, %40 ], [ %28, %34 ]
%43 = add nsw i32 %42, %41
%44 = sdiv i32 %42, %23
%45 = load i32, i32* @SEEK_SET
%46 = call i32 @fseek(i32* %35, i32 %41, i32 %45)
%47 = call i32 @fread(i8* %30, i32 %42, i32 1, i32* %35)
%48 = call i32 @swapn(i8* %30, i32 %23, i32 %44)
%49 = load i32, i32* @SEEK_SET
%50 = call i32 @fseek(i32* %35, i32 %41, i32 %49)
%51 = call i32 @fwrite(i8* %30, i32 %42, i32 1, i32* %35)
%52 = load i32, i32* %8
%53 = sub nsw i32 %52, %43
%54 = icmp slt i32 %53, %42
%55 = icmp sgt i32 %53, 0
%56 = and i1 %54, %55
%57 = select i1 %56, i32 %53, i32 %42
%58 = icmp eq i32 %52, %43
br i1 %58, label %59, label %40
59: ; preds = %40
%60 = call i32 @fclose(i32* %35)
%61 = call i32 @free(i8* %30)
br label %62
62: ; preds = %59, %19
%63 = phi i32 [ %14, %19 ], [ %57, %59 ]
%64 = add nuw nsw i64 %13, 1
%65 = icmp eq i64 %64, %9
br i1 %65, label %66, label %12
66: ; preds = %62, %37, %10
%67 = phi i32 [ 1, %10 ], [ 1, %37 ], [ 0, %62 ]
ret i32 %67
}