#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 */

/* Variables and functions */
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char *);
void change_greeting(char *buf) {
  char data[1024];
  printf("New greeting: ");
  if (!fgets(data, 1023, stdin))
    _exit(1);
  size_t len = strlen(data);
  if ((len > 0) && (data[len - 1] == '\n'))
    data[len - 1] = 0;
  strcpy(buf, data);
}


x86 assembly (clang, -O0):
<change_greeting>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe9a(%rip),%rdi
call   1060 <printf@plt>
lea    -0x410(%rbp),%rdi
mov    0x2e6f(%rip),%rax
mov    (%rax),%edx
mov    $0x3ff,%esi
call   1070 <fgets@plt>
cmp    $0x0,%eax
jne    1198 <change_greeting+0x48>
mov    $0x1,%edi
call   1030 <_exit@plt>
lea    -0x410(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
jbe    11e8 <change_greeting+0x98>
mov    -0x418(%rbp),%rax
sub    $0x1,%rax
movsbl -0x410(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    11e8 <change_greeting+0x98>
mov    -0x418(%rbp),%rax
sub    $0x1,%rax
movb   $0x0,-0x410(%rbp,%rax,1)
mov    -0x8(%rbp),%rdi
lea    -0x410(%rbp),%rsi
call   1040 <strcpy@plt>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<change_greeting>:
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%r14
lea    0xe9c(%rip),%rdi
call   1060 <printf@plt>
mov    0x2e78(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rdi
mov    $0x3ff,%esi
call   1070 <fgets@plt>
test   %eax,%eax
je     11b5 <change_greeting+0x65>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
test   %rax,%rax
je     119f <change_greeting+0x4f>
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    119f <change_greeting+0x4f>
movb   $0x0,-0x1(%rsp,%rax,1)
mov    %rsp,%rsi
mov    %r14,%rdi
call   1040 <strcpy@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret
mov    $0x1,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"New greeting: \00"
@stdin = global i32 0
define void @change_greeting(i8* %0) {
%2 = alloca i8*
%3 = alloca [1024 x i8]
%4 = alloca i64
store i8* %0, i8** %2
%5 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = load i32, i32* @stdin
%8 = call i32 @fgets(i8* %6, i32 1023, i32 %7)
%9 = icmp ne i32 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %1
%11 = call i32 @_exit(i32 1)
unreachable
12: ; preds = %1
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%14 = call i64 @strlen(i8* %13)
store i64 %14, i64* %4
%15 = load i64, i64* %4
%16 = icmp ugt i64 %15, 0
br i1 %16, label %17, label %28
17: ; preds = %12
%18 = load i64, i64* %4
%19 = sub i64 %18, 1
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %19
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 10
br i1 %23, label %24, label %28
24: ; preds = %17
%25 = load i64, i64* %4
%26 = sub i64 %25, 1
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %26
store i8 0, i8* %27
br label %28
28: ; preds = %24, %17, %12
%29 = load i8*, i8** %2
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%31 = call i32 @strcpy(i8* %29, i8* %30)
ret void
}

LLVM IR (-O2):
@.str = [15 x i8] c"New greeting: \00"
@stdin = global i32 0
define void @change_greeting(i8* %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @stdin
%6 = call i32 @fgets(i8* %3, i32 1023, i32 %5)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @_exit(i32 1)
unreachable
10: ; preds = %1
%11 = call i64 @strlen(i8* %3)
%12 = icmp eq i64 %11, 0
br i1 %12, label %19, label %13
13: ; preds = %10
%14 = add i64 %11, -1
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %14
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 10
br i1 %17, label %18, label %19
18: ; preds = %13
store i8 0, i8* %15
br label %19
19: ; preds = %18, %13, %10
%20 = call i32 @strcpy(i8* %0, i8* %3)
ret void
}