#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*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputc(int, int /*<<< orphan*/ *);
int getchar();
int /*<<< orphan*/ printf(char *);
void test_write_char() {
  FILE *fp;
  int ch;
  if ((fp = fopen("demo.txt", "w")) == NULL) {
    printf("\t Failure to open demo.txt!\n");
    exit(0);
  }
  printf("\n");
  ch = getchar();
  while (ch != '\n') {
    fputc(ch, fp);
    ch = getchar();
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<test_write_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xe91(%rip),%rdi
lea    0xe93(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
jne    119c <test_write_char+0x3c>
lea    0xe7b(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
lea    0xe83(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <getchar@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xa,-0xc(%rbp)
je     11d7 <test_write_char+0x77>
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%rsi
call   1050 <fputc@plt>
mov    $0x0,%al
call   1060 <getchar@plt>
mov    %eax,-0xc(%rbp)
jmp    11b2 <test_write_char+0x52>
mov    -0x8(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_write_char>:
push   %rbx
lea    0xe88(%rip),%rdi
lea    0xe8a(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11c1 <test_write_char+0x51>
mov    %rax,%rbx
mov    $0xa,%edi
call   1030 <putchar@plt>
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1070 <getchar@plt>
cmp    $0xa,%eax
je     11b8 <test_write_char+0x48>
mov    %eax,%edi
mov    %rbx,%rsi
call   1060 <fputc@plt>
jmp    11a0 <test_write_char+0x30>
mov    %rbx,%rdi
pop    %rbx
jmp    1050 <fclose@plt>
lea    0xe43(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"demo.txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [29 x i8] c"\09 Failure to open demo.txt!\0A\00"
@.str.3 = [2 x i8] c"\0A\00"
define void @test_write_char() {
%1 = alloca i32*
%2 = alloca i32
%3 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %3, i32** %1
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%7 = call i32 @exit(i32 0)
unreachable
8: ; preds = %0
%9 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%10 = call i32 (...) @getchar()
store i32 %10, i32* %2
br label %11
11: ; preds = %14, %8
%12 = load i32, i32* %2
%13 = icmp ne i32 %12, 10
br i1 %13, label %14, label %19
14: ; preds = %11
%15 = load i32, i32* %2
%16 = load i32*, i32** %1
%17 = call i32 @fputc(i32 %15, i32* %16)
%18 = call i32 (...) @getchar()
store i32 %18, i32* %2
br label %11
19: ; preds = %11
%20 = load i32*, i32** %1
%21 = call i32 @fclose(i32* %20)
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"demo.txt\00"
@.str.1 = [2 x i8] c"w\00"
@str = [28 x i8] c"\09 Failure to open demo.txt!\00"
define void @test_write_char() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%5 = tail call i32 @exit(i32 0)
unreachable
6: ; preds = %0
%7 = tail call i32 @putchar(i32 10)
%8 = tail call i32 (...) @getchar()
%9 = icmp eq i32 %8, 10
br i1 %9, label %15, label %10
10: ; preds = %6, %10
%11 = phi i32 [ %13, %10 ], [ %8, %6 ]
%12 = tail call i32 @fputc(i32 %11, i32* %1)
%13 = tail call i32 (...) @getchar()
%14 = icmp eq i32 %13, 10
br i1 %14, label %15, label %10
15: ; preds = %10, %6
%16 = tail call i32 @fclose(i32* %1)
ret void
}