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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int getline(char **, int *, int /*<<< orphan*/ *);
int strlen(char *);
void declare_variables() {
  FILE *fin = fopen("8086.txt", "r");
  int nbytes = 10;
  char *line = NULL;
  FILE *fout = fopen("output.txt", "w");
  fprintf(fout, "jmp start \n\n");
  FILE *var = fopen("variables.txt", "r");
  int a = getline(&line, &nbytes, var);
  int len = strlen(line);
  line[len - 1] = '\0';
  while (a != -1) {
    fprintf(fout, "%s db 0\n", line);
    a = getline(&line, &nbytes, var);
    int len = strlen(line);
    line[len - 1] = '\0';
  }
  fclose(var);
  fprintf(fout, "start:  ");
  a = getline(&line, &nbytes, fin);
  while (a != -1) {
    fprintf(fout, "%s\n", line);
    a = getline(&line, &nbytes, fin);
  }
  fprintf(fout, "ret\n");
  fclose(fin);
  fclose(fout);
  return;
}


x86 assembly (clang, -O0):
<declare_variables>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
lea    0xea1(%rip),%rdi
lea    0xea3(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x8(%rbp)
movl   $0xa,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
lea    0xe86(%rip),%rdi
lea    0xe8a(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe78(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
lea    0xe77(%rip),%rdi
lea    0xe54(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdx
lea    -0x18(%rbp),%rdi
lea    -0xc(%rbp),%rsi
call   1070 <getline@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
cmpl   $0xffffffff,-0x2c(%rbp)
je     1245 <declare_variables+0xf5>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe2b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rdx
lea    -0x18(%rbp),%rdi
lea    -0xc(%rbp),%rsi
call   1070 <getline@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x34(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11ef <declare_variables+0x9f>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rdi
lea    0xde3(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x8(%rbp),%rdx
lea    -0x18(%rbp),%rdi
lea    -0xc(%rbp),%rsi
call   1070 <getline@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
je     12ad <declare_variables+0x15d>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xdb8(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x8(%rbp),%rdx
lea    -0x18(%rbp),%rdi
lea    -0xc(%rbp),%rsi
call   1070 <getline@plt>
mov    %eax,-0x2c(%rbp)
jmp    1274 <declare_variables+0x124>
mov    -0x20(%rbp),%rdi
lea    0xd91(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x8(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<declare_variables>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe8b(%rip),%rdi
lea    0xe8d(%rip),%rbx
mov    %rbx,%rsi
call   1060 <fopen@plt>
mov    %rax,0x10(%rsp)
movl   $0xa,0xc(%rsp)
movq   $0x0,(%rsp)
lea    0xe6b(%rip),%rdi
lea    0xe6f(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r15
lea    0xe62(%rip),%rdi
mov    $0xc,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1080 <fwrite@plt>
lea    0xe56(%rip),%rdi
mov    %rbx,%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
mov    %rsp,%rdi
lea    0xc(%rsp),%rsi
mov    %rax,%rdx
call   1070 <getline@plt>
mov    %eax,%ebp
mov    (%rsp),%rdi
call   1040 <strlen@plt>
mov    (%rsp),%rcx
cltq
movb   $0x0,-0x1(%rax,%rcx,1)
cmp    $0xffffffff,%ebp
je     125c <declare_variables+0xfc>
lea    0xe27(%rip),%r12
mov    %rsp,%r13
lea    0xc(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%rsp),%rdx
mov    %r15,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r13,%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1070 <getline@plt>
mov    %eax,%r14d
mov    (%rsp),%rdi
call   1040 <strlen@plt>
mov    (%rsp),%rcx
cltq
movb   $0x0,-0x1(%rax,%rcx,1)
cmp    $0xffffffff,%r14d
jne    1220 <declare_variables+0xc0>
mov    %rbx,%rdi
call   1030 <fclose@plt>
lea    0xdd1(%rip),%rdi
mov    $0x8,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1080 <fwrite@plt>
mov    %rsp,%rdi
lea    0xc(%rsp),%rsi
mov    0x10(%rsp),%rbp
mov    %rbp,%rdx
call   1070 <getline@plt>
cmp    $0xffffffff,%eax
je     12d4 <declare_variables+0x174>
lea    0xda7(%rip),%r12
mov    %rsp,%r14
lea    0xc(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
mov    (%rsp),%rdx
mov    %r15,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1070 <getline@plt>
cmp    $0xffffffff,%eax
jne    12b0 <declare_variables+0x150>
lea    0xd6e(%rip),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1080 <fwrite@plt>
mov    %rbp,%rdi
call   1030 <fclose@plt>
mov    %r15,%rdi
call   1030 <fclose@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"8086.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [11 x i8] c"output.txt\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [13 x i8] c"jmp start \0A\0A\00"
@.str.5 = [14 x i8] c"variables.txt\00"
@.str.6 = [9 x i8] c"%s db 0\0A\00"
@.str.7 = [9 x i8] c"start: \00"
@.str.8 = [4 x i8] c"%s\0A\00"
@.str.9 = [5 x i8] c"ret\0A\00"
define void @declare_variables() {
%1 = alloca i32*
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %1
store i32 10, i32* %2
store i8* null, i8** %3
%10 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %10, i32** %4
%11 = load i32*, i32** %4
%12 = call i32 (i32*, i8*, ...) @fprintf(i32* %11, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%13 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %5
%14 = load i32*, i32** %5
%15 = call i32 @getline(i8** %3, i32* %2, i32* %14)
store i32 %15, i32* %6
%16 = load i8*, i8** %3
%17 = call i32 @strlen(i8* %16)
store i32 %17, i32* %7
%18 = load i8*, i8** %3
%19 = load i32, i32* %7
%20 = sub nsw i32 %19, 1
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %18, i64 %21
store i8 0, i8* %22
br label %23
23: ; preds = %26, %0
%24 = load i32, i32* %6
%25 = icmp ne i32 %24, -1
br i1 %25, label %26, label %39
26: ; preds = %23
%27 = load i32*, i32** %4
%28 = load i8*, i8** %3
%29 = call i32 (i32*, i8*, ...) @fprintf(i32* %27, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i8* %28)
%30 = load i32*, i32** %5
%31 = call i32 @getline(i8** %3, i32* %2, i32* %30)
store i32 %31, i32* %6
%32 = load i8*, i8** %3
%33 = call i32 @strlen(i8* %32)
store i32 %33, i32* %8
%34 = load i8*, i8** %3
%35 = load i32, i32* %8
%36 = sub nsw i32 %35, 1
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %34, i64 %37
store i8 0, i8* %38
br label %23
39: ; preds = %23
%40 = load i32*, i32** %5
%41 = call i32 @fclose(i32* %40)
%42 = load i32*, i32** %4
%43 = call i32 (i32*, i8*, ...) @fprintf(i32* %42, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0))
%44 = load i32*, i32** %1
%45 = call i32 @getline(i8** %3, i32* %2, i32* %44)
store i32 %45, i32* %6
br label %46
46: ; preds = %49, %39
%47 = load i32, i32* %6
%48 = icmp ne i32 %47, -1
br i1 %48, label %49, label %55
49: ; preds = %46
%50 = load i32*, i32** %4
%51 = load i8*, i8** %3
%52 = call i32 (i32*, i8*, ...) @fprintf(i32* %50, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0), i8* %51)
%53 = load i32*, i32** %1
%54 = call i32 @getline(i8** %3, i32* %2, i32* %53)
store i32 %54, i32* %6
br label %46
55: ; preds = %46
%56 = load i32*, i32** %4
%57 = call i32 (i32*, i8*, ...) @fprintf(i32* %56, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
%58 = load i32*, i32** %1
%59 = call i32 @fclose(i32* %58)
%60 = load i32*, i32** %4
%61 = call i32 @fclose(i32* %60)
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"8086.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [11 x i8] c"output.txt\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [13 x i8] c"jmp start \0A\0A\00"
@.str.5 = [14 x i8] c"variables.txt\00"
@.str.6 = [9 x i8] c"%s db 0\0A\00"
@.str.7 = [9 x i8] c"start: \00"
@.str.8 = [4 x i8] c"%s\0A\00"
@.str.9 = [5 x i8] c"ret\0A\00"
define void @declare_variables() {
%1 = alloca i32
%2 = alloca i8*
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = bitcast i32* %1 to i8*
store i32 10, i32* %1
%5 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%7 = tail call i64 @fwrite(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i64 12, i64 1, i32* %6)
%8 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @getline(i8** %2, i32* %1, i32* %8)
%10 = load i8*, i8** %2
%11 = call i32 @strlen(i8* %10)
%12 = load i8*, i8** %2
%13 = add nsw i32 %11, -1
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %12, i64 %14
store i8 0, i8* %15
%16 = icmp eq i32 %9, -1
br i1 %16, label %28, label %17
17: ; preds = %0, %17
%18 = load i8*, i8** %2
%19 = call i32 (i32*, i8*, ...) @fprintf(i32* %6, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i8* %18)
%20 = call i32 @getline(i8** %2, i32* %1, i32* %8)
%21 = load i8*, i8** %2
%22 = call i32 @strlen(i8* %21)
%23 = load i8*, i8** %2
%24 = add nsw i32 %22, -1
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
store i8 0, i8* %26
%27 = icmp eq i32 %20, -1
br i1 %27, label %28, label %17
28: ; preds = %17, %0
%29 = call i32 @fclose(i32* %8)
%30 = call i64 @fwrite(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i64 8, i64 1, i32* %6)
%31 = call i32 @getline(i8** %2, i32* %1, i32* %3)
%32 = icmp eq i32 %31, -1
br i1 %32, label %38, label %33
33: ; preds = %28, %33
%34 = load i8*, i8** %2
%35 = call i32 (i32*, i8*, ...) @fprintf(i32* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0), i8* %34)
%36 = call i32 @getline(i8** %2, i32* %1, i32* %3)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %33
38: ; preds = %33, %28
%39 = call i64 @fwrite(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0), i64 4, i64 1, i32* %6)
%40 = call i32 @fclose(i32* %3)
%41 = call i32 @fclose(i32* %6)
ret void
}