#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 */
struct stat {
  scalar_t__ st_size;
};
typedef int /*<<< orphan*/ list;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EXIT_SUCCESS;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ remove(char *);
int /*<<< orphan*/ stat(char *, struct stat *);
int /*<<< orphan*/ strlen(char *);
int main(void) {
  FILE *fp = fopen("fgets.temp", "w");
  char list[] = {'0', '0', '0', '\0', 'e', 'o', 'f', -1,
                 'n', 'e', 'w', '\n', 'e', 'n', 'd', 'X'};
  printf("writing %lu bytes to fgets.temp; please view it with xxd.\n",
         sizeof(list));
  fwrite(list, sizeof(list), 1, fp);
  fclose(fp);
  struct stat s;
  stat("fgets.temp", &s);
  printf("file size is: %u\n", (int)s.st_size);
  fp = fopen("fgets.temp", "r");
  char bytes[100];
  fgets(bytes, 100, fp);
  printf("length of string read by fgets() is %lu bytes. String is: %s\n",
         strlen(bytes), bytes);
  fgets(bytes, 100, fp);
  printf("length of string read by fgets() is %lu bytes. String is: %s\n",
         strlen(bytes), bytes);
  fclose(fp);
  remove("fgets.temp");
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe67(%rip),%rdi
lea    0xe6b(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    0xef0(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xeed(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0xe47(%rip),%rdi
mov    $0x10,%esi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x10,%esi
mov    $0x1,%edx
call   10a0 <fwrite@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xe07(%rip),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <stat@plt>
mov    -0x28(%rbp),%rax
mov    %eax,%esi
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xde3(%rip),%rdi
lea    0xe36(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x64,%esi
call   1070 <fgets@plt>
lea    -0x90(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%esi
lea    -0x90(%rbp),%rdx
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x90(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x64,%esi
call   1070 <fgets@plt>
lea    -0x90(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%esi
lea    -0x90(%rbp),%rdx
lea    0xdc6(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xd53(%rip),%rdi
call   1030 <remove@plt>
mov    0x2d27(%rip),%rax
mov    (%rax),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x88,%rsp
lea    0xe6b(%rip),%r14
lea    0xe6f(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
mov    %rax,%rbx
movaps 0xef2(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
lea    0xe53(%rip),%rdi
mov    $0x10,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0x10(%rsp),%rdi
mov    $0x10,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10a0 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
call   1080 <stat@plt>
mov    0x8(%rsp),%esi
lea    0xe4b(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe4f(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
mov    %rax,%rbx
lea    0x20(%rsp),%r15
mov    %r15,%rdi
mov    $0x64,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
mov    %r15,%rdi
call   1050 <strlen@plt>
lea    0xe22(%rip),%r12
mov    %r12,%rdi
mov    %eax,%esi
mov    %r15,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15,%rdi
mov    $0x64,%esi
mov    %rbx,%rdx
call   1070 <fgets@plt>
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %r12,%rdi
mov    %eax,%esi
mov    %r15,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   1030 <remove@plt>
mov    0x2d59(%rip),%rax
mov    (%rax),%eax
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [11 x i8] c"fgets.temp\00"
@.str.1 = [2 x i8] c"w\00"
@__const.main.list = [16 x i8] c"000\00eof\FFnew\0AendX"
@.str.2 = [59 x i8] c"writing %lu bytes to fgets.temp; please view it with xxd.\0A\00"
@.str.3 = [18 x i8] c"file size is: %u\0A\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [62 x i8] c"length of string read by fgets() is %lu bytes. String is: %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [16 x i8]
%4 = alloca %struct.stat
%5 = alloca [100 x i8]
store i32 0, i32* %1
%6 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %2
%7 = bitcast [16 x i8]* %3 to i8*
%8 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0), i32 16)
%9 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%10 = load i32*, i32** %2
%11 = call i32 @fwrite(i8* %9, i32 16, i32 1, i32* %10)
%12 = load i32*, i32** %2
%13 = call i32 @fclose(i32* %12)
%14 = call i32 @stat(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), %struct.stat* %4)
%15 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%16 = load i64, i64* %15
%17 = trunc i64 %16 to i32
%18 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %17)
%19 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %19, i32** %2
%20 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%21 = load i32*, i32** %2
%22 = call i32 @fgets(i8* %20, i32 100, i32* %21)
%23 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%24 = call i32 @strlen(i8* %23)
%25 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%26 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.5, i64 0, i64 0), i32 %24, i8* %25)
%27 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%28 = load i32*, i32** %2
%29 = call i32 @fgets(i8* %27, i32 100, i32* %28)
%30 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%31 = call i32 @strlen(i8* %30)
%32 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%33 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.5, i64 0, i64 0), i32 %31, i8* %32)
%34 = load i32*, i32** %2
%35 = call i32 @fclose(i32* %34)
%36 = call i32 @remove(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%37 = load i32, i32* @EXIT_SUCCESS
ret i32 %37
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [11 x i8] c"fgets.temp\00"
@.str.1 = [2 x i8] c"w\00"
@__const.main.list = [16 x i8] c"000\00eof\FFnew\0AendX"
@.str.2 = [59 x i8] c"writing %lu bytes to fgets.temp; please view it with xxd.\0A\00"
@.str.3 = [18 x i8] c"file size is: %u\0A\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [62 x i8] c"length of string read by fgets() is %lu bytes. String is: %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca [16 x i8]
%2 = alloca %struct.stat
%3 = alloca [100 x i8]
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%6 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0), i32 16)
%7 = call i32 @fwrite(i8* %5, i32 16, i32 1, i32* %4)
%8 = tail call i32 @fclose(i32* %4)
%9 = bitcast %struct.stat* %2 to i8*
%10 = call i32 @stat(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), %struct.stat* %2)
%11 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%12 = load i64, i64* %11
%13 = trunc i64 %12 to i32
%14 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%15 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%16 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%17 = call i32 @fgets(i8* %16, i32 100, i32* %15)
%18 = call i32 @strlen(i8* %16)
%19 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.5, i64 0, i64 0), i32 %18, i8* %16)
%20 = call i32 @fgets(i8* %16, i32 100, i32* %15)
%21 = call i32 @strlen(i8* %16)
%22 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.5, i64 0, i64 0), i32 %21, i8* %16)
%23 = call i32 @fclose(i32* %15)
%24 = call i32 @remove(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%25 = load i32, i32* @EXIT_SUCCESS
ret i32 %25
}