int main() {
  int i;
  char buf[5];
  i = 78;
  puts("This program will demonstrate why gets() is a bad idea.\n"
       "When prompted for a string, make it longer than 5 characters.\n"
       "If you make it long enough, the program should crash.\n"
       "If it doesn't crash, that is worse, because now it will have\n"
       "unpredicatable state.\n");
  printf("Before string input, the value of the integer is %d\n", i);
  printf("Enter a string (5 characters reserved): ");
  gets(buf);
  printf("After string input:\n");
  printf("Address of string: %x\n", &buf);
  printf("Length of string: %d\n", strlen(buf));
  printf("String value: %s\n", buf);
  printf("Address of integer: %x\n", &i);
  printf("Integer value: %d\n", i);
  if (i != 78) {
    printf("The string has destroyed the integer value.\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x4e,-0x8(%rbp)
lea    0xea3(%rip),%rdi
mov    $0x0,%al
call   1030 <puts@plt>
mov    -0x8(%rbp),%esi
lea    0xf92(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xfb9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0xd(%rbp),%rdi
mov    $0x0,%al
call   1060 <gets@plt>
lea    0xfc9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xfd0(%rip),%rdi
lea    -0xd(%rbp),%rsi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0xd(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rsi
lea    0xfc9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0xd(%rbp),%rsi
lea    0xfcd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xfd1(%rip),%rdi
lea    -0x8(%rbp),%rsi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%esi
lea    0xfd4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x4e,-0x8(%rbp)
je     1215 <main+0xd5>
lea    0xfcf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
movl   $0x4e,0x4(%rsp)
lea    0xeac(%rip),%rdi
xor    %eax,%eax
call   1030 <puts@plt>
lea    0xf9e(%rip),%rdi
mov    $0x4e,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xfc0(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xb(%rsp),%rbx
mov    %rbx,%rdi
xor    %eax,%eax
call   1060 <gets@plt>
lea    0x1036(%rip),%rdi
call   1030 <puts@plt>
lea    0xfc0(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0xfbe(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xfc3(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xfc4(%rip),%rdi
lea    0x4(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x4(%rsp),%esi
lea    0xfc5(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
cmpl   $0x4e,0x4(%rsp)
je     120a <main+0xca>
lea    0xfd7(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [256 x i8] c"This program will demonstrate why gets() is a bad idea.\0AWhen prompted for a string, make it longer than 5 characters.\0AIf you make it long enough, the program should crash.\0AIf it doesn't crash, that is worse, because now it will have\0Aunpredicatable state.\0A\00"
@.str.1 = [53 x i8] c"Before string input, the value of the integer is %d\0A\00"
@.str.2 = [41 x i8] c"Enter a string (5 characters reserved): \00"
@.str.3 = [21 x i8] c"After string input:\0A\00"
@.str.4 = [23 x i8] c"Address of string: %x\0A\00"
@.str.5 = [22 x i8] c"Length of string: %d\0A\00"
@.str.6 = [18 x i8] c"String value: %s\0A\00"
@.str.7 = [24 x i8] c"Address of integer: %x\0A\00"
@.str.8 = [19 x i8] c"Integer value: %d\0A\00"
@.str.9 = [45 x i8] c"The string has destroyed the integer value.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [5 x i8]
store i32 0, i32* %1
store i32 78, i32* %2
%4 = call i32 (i8*, ...) bitcast (i32 (...)* @puts to i32 (i8*, ...)*)(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* %2
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
%8 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%9 = call i32 (i8*, ...) bitcast (i32 (...)* @gets to i32 (i8*, ...)*)(i8* %8)
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), [5 x i8]* %3)
%12 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%13 = call i64 @strlen(i8* %12)
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i64 %13)
%15 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i8* %15)
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i32* %2)
%18 = load i32, i32* %2
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0), i32 %18)
%20 = load i32, i32* %2
%21 = icmp ne i32 %20, 78
br i1 %21, label %22, label %24
22: ; preds = %0
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.9, i64 0, i64 0))
br label %24
24: ; preds = %22, %0
ret i32 0
}

LLVM IR (-O2):
@.str = [256 x i8] c"This program will demonstrate why gets() is a bad idea.\0AWhen prompted for a string, make it longer than 5 characters.\0AIf you make it long enough, the program should crash.\0AIf it doesn't crash, that is worse, because now it will have\0Aunpredicatable state.\0A\00"
@.str.1 = [53 x i8] c"Before string input, the value of the integer is %d\0A\00"
@.str.2 = [41 x i8] c"Enter a string (5 characters reserved): \00"
@.str.4 = [23 x i8] c"Address of string: %x\0A\00"
@.str.5 = [22 x i8] c"Length of string: %d\0A\00"
@.str.6 = [18 x i8] c"String value: %s\0A\00"
@.str.7 = [24 x i8] c"Address of integer: %x\0A\00"
@.str.8 = [19 x i8] c"Integer value: %d\0A\00"
@str = [20 x i8] c"After string input:\00"
@str.10 = [44 x i8] c"The string has destroyed the integer value.\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [5 x i8]
%3 = bitcast i32* %1 to i8*
%4 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 0
store i32 78, i32* %1
%5 = tail call i32 (i8*, ...) bitcast (i32 (...)* @puts to i32 (i8*, ...)*)(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 78)
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
%8 = call i32 (i8*, ...) bitcast (i32 (...)* @gets to i32 (i8*, ...)*)(i8* %4)
%9 = call i32 bitcast (i32 (...)* @puts to i32 (i8*)*)(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), [5 x i8]* %2)
%11 = call i64 @strlen(i8* %4)
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i64 %11)
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i8* %4)
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i32* %1)
%15 = load i32, i32* %1
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0), i32 %15)
%17 = load i32, i32* %1
%18 = icmp eq i32 %17, 78
br i1 %18, label %21, label %19
19: ; preds = %0
%20 = call i32 bitcast (i32 (...)* @puts to i32 (i8*)*)(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @str.10, i64 0, i64 0))
br label %21
21: ; preds = %19, %0
ret i32 0
}