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

/* Variables and functions */
int BUFSIZ;
int EOF;
int /*<<< orphan*/ _IOFBF;
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fstat(int /*<<< orphan*/, struct stat *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int /*<<< orphan*/);
scalar_t__ setvbuf(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                   int /*<<< orphan*/, int /*<<< orphan*/);
int main(void) {
  FILE *fp = fopen("test.txt", "r");
  if (fp == NULL) {
    perror("fopen");
    return 1;
  }
  struct stat stats;
  if (fstat(fileno(fp), &stats) == -1) {
    perror("fstat");
    return 1;
  }
  printf("BUFSIZ is %d, but optimal block size is %ld\n", BUFSIZ,
         stats.st_blksize);
  if (setvbuf(fp, NULL, _IOFBF, stats.st_blksize) != 0) {
    perror("setvbuf failed");
    return 1;
  }
  int ch;
  while ((ch = fgetc(fp)) != EOF)
    ;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rdi
lea    0xe7c(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b9 <main+0x49>
lea    0xe63(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1266 <main+0xf6>
mov    -0x10(%rbp),%rdi
call   1050 <fileno@plt>
mov    %eax,%edi
lea    -0x18(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11ee <main+0x7e>
lea    0xe34(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1266 <main+0xf6>
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%edx
lea    0xe16(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2dbf(%rip),%rax
mov    (%rax),%edx
mov    -0x18(%rbp),%ecx
xor    %eax,%eax
mov    %eax,%esi
call   1060 <setvbuf@plt>
cmp    $0x0,%rax
je     1241 <main+0xd1>
lea    0xe14(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1266 <main+0xf6>
jmp    1246 <main+0xd6>
mov    -0x10(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d8f(%rip),%rcx
cmp    (%rcx),%eax
je     1266 <main+0xf6>
jmp    1246 <main+0xd6>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
lea    0xe86(%rip),%rdi
lea    0xe88(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11f6 <main+0x86>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <fileno@plt>
mov    %rsp,%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
cmp    $0xffffffff,%eax
je     11ff <main+0x8f>
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
mov    (%rsp),%ebp
lea    0xe5f(%rip),%rdi
mov    %ebp,%edx
call   1030 <printf@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
mov    %ebp,%ecx
call   1060 <setvbuf@plt>
test   %rax,%rax
jne    1208 <main+0x98>
mov    0x2e08(%rip),%rbp
mov    %rbx,%rdi
call   1040 <fgetc@plt>
cmp    0x0(%rbp),%eax
jne    11e0 <main+0x70>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe0e(%rip),%rdi
jmp    120f <main+0x9f>
lea    0xe0b(%rip),%rdi
jmp    120f <main+0x9f>
lea    0xe35(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
jmp    11ef <main+0x7f>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [6 x i8] c"fstat\00"
@.str.4 = [45 x i8] c"BUFSIZ is %d, but optimal block size is %ld\0A\00"
@BUFSIZ = global i32 0
@_IOFBF = global i32 0
@.str.5 = [15 x i8] c"setvbuf failed\00"
@EOF = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca %struct.stat
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %2
%6 = load i32*, i32** %2
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %37
10: ; preds = %0
%11 = load i32*, i32** %2
%12 = call i32 @fileno(i32* %11)
%13 = call i32 @fstat(i32 %12, %struct.stat* %3)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %37
17: ; preds = %10
%18 = load i32, i32* @BUFSIZ
%19 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0), i32 %18, i32 %20)
%22 = load i32*, i32** %2
%23 = load i32, i32* @_IOFBF
%24 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i64 @setvbuf(i32* %22, i32* null, i32 %23, i32 %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %17
%29 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %37
30: ; preds = %17
br label %31
31: ; preds = %36, %30
%32 = load i32*, i32** %2
%33 = call i32 @fgetc(i32* %32)
store i32 %33, i32* %4
%34 = load i32, i32* @EOF
%35 = icmp ne i32 %33, %34
br i1 %35, label %36, label %37
36: ; preds = %31
br label %31
37: ; preds = %8, %15, %28, %31
%38 = load i32, i32* %1
ret i32 %38
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [6 x i8] c"fstat\00"
@.str.4 = [45 x i8] c"BUFSIZ is %d, but optimal block size is %ld\0A\00"
@BUFSIZ = global i32 0
@_IOFBF = global i32 0
@.str.5 = [15 x i8] c"setvbuf failed\00"
@EOF = global i32 0
define i32 @main() {
%1 = alloca %struct.stat
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %28
6: ; preds = %0
%7 = bitcast %struct.stat* %1 to i8*
%8 = tail call i32 @fileno(i32* %2)
%9 = call i32 @fstat(i32 %8, %struct.stat* %1)
%10 = icmp eq i32 %9, -1
br i1 %10, label %23, label %11
11: ; preds = %6
%12 = load i32, i32* @BUFSIZ
%13 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%14 = load i32, i32* %13
%15 = tail call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0), i32 %12, i32 %14)
%16 = load i32, i32* @_IOFBF
%17 = tail call i64 @setvbuf(i32* %2, i32* null, i32 %16, i32 %14)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %11, %19
%20 = tail call i32 @fgetc(i32* %2)
%21 = load i32, i32* @EOF
%22 = icmp eq i32 %20, %21
br i1 %22, label %26, label %19
23: ; preds = %11, %6
%24 = phi i8* [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), %6 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), %11 ]
%25 = tail call i32 @perror(i8* %24)
br label %26
26: ; preds = %19, %23
%27 = phi i32 [ 1, %23 ], [ 0, %19 ]
br label %28
28: ; preds = %26, %4
%29 = phi i32 [ 1, %4 ], [ %27, %26 ]
ret i32 %29
}