#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*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ metawrite(int, char **, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
int main(int argc, char **argv) {
  char buffer[1024];
  int fd, i, len;
  if (argc < 3) {
    fprintf(stderr, "Usage: metatag FILE TAG\n");
    exit(1);
  }
  fd = open(argv[1], O_RDWR);
  if (fd == -1) {
    fprintf(stderr, "Error, file not found\n");
    exit(1);
  }
  for (i = 2; i < argc; i++) {
    len = strlen(buffer) + strlen(argv[i]) + 1;
    if (len > 1024 - 1) {
      fprintf(stderr, "Input stream exceeds max length\n");
      exit(1);
    }
    strcat(buffer, argv[i]);
    strcat(buffer, " ");
  }
  metawrite(fd, &buffer, len);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11b2 <main+0x42>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
lea    0xe5d(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0xffffffff,-0x414(%rbp)
jne    11fa <main+0x8a>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
lea    0xe2e(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
movl   $0x2,-0x418(%rbp)
mov    -0x418(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12bd <main+0x14d>
lea    -0x410(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x420(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x418(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x420(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x41c(%rbp)
cmpl   $0x3ff,-0x41c(%rbp)
jle    127b <main+0x10b>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
lea    0xdc4(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x418(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
call   1070 <strcat@plt>
lea    -0x410(%rbp),%rdi
lea    0xdad(%rip),%rsi
call   1070 <strcat@plt>
mov    -0x418(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x418(%rbp)
jmp    1204 <main+0x94>
mov    -0x414(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x41c(%rbp),%edx
call   1080 <metawrite@plt>
mov    -0x414(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
cmp    $0x2,%edi
jle    1254 <main+0xe4>
mov    %rsi,%r13
mov    %edi,%ebp
mov    0x8(%rsi),%rdi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1266 <main+0xf6>
mov    %eax,0xc(%rsp)
mov    %ebp,%r14d
mov    $0x2,%r15d
lea    0x10(%rsp),%r12
nopl   0x0(%rax)
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebx
mov    0x0(%r13,%r15,8),%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
add    %ebx,%ebp
cmp    $0x3ff,%ebp
jge    1235 <main+0xc5>
mov    0x0(%r13,%r15,8),%rsi
mov    %r12,%rdi
call   1070 <strcat@plt>
mov    %r12,%rdi
lea    0xe5a(%rip),%rsi
call   1070 <strcat@plt>
add    $0x1,%r15
cmp    %r15,%r14
jne    11c0 <main+0x50>
add    $0x1,%ebp
lea    0x10(%rsp),%rsi
mov    0xc(%rsp),%ebx
mov    %ebx,%edi
mov    %ebp,%edx
call   1080 <metawrite@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
lea    0xdeb(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
lea    0xd9c(%rip),%rsi
jmp    1245 <main+0xd5>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
lea    0xda3(%rip),%rsi
jmp    1245 <main+0xd5>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [25 x i8] c"Usage: metatag FILE TAG\0A\00"
@O_RDWR = global i32 0
@.str.1 = [23 x i8] c"Error, file not found\0A\00"
@.str.2 = [33 x i8] c"Input stream exceeds max length\0A\00"
@.str.3 = [2 x i8] c" \00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1024 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 3
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = load i32, i32* @O_RDWR
%21 = call i32 @open(i8* %19, i32 %20)
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %28
24: ; preds = %16
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %16
store i32 2, i32* %8
br label %29
29: ; preds = %60, %28
%30 = load i32, i32* %8
%31 = load i32, i32* %4
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %63
33: ; preds = %29
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%35 = call i32 @strlen(i8* %34)
%36 = load i8**, i8*** %5
%37 = load i32, i32* %8
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8*, i8** %36, i64 %38
%40 = load i8*, i8** %39
%41 = call i32 @strlen(i8* %40)
%42 = add nsw i32 %35, %41
%43 = add nsw i32 %42, 1
store i32 %43, i32* %9
%44 = load i32, i32* %9
%45 = icmp sgt i32 %44, 1023
br i1 %45, label %46, label %50
46: ; preds = %33
%47 = load i32, i32* @stderr
%48 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %33
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%52 = load i8**, i8*** %5
%53 = load i32, i32* %8
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8*, i8** %52, i64 %54
%56 = load i8*, i8** %55
%57 = call i32 @strcat(i8* %51, i8* %56)
%58 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%59 = call i32 @strcat(i8* %58, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %60
60: ; preds = %50
%61 = load i32, i32* %8
%62 = add nsw i32 %61, 1
store i32 %62, i32* %8
br label %29
63: ; preds = %29
%64 = load i32, i32* %7
%65 = bitcast [1024 x i8]* %6 to i8**
%66 = load i32, i32* %9
%67 = call i32 @metawrite(i32 %64, i8** %65, i32 %66)
%68 = load i32, i32* %7
%69 = call i32 @close(i32 %68)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [25 x i8] c"Usage: metatag FILE TAG\0A\00"
@O_RDWR = global i32 0
@.str.1 = [23 x i8] c"Error, file not found\0A\00"
@.str.2 = [33 x i8] c"Input stream exceeds max length\0A\00"
@.str.3 = [2 x i8] c" \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = icmp slt i32 %0, 3
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_RDWR
%14 = tail call i32 @open(i8* %12, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %18, label %16
16: ; preds = %10
%17 = zext i32 %0 to i64
br label %22
18: ; preds = %10
%19 = load i32, i32* @stderr
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %16, %34
%23 = phi i64 [ 2, %16 ], [ %38, %34 ]
%24 = call i32 @strlen(i8* %4)
%25 = getelementptr inbounds i8*, i8** %1, i64 %23
%26 = load i8*, i8** %25
%27 = call i32 @strlen(i8* %26)
%28 = add nsw i32 %27, %24
%29 = icmp sgt i32 %28, 1022
br i1 %29, label %30, label %34
30: ; preds = %22
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %22
%35 = load i8*, i8** %25
%36 = call i32 @strcat(i8* %4, i8* %35)
%37 = call i32 @strcat(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%38 = add nuw nsw i64 %23, 1
%39 = icmp eq i64 %38, %17
br i1 %39, label %40, label %22
40: ; preds = %34
%41 = add nsw i32 %28, 1
%42 = bitcast [1024 x i8]* %3 to i8**
%43 = call i32 @metawrite(i32 %14, i8** %42, i32 %41)
%44 = call i32 @close(i32 %14)
ret i32 0
}