#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*/ close(int);
int creat(char *, int);
int create_socket(char *, int);
int /*<<< orphan*/ printf(char *);
scalar_t__ read(int, int *, int);
int /*<<< orphan*/ readline(int, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ write(int, int *, int);
int /*<<< orphan*/ writeline(int, char *);
int read_file_from_peer_api(char *filename, char *ip) {
  int connfd = create_socket(ip, 2000);
  char result[10];
  int fd, c;
  if (connfd < 0) {
    printf("connection failed\n");
    return -1;
  }
  printf("request sent\n");
  writeline(connfd, "READ");
  writeline(connfd, filename);
  readline(connfd, result);
  if (strcmp(result, "NOT_FOUND") == 0) {
    printf("file not found\n");
    return -1;
  }
  if ((fd = creat(filename, 0666)) < 0) {
    printf("couldnt create local file\n");
    return -1;
  }
  while (read(connfd, &c, 1) > 0) {
    write(fd, &c, 1);
  }
  close(connfd);
  close(fd);
  return 1;
}


x86 assembly (clang, -O0):
<read_file_from_peer_api>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x7d0,%esi
call   1090 <create_socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11d3 <read_file_from_peer_api+0x43>
lea    0xe3e(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b7 <read_file_from_peer_api+0x127>
lea    0xe39(%rip),%rdi
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
lea    0xe38(%rip),%rsi
call   10a0 <writeline@plt>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   10a0 <writeline@plt>
mov    -0x1c(%rbp),%edi
lea    -0x26(%rbp),%rsi
call   1050 <readline@plt>
lea    -0x26(%rbp),%rdi
lea    0xe15(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1238 <read_file_from_peer_api+0xa8>
lea    0xe09(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b7 <read_file_from_peer_api+0x127>
mov    -0x10(%rbp),%rdi
mov    $0x1b6,%esi
call   10b0 <creat@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    126a <read_file_from_peer_api+0xda>
lea    0xde7(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b7 <read_file_from_peer_api+0x127>
jmp    126f <read_file_from_peer_api+0xdf>
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0x0,%rax
jle    12a0 <read_file_from_peer_api+0x110>
mov    -0x2c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    126f <read_file_from_peer_api+0xdf>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file_from_peer_api>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbp
mov    %rsi,%rdi
mov    $0x7d0,%esi
call   1090 <create_socket@plt>
test   %eax,%eax
js     1275 <read_file_from_peer_api+0xe5>
mov    %eax,%r15d
lea    0xe53(%rip),%rdi
call   1030 <puts@plt>
lea    0xe38(%rip),%rsi
mov    %r15d,%edi
call   10a0 <writeline@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
call   10a0 <writeline@plt>
lea    0xe(%rsp),%rbx
mov    %r15d,%edi
mov    %rbx,%rsi
call   1050 <readline@plt>
lea    0xe13(%rip),%rsi
mov    %rbx,%rdi
call   1080 <strcmp@plt>
test   %rax,%rax
je     127e <read_file_from_peer_api+0xee>
mov    %rbp,%rdi
mov    $0x1b6,%esi
call   10b0 <creat@plt>
test   %eax,%eax
js     1287 <read_file_from_peer_api+0xf7>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    $0x1,%r14d
mov    %r15d,%edi
mov    $0x1,%edx
call   1070 <read@plt>
test   %rax,%rax
jle    1264 <read_file_from_peer_api+0xd4>
lea    0x8(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
test   %rax,%rax
jg     1240 <read_file_from_peer_api+0xb0>
mov    %r15d,%edi
call   1060 <close@plt>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    1299 <read_file_from_peer_api+0x109>
lea    0xdc9(%rip),%rdi
jmp    128e <read_file_from_peer_api+0xfe>
lea    0xdb1(%rip),%rdi
jmp    128e <read_file_from_peer_api+0xfe>
lea    0xd8e(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c"connection failed\0A\00"
@.str.1 = [14 x i8] c"request sent\0A\00"
@.str.2 = [5 x i8] c"READ\00"
@.str.3 = [10 x i8] c"NOT_FOUND\00"
@.str.4 = [16 x i8] c"file not found\0A\00"
@.str.5 = [27 x i8] c"couldnt create local file\0A\00"
define i32 @read_file_from_peer_api(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [10 x i8]
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %5
%11 = call i32 @create_socket(i8* %10, i32 2000)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %50
16: ; preds = %2
%17 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* %6
%19 = call i32 @writeline(i32 %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%20 = load i32, i32* %6
%21 = load i8*, i8** %4
%22 = call i32 @writeline(i32 %20, i8* %21)
%23 = load i32, i32* %6
%24 = getelementptr inbounds [10 x i8], [10 x i8]* %7, i64 0, i64 0
%25 = call i32 @readline(i32 %23, i8* %24)
%26 = getelementptr inbounds [10 x i8], [10 x i8]* %7, i64 0, i64 0
%27 = call i64 @strcmp(i8* %26, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %16
%30 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %50
31: ; preds = %16
%32 = load i8*, i8** %4
%33 = call i32 @creat(i8* %32, i32 438)
store i32 %33, i32* %8
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %3
br label %50
37: ; preds = %31
br label %38
38: ; preds = %42, %37
%39 = load i32, i32* %6
%40 = call i64 @read(i32 %39, i32* %9, i32 1)
%41 = icmp sgt i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %38
%43 = load i32, i32* %8
%44 = call i32 @write(i32 %43, i32* %9, i32 1)
br label %38
45: ; preds = %38
%46 = load i32, i32* %6
%47 = call i32 @close(i32 %46)
%48 = load i32, i32* %8
%49 = call i32 @close(i32 %48)
store i32 1, i32* %3
br label %50
50: ; preds = %45, %35, %29, %14
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
@.str.2 = [5 x i8] c"READ\00"
@.str.3 = [10 x i8] c"NOT_FOUND\00"
@str = [13 x i8] c"request sent\00"
@str.6 = [26 x i8] c"couldnt create local file\00"
@str.7 = [15 x i8] c"file not found\00"
@str.8 = [18 x i8] c"connection failed\00"
define i32 @read_file_from_peer_api(i8* %0, i8* %1) {
%3 = alloca [10 x i8]
%4 = alloca i32
%5 = tail call i32 @create_socket(i8* %1, i32 2000)
%6 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%7 = bitcast i32* %4 to i8*
%8 = icmp slt i32 %5, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.8, i64 0, i64 0))
br label %35
11: ; preds = %2
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%13 = tail call i32 @writeline(i32 %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%14 = tail call i32 @writeline(i32 %5, i8* %0)
%15 = call i32 @readline(i32 %5, i8* %6)
%16 = call i64 @strcmp(i8* %6, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %11
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.7, i64 0, i64 0))
br label %35
20: ; preds = %11
%21 = call i32 @creat(i8* %0, i32 438)
%22 = icmp slt i32 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %20
%24 = call i64 @read(i32 %5, i32* %4, i32 1)
%25 = icmp sgt i64 %24, 0
br i1 %25, label %28, label %32
26: ; preds = %20
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.6, i64 0, i64 0))
br label %35
28: ; preds = %23, %28
%29 = call i32 @write(i32 %21, i32* %4, i32 1)
%30 = call i64 @read(i32 %5, i32* %4, i32 1)
%31 = icmp sgt i64 %30, 0
br i1 %31, label %28, label %32
32: ; preds = %28, %23
%33 = call i32 @close(i32 %5)
%34 = call i32 @close(i32 %21)
br label %35
35: ; preds = %32, %26, %18, %9
%36 = phi i32 [ -1, %9 ], [ -1, %18 ], [ -1, %26 ], [ 1, %32 ]
ret i32 %36
}