#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 /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ send(int, char *, int, int /*<<< orphan*/);
void do_get(int connfd, char *fname) {
  int fd, nbyte;
  char buf[64];
  if ((fd = open(fname, O_RDONLY)) < 0) {
    perror("fail to open");
    buf[0] = 'N';
    send(connfd, buf, 64, 0);
    return;
  }
  buf[0] = 'Y';
  send(connfd, buf, 64, 0);
  while ((nbyte = read(fd, buf, 64)) > 0) {
    send(connfd, buf, nbyte, 0);
  }
  close(fd);
  return;
}


x86 assembly (clang, -O0):
<do_get>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11a5 <do_get+0x55>
lea    0xe7c(%rip),%rdi
call   1070 <perror@plt>
movb   $0x4e,-0x60(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0x40,%edx
xor    %ecx,%ecx
call   1030 <send@plt>
jmp    11f7 <do_get+0xa7>
movb   $0x59,-0x60(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0x40,%edx
xor    %ecx,%ecx
call   1030 <send@plt>
mov    -0x14(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0x40,%edx
call   1050 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    11ef <do_get+0x9f>
mov    -0x4(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    -0x18(%rbp),%edx
xor    %ecx,%ecx
call   1030 <send@plt>
jmp    11bc <do_get+0x6c>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_get>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %rsi,%rax
mov    %edi,%r14d
mov    0x2e83(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11e2 <do_get+0x92>
mov    %eax,%ebp
movb   $0x59,(%rsp)
mov    %rsp,%rbx
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x40,%edx
xor    %ecx,%ecx
call   1030 <send@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x40,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    11d2 <do_get+0x82>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1030 <send@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x40,%edx
call   1050 <read@plt>
test   %eax,%eax
jg     11b0 <do_get+0x60>
mov    %ebp,%edi
call   1040 <close@plt>
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe17(%rip),%rdi
call   1070 <perror@plt>
movb   $0x4e,(%rsp)
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x40,%edx
xor    %ecx,%ecx
call   1030 <send@plt>
jmp    11d9 <do_get+0x89>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [13 x i8] c"fail to open\00"
define void @do_get(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [64 x i8]
store i32 %0, i32* %3
store i8* %1, i8** %4
%8 = load i8*, i8** %4
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %5
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%14 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
store i8 78, i8* %14
%15 = load i32, i32* %3
%16 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%17 = call i32 @send(i32 %15, i8* %16, i32 64, i32 0)
br label %36
18: ; preds = %2
%19 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
store i8 89, i8* %19
%20 = load i32, i32* %3
%21 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%22 = call i32 @send(i32 %20, i8* %21, i32 64, i32 0)
br label %23
23: ; preds = %28, %18
%24 = load i32, i32* %5
%25 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%26 = call i32 @read(i32 %24, i8* %25, i32 64)
store i32 %26, i32* %6
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %23
%29 = load i32, i32* %3
%30 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%31 = load i32, i32* %6
%32 = call i32 @send(i32 %29, i8* %30, i32 %31, i32 0)
br label %23
33: ; preds = %23
%34 = load i32, i32* %5
%35 = call i32 @close(i32 %34)
br label %36
36: ; preds = %33, %12
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [13 x i8] c"fail to open\00"
define void @do_get(i32 %0, i8* nocapture %1) {
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %1, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i8 78, i8* %4
%10 = call i32 @send(i32 %0, i8* %4, i32 64, i32 0)
br label %22
11: ; preds = %2
store i8 89, i8* %4
%12 = call i32 @send(i32 %0, i8* %4, i32 64, i32 0)
%13 = call i32 @read(i32 %6, i8* %4, i32 64)
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %11, %15
%16 = phi i32 [ %18, %15 ], [ %13, %11 ]
%17 = call i32 @send(i32 %0, i8* %4, i32 %16, i32 0)
%18 = call i32 @read(i32 %6, i8* %4, i32 64)
%19 = icmp sgt i32 %18, 0
br i1 %19, label %15, label %20
20: ; preds = %15, %11
%21 = call i32 @close(i32 %6)
br label %22
22: ; preds = %20, %8
ret void
}