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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char **, int);
int send_control_response(int, int, int);
int /*<<< orphan*/ write(int, char **, int);
int transmit_file(char *filename, int data_sock, int control_sock) {
  int err;
  int file;
  int data_length = 0;
  ssize_t bytes;
  char buffer[512];
  file = open(filename, O_RDONLY);
  if (file < 0) {
    err = send_control_response(control_sock, 403, 0);
    return err;
  }
  while (true) {
    bytes = read(file, &buffer, 512);
    if (bytes == -1) {
      err = send_control_response(control_sock, 501, data_length);
      break;
    } else if (bytes == 0) {
      err = send_control_response(control_sock, 200, data_length);
      break;
    }
    data_length += bytes;
    write(data_sock, &buffer, bytes);
  }
  close(file);
  return err;
}


x86 assembly (clang, -O0):
<transmit_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11a8 <transmit_file+0x58>
mov    -0x18(%rbp),%edi
mov    $0x193,%esi
xor    %edx,%edx
call   1060 <send_control_response@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    123b <transmit_file+0xeb>
jmp    11ad <transmit_file+0x5d>
mov    -0x20(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    $0x200,%edx
call   1050 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    11e6 <transmit_file+0x96>
mov    -0x18(%rbp),%edi
mov    -0x24(%rbp),%edx
mov    $0x1f5,%esi
call   1060 <send_control_response@plt>
mov    %eax,-0x1c(%rbp)
jmp    122d <transmit_file+0xdd>
cmpl   $0x0,-0x28(%rbp)
jne    1208 <transmit_file+0xb8>
mov    -0x18(%rbp),%edi
mov    -0x24(%rbp),%edx
mov    $0xc8,%esi
call   1060 <send_control_response@plt>
mov    %eax,-0x1c(%rbp)
jmp    122d <transmit_file+0xdd>
jmp    120d <transmit_file+0xbd>
mov    -0x28(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1030 <write@plt>
jmp    11ad <transmit_file+0x5d>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<transmit_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x200,%rsp
mov    %edx,%r14d
mov    %esi,%r15d
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     11aa <transmit_file+0x5a>
mov    %eax,%r12d
xor    %ebx,%ebx
mov    %rsp,%rbp
nop
mov    %r12d,%edi
mov    %rbp,%rsi
mov    $0x200,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
je     11c8 <transmit_file+0x78>
test   %eax,%eax
je     11cf <transmit_file+0x7f>
add    %eax,%ebx
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    1180 <transmit_file+0x30>
mov    %r14d,%edi
mov    $0x193,%esi
xor    %edx,%edx
add    $0x200,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <send_control_response@plt>
mov    $0x1f5,%esi
jmp    11d4 <transmit_file+0x84>
mov    $0xc8,%esi
mov    %r14d,%edi
mov    %ebx,%edx
call   1060 <send_control_response@plt>
mov    %eax,%ebx
mov    %r12d,%edi
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x200,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
define i32 @transmit_file(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca [512 x i8]
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 0, i32* %10
%13 = load i8*, i8** %5
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %9
%16 = load i32, i32* %9
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %3
%19 = load i32, i32* %7
%20 = call i32 @send_control_response(i32 %19, i32 403, i32 0)
store i32 %20, i32* %8
%21 = load i32, i32* %8
store i32 %21, i32* %4
br label %53
22: ; preds = %3
br label %23
23: ; preds = %22, %41
%24 = load i32, i32* %9
%25 = bitcast [512 x i8]* %12 to i8**
%26 = call i32 @read(i32 %24, i8** %25, i32 512)
store i32 %26, i32* %11
%27 = load i32, i32* %11
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %33
29: ; preds = %23
%30 = load i32, i32* %7
%31 = load i32, i32* %10
%32 = call i32 @send_control_response(i32 %30, i32 501, i32 %31)
store i32 %32, i32* %8
br label %49
33: ; preds = %23
%34 = load i32, i32* %11
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %33
%37 = load i32, i32* %7
%38 = load i32, i32* %10
%39 = call i32 @send_control_response(i32 %37, i32 200, i32 %38)
store i32 %39, i32* %8
br label %49
40: ; preds = %33
br label %41
41: ; preds = %40
%42 = load i32, i32* %11
%43 = load i32, i32* %10
%44 = add nsw i32 %43, %42
store i32 %44, i32* %10
%45 = load i32, i32* %6
%46 = bitcast [512 x i8]* %12 to i8**
%47 = load i32, i32* %11
%48 = call i32 @write(i32 %45, i8** %46, i32 %47)
br label %23
49: ; preds = %36, %29
%50 = load i32, i32* %9
%51 = call i32 @close(i32 %50)
%52 = load i32, i32* %8
store i32 %52, i32* %4
br label %53
53: ; preds = %49, %18
%54 = load i32, i32* %4
ret i32 %54
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
define i32 @transmit_file(i8* nocapture %0, i32 %1, i32 %2) {
%4 = alloca [512 x i8]
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %3
%10 = bitcast [512 x i8]* %4 to i8**
br label %13
11: ; preds = %3
%12 = tail call i32 @send_control_response(i32 %2, i32 403, i32 0)
br label %24
13: ; preds = %9, %16
%14 = phi i32 [ %17, %16 ], [ 0, %9 ]
%15 = call i32 @read(i32 %7, i8** %10, i32 512)
switch i32 %15, label %16 [
i32 -1, label %19
i32 0, label %20
]
16: ; preds = %13
%17 = add nsw i32 %15, %14
%18 = call i32 @write(i32 %1, i8** %10, i32 %15)
br label %13
19: ; preds = %13
br label %20
20: ; preds = %13, %19
%21 = phi i32 [ 501, %19 ], [ 200, %13 ]
%22 = tail call i32 @send_control_response(i32 %2, i32 %21, i32 %14)
%23 = tail call i32 @close(i32 %7)
br label %24
24: ; preds = %20, %11
%25 = phi i32 [ %12, %11 ], [ %22, %20 ]
ret i32 %25
}