#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*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdin;
int strlen(char *);
int main(void) {
  int len;
  char filename[256];
  char text[256];
  FILE *fp;
  printf("filename: ");
  fgets(filename, 256, stdin);
  len = strlen(filename);
  if (filename[len - 1] == '\n') {
    filename[len - 1] = '\0';
  }
  fp = fopen(filename, "w");
  if (fp == NULL) {
    printf("Can't open file!\n");
    return -1;
  }
  printf("text: ");
  fgets(text, 256, stdin);
  len = strlen(text);
  if (text[len - 1] == '\n') {
    text[len - 1] = '\0';
  }
  fputs(text, fp);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe87(%rip),%rdi
call   1050 <printf@plt>
lea    -0x110(%rbp),%rdi
mov    0x2e5c(%rip),%rax
mov    (%rax),%edx
mov    $0x100,%esi
call   1070 <fgets@plt>
lea    -0x110(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x110(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    11d0 <main+0x70>
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x110(%rbp,%rax,1)
lea    -0x110(%rbp),%rdi
lea    0xe2d(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x218(%rbp)
cmpq   $0x0,-0x218(%rbp)
jne    1210 <main+0xb0>
lea    0xe0e(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1294 <main+0x134>
lea    0xe08(%rip),%rdi
call   1050 <printf@plt>
lea    -0x210(%rbp),%rdi
mov    0x2dbe(%rip),%rax
mov    (%rax),%edx
mov    $0x100,%esi
call   1070 <fgets@plt>
lea    -0x210(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x210(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    126e <main+0x10e>
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x210(%rbp,%rax,1)
lea    -0x210(%rbp),%rdi
mov    -0x218(%rbp),%rsi
call   1060 <fputs@plt>
mov    -0x218(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x208,%rsp
lea    0xe7f(%rip),%rdi
call   1060 <printf@plt>
mov    0x2e5b(%rip),%r14
mov    (%r14),%edx
lea    0x100(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1080 <fgets@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    11be <main+0x4e>
movb   $0x0,0xff(%rsp,%rax,1)
lea    0xe46(%rip),%rsi
lea    0x100(%rsp),%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1227 <main+0xb7>
mov    %rax,%rbx
lea    0xe2c(%rip),%rdi
call   1060 <printf@plt>
mov    (%r14),%edx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x100,%esi
call   1080 <fgets@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%r14,1)
jne    1210 <main+0xa0>
movb   $0x0,-0x1(%rsp,%rax,1)
mov    %rsp,%rdi
mov    %rbx,%rsi
call   1070 <fputs@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    1238 <main+0xc8>
lea    0xde6(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [11 x i8] c"filename: \00"
@stdin = global i32 0
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [18 x i8] c"Can't open file!\0A\00"
@.str.3 = [7 x i8] c"text: \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [256 x i8]
%4 = alloca [256 x i8]
%5 = alloca i32*
store i32 0, i32* %1
%6 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%8 = load i32, i32* @stdin
%9 = call i32 @fgets(i8* %7, i32 256, i32 %8)
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%11 = call i32 @strlen(i8* %10)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = sub nsw i32 %12, 1
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %14
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 10
br i1 %18, label %19, label %24
19: ; preds = %0
%20 = load i32, i32* %2
%21 = sub nsw i32 %20, 1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %22
store i8 0, i8* %23
br label %24
24: ; preds = %19, %0
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %26, i32** %5
%27 = load i32*, i32** %5
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %56
31: ; preds = %24
%32 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%34 = load i32, i32* @stdin
%35 = call i32 @fgets(i8* %33, i32 256, i32 %34)
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%37 = call i32 @strlen(i8* %36)
store i32 %37, i32* %2
%38 = load i32, i32* %2
%39 = sub nsw i32 %38, 1
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 %40
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp eq i32 %43, 10
br i1 %44, label %45, label %50
45: ; preds = %31
%46 = load i32, i32* %2
%47 = sub nsw i32 %46, 1
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 %48
store i8 0, i8* %49
br label %50
50: ; preds = %45, %31
%51 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%52 = load i32*, i32** %5
%53 = call i32 @fputs(i8* %51, i32* %52)
%54 = load i32*, i32** %5
%55 = call i32 @fclose(i32* %54)
store i32 0, i32* %1
br label %56
56: ; preds = %50, %29
%57 = load i32, i32* %1
ret i32 %57
}

LLVM IR (-O2):
@.str = [11 x i8] c"filename: \00"
@stdin = global i32 0
@.str.1 = [2 x i8] c"w\00"
@.str.3 = [7 x i8] c"text: \00"
@str = [17 x i8] c"Can't open file!\00"
define i32 @main() {
%1 = alloca [256 x i8]
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%5 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @stdin
%7 = call i32 @fgets(i8* %3, i32 256, i32 %6)
%8 = call i32 @strlen(i8* %3)
%9 = add nsw i32 %8, -1
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 %10
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 10
br i1 %13, label %14, label %15
14: ; preds = %0
store i8 0, i8* %11
br label %15
15: ; preds = %14, %0
%16 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %34
20: ; preds = %15
%21 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%22 = load i32, i32* @stdin
%23 = call i32 @fgets(i8* %4, i32 256, i32 %22)
%24 = call i32 @strlen(i8* %4)
%25 = add nsw i32 %24, -1
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %26
%28 = load i8, i8* %27
%29 = icmp eq i8 %28, 10
br i1 %29, label %30, label %31
30: ; preds = %20
store i8 0, i8* %27
br label %31
31: ; preds = %30, %20
%32 = call i32 @fputs(i8* %4, i32* %16)
%33 = call i32 @fclose(i32* %16)
br label %34
34: ; preds = %31, %18
%35 = phi i32 [ -1, %18 ], [ 0, %31 ]
ret i32 %35
}