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

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ fcloseall();
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char **, char, int);
int /*<<< orphan*/ trim(char **, int);
int /*<<< orphan*/ usage(char *);
int main(int argc, char **argv) {
  int ECODE = 0;
  if (argc < 4 || argc > 4) {
    usage(argv[0]);
    ECODE = 1;
    goto exit;
  }
  FILE *in, *out;
  int maxlen = 0;
  if ((in = fopen(argv[1], "r")) == NULL) {
    ECODE = 2;
    goto exit;
  }
  if ((out = fopen(argv[2], "w")) == NULL) {
    ECODE = 2;
    goto close;
  }
  maxlen = atoi(argv[3]);
  int n;
  char *str;
  char buffer[256];
  memset(&buffer, '\0', sizeof(buffer));
  while ((str = fgets(buffer, sizeof(buffer), in)) != NULL) {
    if (buffer[0] != 4)
      trim(&buffer, maxlen);
    fputs(buffer, out);
  }
close:
  fcloseall();
exit:
  return ECODE;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x4,-0x8(%rbp)
jl     11b4 <main+0x34>
cmpl   $0x4,-0x8(%rbp)
jle    11cc <main+0x4c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1070 <usage@plt>
movl   $0x1,-0x14(%rbp)
jmp    12b5 <main+0x135>
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe1e(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1201 <main+0x81>
movl   $0x2,-0x14(%rbp)
jmp    12b5 <main+0x135>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xdf2(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    122f <main+0xaf>
movl   $0x2,-0x14(%rbp)
jmp    12ae <main+0x12e>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x2c(%rbp)
lea    -0x140(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
lea    -0x140(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x100,%esi
call   1060 <fgets@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     12a9 <main+0x129>
movsbl -0x140(%rbp),%eax
cmp    $0x4,%eax
je     1294 <main+0x114>
lea    -0x140(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1030 <trim@plt>
lea    -0x140(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <fputs@plt>
jmp    1252 <main+0xd2>
jmp    12ae <main+0x12e>
mov    $0x0,%al
call   10a0 <fcloseall@plt>
mov    -0x14(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x100,%rsp
mov    %rsi,%r14
cmp    $0x4,%edi
jne    1243 <main+0xc3>
mov    0x8(%r14),%rdi
lea    0xe5a(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1253 <main+0xd3>
mov    %rax,%r12
mov    0x10(%r14),%rdi
lea    0xe40(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     125b <main+0xdb>
mov    %rax,%rbp
mov    0x18(%r14),%rdi
call   1090 <atoi@plt>
mov    %eax,%r15d
mov    %rsp,%rbx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %r12,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1261 <main+0xe1>
mov    %rsp,%rbx
jmp    1230 <main+0xb0>
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1040 <fputs@plt>
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %r12,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1261 <main+0xe1>
cmpb   $0x4,(%rsp)
je     1210 <main+0x90>
mov    %rbx,%rdi
mov    %r15d,%esi
call   1030 <trim@plt>
jmp    1210 <main+0x90>
mov    (%r14),%rdi
call   1070 <usage@plt>
mov    $0x1,%r14d
jmp    1268 <main+0xe8>
mov    $0x2,%r14d
jmp    1268 <main+0xe8>
mov    $0x2,%r14d
xor    %eax,%eax
call   10a0 <fcloseall@plt>
mov    %r14d,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"w\00"
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 i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca [256 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 4
br i1 %14, label %18, label %15
15: ; preds = %2
%16 = load i32, i32* %4
%17 = icmp sgt i32 %16, 4
br i1 %17, label %18, label %23
18: ; preds = %15, %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = call i32 @usage(i8* %21)
store i32 1, i32* %6
br label %65
23: ; preds = %15
store i32 0, i32* %9
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32* @fopen(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %27, i32** %7
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %30
29: ; preds = %23
store i32 2, i32* %6
br label %65
30: ; preds = %23
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = call i32* @fopen(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %34, i32** %8
%35 = icmp eq i32* %34, null
br i1 %35, label %36, label %37
36: ; preds = %30
store i32 2, i32* %6
br label %63
37: ; preds = %30
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 3
%40 = load i8*, i8** %39
%41 = call i32 @atoi(i8* %40)
store i32 %41, i32* %9
%42 = bitcast [256 x i8]* %12 to i8**
%43 = call i32 @memset(i8** %42, i8 signext 0, i32 256)
br label %44
44: ; preds = %58, %37
%45 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%46 = load i32*, i32** %7
%47 = call i8* @fgets(i8* %45, i32 256, i32* %46)
store i8* %47, i8** %11
%48 = icmp ne i8* %47, null
br i1 %48, label %49, label %62
49: ; preds = %44
%50 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = icmp ne i32 %52, 4
br i1 %53, label %54, label %58
54: ; preds = %49
%55 = bitcast [256 x i8]* %12 to i8**
%56 = load i32, i32* %9
%57 = call i32 @trim(i8** %55, i32 %56)
br label %58
58: ; preds = %54, %49
%59 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%60 = load i32*, i32** %8
%61 = call i32 @fputs(i8* %59, i32* %60)
br label %44
62: ; preds = %44
br label %63
63: ; preds = %62, %36
%64 = call i32 (...) @fcloseall()
br label %65
65: ; preds = %63, %29, %18
%66 = load i32, i32* %6
ret i32 %66
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"w\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [256 x i8]
%4 = icmp eq i32 %0, 4
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = load i8*, i8** %1
%7 = tail call i32 @usage(i8* %6)
br label %39
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %39, label %13
13: ; preds = %8
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = tail call i32* @fopen(i8* %15, 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 %36, label %18
18: ; preds = %13
%19 = getelementptr inbounds i8*, i8** %1, i64 3
%20 = load i8*, i8** %19
%21 = tail call i32 @atoi(i8* %20)
%22 = bitcast [256 x i8]* %3 to i8**
%23 = call i32 @memset(i8** %22, i8 signext 0, i32 256)
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%25 = call i8* @fgets(i8* %24, i32 256, i32* %11)
%26 = icmp eq i8* %25, null
br i1 %26, label %36, label %27
27: ; preds = %18, %32
%28 = load i8, i8* %24
%29 = icmp eq i8 %28, 4
br i1 %29, label %32, label %30
30: ; preds = %27
%31 = call i32 @trim(i8** %22, i32 %21)
br label %32
32: ; preds = %30, %27
%33 = call i32 @fputs(i8* %24, i32* %16)
%34 = call i8* @fgets(i8* %24, i32 256, i32* %11)
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %27
36: ; preds = %32, %18, %13
%37 = phi i32 [ 2, %13 ], [ 0, %18 ], [ 0, %32 ]
%38 = call i32 (...) @fcloseall()
br label %39
39: ; preds = %8, %36, %5
%40 = phi i32 [ 1, %5 ], [ %37, %36 ], [ 2, %8 ]
ret i32 %40
}