#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*/ s;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ gets(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
scalar_t__ strcmp(char *, char *);
int strlen(char *);
int main01(void) {
  char s[1024] = {0};
  FILE *p = fopen("D:\\temp\\a.txt", "w");
  while (1) {
    memset(s, 0, sizeof(s));
    gets(s);
    if (strcmp(s, "exit") == 0)
      break;
    int len = strlen(s);
    s[len] = '\n';
    fputs(s, p);
  }
  fclose(p);
  printf("end\n");
  return 0;
}


x86 assembly (clang, -O0):
<main01>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
lea    -0x400(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
lea    0xe5b(%rip),%rdi
lea    0xe62(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x408(%rbp)
lea    -0x400(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
lea    -0x400(%rbp),%rdi
call   1090 <gets@plt>
lea    -0x400(%rbp),%rdi
lea    0xe2b(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    11f9 <main01+0x79>
jmp    1232 <main01+0xb2>
lea    -0x400(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x40c(%rbp)
movslq -0x40c(%rbp),%rax
movb   $0xa,-0x400(%rbp,%rax,1)
0a
lea    -0x400(%rbp),%rdi
mov    -0x408(%rbp),%rsi
call   1060 <fputs@plt>
jmp    11b8 <main01+0x38>
mov    -0x408(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xdd0(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main01>:
push   %r15
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %rsp,%rbx
mov    $0x400,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <memset@plt>
lea    0xe5b(%rip),%rdi
lea    0xe62(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%r14
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    %rbx,%rdi
call   1090 <gets@plt>
lea    0xe3e(%rip),%rsi
mov    %rbx,%rdi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1230 <main01+0xb0>
mov    %rsp,%rbx
lea    0xe27(%rip),%r15
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
movb   $0xa,(%rsp,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <fputs@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    %rbx,%rdi
call   1090 <gets@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
jne    11f0 <main01+0x70>
mov    %r14,%rdi
call   1040 <fclose@plt>
lea    0xdd6(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [14 x i8] c"D:\\temp\\a.txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [5 x i8] c"exit\00"
@.str.3 = [5 x i8] c"end\0A\00"
define i32 @main01() {
%1 = alloca [1024 x i8]
%2 = alloca i32*
%3 = alloca i32
%4 = bitcast [1024 x i8]* %1 to i8*
%5 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %2
br label %6
6: ; preds = %0, %15
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%8 = call i32 @memset(i8* %7, i32 0, i32 1024)
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%10 = call i32 @gets(i8* %9)
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%12 = call i64 @strcmp(i8* %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %6
br label %24
15: ; preds = %6
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%17 = call i32 @strlen(i8* %16)
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %19
store i8 10, i8* %20
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%22 = load i32*, i32** %2
%23 = call i32 @fputs(i8* %21, i32* %22)
br label %6
24: ; preds = %14
%25 = load i32*, i32** %2
%26 = call i32 @fclose(i32* %25)
%27 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [14 x i8] c"D:\\temp\\a.txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [5 x i8] c"exit\00"
@str = [4 x i8] c"end\00"
define i32 @main01() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = call i32 @memset(i8* %2, i32 0, i32 1024)
%5 = call i32 @gets(i8* %2)
%6 = call i64 @strcmp(i8* %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%7 = icmp eq i64 %6, 0
br i1 %7, label %17, label %8
8: ; preds = %0, %8
%9 = call i32 @strlen(i8* %2)
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %10
store i8 10, i8* %11
%12 = call i32 @fputs(i8* %2, i32* %3)
%13 = call i32 @memset(i8* %2, i32 0, i32 1024)
%14 = call i32 @gets(i8* %2)
%15 = call i64 @strcmp(i8* %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %8
17: ; preds = %8, %0
%18 = call i32 @fclose(i32* %3)
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @str, i64 0, i64 0))
ret i32 0
}