#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_APPEND;
int /*<<< orphan*/ close(int);
int creat(char *, int);
int open(char *, int /*<<< orphan*/);
scalar_t__ read(int, int *, int);
int /*<<< orphan*/ readline(int, char *);
int /*<<< orphan*/ write(int, int *, int);
void *append_file_for_remote_peer(void *t) {
  int connfd = *(int *)t, fd, c;
  char filename[100], temp[10];
  readline(connfd, filename);
  readline(connfd, temp);
  if ((fd = open(filename, O_APPEND)) < 0) {
    fd = creat(filename, 0666);
  }
  while (read(connfd, &c, 1) > 0) {
    write(fd, &c, 1);
  }
  close(connfd);
  close(fd);
}


x86 assembly (clang, -O0):
<append_file_for_remote_peer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%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   1040 <readline@plt>
mov    -0x14(%rbp),%edi
lea    -0x8a(%rbp),%rsi
call   1040 <readline@plt>
lea    -0x80(%rbp),%rdi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11c2 <append_file_for_remote_peer+0x62>
lea    -0x80(%rbp),%rdi
mov    $0x1b6,%esi
call   1080 <creat@plt>
mov    %eax,-0x18(%rbp)
jmp    11c7 <append_file_for_remote_peer+0x67>
mov    -0x14(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
cmp    $0x0,%rax
jle    11f8 <append_file_for_remote_peer+0x98>
mov    -0x18(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    11c7 <append_file_for_remote_peer+0x67>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%rax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_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   1040 <readline@plt>
lea    0x6(%rsp),%rsi
mov    %r14d,%edi
call   1040 <readline@plt>
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11b3 <append_file_for_remote_peer+0x53>
lea    0x10(%rsp),%rdi
mov    $0x1b6,%esi
call   1080 <creat@plt>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1060 <read@plt>
test   %rax,%rax
jle    11f4 <append_file_for_remote_peer+0x94>
mov    %rsp,%rbx
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
test   %rax,%rax
jg     11d0 <append_file_for_remote_peer+0x70>
mov    %r14d,%edi
call   1050 <close@plt>
mov    %ebp,%edi
call   1050 <close@plt>
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_APPEND = global i32 0
define i8* @append_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]
%8 = alloca [10 x i8]
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = bitcast i8* %9 to i32*
%11 = load i32, i32* %10
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%14 = call i32 @readline(i32 %12, i8* %13)
%15 = load i32, i32* %4
%16 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%17 = call i32 @readline(i32 %15, i8* %16)
%18 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%19 = load i32, i32* @O_APPEND
%20 = call i32 @open(i8* %18, i32 %19)
store i32 %20, i32* %5
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %1
%23 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%24 = call i32 @creat(i8* %23, i32 438)
store i32 %24, i32* %5
br label %25
25: ; preds = %22, %1
br label %26
26: ; preds = %30, %25
%27 = load i32, i32* %4
%28 = call i64 @read(i32 %27, i32* %6, i32 1)
%29 = icmp sgt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = load i32, i32* %5
%32 = call i32 @write(i32 %31, i32* %6, i32 1)
br label %26
33: ; preds = %26
%34 = load i32, i32* %4
%35 = call i32 @close(i32 %34)
%36 = load i32, i32* %5
%37 = call i32 @close(i32 %36)
%38 = load i8*, i8** %2
ret i8* %38
}

LLVM IR (-O2):
@O_APPEND = global i32 0
define noalias i8* @append_file_for_remote_peer(i8* nocapture %0) {
%2 = alloca i32
%3 = alloca [100 x i8]
%4 = alloca [10 x i8]
%5 = bitcast i8* %0 to i32*
%6 = load i32, i32* %5
%7 = bitcast i32* %2 to i8*
%8 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%9 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%10 = call i32 @readline(i32 %6, i8* %8)
%11 = call i32 @readline(i32 %6, i8* %9)
%12 = load i32, i32* @O_APPEND
%13 = call i32 @open(i8* %8, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = call i32 @creat(i8* %8, i32 438)
br label %17
17: ; preds = %15, %1
%18 = phi i32 [ %16, %15 ], [ %13, %1 ]
%19 = call i64 @read(i32 %6, i32* %2, i32 1)
%20 = icmp sgt i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %17, %21
%22 = call i32 @write(i32 %18, i32* %2, i32 1)
%23 = call i64 @read(i32 %6, i32* %2, i32 1)
%24 = icmp sgt i64 %23, 0
br i1 %24, label %21, label %25
25: ; preds = %21, %17
%26 = call i32 @close(i32 %6)
%27 = call i32 @close(i32 %18)
ret i8* undef
}