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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ stat(char *, struct stat *);
int strlen(char *);
int /*<<< orphan*/ trio_printf(char *, char *, ...);
void test_read_dir(char *file) {
  FILE *f;
  char buff[200];
  struct stat st;
  f = fopen("/docs/list.txt", "r");
  if (!f) {
    trio_printf("file %s nof found\n", file);
    return;
  }
  while (fgets(buff, 200, f) != NULL) {
    buff[strlen(buff) - 1] = '\0';
    stat(buff, &st);
    trio_printf("%s, %zu,\n", buff, st.st_size);
  }
  fclose(f);
  return;
}


x86 assembly (clang, -O0):
<test_read_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe8a(%rip),%rdi
lea    0xe92(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a8 <test_read_dir+0x48>
mov    -0x8(%rbp),%rsi
lea    0xe75(%rip),%rdi
mov    $0x0,%al
call   1030 <trio_printf@plt>
jmp    1221 <test_read_dir+0xc1>
jmp    11ad <test_read_dir+0x4d>
lea    -0xe0(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0xc8,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     1218 <test_read_dir+0xb8>
lea    -0xe0(%rbp),%rdi
call   1050 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0xe0(%rbp,%rax,1)
lea    -0xe0(%rbp),%rdi
lea    -0xe8(%rbp),%rsi
call   1070 <stat@plt>
lea    -0xe0(%rbp),%rsi
mov    -0xe8(%rbp),%edx
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1030 <trio_printf@plt>
jmp    11ad <test_read_dir+0x4d>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_read_dir>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0xd8,%rsp
mov    %rdi,%rbx
lea    0xe88(%rip),%rdi
lea    0xe90(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1217 <test_read_dir+0xb7>
mov    %rax,%r14
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xc8,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1200 <test_read_dir+0xa0>
lea    0x8(%rsp),%r15
lea    0xe6e(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %rbx,%rdi
mov    %r15,%rsi
call   1070 <stat@plt>
mov    0x8(%rsp),%edx
mov    %r12,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1030 <trio_printf@plt>
mov    %rbx,%rdi
mov    $0xc8,%esi
mov    %r14,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
jne    11c0 <test_read_dir+0x60>
mov    %r14,%rdi
call   1040 <fclose@plt>
add    $0xd8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdf3(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
add    $0xd8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <trio_printf@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [15 x i8] c"/docs/list.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [19 x i8] c"file %s nof found\0A\00"
@.str.3 = [10 x i8] c"%s, %zu,\0A\00"
define void @test_read_dir(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca [200 x i8]
%5 = alloca %struct.stat
store i8* %0, i8** %2
%6 = call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 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** %3
%7 = load i32*, i32** %3
%8 = icmp ne i32* %7, null
br i1 %8, label %12, label %9
9: ; preds = %1
%10 = load i8*, i8** %2
%11 = call i32 (i8*, i8*, ...) @trio_printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %10)
br label %33
12: ; preds = %1
br label %13
13: ; preds = %18, %12
%14 = getelementptr inbounds [200 x i8], [200 x i8]* %4, i64 0, i64 0
%15 = load i32*, i32** %3
%16 = call i32* @fgets(i8* %14, i32 200, i32* %15)
%17 = icmp ne i32* %16, null
br i1 %17, label %18, label %30
18: ; preds = %13
%19 = getelementptr inbounds [200 x i8], [200 x i8]* %4, i64 0, i64 0
%20 = call i32 @strlen(i8* %19)
%21 = sub nsw i32 %20, 1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [200 x i8], [200 x i8]* %4, i64 0, i64 %22
store i8 0, i8* %23
%24 = getelementptr inbounds [200 x i8], [200 x i8]* %4, i64 0, i64 0
%25 = call i32 @stat(i8* %24, %struct.stat* %5)
%26 = getelementptr inbounds [200 x i8], [200 x i8]* %4, i64 0, i64 0
%27 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 (i8*, i8*, ...) @trio_printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %26, i32 %28)
br label %13
30: ; preds = %13
%31 = load i32*, i32** %3
%32 = call i32 @fclose(i32* %31)
br label %33
33: ; preds = %30, %9
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [15 x i8] c"/docs/list.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [19 x i8] c"file %s nof found\0A\00"
@.str.3 = [10 x i8] c"%s, %zu,\0A\00"
define void @test_read_dir(i8* %0) {
%2 = alloca [200 x i8]
%3 = alloca %struct.stat
%4 = getelementptr inbounds [200 x i8], [200 x i8]* %2, i64 0, i64 0
%5 = bitcast %struct.stat* %3 to i8*
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %13, label %8
8: ; preds = %1
%9 = call i32* @fgets(i8* %4, i32 200, i32* %6)
%10 = icmp eq i32* %9, null
br i1 %10, label %25, label %11
11: ; preds = %8
%12 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
br label %15
13: ; preds = %1
%14 = tail call i32 (i8*, i8*, ...) @trio_printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %27
15: ; preds = %11, %15
%16 = call i32 @strlen(i8* %4)
%17 = add nsw i32 %16, -1
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [200 x i8], [200 x i8]* %2, i64 0, i64 %18
store i8 0, i8* %19
%20 = call i32 @stat(i8* %4, %struct.stat* %3)
%21 = load i32, i32* %12
%22 = call i32 (i8*, i8*, ...) @trio_printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %4, i32 %21)
%23 = call i32* @fgets(i8* %4, i32 200, i32* %6)
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %15
25: ; preds = %15, %8
%26 = call i32 @fclose(i32* %6)
br label %27
27: ; preds = %25, %13
ret void
}