#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 string {
  int dummy;
};
typedef int /*<<< orphan*/ ssize_t;
typedef int /*<<< orphan*/ chunk;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fread(char **, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ string_concatb(struct string *, char *, int /*<<< orphan*/);
bool fs_read_fd(struct string *buffer, FILE *fd) {
  bool retval = false;
  char chunk[4096];
  ssize_t read_size;
  while ((read_size = fread(&chunk, 1, sizeof(chunk), fd)) > 0) {
    if (!string_concatb(buffer, chunk, read_size))
      goto bail_out;
  }
  retval = true;
bail_out:
  return retval;
}


x86 assembly (clang, -O0):
<fs_read_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    -0x1020(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1030 <fread@plt>
mov    %eax,-0x1024(%rbp)
cmp    $0x0,%eax
jle    118c <fs_read_fd+0x6c>
mov    -0x8(%rbp),%rdi
lea    -0x1020(%rbp),%rsi
mov    -0x1024(%rbp),%edx
call   1040 <string_concatb@plt>
cmp    $0x0,%eax
jne    1187 <fs_read_fd+0x67>
jmp    1193 <fs_read_fd+0x73>
jmp    113a <fs_read_fd+0x1a>
movl   $0x1,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fs_read_fd>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsp,%r15
mov    $0x1,%ebp
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jle    116c <fs_read_fd+0x4c>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1040 <string_concatb@plt>
test   %eax,%eax
jne    1140 <fs_read_fd+0x20>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.string = type { i32 }
define i32 @fs_read_fd(%struct.string* %0, i32* %1) {
%3 = alloca %struct.string*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca [4096 x i8]
%7 = alloca i32
store %struct.string* %0, %struct.string** %3
store i32* %1, i32** %4
store i32 0, i32* %5
br label %8
8: ; preds = %20, %2
%9 = bitcast [4096 x i8]* %6 to i8**
%10 = load i32*, i32** %4
%11 = call i32 @fread(i8** %9, i32 1, i32 4096, i32* %10)
store i32 %11, i32* %7
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %21
13: ; preds = %8
%14 = load %struct.string*, %struct.string** %3
%15 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%16 = load i32, i32* %7
%17 = call i32 @string_concatb(%struct.string* %14, i8* %15, i32 %16)
%18 = icmp ne i32 %17, 0
br i1 %18, label %20, label %19
19: ; preds = %13
br label %22
20: ; preds = %13
br label %8
21: ; preds = %8
store i32 1, i32* %5
br label %22
22: ; preds = %21, %19
%23 = load i32, i32* %5
ret i32 %23
}

LLVM IR (-O2):
%struct.string = type { i32 }
define i32 @fs_read_fd(%struct.string* %0, i32* nocapture %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = bitcast [4096 x i8]* %3 to i8**
br label %6
6: ; preds = %9, %2
%7 = call i32 @fread(i8** %5, i32 1, i32 4096, i32* %1)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %6
%10 = call i32 @string_concatb(%struct.string* %0, i8* %4, i32 %7)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %6
12: ; preds = %6, %9
%13 = phi i32 [ 0, %9 ], [ 1, %6 ]
ret i32 %13
}