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

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *stdin;
int /*<<< orphan*/ *stdout;
int /*<<< orphan*/ strcmp(char *, char *);
char *strerror(int /*<<< orphan*/);
int swap_fs(int, int, int /*<<< orphan*/, int /*<<< orphan*/ *,
            int /*<<< orphan*/ *);
int swap_fn(int unit, int many, ssize_t size, char *ifn, char *ofn) {
  FILE *ifs, *ofs;
  if (!strcmp(ifn, "-") || !strcmp(ifn, "stdin"))
    ifs = stdin;
  else if ((ifs = fopen(ifn, "r")) < 0) {
    fprintf(stderr, "Fail to open %s: %s\n", ifn, strerror(errno));
    return -1;
  }
  if (!strcmp(ofn, "-") || !strcmp(ofn, "stdout"))
    ofs = stdout;
  else if ((ofs = fopen(ofn, "w")) < 0) {
    fprintf(stderr, "Fail to open %s: %s\n", ofn, strerror(errno));
    fclose(ifs);
    return -1;
  }
  return swap_fs(unit, many, size, ifs, ofs);
}


x86 assembly (clang, -O0):
<swap_fn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe7c(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
je     11ab <swap_fn+0x4b>
mov    -0x18(%rbp),%rdi
lea    0xe65(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    11be <swap_fn+0x5e>
mov    0x2e36(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    1225 <swap_fn+0xc5>
mov    -0x18(%rbp),%rdi
lea    0xe3f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jae    1220 <swap_fn+0xc0>
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xdfb(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f3 <swap_fn+0x193>
jmp    1225 <swap_fn+0xc5>
mov    -0x20(%rbp),%rdi
lea    0xdd0(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
je     1257 <swap_fn+0xf7>
mov    -0x20(%rbp),%rdi
lea    0xdd6(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    126a <swap_fn+0x10a>
mov    0x2d6a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    12da <swap_fn+0x17a>
mov    -0x20(%rbp),%rdi
lea    0xdb1(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jae    12d5 <swap_fn+0x175>
mov    0x2d49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xd4f(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f3 <swap_fn+0x193>
jmp    12da <swap_fn+0x17a>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%r8
call   1030 <swap_fs@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<swap_fn>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r8,%rbp
mov    %rcx,%rbx
mov    %edx,%r14d
mov    %esi,%r15d
mov    %edi,%r12d
lea    0xeb2(%rip),%rsi
mov    %rcx,%rdi
call   1040 <strcmp@plt>
test   %eax,%eax
je     1181 <swap_fn+0x51>
lea    0xea1(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcmp@plt>
test   %eax,%eax
je     1181 <swap_fn+0x51>
lea    0xe94(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
mov    %rax,%rbx
jmp    118b <swap_fn+0x5b>
mov    0x2e60(%rip),%rax
mov    (%rax),%rbx
lea    0xe6e(%rip),%rsi
mov    %rbp,%rdi
call   1040 <strcmp@plt>
test   %eax,%eax
je     11c5 <swap_fn+0x95>
lea    0xe65(%rip),%rsi
mov    %rbp,%rdi
call   1040 <strcmp@plt>
test   %eax,%eax
je     11c5 <swap_fn+0x95>
lea    0xe59(%rip),%rsi
mov    %rbp,%rdi
call   1050 <fopen@plt>
mov    %rax,%r8
jmp    11cf <swap_fn+0x9f>
mov    0x2e0c(%rip),%rax
mov    (%rax),%r8
mov    %r12d,%edi
mov    %r15d,%esi
mov    %r14d,%edx
mov    %rbx,%rcx
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <swap_fs@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"-\00"
@.str.1 = [6 x i8] c"stdin\00"
@stdin = global i32* null
@.str.2 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.3 = [21 x i8] c"Fail to open %s: %s\0A\00"
@errno = global i32 0
@.str.4 = [7 x i8] c"stdout\00"
@stdout = global i32* null
@.str.5 = [2 x i8] c"w\00"
define i32 @swap_fn(i32 %0, i32 %1, i32 %2, i8* %3, i8* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32*
%13 = alloca i32*
store i32 %0, i32* %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i8* %4, i8** %11
%14 = load i8*, i8** %10
%15 = call i32 @strcmp(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %5
%18 = load i8*, i8** %10
%19 = call i32 @strcmp(i8* %18, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%20 = icmp ne i32 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %17, %5
%22 = load i32*, i32** @stdin
store i32* %22, i32** %12
br label %34
23: ; preds = %17
%24 = load i8*, i8** %10
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %25, i32** %12
%26 = icmp ult i32* %25, null
br i1 %26, label %27, label %33
27: ; preds = %23
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %10
%30 = load i32, i32* @errno
%31 = call i8* @strerror(i32 %30)
%32 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %29, i8* %31)
store i32 -1, i32* %6
br label %64
33: ; preds = %23
br label %34
34: ; preds = %33, %21
%35 = load i8*, i8** %11
%36 = call i32 @strcmp(i8* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %34
%39 = load i8*, i8** %11
%40 = call i32 @strcmp(i8* %39, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%41 = icmp ne i32 %40, 0
br i1 %41, label %44, label %42
42: ; preds = %38, %34
%43 = load i32*, i32** @stdout
store i32* %43, i32** %13
br label %57
44: ; preds = %38
%45 = load i8*, i8** %11
%46 = call i32* @fopen(i8* %45, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %46, i32** %13
%47 = icmp ult i32* %46, null
br i1 %47, label %48, label %56
48: ; preds = %44
%49 = load i32, i32* @stderr
%50 = load i8*, i8** %11
%51 = load i32, i32* @errno
%52 = call i8* @strerror(i32 %51)
%53 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %50, i8* %52)
%54 = load i32*, i32** %12
%55 = call i32 @fclose(i32* %54)
store i32 -1, i32* %6
br label %64
56: ; preds = %44
br label %57
57: ; preds = %56, %42
%58 = load i32, i32* %7
%59 = load i32, i32* %8
%60 = load i32, i32* %9
%61 = load i32*, i32** %12
%62 = load i32*, i32** %13
%63 = call i32 @swap_fs(i32 %58, i32 %59, i32 %60, i32* %61, i32* %62)
store i32 %63, i32* %6
br label %64
64: ; preds = %57, %48, %27
%65 = load i32, i32* %6
ret i32 %65
}

LLVM IR (-O2):
@.str = [2 x i8] c"-\00"
@.str.1 = [6 x i8] c"stdin\00"
@stdin = global i32* null
@.str.2 = [2 x i8] c"r\00"
@stderr = global i32 0
@errno = global i32 0
@.str.4 = [7 x i8] c"stdout\00"
@stdout = global i32* null
@.str.5 = [2 x i8] c"w\00"
define i32 @swap_fn(i32 %0, i32 %1, i32 %2, i8* nocapture %3, i8* nocapture %4) {
%6 = tail call i32 @strcmp(i8* dereferenceable(1) %3, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %5
%9 = tail call i32 @strcmp(i8* dereferenceable(1) %3, i8* dereferenceable(6) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8, %5
%12 = load i32*, i32** @stdin
br label %15
13: ; preds = %8
%14 = tail call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %13, %11
%16 = phi i32* [ %14, %13 ], [ %12, %11 ]
%17 = tail call i32 @strcmp(i8* dereferenceable(1) %4, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%18 = icmp eq i32 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %15
%20 = tail call i32 @strcmp(i8* dereferenceable(1) %4, i8* dereferenceable(7) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19, %15
%23 = load i32*, i32** @stdout
br label %26
24: ; preds = %19
%25 = tail call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br label %26
26: ; preds = %24, %22
%27 = phi i32* [ %25, %24 ], [ %23, %22 ]
%28 = tail call i32 @swap_fs(i32 %0, i32 %1, i32 %2, i32* %16, i32* %27)
ret i32 %28
}