#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 EOF;
int /*<<< orphan*/ exit(int);
int fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int getchar();
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ stderr;
int main() {
  char *buffer;
  int index, r;
  int ch;
  const char filename[] = "challenge.txt";
  FILE *fp;
  buffer = (char *)malloc(sizeof(char) * 32);
  if (buffer == NULL) {
    fprintf(stderr, "Unable to allocate buffer\n");
    exit(1);
  }
  printf("Type some text: ");
  index = 0;
  while (1) {
    ch = getchar();
    *(buffer + index) = ch;
    index++;
    if (index == 31)
      break;
    if (ch == '\n' || ch == EOF)
      break;
  }
  *(buffer + index) = '\0';
  fp = fopen(filename, "w");
  if (fp == NULL) {
    fprintf(stderr, "Unable to create %s\n", filename);
    exit(1);
  }
  r = fwrite(buffer, 1, index, fp);
  if (r != index)
    puts("Error writing data");
  else
    printf("Wrote %d bytes to %s\n", index, filename);
  r = fclose(fp);
  if (r != 0) {
    fprintf(stderr, "Unable to properly close file\n");
    exit(1);
  }
  printf("%s created\n", filename);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe5a(%rip),%rax
mov    %rax,-0x2a(%rbp)
mov    0xe58(%rip),%eax
mov    %eax,-0x22(%rbp)
mov    0xe52(%rip),%ax
mov    %ax,-0x1e(%rbp)
mov    $0x20,%edi
call   1080 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11f8 <main+0x68>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
lea    0xe27(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x14(%rbp)
mov    $0x0,%al
call   1060 <getchar@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x1f,-0x14(%rbp)
jne    123f <main+0xaf>
jmp    1265 <main+0xd5>
cmpl   $0xa,-0x1c(%rbp)
je     125b <main+0xcb>
mov    -0x1c(%rbp),%eax
mov    0x2d95(%rip),%rcx
cmp    (%rcx),%eax
jne    1260 <main+0xd0>
jmp    1265 <main+0xd5>
jmp    120d <main+0x7d>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
lea    -0x2a(%rbp),%rdi
lea    0xdbe(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12b5 <main+0x125>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
lea    -0x2a(%rbp),%rdx
lea    0xd98(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
call   10b0 <fwrite@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     12ea <main+0x15a>
lea    0xd71(%rip),%rdi
call   1030 <puts@plt>
jmp    12ff <main+0x16f>
mov    -0x14(%rbp),%esi
lea    -0x2a(%rbp),%rdx
lea    0xd6c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1336 <main+0x1a6>
mov    0x2cbc(%rip),%rax
mov    (%rax),%edi
lea    0xd55(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    -0x2a(%rbp),%rsi
lea    0xd58(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
movabs $0x7478742e65676e,%rax
mov    %rax,0xe(%rsp)
movabs $0x676e656c6c616863,%rax
65 6e 67
mov    %rax,0x8(%rsp)
mov    $0x20,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1299 <main+0x109>
mov    %rax,%r14
lea    0xe53(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e04(%rip),%r15
xor    %r12d,%r12d
nopw   0x0(%rax,%rax,1)
mov    %r12,%rbx
xor    %eax,%eax
call   1060 <getchar@plt>
mov    %al,(%r14,%r12,1)
add    $0x1,%r12
cmp    $0x1e,%rbx
je     1212 <main+0x82>
cmp    $0xa,%eax
je     1212 <main+0x82>
cmp    (%r15),%eax
jne    11f0 <main+0x60>
movb   $0x0,(%r14,%r12,1)
lea    0xe1c(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     12ab <main+0x11b>
mov    %rax,%r15
mov    %r14,%rdi
mov    $0x1,%esi
mov    %r12d,%edx
mov    %rax,%rcx
call   10b0 <fwrite@plt>
cmp    %r12d,%eax
jne    1260 <main+0xd0>
lea    0xe15(%rip),%rdi
lea    0x8(%rsp),%rdx
mov    %r12d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    126c <main+0xdc>
lea    0xdea(%rip),%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
test   %eax,%eax
jne    12d1 <main+0x141>
lea    0xe1a(%rip),%rdi
lea    0x8(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
lea    0xd65(%rip),%rsi
jmp    12e1 <main+0x151>
mov    0x2d26(%rip),%rax
mov    (%rax),%edi
lea    0xd81(%rip),%rsi
lea    0x8(%rsp),%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2d00(%rip),%rax
mov    (%rax),%edi
lea    0xd99(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@__const.main.filename = [14 x i8] c"challenge.txt\00"
@stderr = global i32 0
@.str = [27 x i8] c"Unable to allocate buffer\0A\00"
@.str.1 = [17 x i8] c"Type some text: \00"
@EOF = global i32 0
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [21 x i8] c"Unable to create %s\0A\00"
@.str.4 = [19 x i8] c"Error writing data\00"
@.str.5 = [22 x i8] c"Wrote %d bytes to %s\0A\00"
@.str.6 = [31 x i8] c"Unable to properly close file\0A\00"
@.str.7 = [12 x i8] c"%s created\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [14 x i8]
%7 = alloca i32*
store i32 0, i32* %1
%8 = bitcast [14 x i8]* %6 to i8*
%9 = call i64 @malloc(i32 32)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %2
%11 = load i8*, i8** %2
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %17
13: ; preds = %0
%14 = load i32, i32* @stderr
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %0
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %19
19: ; preds = %17, %40
%20 = call i32 (...) @getchar()
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = trunc i32 %21 to i8
%23 = load i8*, i8** %2
%24 = load i32, i32* %3
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
store i8 %22, i8* %26
%27 = load i32, i32* %3
%28 = add nsw i32 %27, 1
store i32 %28, i32* %3
%29 = load i32, i32* %3
%30 = icmp eq i32 %29, 31
br i1 %30, label %31, label %32
31: ; preds = %19
br label %41
32: ; preds = %19
%33 = load i32, i32* %5
%34 = icmp eq i32 %33, 10
br i1 %34, label %39, label %35
35: ; preds = %32
%36 = load i32, i32* %5
%37 = load i32, i32* @EOF
%38 = icmp eq i32 %36, %37
br i1 %38, label %39, label %40
39: ; preds = %35, %32
br label %41
40: ; preds = %35
br label %19
41: ; preds = %39, %31
%42 = load i8*, i8** %2
%43 = load i32, i32* %3
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
store i8 0, i8* %45
%46 = getelementptr inbounds [14 x i8], [14 x i8]* %6, i64 0, i64 0
%47 = call i32* @fopen(i8* %46, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %47, i32** %7
%48 = load i32*, i32** %7
%49 = icmp eq i32* %48, null
br i1 %49, label %50, label %55
50: ; preds = %41
%51 = load i32, i32* @stderr
%52 = getelementptr inbounds [14 x i8], [14 x i8]* %6, i64 0, i64 0
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %52)
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %41
%56 = load i8*, i8** %2
%57 = load i32, i32* %3
%58 = load i32*, i32** %7
%59 = call i32 @fwrite(i8* %56, i32 1, i32 %57, i32* %58)
store i32 %59, i32* %4
%60 = load i32, i32* %4
%61 = load i32, i32* %3
%62 = icmp ne i32 %60, %61
br i1 %62, label %63, label %65
63: ; preds = %55
%64 = call i32 @puts(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
br label %69
65: ; preds = %55
%66 = load i32, i32* %3
%67 = getelementptr inbounds [14 x i8], [14 x i8]* %6, i64 0, i64 0
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %66, i8* %67)
br label %69
69: ; preds = %65, %63
%70 = load i32*, i32** %7
%71 = call i32 @fclose(i32* %70)
store i32 %71, i32* %4
%72 = load i32, i32* %4
%73 = icmp ne i32 %72, 0
br i1 %73, label %74, label %78
74: ; preds = %69
%75 = load i32, i32* @stderr
%76 = call i32 (i32, i8*, ...) @fprintf(i32 %75, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0))
%77 = call i32 @exit(i32 1)
unreachable
78: ; preds = %69
%79 = getelementptr inbounds [14 x i8], [14 x i8]* %6, i64 0, i64 0
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0), i8* %79)
ret i32 0
}

LLVM IR (-O2):
@__const.main.filename = [14 x i8] c"challenge.txt\00"
@stderr = global i32 0
@.str = [27 x i8] c"Unable to allocate buffer\0A\00"
@.str.1 = [17 x i8] c"Type some text: \00"
@EOF = global i32 0
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [21 x i8] c"Unable to create %s\0A\00"
@.str.4 = [19 x i8] c"Error writing data\00"
@.str.5 = [22 x i8] c"Wrote %d bytes to %s\0A\00"
@.str.6 = [31 x i8] c"Unable to properly close file\0A\00"
@.str.7 = [12 x i8] c"%s created\0A\00"
define i32 @main() {
%1 = alloca [14 x i8]
%2 = getelementptr inbounds [14 x i8], [14 x i8]* %1, i64 0, i64 0
%3 = tail call i64 @malloc(i32 32)
%4 = inttoptr i64 %3 to i8*
%5 = icmp eq i64 %3, 0
br i1 %5, label %6, label %10
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %12, %10
%13 = phi i64 [ %17, %12 ], [ 0, %10 ]
%14 = tail call i32 (...) @getchar()
%15 = trunc i32 %14 to i8
%16 = getelementptr inbounds i8, i8* %4, i64 %13
store i8 %15, i8* %16
%17 = add nuw nsw i64 %13, 1
%18 = icmp eq i64 %17, 31
%19 = icmp eq i32 %14, 10
%20 = or i1 %18, %19
%21 = load i32, i32* @EOF
%22 = icmp eq i32 %14, %21
%23 = select i1 %20, i1 true, i1 %22
br i1 %23, label %24, label %12
24: ; preds = %12
%25 = trunc i64 %17 to i32
%26 = and i64 %17, 4294967295
%27 = getelementptr inbounds i8, i8* %4, i64 %26
store i8 0, i8* %27
%28 = call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %34
30: ; preds = %24
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %2)
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %24
%35 = tail call i32 @fwrite(i8* %4, i32 1, i32 %25, i32* %28)
%36 = icmp eq i32 %35, %25
br i1 %36, label %39, label %37
37: ; preds = %34
%38 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
br label %41
39: ; preds = %34
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %25, i8* %2)
br label %41
41: ; preds = %39, %37
%42 = call i32 @fclose(i32* %28)
%43 = icmp eq i32 %42, 0
br i1 %43, label %48, label %44
44: ; preds = %41
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %41
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0), i8* %2)
ret i32 0
}