#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 MD5Context {
  int dummy;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ off_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ MD5Update(struct MD5Context *, unsigned char *, int);
int read(int, unsigned char *, int);
off_t hash_file(struct MD5Context *ctx, int fd) {
  off_t count = 0;
  ssize_t r;
  unsigned char buf[4096];
  while ((r = read(fd, buf, sizeof(buf))) > 0) {
    MD5Update(ctx, buf, r);
    count += r;
  }
  buf[0] = 0;
  MD5Update(ctx, buf, 1);
  return count;
}


x86 assembly (clang, -O0):
<hash_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jle    117a <hash_file+0x5a>
mov    -0x8(%rbp),%rdi
lea    -0x1020(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <MD5Update@plt>
mov    -0x14(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1139 <hash_file+0x19>
movb   $0x0,-0x1020(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x1020(%rbp),%rsi
mov    $0x1,%edx
call   1030 <MD5Update@plt>
mov    -0x10(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hash_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1000,%rsp
mov    %esi,%r15d
mov    %rdi,%r14
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0x1000,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    1177 <hash_file+0x57>
mov    %eax,%ebp
xor    %ebx,%ebx
mov    %rsp,%r12
mov    %r14,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
call   1030 <MD5Update@plt>
add    %ebp,%ebx
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     1150 <hash_file+0x30>
jmp    1179 <hash_file+0x59>
xor    %ebx,%ebx
movb   $0x0,(%rsp)
mov    %rsp,%rsi
mov    %r14,%rdi
mov    $0x1,%edx
call   1030 <MD5Update@plt>
mov    %ebx,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.MD5Context = type { i32 }
define i32 @hash_file(%struct.MD5Context* %0, i32 %1) {
%3 = alloca %struct.MD5Context*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [4096 x i8]
store %struct.MD5Context* %0, %struct.MD5Context** %3
store i32 %1, i32* %4
store i32 0, i32* %5
br label %8
8: ; preds = %13, %2
%9 = load i32, i32* %4
%10 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%11 = call i32 @read(i32 %9, i8* %10, i32 4096)
store i32 %11, i32* %6
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %21
13: ; preds = %8
%14 = load %struct.MD5Context*, %struct.MD5Context** %3
%15 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%16 = load i32, i32* %6
%17 = call i32 @MD5Update(%struct.MD5Context* %14, i8* %15, i32 %16)
%18 = load i32, i32* %6
%19 = load i32, i32* %5
%20 = add nsw i32 %19, %18
store i32 %20, i32* %5
br label %8
21: ; preds = %8
%22 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
store i8 0, i8* %22
%23 = load %struct.MD5Context*, %struct.MD5Context** %3
%24 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%25 = call i32 @MD5Update(%struct.MD5Context* %23, i8* %24, i32 1)
%26 = load i32, i32* %5
ret i32 %26
}

LLVM IR (-O2):
%struct.MD5Context = type { i32 }
define i32 @hash_file(%struct.MD5Context* %0, i32 %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = call i32 @read(i32 %1, i8* %4, i32 4096)
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %14
7: ; preds = %2, %7
%8 = phi i32 [ %12, %7 ], [ %5, %2 ]
%9 = phi i32 [ %11, %7 ], [ 0, %2 ]
%10 = call i32 @MD5Update(%struct.MD5Context* %0, i8* %4, i32 %8)
%11 = add nsw i32 %8, %9
%12 = call i32 @read(i32 %1, i8* %4, i32 4096)
%13 = icmp sgt i32 %12, 0
br i1 %13, label %7, label %14
14: ; preds = %7, %2
%15 = phi i32 [ 0, %2 ], [ %11, %7 ]
store i8 0, i8* %4
%16 = call i32 @MD5Update(%struct.MD5Context* %0, i8* %4, i32 1)
ret i32 %15
}