#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, short *, int);
scalar_t__ write(int, short *, int);
int deliver_file(char *path, int fdout) {
  int fd = 0, bytes = 0, error = 0;
  short buf[320];
  if ((fd = open(path, O_RDONLY))) {
    while ((bytes = read(fd, buf, 320)) > 0) {
      if (write(fdout, buf, bytes) < 0) {
        error = -2;
        break;
      }
    }
    if (fd)
      close(fd);
  } else
    return -1;
  return error;
}


x86 assembly (clang, -O0):
<deliver_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2a0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
je     11ee <deliver_file+0xae>
jmp    118a <deliver_file+0x4a>
mov    -0x18(%rbp),%edi
lea    -0x2a0(%rbp),%rsi
mov    $0x140,%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    11d7 <deliver_file+0x97>
mov    -0x14(%rbp),%edi
lea    -0x2a0(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    11d2 <deliver_file+0x92>
movl   $0xfffffffe,-0x20(%rbp)
jmp    11d7 <deliver_file+0x97>
jmp    118a <deliver_file+0x4a>
cmpl   $0x0,-0x18(%rbp)
je     11e9 <deliver_file+0xa9>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
jmp    11fa <deliver_file+0xba>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <deliver_file+0xc0>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2a0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<deliver_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x280,%rsp
mov    %esi,%r14d
mov    0x2e93(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
je     119c <deliver_file+0x5c>
mov    %eax,%ebx
mov    %rsp,%rbp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x140,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    11a3 <deliver_file+0x63>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
jns    1170 <deliver_file+0x30>
mov    $0xfffffffe,%ebp
jmp    11a5 <deliver_file+0x65>
mov    $0xffffffff,%ebp
jmp    11ac <deliver_file+0x6c>
xor    %ebp,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%eax
add    $0x280,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
define i32 @deliver_file(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [320 x i16]
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
%10 = load i8*, i8** %4
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %6
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %35
14: ; preds = %2
br label %15
15: ; preds = %27, %14
%16 = load i32, i32* %6
%17 = getelementptr inbounds [320 x i16], [320 x i16]* %9, i64 0, i64 0
%18 = call i32 @read(i32 %16, i16* %17, i32 320)
store i32 %18, i32* %7
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %28
20: ; preds = %15
%21 = load i32, i32* %5
%22 = getelementptr inbounds [320 x i16], [320 x i16]* %9, i64 0, i64 0
%23 = load i32, i32* %7
%24 = call i64 @write(i32 %21, i16* %22, i32 %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %20
store i32 -2, i32* %8
br label %28
27: ; preds = %20
br label %15
28: ; preds = %26, %15
%29 = load i32, i32* %6
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = load i32, i32* %6
%33 = call i32 @close(i32 %32)
br label %34
34: ; preds = %31, %28
br label %36
35: ; preds = %2
store i32 -1, i32* %3
br label %38
36: ; preds = %34
%37 = load i32, i32* %8
store i32 %37, i32* %3
br label %38
38: ; preds = %36, %35
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
define i32 @deliver_file(i8* nocapture %0, i32 %1) {
%3 = alloca [320 x i16]
%4 = bitcast [320 x i16]* %3 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp eq i32 %6, 0
br i1 %7, label %19, label %8
8: ; preds = %2
%9 = getelementptr inbounds [320 x i16], [320 x i16]* %3, i64 0, i64 0
br label %10
10: ; preds = %8, %13
%11 = call i32 @read(i32 %6, i16* %9, i32 320)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = call i64 @write(i32 %1, i16* %9, i32 %11)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %10
16: ; preds = %10, %13
%17 = phi i32 [ 0, %10 ], [ -2, %13 ]
%18 = tail call i32 @close(i32 %6)
br label %19
19: ; preds = %2, %16
%20 = phi i32 [ %17, %16 ], [ -1, %2 ]
ret i32 %20
}