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

/* Variables and functions */
int /*<<< orphan*/ exit(int /*<<< orphan*/);
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;
void test_fgets() {
  FILE *fp;
  char str[80];
  if ((fp = fopen("fgets.txt", "w")) == NULL) {
    printf("\t Failure to open demo.txt!\n");
    exit(0);
  }
  printf("\n");
  fgets(str, sizeof(str), stdin);
  fputs(str, fp);
  fclose(fp);
}


x86 assembly (clang, -O0):
<test_fgets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
lea    0xe91(%rip),%rdi
lea    0xe94(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
jne    119c <test_fgets+0x3c>
lea    0xe7c(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
lea    0xe84(%rip),%rdi
call   1040 <printf@plt>
lea    -0x60(%rbp),%rdi
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
mov    $0x50,%esi
call   1060 <fgets@plt>
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <fputs@plt>
mov    -0x8(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_fgets>:
push   %r14
push   %rbx
sub    $0x58,%rsp
lea    0xe82(%rip),%rdi
lea    0xe85(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11d0 <test_fgets+0x60>
mov    %rax,%rbx
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x2e45(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x50,%esi
call   1070 <fgets@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <fputs@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
add    $0x58,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe35(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"fgets.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"
@stdin = global i32 0
define void @test_fgets() {
%1 = alloca i32*
%2 = alloca [80 x i8]
%3 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 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 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%11 = load i32, i32* @stdin
%12 = call i32 @fgets(i8* %10, i32 80, i32 %11)
%13 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%14 = load i32*, i32** %1
%15 = call i32 @fputs(i8* %13, i32* %14)
%16 = load i32*, i32** %1
%17 = call i32 @fclose(i32* %16)
ret void
}

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