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

/* Variables and functions */
int /*<<< orphan*/ *calloc(int, unsigned int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int process(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
            unsigned int);
int /*<<< orphan*/ stderr;
int usage(char const *);
int work(const char *me, const char *infile, const char *outfile,
         unsigned bufsize) {
  uint8_t *buf;
  FILE *input;
  FILE *output;
  int ret;
  if (bufsize < 512 || bufsize > 65536)
    return usage(me);
  buf = calloc(1, bufsize);
  if (!buf) {
    fprintf(stderr, "Unable to allocate %u bytes of memory\n", bufsize);
    return 2;
  }
  input = fopen(infile, "r");
  if (!input) {
    fprintf(stderr, "Cannot open `%s' for reading\n", infile);
    free(buf);
    return 3;
  }
  output = fopen(outfile, "w");
  if (!output) {
    fprintf(stderr, "Cannot open `%s' for writing\n", outfile);
    fclose(input);
    free(buf);
    return 3;
  }
  ret = process(input, output, buf, bufsize);
  fclose(output);
  fclose(input);
  free(buf);
  return ret;
}


x86 assembly (clang, -O0):
<work>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
cmpl   $0x200,-0x24(%rbp)
jb     11a1 <work+0x31>
cmpl   $0x10000,-0x24(%rbp)
jbe    11b2 <work+0x42>
mov    -0x10(%rbp),%rdi
call   1080 <usage@plt>
mov    %eax,-0x4(%rbp)
jmp    12d3 <work+0x163>
mov    -0x24(%rbp),%esi
mov    $0x1,%edi
call   1060 <calloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11f4 <work+0x84>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
lea    0xe1f(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    12d3 <work+0x163>
mov    -0x18(%rbp),%rdi
lea    0xe28(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1243 <work+0xd3>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe02(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x3,-0x4(%rbp)
jmp    12d3 <work+0x163>
mov    -0x20(%rbp),%rdi
lea    0xdf9(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    129b <work+0x12b>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xdd3(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x3,-0x4(%rbp)
jmp    12d3 <work+0x163>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x24(%rbp),%ecx
call   1050 <process@plt>
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<work>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %ecx,%ebx
lea    -0x10001(%rbx),%eax
cmp    $0xffff01fe,%eax
ja     1196 <work+0x26>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1080 <usage@plt>
mov    %rdx,%r15
mov    %rsi,%r13
mov    $0x1,%edi
mov    %ebx,%esi
call   1060 <calloc@plt>
test   %rax,%rax
je     120c <work+0x9c>
mov    %rax,%r14
lea    0xe70(%rip),%rsi
mov    %r13,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     122c <work+0xbc>
mov    %rax,%r12
lea    0xe79(%rip),%rsi
mov    %r15,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1248 <work+0xd8>
mov    %rax,%r13
mov    %r12,%rdi
mov    %rax,%rsi
mov    %r14,%rdx
mov    %ebx,%ecx
call   1050 <process@plt>
mov    %eax,%ebx
mov    %r13,%rdi
call   1040 <fclose@plt>
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
jmp    1277 <work+0x107>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
lea    0xde4(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x2,%eax
jmp    1277 <work+0x107>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
lea    0xded(%rip),%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    126a <work+0xfa>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
lea    0xdf1(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    $0x3,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [39 x i8] c"Unable to allocate %u bytes of memory\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [30 x i8] c"Cannot open `%s' for reading\0A\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [30 x i8] c"Cannot open `%s' for writing\0A\00"
define i32 @work(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%14 = load i32, i32* %9
%15 = icmp ult i32 %14, 512
br i1 %15, label %19, label %16
16: ; preds = %4
%17 = load i32, i32* %9
%18 = icmp ugt i32 %17, 65536
br i1 %18, label %19, label %22
19: ; preds = %16, %4
%20 = load i8*, i8** %6
%21 = call i32 @usage(i8* %20)
store i32 %21, i32* %5
br label %68
22: ; preds = %16
%23 = load i32, i32* %9
%24 = call i32* @calloc(i32 1, i32 %23)
store i32* %24, i32** %10
%25 = load i32*, i32** %10
%26 = icmp ne i32* %25, null
br i1 %26, label %31, label %27
27: ; preds = %22
%28 = load i32, i32* @stderr
%29 = load i32, i32* %9
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %29)
store i32 2, i32* %5
br label %68
31: ; preds = %22
%32 = load i8*, i8** %7
%33 = call i32* @fopen(i8* %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %33, i32** %11
%34 = load i32*, i32** %11
%35 = icmp ne i32* %34, null
br i1 %35, label %42, label %36
36: ; preds = %31
%37 = load i32, i32* @stderr
%38 = load i8*, i8** %7
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %38)
%40 = load i32*, i32** %10
%41 = call i32 @free(i32* %40)
store i32 3, i32* %5
br label %68
42: ; preds = %31
%43 = load i8*, i8** %8
%44 = call i32* @fopen(i8* %43, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %44, i32** %12
%45 = load i32*, i32** %12
%46 = icmp ne i32* %45, null
br i1 %46, label %55, label %47
47: ; preds = %42
%48 = load i32, i32* @stderr
%49 = load i8*, i8** %8
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %49)
%51 = load i32*, i32** %11
%52 = call i32 @fclose(i32* %51)
%53 = load i32*, i32** %10
%54 = call i32 @free(i32* %53)
store i32 3, i32* %5
br label %68
55: ; preds = %42
%56 = load i32*, i32** %11
%57 = load i32*, i32** %12
%58 = load i32*, i32** %10
%59 = load i32, i32* %9
%60 = call i32 @process(i32* %56, i32* %57, i32* %58, i32 %59)
store i32 %60, i32* %13
%61 = load i32*, i32** %12
%62 = call i32 @fclose(i32* %61)
%63 = load i32*, i32** %11
%64 = call i32 @fclose(i32* %63)
%65 = load i32*, i32** %10
%66 = call i32 @free(i32* %65)
%67 = load i32, i32* %13
store i32 %67, i32* %5
br label %68
68: ; preds = %55, %47, %36, %27, %19
%69 = load i32, i32* %5
ret i32 %69
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [39 x i8] c"Unable to allocate %u bytes of memory\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [30 x i8] c"Cannot open `%s' for reading\0A\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [30 x i8] c"Cannot open `%s' for writing\0A\00"
define i32 @work(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = add i32 %3, -65537
%6 = icmp ult i32 %5, -65025
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call i32 @usage(i8* %0)
br label %35
9: ; preds = %4
%10 = tail call i32* @calloc(i32 1, i32 %3)
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i32, i32* @stderr
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %3)
br label %35
15: ; preds = %9
%16 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%21 = tail call i32 @free(i32* %10)
br label %35
22: ; preds = %15
%23 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %30
25: ; preds = %22
%26 = load i32, i32* @stderr
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %2)
%28 = tail call i32 @fclose(i32* %16)
%29 = tail call i32 @free(i32* %10)
br label %35
30: ; preds = %22
%31 = tail call i32 @process(i32* %16, i32* %23, i32* %10, i32 %3)
%32 = tail call i32 @fclose(i32* %23)
%33 = tail call i32 @fclose(i32* %16)
%34 = tail call i32 @free(i32* %10)
br label %35
35: ; preds = %30, %25, %18, %12, %7
%36 = phi i32 [ %8, %7 ], [ %31, %30 ], [ 3, %25 ], [ 3, %18 ], [ 2, %12 ]
ret i32 %36
}