#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 const *, char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
char getchar();
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  char *buffer;
  int index;
  char ch;
  const char filename[] = "challenge.txt";
  FILE *fp;
  buffer = (char *)malloc(sizeof(char) * 32);
  if (buffer == NULL) {
    printf("failed to malloc memory\n");
    return 1;
  }
  printf("Type some text: ");
  index = 0;
  while ((ch = getchar())) {
    *(buffer + index) = ch;
    index++;
    if (index > 31)
      break;
    if (ch == '\n')
      break;
  }
  *(buffer + index) = '\0';
  fp = fopen(filename, "w");
  fwrite(buffer, 1, index, fp);
  fclose(fp);
  printf("%s created\n", filename);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe8a(%rip),%rax
mov    %rax,-0x23(%rbp)
mov    0xe88(%rip),%eax
mov    %eax,-0x1b(%rbp)
mov    0xe82(%rip),%ax
mov    %ax,-0x17(%rbp)
mov    $0x20,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c1 <main+0x61>
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    127c <main+0x11c>
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x14(%rbp)
mov    $0x0,%al
call   1050 <getchar@plt>
mov    %al,-0x15(%rbp)
cmp    $0x0,%al
je     1225 <main+0xc5>
mov    -0x15(%rbp),%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)
jle    120e <main+0xae>
jmp    1225 <main+0xc5>
movsbl -0x15(%rbp),%eax
cmp    $0xa,%eax
jne    1220 <main+0xc0>
jmp    1225 <main+0xc5>
jmp    11d6 <main+0x76>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
lea    -0x23(%rbp),%rdi
lea    0xdfc(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1080 <fwrite@plt>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x23(%rbp),%rsi
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
movabs $0x7478742e65676e,%rax
mov    %rax,0x6(%rsp)
movabs $0x676e656c6c616863,%rax
65 6e 67
mov    %rax,(%rsp)
mov    $0x20,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11e3 <main+0x73>
mov    %rax,%r13
lea    0xe5b(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %ebx,%ebx
nopl   0x0(%rax)
xor    %eax,%eax
call   1060 <getchar@plt>
test   %al,%al
je     11f6 <main+0x86>
mov    %al,0x0(%r13,%rbx,1)
lea    0x1(%rbx),%rcx
cmp    $0x1e,%rbx
ja     11f9 <main+0x89>
mov    %rcx,%rbx
cmp    $0xa,%al
jne    11c0 <main+0x50>
jmp    11f9 <main+0x89>
lea    0xe43(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
jmp    1245 <main+0xd5>
mov    %rbx,%rcx
movslq %ecx,%r14
movb   $0x0,0x0(%r13,%r14,1)
lea    0xe16(%rip),%rsi
mov    %rsp,%r15
mov    %r15,%rdi
call   1080 <fopen@plt>
mov    %rax,%r12
mov    %r13,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %rax,%rcx
call   1090 <fwrite@plt>
mov    %r12,%rdi
call   1040 <fclose@plt>
lea    0xde8(%rip),%rdi
xor    %ebx,%ebx
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.main.filename = [14 x i8] c"challenge.txt\00"
@.str = [25 x i8] c"failed to malloc memory\0A\00"
@.str.1 = [17 x i8] c"Type some text: \00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [12 x i8] c"%s created\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8
%5 = alloca [14 x i8]
%6 = alloca i32*
store i32 0, i32* %1
%7 = bitcast [14 x i8]* %5 to i8*
%8 = call i64 @malloc(i32 32)
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** %2
%10 = load i8*, i8** %2
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %51
14: ; preds = %0
%15 = 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 %16
16: ; preds = %35, %14
%17 = call signext i8 (...) @getchar()
store i8 %17, i8* %4
%18 = icmp ne i8 %17, 0
br i1 %18, label %19, label %36
19: ; preds = %16
%20 = load i8, i8* %4
%21 = load i8*, i8** %2
%22 = load i32, i32* %3
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
store i8 %20, i8* %24
%25 = load i32, i32* %3
%26 = add nsw i32 %25, 1
store i32 %26, i32* %3
%27 = load i32, i32* %3
%28 = icmp sgt i32 %27, 31
br i1 %28, label %29, label %30
29: ; preds = %19
br label %36
30: ; preds = %19
%31 = load i8, i8* %4
%32 = sext i8 %31 to i32
%33 = icmp eq i32 %32, 10
br i1 %33, label %34, label %35
34: ; preds = %30
br label %36
35: ; preds = %30
br label %16
36: ; preds = %34, %29, %16
%37 = load i8*, i8** %2
%38 = load i32, i32* %3
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 0, i8* %40
%41 = getelementptr inbounds [14 x i8], [14 x i8]* %5, i64 0, i64 0
%42 = call i32* @fopen(i8* %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %42, i32** %6
%43 = load i8*, i8** %2
%44 = load i32, i32* %3
%45 = load i32*, i32** %6
%46 = call i32 @fwrite(i8* %43, i32 1, i32 %44, i32* %45)
%47 = load i32*, i32** %6
%48 = call i32 @fclose(i32* %47)
%49 = getelementptr inbounds [14 x i8], [14 x i8]* %5, i64 0, i64 0
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %49)
store i32 0, i32* %1
br label %51
51: ; preds = %36, %12
%52 = load i32, i32* %1
ret i32 %52
}

LLVM IR (-O2):
@__const.main.filename = [14 x i8] c"challenge.txt\00"
@.str.1 = [17 x i8] c"Type some text: \00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [12 x i8] c"%s created\0A\00"
@str = [24 x i8] c"failed to malloc memory\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 %8
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %30
8: ; preds = %0
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %14, %8
%11 = phi i64 [ %16, %14 ], [ 0, %8 ]
%12 = tail call signext i8 (...) @getchar()
%13 = icmp eq i8 %12, 0
br i1 %13, label %20, label %14
14: ; preds = %10
%15 = getelementptr inbounds i8, i8* %4, i64 %11
store i8 %12, i8* %15
%16 = add nuw nsw i64 %11, 1
%17 = icmp ugt i64 %11, 30
%18 = icmp eq i8 %12, 10
%19 = or i1 %17, %18
br i1 %19, label %20, label %10
20: ; preds = %14, %10
%21 = phi i64 [ %16, %14 ], [ %11, %10 ]
%22 = trunc i64 %21 to i32
%23 = shl i64 %21, 32
%24 = ashr exact i64 %23, 32
%25 = getelementptr inbounds i8, i8* %4, i64 %24
store i8 0, i8* %25
%26 = call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%27 = tail call i32 @fwrite(i8* %4, i32 1, i32 %22, i32* %26)
%28 = tail call i32 @fclose(i32* %26)
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %2)
br label %30
30: ; preds = %20, %6
%31 = phi i32 [ 1, %6 ], [ 0, %20 ]
ret i32 %31
}