#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, int, int);
int fscanf(int /*<<< orphan*/ *, char *, ...);
scalar_t__ strcmp(char *, char *);
int main(int argc, char **argv) {
  FILE *f1, *f2;
  int i, j = 0;
  char key[10];
  int prev_node, node, code;
  f1 = fopen(argv[2], "a");
  f2 = fopen(argv[1], "r");
  node = 0;
  prev_node = 0;
  while ((code = fscanf(f2, "%i", &node)) >= 0) {
    if (!code && node) {
      fscanf(f2, "%s", key);
      if (strcmp("cost", key) == 0)
        break;
      node = 0;
      j++;
      if (prev_node)
        fprintf(f1, "a %i %i %i 0\n", j, prev_node, node);
    } else if (!code) {
      fscanf(f2, "%s", key);
      prev_node = 0;
    } else if (code == 1) {
      j++;
      fprintf(f1, "a %i %i %i 0\n", j, prev_node, node);
      prev_node = node;
    }
  }
  j++;
  fprintf(f1, "a %i %i %i 0\n", j, node, 0);
  fclose(f1);
  fclose(f2);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe84(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe6e(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe4e(%rip),%rsi
lea    -0x3c(%rbp),%rdx
mov    $0x0,%al
call   1040 <fscanf@plt>
mov    %eax,-0x40(%rbp)
cmp    $0x0,%eax
jl     12bd <main+0x16d>
cmpl   $0x0,-0x40(%rbp)
jne    124f <main+0xff>
cmpl   $0x0,-0x3c(%rbp)
je     124f <main+0xff>
mov    -0x20(%rbp),%rdi
lea    -0x32(%rbp),%rdx
lea    0xe17(%rip),%rsi
mov    $0x0,%al
call   1040 <fscanf@plt>
lea    -0x32(%rbp),%rsi
lea    0xe08(%rip),%rdi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    1216 <main+0xc6>
jmp    12bd <main+0x16d>
movl   $0x0,-0x3c(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x38(%rbp)
je     124a <main+0xfa>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%edx
mov    -0x38(%rbp),%ecx
mov    -0x3c(%rbp),%r8d
lea    0xdca(%rip),%rsi
call   1060 <fprintf@plt>
jmp    12b8 <main+0x168>
cmpl   $0x0,-0x40(%rbp)
jne    127b <main+0x12b>
mov    -0x20(%rbp),%rdi
lea    -0x32(%rbp),%rdx
lea    0xd9f(%rip),%rsi
mov    $0x0,%al
call   1040 <fscanf@plt>
movl   $0x0,-0x38(%rbp)
jmp    12b3 <main+0x163>
cmpl   $0x1,-0x40(%rbp)
jne    12ae <main+0x15e>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%edx
mov    -0x38(%rbp),%ecx
mov    -0x3c(%rbp),%r8d
lea    0xd6c(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x38(%rbp)
jmp    12b3 <main+0x163>
jmp    12b8 <main+0x168>
jmp    11ab <main+0x5b>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%edx
mov    -0x3c(%rbp),%ecx
lea    0xd38(%rip),%rsi
xor    %r8d,%r8d
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
mov    0x10(%rsi),%rdi
lea    0xe94(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,0x10(%rsp)
mov    0x8(%rbx),%rdi
lea    0xe81(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
movl   $0x0,0xc(%rsp)
lea    0xe6c(%rip),%rsi
xor    %ebp,%ebp
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
xor    %eax,%eax
call   1040 <fscanf@plt>
test   %eax,%eax
js     1298 <main+0x148>
lea    0x1e(%rsp),%r12
lea    0xe47(%rip),%r13
lea    0xc(%rsp),%r15
xor    %ebp,%ebp
xor    %r14d,%r14d
jmp    1201 <main+0xb1>
nopl   0x0(%rax)
mov    0x10(%rsp),%rdi
lea    0xe33(%rip),%rsi
mov    %ebp,%edx
mov    %r14d,%ecx
xor    %r8d,%r8d
call   1060 <fprintf@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1040 <fscanf@plt>
test   %eax,%eax
js     1298 <main+0x148>
mov    0xc(%rsp),%r8d
test   %eax,%eax
jne    1250 <main+0x100>
test   %r8d,%r8d
je     1250 <main+0x100>
mov    %rbx,%rdi
lea    0xdee(%rip),%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1040 <fscanf@plt>
lea    0xde0(%rip),%rdi
mov    %r12,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     1298 <main+0x148>
movl   $0x0,0xc(%rsp)
add    $0x1,%ebp
test   %r14d,%r14d
jne    11d0 <main+0x80>
xor    %r14d,%r14d
jmp    11e9 <main+0x99>
nopl   0x0(%rax)
cmp    $0x1,%eax
je     1275 <main+0x125>
test   %eax,%eax
jne    11e9 <main+0x99>
xor    %r14d,%r14d
mov    %rbx,%rdi
lea    0xda1(%rip),%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1040 <fscanf@plt>
jmp    11e9 <main+0x99>
add    $0x1,%ebp
mov    0x10(%rsp),%rdi
lea    0xd8b(%rip),%rsi
mov    %ebp,%edx
mov    %r14d,%ecx
call   1060 <fprintf@plt>
mov    0xc(%rsp),%r14d
jmp    11e9 <main+0x99>
add    $0x1,%ebp
mov    0xc(%rsp),%ecx
lea    0xd69(%rip),%rsi
mov    0x10(%rsp),%r14
mov    %r14,%rdi
mov    %ebp,%edx
xor    %r8d,%r8d
call   1060 <fprintf@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"a\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%i\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [5 x i8] c"cost\00"
@.str.5 = [14 x i8] c"a %i %i %i 0\0A\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 [10 x i8]
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %9
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 2
%16 = load i8*, i8** %15
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %17, i32** %6
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %21, i32** %7
store i32 0, i32* %12
store i32 0, i32* %11
br label %22
22: ; preds = %73, %2
%23 = load i32*, i32** %7
%24 = call i32 (i32*, i8*, ...) @fscanf(i32* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %12)
store i32 %24, i32* %13
%25 = icmp sge i32 %24, 0
br i1 %25, label %26, label %74
26: ; preds = %22
%27 = load i32, i32* %13
%28 = icmp ne i32 %27, 0
br i1 %28, label %52, label %29
29: ; preds = %26
%30 = load i32, i32* %12
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %52
32: ; preds = %29
%33 = load i32*, i32** %7
%34 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
%35 = call i32 (i32*, i8*, ...) @fscanf(i32* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %34)
%36 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
%37 = call i64 @strcmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %36)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %32
br label %74
40: ; preds = %32
store i32 0, i32* %12
%41 = load i32, i32* %9
%42 = add nsw i32 %41, 1
store i32 %42, i32* %9
%43 = load i32, i32* %11
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %51
45: ; preds = %40
%46 = load i32*, i32** %6
%47 = load i32, i32* %9
%48 = load i32, i32* %11
%49 = load i32, i32* %12
%50 = call i32 @fprintf(i32* %46, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 %47, i32 %48, i32 %49)
br label %51
51: ; preds = %45, %40
br label %73
52: ; preds = %29, %26
%53 = load i32, i32* %13
%54 = icmp ne i32 %53, 0
br i1 %54, label %59, label %55
55: ; preds = %52
%56 = load i32*, i32** %7
%57 = getelementptr inbounds [10 x i8], [10 x i8]* %10, i64 0, i64 0
%58 = call i32 (i32*, i8*, ...) @fscanf(i32* %56, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %57)
store i32 0, i32* %11
br label %72
59: ; preds = %52
%60 = load i32, i32* %13
%61 = icmp eq i32 %60, 1
br i1 %61, label %62, label %71
62: ; preds = %59
%63 = load i32, i32* %9
%64 = add nsw i32 %63, 1
store i32 %64, i32* %9
%65 = load i32*, i32** %6
%66 = load i32, i32* %9
%67 = load i32, i32* %11
%68 = load i32, i32* %12
%69 = call i32 @fprintf(i32* %65, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 %66, i32 %67, i32 %68)
%70 = load i32, i32* %12
store i32 %70, i32* %11
br label %71
71: ; preds = %62, %59
br label %72
72: ; preds = %71, %55
br label %73
73: ; preds = %72, %51
br label %22
74: ; preds = %39, %22
%75 = load i32, i32* %9
%76 = add nsw i32 %75, 1
store i32 %76, i32* %9
%77 = load i32*, i32** %6
%78 = load i32, i32* %9
%79 = load i32, i32* %12
%80 = call i32 @fprintf(i32* %77, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 %78, i32 %79, i32 0)
%81 = load i32*, i32** %6
%82 = call i32 @fclose(i32* %81)
%83 = load i32*, i32** %7
%84 = call i32 @fclose(i32* %83)
%85 = load i32, i32* %3
ret i32 %85
}

LLVM IR (-O2):
@.str = [2 x i8] c"a\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%i\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [5 x i8] c"cost\00"
@.str.5 = [14 x i8] c"a %i %i %i 0\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [10 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%6 = bitcast i32* %4 to i8*
%7 = getelementptr inbounds i8*, i8** %1, i64 2
%8 = load i8*, i8** %7
%9 = tail call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %4
%13 = call i32 (i32*, i8*, ...) @fscanf(i32* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%14 = icmp sgt i32 %13, -1
br i1 %14, label %15, label %44
15: ; preds = %2, %39
%16 = phi i32 [ %42, %39 ], [ %13, %2 ]
%17 = phi i32 [ %41, %39 ], [ 0, %2 ]
%18 = phi i32 [ %40, %39 ], [ 0, %2 ]
%19 = icmp eq i32 %16, 0
%20 = load i32, i32* %4
%21 = icmp ne i32 %20, 0
%22 = select i1 %19, i1 %21, i1 false
br i1 %22, label %23, label %32
23: ; preds = %15
%24 = call i32 (i32*, i8*, ...) @fscanf(i32* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %5)
%25 = call i64 @strcmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %5)
%26 = icmp eq i64 %25, 0
br i1 %26, label %44, label %27
27: ; preds = %23
store i32 0, i32* %4
%28 = add nsw i32 %17, 1
%29 = icmp eq i32 %18, 0
br i1 %29, label %39, label %30
30: ; preds = %27
%31 = call i32 @fprintf(i32* %9, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 %28, i32 %18, i32 0)
br label %39
32: ; preds = %15
switch i32 %16, label %39 [
i32 0, label %33
i32 1, label %35
]
33: ; preds = %32
%34 = call i32 (i32*, i8*, ...) @fscanf(i32* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %5)
br label %39
35: ; preds = %32
%36 = add nsw i32 %17, 1
%37 = call i32 @fprintf(i32* %9, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 %36, i32 %18, i32 %20)
%38 = load i32, i32* %4
br label %39
39: ; preds = %32, %33, %35, %27, %30
%40 = phi i32 [ %18, %30 ], [ 0, %27 ], [ %38, %35 ], [ 0, %33 ], [ %18, %32 ]
%41 = phi i32 [ %28, %30 ], [ %28, %27 ], [ %36, %35 ], [ %17, %33 ], [ %17, %32 ]
%42 = call i32 (i32*, i8*, ...) @fscanf(i32* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%43 = icmp sgt i32 %42, -1
br i1 %43, label %15, label %44
44: ; preds = %39, %23, %2
%45 = phi i32 [ 0, %2 ], [ %17, %23 ], [ %41, %39 ]
%46 = add nsw i32 %45, 1
%47 = load i32, i32* %4
%48 = call i32 @fprintf(i32* %9, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 %46, i32 %47, i32 0)
%49 = call i32 @fclose(i32* %9)
%50 = call i32 @fclose(i32* %12)
ret i32 0
}