#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 */
struct input {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
char *input_line(struct input *);
int /*<<< orphan*/ input_next(struct input *);
int /*<<< orphan*/ nthtoken(char *, char *, char *, int);
scalar_t__ startswith(char *, char *);
int write_file(struct input *input) {
  char separator[128];
  char filename[1024];
  FILE *output;
  char *line = input_line(input);
  nthtoken(separator, line, " \n", 1);
  nthtoken(filename, line, " \n", 3);
  output = fopen(filename, "w");
  input_next(input);
  while ((line = input_line(input))) {
    if (startswith(line, separator))
      break;
    fputs(line, output);
    input_next(input);
  }
  fclose(output);
  return 0;
}


x86 assembly (clang, -O0):
<write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4a0,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <input_line@plt>
mov    %rax,-0x4a0(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x4a0(%rbp),%rsi
lea    0xe5c(%rip),%rdx
mov    $0x1,%ecx
call   1030 <nthtoken@plt>
lea    -0x490(%rbp),%rdi
mov    -0x4a0(%rbp),%rsi
lea    0xe3d(%rip),%rdx
mov    $0x3,%ecx
call   1030 <nthtoken@plt>
lea    -0x490(%rbp),%rdi
lea    0xe28(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x498(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <input_next@plt>
mov    -0x8(%rbp),%rdi
call   1080 <input_line@plt>
mov    %rax,-0x4a0(%rbp)
cmp    $0x0,%rax
je     124d <write_file+0xdd>
mov    -0x4a0(%rbp),%rdi
lea    -0x90(%rbp),%rsi
call   1070 <startswith@plt>
cmp    $0x0,%rax
je     122c <write_file+0xbc>
jmp    124d <write_file+0xdd>
mov    -0x4a0(%rbp),%rdi
mov    -0x498(%rbp),%rsi
call   1060 <fputs@plt>
mov    -0x8(%rbp),%rdi
call   1040 <input_next@plt>
jmp    11f0 <write_file+0x80>
mov    -0x498(%rbp),%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x4a0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x488,%rsp
mov    %rdi,%r12
call   1080 <input_line@plt>
mov    %rax,%rbx
lea    0xe70(%rip),%r14
mov    %rsp,%rdi
mov    %rax,%rsi
mov    %r14,%rdx
mov    $0x1,%ecx
call   1030 <nthtoken@plt>
lea    0x80(%rsp),%r15
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
mov    $0x3,%ecx
call   1030 <nthtoken@plt>
lea    0xe3e(%rip),%rsi
mov    %r15,%rdi
call   1090 <fopen@plt>
mov    %rax,%r14
mov    %r12,%rdi
call   1040 <input_next@plt>
mov    %r12,%rdi
call   1080 <input_line@plt>
test   %rax,%rax
je     1223 <write_file+0xb3>
mov    %rax,%rbx
mov    %rsp,%r15
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r15,%rsi
call   1070 <startswith@plt>
test   %rax,%rax
jne    1223 <write_file+0xb3>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <fputs@plt>
mov    %r12,%rdi
call   1040 <input_next@plt>
mov    %r12,%rdi
call   1080 <input_line@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11f0 <write_file+0x80>
mov    %r14,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x488,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.input = type { i32 }
@.str = [3 x i8] c" \0A\00"
@.str.1 = [2 x i8] c"w\00"
define i32 @write_file(%struct.input* %0) {
%2 = alloca %struct.input*
%3 = alloca [128 x i8]
%4 = alloca [1024 x i8]
%5 = alloca i32*
%6 = alloca i8*
store %struct.input* %0, %struct.input** %2
%7 = load %struct.input*, %struct.input** %2
%8 = call i8* @input_line(%struct.input* %7)
store i8* %8, i8** %6
%9 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%10 = load i8*, i8** %6
%11 = call i32 @nthtoken(i8* %9, i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 1)
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%13 = load i8*, i8** %6
%14 = call i32 @nthtoken(i8* %12, i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 3)
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %16, i32** %5
%17 = load %struct.input*, %struct.input** %2
%18 = call i32 @input_next(%struct.input* %17)
br label %19
19: ; preds = %29, %1
%20 = load %struct.input*, %struct.input** %2
%21 = call i8* @input_line(%struct.input* %20)
store i8* %21, i8** %6
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %35
23: ; preds = %19
%24 = load i8*, i8** %6
%25 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%26 = call i64 @startswith(i8* %24, i8* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %23
br label %35
29: ; preds = %23
%30 = load i8*, i8** %6
%31 = load i32*, i32** %5
%32 = call i32 @fputs(i8* %30, i32* %31)
%33 = load %struct.input*, %struct.input** %2
%34 = call i32 @input_next(%struct.input* %33)
br label %19
35: ; preds = %28, %19
%36 = load i32*, i32** %5
%37 = call i32 @fclose(i32* %36)
ret i32 0
}

LLVM IR (-O2):
%struct.input = type { i32 }
@.str = [3 x i8] c" \0A\00"
@.str.1 = [2 x i8] c"w\00"
define i32 @write_file(%struct.input* %0) {
%2 = alloca [128 x i8]
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = tail call i8* @input_line(%struct.input* %0)
%7 = call i32 @nthtoken(i8* %4, i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 1)
%8 = call i32 @nthtoken(i8* %5, i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 3)
%9 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = call i32 @input_next(%struct.input* %0)
%11 = call i8* @input_line(%struct.input* %0)
%12 = icmp eq i8* %11, null
br i1 %12, label %22, label %13
13: ; preds = %1, %17
%14 = phi i8* [ %20, %17 ], [ %11, %1 ]
%15 = call i64 @startswith(i8* %14, i8* %4)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %13
%18 = call i32 @fputs(i8* %14, i32* %9)
%19 = call i32 @input_next(%struct.input* %0)
%20 = call i8* @input_line(%struct.input* %0)
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %13
22: ; preds = %17, %13, %1
%23 = call i32 @fclose(i32* %9)
ret i32 0
}