#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 /*<<< orphan*/ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ read(int, char *, int);
char *safe_malloc(int);
int /*<<< orphan*/ server_error(int, char *);
scalar_t__ write(int, char *, int /*<<< orphan*/);
void client_write_file(int sock, int fd) {
  char *read_buf = safe_malloc(256);
  ssize_t bytes;
  while ((bytes = read(fd, read_buf, 256)) > 0) {
    if (write(sock, read_buf, bytes) < 0) {
      free(read_buf);
      close(fd);
      server_error(sock, "writing file to client");
    }
  }
  free(read_buf);
  close(fd);
}


x86 assembly (clang, -O0):
<client_write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    $0x100,%edi
call   1040 <safe_malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x100,%edx
call   1070 <read@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jle    11d7 <client_write_file+0x77>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <write@plt>
cmp    $0x0,%rax
jge    11d2 <client_write_file+0x72>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0x4(%rbp),%edi
lea    0xe33(%rip),%rsi
call   1080 <server_error@plt>
jmp    117c <client_write_file+0x1c>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<client_write_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%ebp
mov    $0x100,%edi
call   1040 <safe_malloc@plt>
mov    %rax,%rbx
mov    %r14d,%edi
mov    %rax,%rsi
mov    $0x100,%edx
call   1070 <read@plt>
test   %eax,%eax
jle    11e1 <client_write_file+0x81>
lea    0xe6c(%rip),%r15
jmp    11b4 <client_write_file+0x54>
cs nopw 0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
call   1070 <read@plt>
test   %eax,%eax
jle    11e1 <client_write_file+0x81>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write@plt>
test   %rax,%rax
jns    11a0 <client_write_file+0x40>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14d,%edi
call   1060 <close@plt>
mov    %ebp,%edi
mov    %r15,%rsi
call   1080 <server_error@plt>
jmp    11a0 <client_write_file+0x40>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14d,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <close@plt>

LLVM IR (-O0):
@.str = [23 x i8] c"writing file to client\00"
define void @client_write_file(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = call i8* @safe_malloc(i32 256)
store i8* %7, i8** %5
br label %8
8: ; preds = %26, %2
%9 = load i32, i32* %4
%10 = load i8*, i8** %5
%11 = call i32 @read(i32 %9, i8* %10, i32 256)
store i32 %11, i32* %6
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %27
13: ; preds = %8
%14 = load i32, i32* %3
%15 = load i8*, i8** %5
%16 = load i32, i32* %6
%17 = call i64 @write(i32 %14, i8* %15, i32 %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %26
19: ; preds = %13
%20 = load i8*, i8** %5
%21 = call i32 @free(i8* %20)
%22 = load i32, i32* %4
%23 = call i32 @close(i32 %22)
%24 = load i32, i32* %3
%25 = call i32 @server_error(i32 %24, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %26
26: ; preds = %19, %13
br label %8
27: ; preds = %8
%28 = load i8*, i8** %5
%29 = call i32 @free(i8* %28)
%30 = load i32, i32* %4
%31 = call i32 @close(i32 %30)
ret void
}

LLVM IR (-O2):
@.str = [23 x i8] c"writing file to client\00"
define void @client_write_file(i32 %0, i32 %1) {
%3 = tail call i8* @safe_malloc(i32 256)
%4 = tail call i32 @read(i32 %1, i8* %3, i32 256)
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %17
6: ; preds = %2, %14
%7 = phi i32 [ %15, %14 ], [ %4, %2 ]
%8 = tail call i64 @write(i32 %0, i8* %3, i32 %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %6
%11 = tail call i32 @free(i8* %3)
%12 = tail call i32 @close(i32 %1)
%13 = tail call i32 @server_error(i32 %0, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %10, %6
%15 = tail call i32 @read(i32 %1, i8* %3, i32 256)
%16 = icmp sgt i32 %15, 0
br i1 %16, label %6, label %17
17: ; preds = %14, %2
%18 = tail call i32 @free(i8* %3)
%19 = tail call i32 @close(i32 %1)
ret void
}