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

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fatal(char *, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ free(char *);
char *getline_wrapped(int /*<<< orphan*/, unsigned int *);
struct index_node *index_create();
int /*<<< orphan*/ index_destroy(struct index_node *);
int /*<<< orphan*/ index_insert(struct index_node *, char *, char *,
                                unsigned int);
int /*<<< orphan*/ index_write(struct index_node *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stdin;
char *strchr(char *, char);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void write_index(const char *filename) {
  struct index_node *index;
  char *line, *pos;
  FILE *cfile;
  unsigned int linenum = 0;
  cfile = fopen(filename, "w");
  if (!cfile)
    fatal("Could not open %s for writing: %s\n", filename, strerror(errno));
  index = index_create();
  while ((line = getline_wrapped(stdin, &linenum))) {
    pos = strchr(line, ' ');
    *pos++ = '\0';
    index_insert(index, line, pos, linenum);
    free(line);
  }
  index_write(index, cfile);
  index_destroy(index);
  fclose(cfile);
}


x86 assembly (clang, -O0):
<write_index>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe32(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    120a <write_index+0x5a>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
lea    0xdfd(%rip),%rdi
call   10c0 <fatal@plt>
mov    $0x0,%al
call   1090 <index_create@plt>
mov    %rax,-0x10(%rbp)
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
lea    -0x2c(%rbp),%rsi
call   1040 <getline_wrapped@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     127b <write_index+0xcb>
mov    -0x18(%rbp),%rdi
mov    $0x20,%esi
call   1070 <strchr@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
call   1080 <index_insert@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
jmp    1215 <write_index+0x65>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <index_write@plt>
mov    -0x10(%rbp),%rdi
call   10d0 <index_destroy@plt>
mov    -0x28(%rbp),%rdi
call   1060 <fclose@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_index>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
movl   $0x0,0xc(%rsp)
lea    0xe31(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11fb <write_index+0x4b>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xe11(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   10c0 <fatal@plt>
xor    %eax,%eax
call   1090 <index_create@plt>
mov    %rax,%r12
mov    0x2ddc(%rip),%r13
mov    0x0(%r13),%edi
lea    0xc(%rsp),%rsi
call   1040 <getline_wrapped@plt>
test   %rax,%rax
je     126f <write_index+0xbf>
mov    %rax,%rbx
lea    0xc(%rsp),%r15
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x20,%esi
call   1070 <strchr@plt>
movb   $0x0,(%rax)
lea    0x1(%rax),%rdx
mov    0xc(%rsp),%ecx
mov    %r12,%rdi
mov    %rbx,%rsi
call   1080 <index_insert@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x0(%r13),%edi
mov    %r15,%rsi
call   1040 <getline_wrapped@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1230 <write_index+0x80>
mov    %r12,%rdi
mov    %r14,%rsi
call   1050 <index_write@plt>
mov    %r12,%rdi
call   10d0 <index_destroy@plt>
mov    %r14,%rdi
call   1060 <fclose@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.index_node = type { i32 }
@.str = [2 x i8] c"w\00"
@.str.1 = [35 x i8] c"Could not open %s for writing: %s\0A\00"
@errno = global i32 0
@stdin = global i32 0
define void @write_index(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.index_node*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %7
%8 = load i8*, i8** %2
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %6
%10 = load i32*, i32** %6
%11 = icmp ne i32* %10, null
br i1 %11, label %17, label %12
12: ; preds = %1
%13 = load i8*, i8** %2
%14 = load i32, i32* @errno
%15 = call i32 @strerror(i32 %14)
%16 = call i32 @fatal(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %13, i32 %15)
br label %17
17: ; preds = %12, %1
%18 = call %struct.index_node* (...) @index_create()
store %struct.index_node* %18, %struct.index_node** %3
br label %19
19: ; preds = %23, %17
%20 = load i32, i32* @stdin
%21 = call i8* @getline_wrapped(i32 %20, i32* %7)
store i8* %21, i8** %4
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %35
23: ; preds = %19
%24 = load i8*, i8** %4
%25 = call i8* @strchr(i8* %24, i8 signext 32)
store i8* %25, i8** %5
%26 = load i8*, i8** %5
%27 = getelementptr inbounds i8, i8* %26, i32 1
store i8* %27, i8** %5
store i8 0, i8* %26
%28 = load %struct.index_node*, %struct.index_node** %3
%29 = load i8*, i8** %4
%30 = load i8*, i8** %5
%31 = load i32, i32* %7
%32 = call i32 @index_insert(%struct.index_node* %28, i8* %29, i8* %30, i32 %31)
%33 = load i8*, i8** %4
%34 = call i32 @free(i8* %33)
br label %19
35: ; preds = %19
%36 = load %struct.index_node*, %struct.index_node** %3
%37 = load i32*, i32** %6
%38 = call i32 @index_write(%struct.index_node* %36, i32* %37)
%39 = load %struct.index_node*, %struct.index_node** %3
%40 = call i32 @index_destroy(%struct.index_node* %39)
%41 = load i32*, i32** %6
%42 = call i32 @fclose(i32* %41)
ret void
}

LLVM IR (-O2):
%struct.index_node = type { i32 }
@.str = [2 x i8] c"w\00"
@.str.1 = [35 x i8] c"Could not open %s for writing: %s\0A\00"
@errno = global i32 0
@stdin = global i32 0
define void @write_index(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %10
6: ; preds = %1
%7 = load i32, i32* @errno
%8 = tail call i32 @strerror(i32 %7)
%9 = tail call i32 @fatal(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %8)
br label %10
10: ; preds = %6, %1
%11 = tail call %struct.index_node* (...) @index_create()
%12 = load i32, i32* @stdin
%13 = call i8* @getline_wrapped(i32 %12, i32* %2)
%14 = icmp eq i8* %13, null
br i1 %14, label %25, label %15
15: ; preds = %10, %15
%16 = phi i8* [ %23, %15 ], [ %13, %10 ]
%17 = call i8* @strchr(i8* %16, i8 signext 32)
%18 = getelementptr inbounds i8, i8* %17, i64 1
store i8 0, i8* %17
%19 = load i32, i32* %2
%20 = call i32 @index_insert(%struct.index_node* %11, i8* %16, i8* %18, i32 %19)
%21 = call i32 @free(i8* %16)
%22 = load i32, i32* @stdin
%23 = call i8* @getline_wrapped(i32 %22, i32* %2)
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %15
25: ; preds = %15, %10
%26 = call i32 @index_write(%struct.index_node* %11, i32* %4)
%27 = call i32 @index_destroy(%struct.index_node* %11)
%28 = call i32 @fclose(i32* %4)
ret void
}