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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(int /*<<< orphan*/ *, char *);
int fread(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int fwrite(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ read_err();
int /*<<< orphan*/ std_err();
int /*<<< orphan*/ write_err();
void putfile(u_char *fname, FILE *fdout, u_int size) {
  FILE *fdin;
  int len;
  u_char buff[32768];
  fdin = fopen(fname, "rb");
  if (!fdin)
    std_err();
  for (len = sizeof(buff) - 1; size; size -= len) {
    if (len > size)
      len = size;
    if (fread(buff, len, 1, fdin) != 1)
      read_err();
    if (fwrite(buff, len, 1, fdout) != 1)
      write_err();
  }
  fclose(fdin);
}


x86 assembly (clang, -O0):
<putfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20030,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe6f(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ac <putfile+0x3c>
mov    $0x0,%al
call   1090 <std_err@plt>
movl   $0x1ffff,-0x24(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1234 <putfile+0xc4>
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    11cf <putfile+0x5f>
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
lea    -0x20030(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     11f7 <putfile+0x87>
mov    $0x0,%al
call   1050 <read_err@plt>
lea    -0x20030(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
call   1070 <fwrite@plt>
cmp    $0x1,%eax
je     121f <putfile+0xaf>
mov    $0x0,%al
call   1080 <write_err@plt>
jmp    1224 <putfile+0xb4>
mov    -0x24(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    11b3 <putfile+0x43>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x20030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<putfile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20000,%rsp
mov    %edx,%ebx
mov    %rsi,%r14
lea    0xe75(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r15
test   %rax,%rax
je     119e <putfile+0x2e>
test   %ebx,%ebx
jne    11a9 <putfile+0x39>
jmp    1207 <putfile+0x97>
xor    %eax,%eax
call   1090 <std_err@plt>
test   %ebx,%ebx
je     1207 <putfile+0x97>
mov    $0x1ffff,%ebp
mov    %rsp,%r12
jmp    11c4 <putfile+0x54>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
sub    %ebp,%ebx
je     1207 <putfile+0x97>
cmp    %ebx,%ebp
cmovg  %ebx,%ebp
mov    %r12,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     11e7 <putfile+0x77>
xor    %eax,%eax
call   1050 <read_err@plt>
mov    %r12,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1070 <fwrite@plt>
cmp    $0x1,%eax
je     11c0 <putfile+0x50>
xor    %eax,%eax
call   1080 <write_err@plt>
jmp    11c0 <putfile+0x50>
mov    %r15,%rdi
add    $0x20000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fclose@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
define void @putfile(i32* %0, i32* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca [32768 x i32]
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%10 = load i32*, i32** %4
%11 = call i32* @fopen(i32* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %7
%12 = load i32*, i32** %7
%13 = icmp ne i32* %12, null
br i1 %13, label %16, label %14
14: ; preds = %3
%15 = call i32 (...) @std_err()
br label %16
16: ; preds = %14, %3
store i32 131071, i32* %8
br label %17
17: ; preds = %43, %16
%18 = load i32, i32* %6
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %47
20: ; preds = %17
%21 = load i32, i32* %8
%22 = load i32, i32* %6
%23 = icmp sgt i32 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = load i32, i32* %6
store i32 %25, i32* %8
br label %26
26: ; preds = %24, %20
%27 = getelementptr inbounds [32768 x i32], [32768 x i32]* %9, i64 0, i64 0
%28 = load i32, i32* %8
%29 = load i32*, i32** %7
%30 = call i32 @fread(i32* %27, i32 %28, i32 1, i32* %29)
%31 = icmp ne i32 %30, 1
br i1 %31, label %32, label %34
32: ; preds = %26
%33 = call i32 (...) @read_err()
br label %34
34: ; preds = %32, %26
%35 = getelementptr inbounds [32768 x i32], [32768 x i32]* %9, i64 0, i64 0
%36 = load i32, i32* %8
%37 = load i32*, i32** %5
%38 = call i32 @fwrite(i32* %35, i32 %36, i32 1, i32* %37)
%39 = icmp ne i32 %38, 1
br i1 %39, label %40, label %42
40: ; preds = %34
%41 = call i32 (...) @write_err()
br label %42
42: ; preds = %40, %34
br label %43
43: ; preds = %42
%44 = load i32, i32* %8
%45 = load i32, i32* %6
%46 = sub nsw i32 %45, %44
store i32 %46, i32* %6
br label %17
47: ; preds = %17
%48 = load i32*, i32** %7
%49 = call i32 @fclose(i32* %48)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
define void @putfile(i32* nocapture %0, i32* nocapture %1, i32 %2) {
%4 = alloca [32768 x i32]
%5 = bitcast [32768 x i32]* %4 to i8*
%6 = tail call i32* @fopen(i32* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 (...) @std_err()
br label %10
10: ; preds = %8, %3
%11 = icmp eq i32 %2, 0
br i1 %11, label %31, label %12
12: ; preds = %10
%13 = getelementptr inbounds [32768 x i32], [32768 x i32]* %4, i64 0, i64 0
br label %14
14: ; preds = %12, %28
%15 = phi i32 [ 131071, %12 ], [ %18, %28 ]
%16 = phi i32 [ %2, %12 ], [ %29, %28 ]
%17 = icmp sgt i32 %15, %16
%18 = select i1 %17, i32 %16, i32 %15
%19 = call i32 @fread(i32* %13, i32 %18, i32 1, i32* %6)
%20 = icmp eq i32 %19, 1
br i1 %20, label %23, label %21
21: ; preds = %14
%22 = tail call i32 (...) @read_err()
br label %23
23: ; preds = %21, %14
%24 = call i32 @fwrite(i32* %13, i32 %18, i32 1, i32* %1)
%25 = icmp eq i32 %24, 1
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = tail call i32 (...) @write_err()
br label %28
28: ; preds = %23, %26
%29 = sub nsw i32 %16, %18
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %14
31: ; preds = %28, %10
%32 = tail call i32 @fclose(i32* %6)
ret void
}