#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_RDWR;
int safe_open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int safe_read(int, char *, int);
int /*<<< orphan*/ safe_write(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
int /*<<< orphan*/ strlen(char *);
void establish_server(const char port[], char *listen_path, int path_size) {
  char session[512], announce_req[512];
  int size, clone = safe_open("/net/tcp/clone", O_RDWR, 0);
  size = safe_read(clone, session, sizeof session);
  session[size - 1] = '\0';
  snprintf(announce_req, sizeof announce_req, "announce *!%s", port);
  snprintf(listen_path, path_size, "/net/tcp/%s/listen", session);
  safe_write(clone, announce_req, strlen(announce_req));
}


x86 assembly (clang, -O0):
<establish_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
lea    0xe8a(%rip),%rdi
xor    %edx,%edx
call   1070 <safe_open@plt>
mov    %eax,-0x428(%rbp)
mov    -0x428(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1050 <safe_read@plt>
mov    %eax,-0x424(%rbp)
mov    -0x424(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x220(%rbp,%rax,1)
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x200,%esi
lea    0xe45(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
lea    -0x220(%rbp),%rcx
lea    0xe39(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x428(%rbp),%eax
mov    %eax,-0x434(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x430(%rbp)
lea    -0x420(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x434(%rbp),%edi
mov    -0x430(%rbp),%rsi
mov    %eax,%edx
call   1060 <safe_write@plt>
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<establish_server>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%r12
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
lea    0xe86(%rip),%rdi
xor    %edx,%edx
call   1070 <safe_open@plt>
mov    %eax,%ebp
lea    0x200(%rsp),%r13
mov    %eax,%edi
mov    %r13,%rsi
mov    $0x200,%edx
call   1050 <safe_read@plt>
cltq
movb   $0x0,-0x1(%rax,%r13,1)
lea    0xe66(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %r12,%rcx
call   1040 <snprintf@plt>
lea    0xe5a(%rip),%rdx
mov    %r15,%rdi
mov    %r14d,%esi
mov    %r13,%rcx
call   1040 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1060 <safe_write@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"/net/tcp/clone\00"
@O_RDWR = global i32 0
@.str.1 = [14 x i8] c"announce *!%s\00"
@.str.2 = [19 x i8] c"/net/tcp/%s/listen\00"
define void @establish_server(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [512 x i8]
%8 = alloca [512 x i8]
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%11 = load i32, i32* @O_RDWR
%12 = call i32 @safe_open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %11, i32 0)
store i32 %12, i32* %10
%13 = load i32, i32* %10
%14 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%15 = call i32 @safe_read(i32 %13, i8* %14, i32 512)
store i32 %15, i32* %9
%16 = load i32, i32* %9
%17 = sub nsw i32 %16, 1
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 %18
store i8 0, i8* %19
%20 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%21 = load i8*, i8** %4
%22 = call i32 @snprintf(i8* %20, i32 512, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = load i8*, i8** %5
%24 = load i32, i32* %6
%25 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%26 = call i32 @snprintf(i8* %23, i32 %24, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %25)
%27 = load i32, i32* %10
%28 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%29 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%30 = call i32 @strlen(i8* %29)
%31 = call i32 @safe_write(i32 %27, i8* %28, i32 %30)
ret void
}

LLVM IR (-O2):
@.str = [15 x i8] c"/net/tcp/clone\00"
@O_RDWR = global i32 0
@.str.1 = [14 x i8] c"announce *!%s\00"
@.str.2 = [19 x i8] c"/net/tcp/%s/listen\00"
define void @establish_server(i8* %0, i8* %1, i32 %2) {
%4 = alloca [512 x i8]
%5 = alloca [512 x i8]
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%8 = load i32, i32* @O_RDWR
%9 = tail call i32 @safe_open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %8, i32 0)
%10 = call i32 @safe_read(i32 %9, i8* %6, i32 512)
%11 = add nsw i32 %10, -1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %12
store i8 0, i8* %13
%14 = call i32 @snprintf(i8* %7, i32 512, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%15 = call i32 @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %6)
%16 = call i32 @strlen(i8* %7)
%17 = call i32 @safe_write(i32 %9, i8* %7, i32 %16)
ret void
}