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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
void my_fgets(char *path) {
  FILE *fp = NULL;
  fp = fopen(path, "r+");
  if (fp == NULL) {
    perror("my_fgets fopen");
    return;
  }
  char buf[100];
  while (!feof(fp)) {
    char *p = fgets(buf, sizeof(buf), fp);
    if (p != NULL) {
      printf("buf = %s", buf);
      printf("p = %s", p);
    }
  }
  printf("\n");
  if (fp != NULL) {
    fclose(fp);
    fp = NULL;
  }
}


x86 assembly (clang, -O0):
<my_fgets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe7e(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a7 <my_fgets+0x47>
lea    0xe66(%rip),%rdi
call   1080 <perror@plt>
jmp    1247 <my_fgets+0xe7>
jmp    11ac <my_fgets+0x4c>
mov    -0x10(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11ca <my_fgets+0x6a>
jmp    121d <my_fgets+0xbd>
lea    -0x80(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x64,%esi
call   1050 <fgets@plt>
mov    %rax,-0x88(%rbp)
cmpq   $0x0,-0x88(%rbp)
je     1218 <my_fgets+0xb8>
lea    -0x80(%rbp),%rsi
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x88(%rbp),%rsi
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11ac <my_fgets+0x4c>
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpq   $0x0,-0x10(%rbp)
je     1247 <my_fgets+0xe7>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x10(%rbp)
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_fgets>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x70,%rsp
lea    0xe7c(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1220 <my_fgets+0xb0>
mov    %rax,%r13
mov    %rax,%rdi
call   1070 <feof@plt>
test   %eax,%eax
je     11c1 <my_fgets+0x51>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r13,%rdi
call   1040 <fclose@plt>
add    $0x70,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    %rsp,%r14
lea    0xe47(%rip),%r15
lea    0xe49(%rip),%r12
jmp    11ec <my_fgets+0x7c>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r13,%rdi
call   1070 <feof@plt>
test   %eax,%eax
jne    11a1 <my_fgets+0x31>
mov    %r14,%rdi
mov    $0x64,%esi
mov    %r13,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     11e0 <my_fgets+0x70>
mov    %rax,%rbx
mov    %r15,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11e0 <my_fgets+0x70>
lea    0xddc(%rip),%rdi
add    $0x70,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1090 <perror@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"r+\00"
@.str.1 = [15 x i8] c"my_fgets fopen\00"
@.str.2 = [9 x i8] c"buf = %s\00"
@.str.3 = [7 x i8] c"p = %s\00"
@.str.4 = [2 x i8] c"\0A\00"
define void @my_fgets(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca [100 x i8]
%5 = alloca i8*
store i8* %0, i8** %2
store i32* null, i32** %3
%6 = load i8*, i8** %2
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %3
%8 = load i32*, i32** %3
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %37
12: ; preds = %1
br label %13
13: ; preds = %29, %12
%14 = load i32*, i32** %3
%15 = call i32 @feof(i32* %14)
%16 = icmp ne i32 %15, 0
%17 = xor i1 %16, true
br i1 %17, label %18, label %30
18: ; preds = %13
%19 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%20 = load i32*, i32** %3
%21 = call i8* @fgets(i8* %19, i32 100, i32* %20)
store i8* %21, i8** %5
%22 = load i8*, i8** %5
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %29
24: ; preds = %18
%25 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %25)
%27 = load i8*, i8** %5
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %24, %18
br label %13
30: ; preds = %13
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%32 = load i32*, i32** %3
%33 = icmp ne i32* %32, null
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = load i32*, i32** %3
%36 = call i32 @fclose(i32* %35)
store i32* null, i32** %3
br label %37
37: ; preds = %10, %34, %30
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"r+\00"
@.str.1 = [15 x i8] c"my_fgets fopen\00"
@.str.2 = [9 x i8] c"buf = %s\00"
@.str.3 = [7 x i8] c"p = %s\00"
define void @my_fgets(i8* nocapture %0) {
%2 = alloca [100 x i8]
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %23
7: ; preds = %1
%8 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%9 = call i32 @feof(i32* %3)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %20
11: ; preds = %7, %17
%12 = call i8* @fgets(i8* %8, i32 100, i32* %3)
%13 = icmp eq i8* %12, null
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %8)
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* %12)
br label %17
17: ; preds = %14, %11
%18 = call i32 @feof(i32* %3)
%19 = icmp eq i32 %18, 0
br i1 %19, label %11, label %20
20: ; preds = %17, %7
%21 = call i32 @putchar(i32 10)
%22 = call i32 @fclose(i32* %3)
br label %23
23: ; preds = %20, %5
ret void
}