#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*/ printf(char *, char *);
scalar_t__ read(int, int *, int);
int /*<<< orphan*/ readline(int, char *);
int /*<<< orphan*/ write(int, int *, int);
int /*<<< orphan*/ writeline(int, char *);
void *read_file_for_remote_peer(void *t) {
  int connfd = *(int *)t, fd, c;
  char buffer[100];
  readline(connfd, buffer);
  if ((fd = open(buffer, O_RDONLY)) < 0) {
    printf("could not read %s*\n", buffer);
    writeline(connfd, "NOT_FOUND");
    close(connfd);
    return NULL;
  }
  writeline(connfd, "FOUND");
  while (read(fd, &c, 1) > 0) {
    write(connfd, &c, 1);
  }
  close(connfd);
  close(fd);
}


x86 assembly (clang, -O0):
<read_file_for_remote_peer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x80(%rbp),%rsi
call   1050 <readline@plt>
lea    -0x80(%rbp),%rdi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11e6 <read_file_for_remote_peer+0x76>
lea    -0x80(%rbp),%rsi
lea    0xe43(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
lea    0xe48(%rip),%rsi
call   1090 <writeline@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1236 <read_file_for_remote_peer+0xc6>
mov    -0x14(%rbp),%edi
lea    0xe28(%rip),%rsi
call   1090 <writeline@plt>
mov    -0x18(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0x0,%rax
jle    1226 <read_file_for_remote_peer+0xb6>
mov    -0x14(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    11f5 <read_file_for_remote_peer+0x85>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%rax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file_for_remote_peer>:
push   %rbp
push   %r14
push   %rbx
sub    $0x80,%rsp
mov    (%rdi),%r14d
lea    0x10(%rsp),%rbx
mov    %r14d,%edi
mov    %rbx,%rsi
call   1050 <readline@plt>
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11fe <read_file_for_remote_peer+0x8e>
mov    %eax,%ebp
lea    0xe6c(%rip),%rsi
mov    %r14d,%edi
call   1090 <writeline@plt>
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1070 <read@plt>
test   %rax,%rax
jle    11f4 <read_file_for_remote_peer+0x84>
lea    0xc(%rsp),%rbx
nop
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
test   %rax,%rax
jg     11d0 <read_file_for_remote_peer+0x60>
mov    %r14d,%edi
call   1060 <close@plt>
jmp    1221 <read_file_for_remote_peer+0xb1>
lea    0xdfb(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1040 <printf@plt>
lea    0xdfe(%rip),%rsi
mov    %r14d,%edi
call   1090 <writeline@plt>
mov    %r14d,%ebp
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [20 x i8] c"could not read %s*\0A\00"
@.str.1 = [10 x i8] c"NOT_FOUND\00"
@.str.2 = [6 x i8] c"FOUND\00"
define i8* @read_file_for_remote_peer(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [100 x i8]
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = bitcast i8* %8 to i32*
%10 = load i32, i32* %9
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%13 = call i32 @readline(i32 %11, i8* %12)
%14 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%15 = load i32, i32* @O_RDONLY
%16 = call i32 @open(i8* %14, i32 %15)
store i32 %16, i32* %5
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %1
%19 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%20 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = load i32, i32* %4
%22 = call i32 @writeline(i32 %21, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* %4
%24 = call i32 @close(i32 %23)
store i8* null, i8** %2
br label %40
25: ; preds = %1
%26 = load i32, i32* %4
%27 = call i32 @writeline(i32 %26, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %32, %25
%29 = load i32, i32* %5
%30 = call i64 @read(i32 %29, i32* %6, i32 1)
%31 = icmp sgt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = load i32, i32* %4
%34 = call i32 @write(i32 %33, i32* %6, i32 1)
br label %28
35: ; preds = %28
%36 = load i32, i32* %4
%37 = call i32 @close(i32 %36)
%38 = load i32, i32* %5
%39 = call i32 @close(i32 %38)
br label %40
40: ; preds = %35, %18
%41 = load i8*, i8** %2
ret i8* %41
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [20 x i8] c"could not read %s*\0A\00"
@.str.1 = [10 x i8] c"NOT_FOUND\00"
@.str.2 = [6 x i8] c"FOUND\00"
define noalias i8* @read_file_for_remote_peer(i8* nocapture %0) {
%2 = alloca i32
%3 = alloca [100 x i8]
%4 = bitcast i8* %0 to i32*
%5 = load i32, i32* %4
%6 = bitcast i32* %2 to i8*
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%8 = call i32 @readline(i32 %5, i8* %7)
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %7, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %7)
%14 = call i32 @writeline(i32 %5, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %25
15: ; preds = %1
%16 = call i32 @writeline(i32 %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%17 = call i64 @read(i32 %10, i32* %2, i32 1)
%18 = icmp sgt i64 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %15, %19
%20 = call i32 @write(i32 %5, i32* %2, i32 1)
%21 = call i64 @read(i32 %10, i32* %2, i32 1)
%22 = icmp sgt i64 %21, 0
br i1 %22, label %19, label %23
23: ; preds = %19, %15
%24 = call i32 @close(i32 %5)
br label %25
25: ; preds = %23, %12
%26 = phi i32 [ %10, %23 ], [ %5, %12 ]
%27 = call i32 @close(i32 %26)
ret i8* null
}