#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 O_CREAT;
int O_RDWR;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stdin;
int strlen(char *);
int write(int, char *, int);
int fileclient(int readfd, int writefd) {
  int fd = 0;
  int len = 0;
  ssize_t r_cnt = 0;
  ssize_t w_cnt = 0;
  char path[256];
  char newpath[256];
  char buf[1024];
  memset(path, 0, 256);
  printf("[Client] input the filepath: ");
  fgets(path, 256, stdin);
  len = strlen(path);
  if ('\n' == path[len - 1]) {
    path[len - 1] = '\0';
    len--;
  }
  write(writefd, path, len);
  sleep(1);
  memset(newpath, 0, 256);
  printf("[Client] input new filename: ");
  fgets(newpath, 256, stdin);
  len = strlen(newpath);
  if ('\n' == newpath[len - 1]) {
    newpath[len - 1] = '\0';
  }
  printf("[Client] save file: \"%s\" to \"%s\" \n", path, newpath);
  if (0 > (fd = open(newpath, (O_CREAT | O_RDWR),
                     (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)))) {
    perror("[Client] open");
    return (-1);
  }
  memset(buf, 0, 1024);
  while (0 < (r_cnt = read(readfd, buf, 1024))) {
    if (-1 == write(fd, buf, r_cnt)) {
      perror("[Client] write");
      break;
    }
    memset(buf, 0, 1024);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<fileclient>:
push   %rbp
mov    %rsp,%rbp
sub    $0x620,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x120(%rbp),%rdi
mov    0x2ddc(%rip),%rax
mov    (%rax),%edx
mov    $0x100,%esi
call   1090 <fgets@plt>
lea    -0x120(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x120(%rbp,%rax,1),%ecx
ff
mov    $0xa,%eax
cmp    %ecx,%eax
jne    124d <fileclient+0xad>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x120(%rbp,%rax,1)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
lea    -0x220(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
lea    0xd9b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x220(%rbp),%rdi
mov    0x2d40(%rip),%rax
mov    (%rax),%edx
mov    $0x100,%esi
call   1090 <fgets@plt>
lea    -0x220(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x220(%rbp,%rax,1),%ecx
ff
mov    $0xa,%eax
cmp    %ecx,%eax
jne    12e0 <fileclient+0x140>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x220(%rbp,%rax,1)
lea    -0x120(%rbp),%rsi
lea    -0x220(%rbp),%rdx
lea    0xd47(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x220(%rbp),%rdi
mov    0x2cee(%rip),%rax
mov    (%rax),%esi
mov    0x2ccd(%rip),%rax
or     (%rax),%esi
mov    0x2c94(%rip),%rax
mov    (%rax),%edx
mov    0x2cab(%rip),%rax
or     (%rax),%edx
mov    0x2c92(%rip),%rax
or     (%rax),%edx
mov    0x2c81(%rip),%rax
or     (%rax),%edx
call   10a0 <open@plt>
mov    %eax,%ecx
mov    %ecx,-0x10(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jle    1365 <fileclient+0x1c5>
lea    0xd0b(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13f4 <fileclient+0x254>
lea    -0x620(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x620(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %eax,%ecx
mov    %ecx,-0x18(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jge    13e5 <fileclient+0x245>
mov    -0x10(%rbp),%edi
lea    -0x620(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <write@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    13cd <fileclient+0x22d>
lea    0xcaa(%rip),%rdi
call   10b0 <perror@plt>
jmp    13e5 <fileclient+0x245>
lea    -0x620(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
jmp    1378 <fileclient+0x1d8>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x620,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fileclient>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x608,%rsp
mov    %esi,%ebp
mov    %edi,%r15d
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
lea    0xe35(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2dff(%rip),%r14
mov    (%r14),%edx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1090 <fgets@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
movslq %eax,%rcx
cmpb   $0xa,-0x1(%rsp,%rcx,1)
jne    1205 <fileclient+0x65>
add    $0xffffffffffffffff,%rcx
movb   $0x0,(%rsp,%rcx,1)
mov    %ecx,%eax
mov    %rsp,%rsi
mov    %ebp,%edi
mov    %eax,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
lea    0x100(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
lea    0xde5(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%r14),%edx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1090 <fgets@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    1269 <fileclient+0xc9>
movb   $0x0,0xff(%rsp,%rax,1)
lea    0xdcc(%rip),%rdi
mov    %rsp,%rsi
lea    0x100(%rsp),%rbx
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d6c(%rip),%rax
mov    0x2d4d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d12(%rip),%rax
mov    0x2d2b(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d10(%rip),%rax
or     (%rax),%edx
mov    0x2cff(%rip),%rax
or     (%rax),%edx
mov    %rbx,%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     135c <fileclient+0x1bc>
mov    %eax,%ebp
lea    0x200(%rsp),%rbx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    1344 <fileclient+0x1a4>
lea    0x200(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     1370 <fileclient+0x1d0>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
test   %eax,%eax
jg     1310 <fileclient+0x170>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %r14d,%eax
add    $0x608,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcfc(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%r14d
jmp    134b <fileclient+0x1ab>
lea    0xcf6(%rip),%rdi
call   10b0 <perror@plt>
jmp    1344 <fileclient+0x1a4>

LLVM IR (-O0):
@.str = [30 x i8] c"[Client] input the filepath: \00"
@stdin = global i32 0
@.str.1 = [30 x i8] c"[Client] input new filename: \00"
@.str.2 = [35 x i8] c"[Client] save file: \22%s\22 to \22%s\22 \0A\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.3 = [14 x i8] c"[Client] open\00"
@.str.4 = [15 x i8] c"[Client] write\00"
define i32 @fileclient(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [256 x i8]
%11 = alloca [256 x i8]
%12 = alloca [1024 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%14 = call i32 @memset(i8* %13, i32 0, i32 256)
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%17 = load i32, i32* @stdin
%18 = call i32 @fgets(i8* %16, i32 256, i32 %17)
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%20 = call i32 @strlen(i8* %19)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = sub nsw i32 %21, 1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 %23
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp eq i32 10, %26
br i1 %27, label %28, label %35
28: ; preds = %2
%29 = load i32, i32* %7
%30 = sub nsw i32 %29, 1
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 %31
store i8 0, i8* %32
%33 = load i32, i32* %7
%34 = add nsw i32 %33, -1
store i32 %34, i32* %7
br label %35
35: ; preds = %28, %2
%36 = load i32, i32* %5
%37 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%38 = load i32, i32* %7
%39 = call i32 @write(i32 %36, i8* %37, i32 %38)
%40 = call i32 @sleep(i32 1)
%41 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%42 = call i32 @memset(i8* %41, i32 0, i32 256)
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%44 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%45 = load i32, i32* @stdin
%46 = call i32 @fgets(i8* %44, i32 256, i32 %45)
%47 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%48 = call i32 @strlen(i8* %47)
store i32 %48, i32* %7
%49 = load i32, i32* %7
%50 = sub nsw i32 %49, 1
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 %51
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = icmp eq i32 10, %54
br i1 %55, label %56, label %61
56: ; preds = %35
%57 = load i32, i32* %7
%58 = sub nsw i32 %57, 1
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 %59
store i8 0, i8* %60
br label %61
61: ; preds = %56, %35
%62 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%63 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %62, i8* %63)
%65 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%66 = load i32, i32* @O_CREAT
%67 = load i32, i32* @O_RDWR
%68 = or i32 %66, %67
%69 = load i32, i32* @S_IRUSR
%70 = load i32, i32* @S_IWUSR
%71 = or i32 %69, %70
%72 = load i32, i32* @S_IRGRP
%73 = or i32 %71, %72
%74 = load i32, i32* @S_IROTH
%75 = or i32 %73, %74
%76 = call i32 @open(i8* %65, i32 %68, i32 %75)
store i32 %76, i32* %6
%77 = icmp sgt i32 0, %76
br i1 %77, label %78, label %80
78: ; preds = %61
%79 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %102
80: ; preds = %61
%81 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%82 = call i32 @memset(i8* %81, i32 0, i32 1024)
br label %83
83: ; preds = %96, %80
%84 = load i32, i32* %4
%85 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%86 = call i32 @read(i32 %84, i8* %85, i32 1024)
store i32 %86, i32* %8
%87 = icmp slt i32 0, %86
br i1 %87, label %88, label %99
88: ; preds = %83
%89 = load i32, i32* %6
%90 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%91 = load i32, i32* %8
%92 = call i32 @write(i32 %89, i8* %90, i32 %91)
%93 = icmp eq i32 -1, %92
br i1 %93, label %94, label %96
94: ; preds = %88
%95 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
br label %99
96: ; preds = %88
%97 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%98 = call i32 @memset(i8* %97, i32 0, i32 1024)
br label %83
99: ; preds = %94, %83
%100 = load i32, i32* %6
%101 = call i32 @close(i32 %100)
store i32 0, i32* %3
br label %102
102: ; preds = %99, %78
%103 = load i32, i32* %3
ret i32 %103
}

LLVM IR (-O2):
@.str = [30 x i8] c"[Client] input the filepath: \00"
@stdin = global i32 0
@.str.1 = [30 x i8] c"[Client] input new filename: \00"
@.str.2 = [35 x i8] c"[Client] save file: \22%s\22 to \22%s\22 \0A\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.3 = [14 x i8] c"[Client] open\00"
@.str.4 = [15 x i8] c"[Client] write\00"
define i32 @fileclient(i32 %0, i32 %1) {
%3 = alloca [256 x i8]
%4 = alloca [256 x i8]
%5 = alloca [1024 x i8]
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%9 = call i32 @memset(i8* %6, i32 0, i32 256)
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @stdin
%12 = call i32 @fgets(i8* %6, i32 256, i32 %11)
%13 = call i32 @strlen(i8* %6)
%14 = add nsw i32 %13, -1
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %15
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 10
br i1 %18, label %19, label %20
19: ; preds = %2
store i8 0, i8* %16
br label %20
20: ; preds = %19, %2
%21 = phi i32 [ %14, %19 ], [ %13, %2 ]
%22 = call i32 @write(i32 %1, i8* %6, i32 %21)
%23 = call i32 @sleep(i32 1)
%24 = call i32 @memset(i8* %7, i32 0, i32 256)
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32, i32* @stdin
%27 = call i32 @fgets(i8* %7, i32 256, i32 %26)
%28 = call i32 @strlen(i8* %7)
%29 = add nsw i32 %28, -1
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 %30
%32 = load i8, i8* %31
%33 = icmp eq i8 %32, 10
br i1 %33, label %34, label %35
34: ; preds = %20
store i8 0, i8* %31
br label %35
35: ; preds = %34, %20
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %6, i8* %7)
%37 = load i32, i32* @O_CREAT
%38 = load i32, i32* @O_RDWR
%39 = or i32 %38, %37
%40 = load i32, i32* @S_IRUSR
%41 = load i32, i32* @S_IWUSR
%42 = or i32 %41, %40
%43 = load i32, i32* @S_IRGRP
%44 = or i32 %42, %43
%45 = load i32, i32* @S_IROTH
%46 = or i32 %44, %45
%47 = call i32 @open(i8* %7, i32 %39, i32 %46)
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %35
%50 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %67
51: ; preds = %35
%52 = call i32 @memset(i8* %8, i32 0, i32 1024)
%53 = call i32 @read(i32 %0, i8* %8, i32 1024)
%54 = icmp sgt i32 %53, 0
br i1 %54, label %55, label %65
55: ; preds = %51, %61
%56 = phi i32 [ %63, %61 ], [ %53, %51 ]
%57 = call i32 @write(i32 %47, i8* %8, i32 %56)
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %61
59: ; preds = %55
%60 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
br label %65
61: ; preds = %55
%62 = call i32 @memset(i8* %8, i32 0, i32 1024)
%63 = call i32 @read(i32 %0, i8* %8, i32 1024)
%64 = icmp sgt i32 %63, 0
br i1 %64, label %55, label %65
65: ; preds = %61, %51, %59
%66 = call i32 @close(i32 %47)
br label %67
67: ; preds = %65, %49
%68 = phi i32 [ -1, %49 ], [ 0, %65 ]
ret i32 %68
}