#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 /*<<< orphan*/ pid;
typedef int /*<<< orphan*/ message;

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
char *malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ remove(char *);
int /*<<< orphan*/ write(int, char *, int);
int server_handshake(int from_client) {
  char *pid = malloc(256);
  read(from_client, pid, sizeof(pid));
  int to_client = open(pid, O_WRONLY);
  remove("mario");
  char message[] = "Why hello there";
  write(to_client, message, sizeof(message));
  printf("Connected with %s\n", pid);
  return to_client;
}


x86 assembly (clang, -O0):
<server_handshake>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x100,%edi
call   1070 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1060 <read@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
lea    0xe5a(%rip),%rdi
call   1030 <remove@plt>
mov    0xe6e(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe6b(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1040 <write@plt>
mov    -0x10(%rbp),%rsi
lea    0xe29(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<server_handshake>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
mov    $0x100,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %rax,%rsi
mov    $0x8,%edx
call   1060 <read@plt>
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
mov    %eax,%ebp
lea    0xe62(%rip),%rdi
call   1030 <remove@plt>
movaps 0xe76(%rip),%xmm0
movaps %xmm0,(%rsp)
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x10,%edx
call   1040 <write@plt>
lea    0xe42(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@.str = [6 x i8] c"mario\00"
@__const.server_handshake.message = [16 x i8] c"Why hello there\00"
@.str.1 = [19 x i8] c"Connected with %s\0A\00"
define i32 @server_handshake(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [16 x i8]
store i32 %0, i32* %2
%6 = call i8* @malloc(i32 256)
store i8* %6, i8** %3
%7 = load i32, i32* %2
%8 = load i8*, i8** %3
%9 = call i32 @read(i32 %7, i8* %8, i32 8)
%10 = load i8*, i8** %3
%11 = load i32, i32* @O_WRONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %4
%13 = call i32 @remove(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%14 = bitcast [16 x i8]* %5 to i8*
%15 = load i32, i32* %4
%16 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%17 = call i32 @write(i32 %15, i8* %16, i32 16)
%18 = load i8*, i8** %3
%19 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = load i32, i32* %4
ret i32 %20
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@.str = [6 x i8] c"mario\00"
@__const.server_handshake.message = [16 x i8] c"Why hello there\00"
@.str.1 = [19 x i8] c"Connected with %s\0A\00"
define i32 @server_handshake(i32 %0) {
%2 = alloca [16 x i8]
%3 = tail call dereferenceable_or_null(256) i8* @malloc(i32 256)
%4 = tail call i32 @read(i32 %0, i8* %3, i32 8)
%5 = load i32, i32* @O_WRONLY
%6 = tail call i32 @open(i8* %3, i32 %5)
%7 = tail call i32 @remove(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%8 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%9 = call i32 @write(i32 %6, i8* %8, i32 16)
%10 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %3)
ret i32 %6
}