#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 off_t;

/* Variables and functions */
int /*<<< orphan*/ ENOMEM;
int /*<<< orphan*/ ERANGE;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
scalar_t__ lseek(int, int, int /*<<< orphan*/);
scalar_t__ malloc(size_t);
int read(int, char *, size_t);
int write(int, char *, int);
int recvfilesocket(int sock, int file, off_t *offset, off_t count) {
  char *buf;
  int rd;
  int wr;
  if (count < 1) {
    errno = ERANGE;
    return (-1);
  }
  if ((buf = (char *)malloc((size_t)count)) == NULL) {
    errno = ENOMEM;
    return (-1);
  }
  if (offset != NULL) {
    if (lseek(file, *offset, SEEK_SET) < 0) {
      free(buf);
      return (-1);
    }
  }
  rd = read(sock, buf, (size_t)count);
  if (rd != count) {
    free(buf);
    return (-1);
  }
  wr = write(file, buf, rd);
  if (offset != NULL)
    (*offset) += wr;
  free(buf);
  return (wr);
}


x86 assembly (clang, -O0):
<recvfilesocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jge    118d <recvfilesocket+0x3d>
mov    0x2e72(%rip),%rax
mov    (%rax),%ecx
mov    0x2e51(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1273 <recvfilesocket+0x123>
movslq -0x1c(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11c2 <recvfilesocket+0x72>
mov    0x2e2d(%rip),%rax
mov    (%rax),%ecx
mov    0x2e1c(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1273 <recvfilesocket+0x123>
cmpq   $0x0,-0x18(%rbp)
je     1208 <recvfilesocket+0xb8>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    0x2deb(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    $0x0,%rax
jge    1203 <recvfilesocket+0xb3>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1273 <recvfilesocket+0x123>
jmp    1208 <recvfilesocket+0xb8>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
movslq -0x1c(%rbp),%rdx
call   1060 <read@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     123c <recvfilesocket+0xec>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1273 <recvfilesocket+0x123>
mov    -0xc(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x30(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1264 <recvfilesocket+0x114>
mov    -0x30(%rbp),%ecx
mov    -0x18(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recvfilesocket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %ecx,%ecx
jle    11cd <recvfilesocket+0x7d>
mov    %ecx,%ebp
mov    %rdx,%r14
mov    %esi,%r15d
mov    %edi,%r12d
mov    %ecx,%ebx
mov    %rbx,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     11d6 <recvfilesocket+0x86>
mov    %rax,%r13
test   %r14,%r14
je     119a <recvfilesocket+0x4a>
mov    (%r14),%esi
mov    0x2e3d(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
call   1050 <lseek@plt>
test   %rax,%rax
js     11ea <recvfilesocket+0x9a>
mov    %r12d,%edi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1060 <read@plt>
cmp    %ebp,%eax
jne    11ea <recvfilesocket+0x9a>
mov    %r15d,%edi
mov    %r13,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
mov    %eax,%ebx
test   %r14,%r14
je     11c3 <recvfilesocket+0x73>
add    %ebx,(%r14)
mov    %r13,%rdi
call   1030 <free@plt>
jmp    11f7 <recvfilesocket+0xa7>
mov    0x2e14(%rip),%rax
jmp    11dd <recvfilesocket+0x8d>
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    0x2dea(%rip),%rcx
mov    %eax,(%rcx)
jmp    11f2 <recvfilesocket+0xa2>
mov    %r13,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@ERANGE = global i32 0
@errno = global i32 0
@ENOMEM = global i32 0
@SEEK_SET = global i32 0
define i32 @recvfilesocket(i32 %0, i32 %1, i32* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32* %2, i32** %8
store i32 %3, i32* %9
%13 = load i32, i32* %9
%14 = icmp slt i32 %13, 1
br i1 %14, label %15, label %17
15: ; preds = %4
%16 = load i32, i32* @ERANGE
store i32 %16, i32* @errno
store i32 -1, i32* %5
br label %67
17: ; preds = %4
%18 = load i32, i32* %9
%19 = sext i32 %18 to i64
%20 = call i64 @malloc(i64 %19)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %10
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = load i32, i32* @ENOMEM
store i32 %24, i32* @errno
store i32 -1, i32* %5
br label %67
25: ; preds = %17
%26 = load i32*, i32** %8
%27 = icmp ne i32* %26, null
br i1 %27, label %28, label %39
28: ; preds = %25
%29 = load i32, i32* %7
%30 = load i32*, i32** %8
%31 = load i32, i32* %30
%32 = load i32, i32* @SEEK_SET
%33 = call i64 @lseek(i32 %29, i32 %31, i32 %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %28
%36 = load i8*, i8** %10
%37 = call i32 @free(i8* %36)
store i32 -1, i32* %5
br label %67
38: ; preds = %28
br label %39
39: ; preds = %38, %25
%40 = load i32, i32* %6
%41 = load i8*, i8** %10
%42 = load i32, i32* %9
%43 = sext i32 %42 to i64
%44 = call i32 @read(i32 %40, i8* %41, i64 %43)
store i32 %44, i32* %11
%45 = load i32, i32* %11
%46 = load i32, i32* %9
%47 = icmp ne i32 %45, %46
br i1 %47, label %48, label %51
48: ; preds = %39
%49 = load i8*, i8** %10
%50 = call i32 @free(i8* %49)
store i32 -1, i32* %5
br label %67
51: ; preds = %39
%52 = load i32, i32* %7
%53 = load i8*, i8** %10
%54 = load i32, i32* %11
%55 = call i32 @write(i32 %52, i8* %53, i32 %54)
store i32 %55, i32* %12
%56 = load i32*, i32** %8
%57 = icmp ne i32* %56, null
br i1 %57, label %58, label %63
58: ; preds = %51
%59 = load i32, i32* %12
%60 = load i32*, i32** %8
%61 = load i32, i32* %60
%62 = add nsw i32 %61, %59
store i32 %62, i32* %60
br label %63
63: ; preds = %58, %51
%64 = load i8*, i8** %10
%65 = call i32 @free(i8* %64)
%66 = load i32, i32* %12
store i32 %66, i32* %5
br label %67
67: ; preds = %63, %48, %35, %23, %15
%68 = load i32, i32* %5
ret i32 %68
}

LLVM IR (-O2):
@ERANGE = global i32 0
@errno = global i32 0
@ENOMEM = global i32 0
@SEEK_SET = global i32 0
define i32 @recvfilesocket(i32 %0, i32 %1, i32* %2, i32 %3) {
%5 = icmp slt i32 %3, 1
br i1 %5, label %6, label %8
6: ; preds = %4
%7 = load i32, i32* @ERANGE
store i32 %7, i32* @errno
br label %36
8: ; preds = %4
%9 = zext i32 %3 to i64
%10 = tail call i64 @malloc(i64 %9)
%11 = inttoptr i64 %10 to i8*
%12 = icmp eq i64 %10, 0
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = load i32, i32* @ENOMEM
store i32 %14, i32* @errno
br label %36
15: ; preds = %8
%16 = icmp eq i32* %2, null
br i1 %16, label %24, label %17
17: ; preds = %15
%18 = load i32, i32* %2
%19 = load i32, i32* @SEEK_SET
%20 = tail call i64 @lseek(i32 %1, i32 %18, i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = tail call i32 @free(i8* %11)
br label %36
24: ; preds = %17, %15
%25 = tail call i32 @read(i32 %0, i8* %11, i64 %9)
%26 = icmp eq i32 %25, %3
br i1 %26, label %29, label %27
27: ; preds = %24
%28 = tail call i32 @free(i8* %11)
br label %36
29: ; preds = %24
%30 = tail call i32 @write(i32 %1, i8* %11, i32 %3)
br i1 %16, label %34, label %31
31: ; preds = %29
%32 = load i32, i32* %2
%33 = add nsw i32 %32, %30
store i32 %33, i32* %2
br label %34
34: ; preds = %31, %29
%35 = tail call i32 @free(i8* %11)
br label %36
36: ; preds = %34, %27, %22, %13, %6
%37 = phi i32 [ -1, %6 ], [ -1, %13 ], [ -1, %22 ], [ -1, %27 ], [ %30, %34 ]
ret i32 %37
}