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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ ftruncate(int, int);
int lseek(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int swap(char *filename1, char *filename2) {
  int file1 = 0;
  int file2 = 0;
  if ((file1 = open(filename1, O_RDWR)) < -1) {
    return 1;
  }
  if ((file2 = open(filename2, O_RDWR)) < -1) {
    return 1;
  }
  int lenght1 = lseek(file1, 0, SEEK_END);
  int lenght2 = lseek(file2, 0, SEEK_END);
  printf("file1 lenght=%d\n", lenght1);
  printf("file2 lenght=%d\n", lenght2);
  int minFile, maxFile, minLenght, maxLenght;
  if (lenght1 <= lenght2) {
    minFile = file1;
    maxFile = file2;
    minLenght = lenght1;
    maxLenght = lenght2;
  } else {
    minFile = file2;
    maxFile = file1;
    minLenght = lenght2;
    maxLenght = lenght1;
  }
  for (int i = 0; i < maxLenght; i++) {
    char buffer1[1];
    char buffer2[1];
    lseek(minFile, i, SEEK_SET);
    lseek(maxFile, i, SEEK_SET);
    if (i < minLenght) {
      read(minFile, buffer1, 1);
    }
    read(maxFile, buffer2, 1);
    lseek(minFile, i, SEEK_SET);
    lseek(maxFile, i, SEEK_SET);
    if (i < minLenght) {
      write(maxFile, buffer1, 1);
    }
    write(minFile, buffer2, 1);
  }
  ftruncate(maxFile, minLenght);
  return 0;
}


x86 assembly (clang, -O0):
<swap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jge    11a8 <swap+0x48>
movl   $0x1,-0x4(%rbp)
jmp    133c <swap+0x1dc>
mov    -0x18(%rbp),%rdi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jge    11d2 <swap+0x72>
movl   $0x1,-0x4(%rbp)
jmp    133c <swap+0x1dc>
mov    -0x1c(%rbp),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <lseek@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <lseek@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%esi
lea    0xdf8(%rip),%rdi
call   1040 <printf@plt>
mov    -0x28(%rbp),%esi
lea    0xdfa(%rip),%rdi
call   1040 <printf@plt>
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jg     1245 <swap+0xe5>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x38(%rbp)
jmp    125d <swap+0xfd>
mov    -0x20(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x38(%rbp),%eax
jge    132a <swap+0x1ca>
mov    -0x2c(%rbp),%edi
mov    -0x3c(%rbp),%esi
mov    0x2d53(%rip),%rax
mov    (%rax),%edx
call   1060 <lseek@plt>
mov    -0x30(%rbp),%edi
mov    -0x3c(%rbp),%esi
mov    0x2d3f(%rip),%rax
mov    (%rax),%edx
call   1060 <lseek@plt>
mov    -0x3c(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    12b5 <swap+0x155>
mov    -0x2c(%rbp),%edi
lea    -0x3d(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    -0x30(%rbp),%edi
lea    -0x3e(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    -0x2c(%rbp),%edi
mov    -0x3c(%rbp),%esi
mov    0x2cfd(%rip),%rax
mov    (%rax),%edx
call   1060 <lseek@plt>
mov    -0x30(%rbp),%edi
mov    -0x3c(%rbp),%esi
mov    0x2ce9(%rip),%rax
mov    (%rax),%edx
call   1060 <lseek@plt>
mov    -0x3c(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    130b <swap+0x1ab>
mov    -0x30(%rbp),%edi
lea    -0x3d(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x2c(%rbp),%edi
lea    -0x3e(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1264 <swap+0x104>
mov    -0x30(%rbp),%edi
mov    -0x34(%rbp),%esi
call   1050 <ftruncate@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<swap>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    0x2e73(%rip),%r14
mov    (%r14),%esi
call   1080 <open@plt>
mov    $0x1,%ebp
cmp    $0xffffffff,%eax
jl     12ed <swap+0x18d>
mov    %eax,%r12d
mov    (%r14),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
jl     12ed <swap+0x18d>
mov    %eax,%r13d
mov    0x2e34(%rip),%rbx
mov    (%rbx),%edx
mov    %r12d,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    %r13d,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
mov    %eax,%r15d
lea    0xe30(%rip),%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
lea    0xe33(%rip),%rdi
mov    %r15d,%esi
call   1040 <printf@plt>
cmp    %r15d,%ebp
mov    %r12d,%r14d
cmovg  %r13d,%r14d
cmovg  %r12d,%r13d
mov    %ebp,%r12d
cmovg  %r15d,%r12d
cmovg  %ebp,%r15d
test   %r15d,%r15d
jle    12e0 <swap+0x180>
xor    %ebp,%ebp
mov    0x2dbf(%rip),%rbx
mov    %r15d,0x4(%rsp)
jmp    1264 <swap+0x104>
nopl   0x0(%rax,%rax,1)
mov    %r13d,%edi
lea    0x2(%rsp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    (%rbx),%edx
mov    %r14d,%edi
mov    %ebp,%esi
call   1060 <lseek@plt>
mov    (%rbx),%edx
mov    %r13d,%edi
mov    %ebp,%esi
call   1060 <lseek@plt>
lea    0x2(%rsp),%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1030 <write@plt>
add    $0x1,%ebp
cmp    %ebp,%r15d
je     12e0 <swap+0x180>
mov    (%rbx),%edx
mov    %r14d,%edi
mov    %ebp,%esi
call   1060 <lseek@plt>
mov    (%rbx),%edx
mov    %r13d,%edi
mov    %ebp,%esi
call   1060 <lseek@plt>
cmp    %r12d,%ebp
jge    1220 <swap+0xc0>
mov    %r14d,%edi
mov    %r12d,%r15d
lea    0x3(%rsp),%r12
mov    %r12,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    %r13d,%edi
lea    0x2(%rsp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    (%rbx),%edx
mov    %r14d,%edi
mov    %ebp,%esi
call   1060 <lseek@plt>
mov    (%rbx),%edx
mov    %r13d,%edi
mov    %ebp,%esi
call   1060 <lseek@plt>
mov    %r13d,%edi
mov    %r12,%rsi
mov    %r15d,%r12d
mov    0x4(%rsp),%r15d
mov    $0x1,%edx
call   1030 <write@plt>
jmp    124a <swap+0xea>
mov    %r13d,%edi
mov    %r12d,%esi
call   1050 <ftruncate@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@SEEK_END = global i32 0
@.str = [17 x i8] c"file1 lenght=%d\0A\00"
@.str.1 = [17 x i8] c"file2 lenght=%d\0A\00"
@SEEK_SET = global i32 0
define i32 @swap(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca [1 x i8]
%16 = alloca [1 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %6
store i32 0, i32* %7
%17 = load i8*, i8** %4
%18 = load i32, i32* @O_RDWR
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %6
%20 = icmp slt i32 %19, -1
br i1 %20, label %21, label %22
21: ; preds = %2
store i32 1, i32* %3
br label %103
22: ; preds = %2
%23 = load i8*, i8** %5
%24 = load i32, i32* @O_RDWR
%25 = call i32 @open(i8* %23, i32 %24)
store i32 %25, i32* %7
%26 = icmp slt i32 %25, -1
br i1 %26, label %27, label %28
27: ; preds = %22
store i32 1, i32* %3
br label %103
28: ; preds = %22
%29 = load i32, i32* %6
%30 = load i32, i32* @SEEK_END
%31 = call i32 @lseek(i32 %29, i32 0, i32 %30)
store i32 %31, i32* %8
%32 = load i32, i32* %7
%33 = load i32, i32* @SEEK_END
%34 = call i32 @lseek(i32 %32, i32 0, i32 %33)
store i32 %34, i32* %9
%35 = load i32, i32* %8
%36 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %35)
%37 = load i32, i32* %9
%38 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %37)
%39 = load i32, i32* %8
%40 = load i32, i32* %9
%41 = icmp sle i32 %39, %40
br i1 %41, label %42, label %47
42: ; preds = %28
%43 = load i32, i32* %6
store i32 %43, i32* %10
%44 = load i32, i32* %7
store i32 %44, i32* %11
%45 = load i32, i32* %8
store i32 %45, i32* %12
%46 = load i32, i32* %9
store i32 %46, i32* %13
br label %52
47: ; preds = %28
%48 = load i32, i32* %7
store i32 %48, i32* %10
%49 = load i32, i32* %6
store i32 %49, i32* %11
%50 = load i32, i32* %9
store i32 %50, i32* %12
%51 = load i32, i32* %8
store i32 %51, i32* %13
br label %52
52: ; preds = %47, %42
store i32 0, i32* %14
br label %53
53: ; preds = %96, %52
%54 = load i32, i32* %14
%55 = load i32, i32* %13
%56 = icmp slt i32 %54, %55
br i1 %56, label %57, label %99
57: ; preds = %53
%58 = load i32, i32* %10
%59 = load i32, i32* %14
%60 = load i32, i32* @SEEK_SET
%61 = call i32 @lseek(i32 %58, i32 %59, i32 %60)
%62 = load i32, i32* %11
%63 = load i32, i32* %14
%64 = load i32, i32* @SEEK_SET
%65 = call i32 @lseek(i32 %62, i32 %63, i32 %64)
%66 = load i32, i32* %14
%67 = load i32, i32* %12
%68 = icmp slt i32 %66, %67
br i1 %68, label %69, label %73
69: ; preds = %57
%70 = load i32, i32* %10
%71 = getelementptr inbounds [1 x i8], [1 x i8]* %15, i64 0, i64 0
%72 = call i32 @read(i32 %70, i8* %71, i32 1)
br label %73
73: ; preds = %69, %57
%74 = load i32, i32* %11
%75 = getelementptr inbounds [1 x i8], [1 x i8]* %16, i64 0, i64 0
%76 = call i32 @read(i32 %74, i8* %75, i32 1)
%77 = load i32, i32* %10
%78 = load i32, i32* %14
%79 = load i32, i32* @SEEK_SET
%80 = call i32 @lseek(i32 %77, i32 %78, i32 %79)
%81 = load i32, i32* %11
%82 = load i32, i32* %14
%83 = load i32, i32* @SEEK_SET
%84 = call i32 @lseek(i32 %81, i32 %82, i32 %83)
%85 = load i32, i32* %14
%86 = load i32, i32* %12
%87 = icmp slt i32 %85, %86
br i1 %87, label %88, label %92
88: ; preds = %73
%89 = load i32, i32* %11
%90 = getelementptr inbounds [1 x i8], [1 x i8]* %15, i64 0, i64 0
%91 = call i32 @write(i32 %89, i8* %90, i32 1)
br label %92
92: ; preds = %88, %73
%93 = load i32, i32* %10
%94 = getelementptr inbounds [1 x i8], [1 x i8]* %16, i64 0, i64 0
%95 = call i32 @write(i32 %93, i8* %94, i32 1)
br label %96
96: ; preds = %92
%97 = load i32, i32* %14
%98 = add nsw i32 %97, 1
store i32 %98, i32* %14
br label %53
99: ; preds = %53
%100 = load i32, i32* %11
%101 = load i32, i32* %12
%102 = call i32 @ftruncate(i32 %100, i32 %101)
store i32 0, i32* %3
br label %103
103: ; preds = %99, %27, %21
%104 = load i32, i32* %3
ret i32 %104
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@SEEK_END = global i32 0
@.str = [17 x i8] c"file1 lenght=%d\0A\00"
@.str.1 = [17 x i8] c"file2 lenght=%d\0A\00"
@SEEK_SET = global i32 0
define i32 @swap(i8* nocapture %0, i8* nocapture %1) {
%3 = alloca [1 x i8]
%4 = alloca [1 x i8]
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp slt i32 %6, -1
br i1 %7, label %55, label %8
8: ; preds = %2
%9 = load i32, i32* @O_RDWR
%10 = tail call i32 @open(i8* %1, i32 %9)
%11 = icmp slt i32 %10, -1
br i1 %11, label %55, label %12
12: ; preds = %8
%13 = load i32, i32* @SEEK_END
%14 = tail call i32 @lseek(i32 %6, i32 0, i32 %13)
%15 = load i32, i32* @SEEK_END
%16 = tail call i32 @lseek(i32 %10, i32 0, i32 %15)
%17 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %14)
%18 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%19 = icmp sgt i32 %14, %16
%20 = select i1 %19, i32 %10, i32 %6
%21 = select i1 %19, i32 %6, i32 %10
%22 = select i1 %19, i32 %16, i32 %14
%23 = select i1 %19, i32 %14, i32 %16
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %12
%26 = getelementptr inbounds [1 x i8], [1 x i8]* %3, i64 0, i64 0
%27 = getelementptr inbounds [1 x i8], [1 x i8]* %4, i64 0, i64 0
br label %30
28: ; preds = %51, %12
%29 = tail call i32 @ftruncate(i32 %21, i32 %22)
br label %55
30: ; preds = %25, %51
%31 = phi i32 [ 0, %25 ], [ %53, %51 ]
%32 = load i32, i32* @SEEK_SET
%33 = tail call i32 @lseek(i32 %20, i32 %31, i32 %32)
%34 = load i32, i32* @SEEK_SET
%35 = tail call i32 @lseek(i32 %21, i32 %31, i32 %34)
%36 = icmp slt i32 %31, %22
br i1 %36, label %37, label %45
37: ; preds = %30
%38 = call i32 @read(i32 %20, i8* %26, i32 1)
%39 = call i32 @read(i32 %21, i8* %27, i32 1)
%40 = load i32, i32* @SEEK_SET
%41 = tail call i32 @lseek(i32 %20, i32 %31, i32 %40)
%42 = load i32, i32* @SEEK_SET
%43 = tail call i32 @lseek(i32 %21, i32 %31, i32 %42)
%44 = call i32 @write(i32 %21, i8* %26, i32 1)
br label %51
45: ; preds = %30
%46 = call i32 @read(i32 %21, i8* %27, i32 1)
%47 = load i32, i32* @SEEK_SET
%48 = tail call i32 @lseek(i32 %20, i32 %31, i32 %47)
%49 = load i32, i32* @SEEK_SET
%50 = tail call i32 @lseek(i32 %21, i32 %31, i32 %49)
br label %51
51: ; preds = %45, %37
%52 = call i32 @write(i32 %20, i8* %27, i32 1)
%53 = add nuw nsw i32 %31, 1
%54 = icmp eq i32 %53, %23
br i1 %54, label %28, label %30
55: ; preds = %8, %2, %28
%56 = phi i32 [ 0, %28 ], [ 1, %2 ], [ 1, %8 ]
ret i32 %56
}