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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int recv(int, char *, int, int /*<<< orphan*/);
void writefile(int sockfd, FILE *fp) {
  ssize_t n;
  char buff[4096] = {0};
  while ((n = recv(sockfd, buff, 4096, 0)) > 0) {
    if (n == -1) {
      perror("Receive File Error");
      exit(1);
    }
    if (fwrite(buff, sizeof(char), n, fp) != n) {
      perror("Write File Error");
      exit(1);
    }
    memset(buff, 0, 4096);
  }
}


x86 assembly (clang, -O0):
<writefile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x1020(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1040 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jle    1206 <writefile+0xb6>
cmpl   $0xffffffff,-0x14(%rbp)
jne    11b7 <writefile+0x67>
lea    0xe58(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
lea    -0x1020(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
cmp    -0x14(%rbp),%eax
je     11ee <writefile+0x9e>
lea    0xe34(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
lea    -0x1020(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1040 <memset@plt>
jmp    1175 <writefile+0x25>
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writefile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %rsi,%r14
mov    %edi,%r15d
mov    %rsp,%rbx
mov    $0x1000,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    11cd <writefile+0x7d>
mov    %eax,%ebp
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r14,%rcx
call   1070 <fwrite@plt>
cmp    %ebp,%eax
jne    11db <writefile+0x8b>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1040 <memset@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     1190 <writefile+0x40>
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe1e(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"Receive File Error\00"
@.str.1 = [17 x i8] c"Write File Error\00"
define void @writefile(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca [4096 x i8]
store i32 %0, i32* %3
store i32* %1, i32** %4
%7 = bitcast [4096 x i8]* %6 to i8*
br label %8
8: ; preds = %29, %2
%9 = load i32, i32* %3
%10 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%11 = call i32 @recv(i32 %9, i8* %10, i32 4096, i32 0)
store i32 %11, i32* %5
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %32
13: ; preds = %8
%14 = load i32, i32* %5
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %13
%20 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%21 = load i32, i32* %5
%22 = load i32*, i32** %4
%23 = call i32 @fwrite(i8* %20, i32 1, i32 %21, i32* %22)
%24 = load i32, i32* %5
%25 = icmp ne i32 %23, %24
br i1 %25, label %26, label %29
26: ; preds = %19
%27 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %19
%30 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%31 = call i32 @memset(i8* %30, i32 0, i32 4096)
br label %8
32: ; preds = %8
ret void
}

LLVM IR (-O2):
@.str.1 = [17 x i8] c"Write File Error\00"
define void @writefile(i32 %0, i32* nocapture %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = call i32 @recv(i32 %0, i8* %4, i32 4096, i32 0)
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %18
7: ; preds = %2, %14
%8 = phi i32 [ %16, %14 ], [ %5, %2 ]
%9 = call i32 @fwrite(i8* %4, i32 1, i32 %8, i32* %1)
%10 = icmp eq i32 %9, %8
br i1 %10, label %14, label %11
11: ; preds = %7
%12 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %7
%15 = call i32 @memset(i8* %4, i32 0, i32 4096)
%16 = call i32 @recv(i32 %0, i8* %4, i32 4096, i32 0)
%17 = icmp sgt i32 %16, 0
br i1 %17, label %7, label %18
18: ; preds = %14, %2
ret void
}