#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*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
void process(int sock) {
  int n;
  int BUFSIZE = 1024;
  char buf[BUFSIZE];
  char *msg = "Welcome, you are connected to a C-based server\n";
  char *userInput;
  n = write(sock, msg, strlen(msg));
  if (n < 0) {
    perror("ERROR writing to socket");
    exit(1);
  }
  bzero(buf, BUFSIZE);
  n = read(sock, buf, BUFSIZE);
  if (n < 0) {
    perror("ERROR reading from socket");
    exit(1);
  }
  userInput = buf;
  printf("Received message: %s\n", userInput);
  close(sock);
}


x86 assembly (clang, -O0):
<process>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x400,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
lea    0xe41(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    120b <process+0x8b>
lea    0xe34(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x48(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1090 <bzero@plt>
mov    -0x48(%rbp),%rsi
mov    -0x4(%rbp),%edi
mov    -0xc(%rbp),%edx
call   1070 <read@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1249 <process+0xc9>
lea    0xe0e(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x48(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rsi
lea    0xe06(%rip),%rdi
call   1050 <printf@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<process>:
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %edi,%ebx
lea    0xe6d(%rip),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %eax,%eax
js     11ef <process+0x6f>
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x400,%esi
call   1090 <bzero@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
test   %eax,%eax
js     11f8 <process+0x78>
lea    0xe8d(%rip),%rdi
mov    %rsp,%rsi
call   1050 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe3a(%rip),%rdi
jmp    11ff <process+0x7f>
lea    0xe49(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [48 x i8] c"Welcome, you are connected to a C-based server\0A\00"
@.str.1 = [24 x i8] c"ERROR writing to socket\00"
@.str.2 = [26 x i8] c"ERROR reading from socket\00"
@.str.3 = [22 x i8] c"Received message: %s\0A\00"
define void @process(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
store i32 %0, i32* %2
store i32 1024, i32* %4
%9 = load i32, i32* %4
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %5
%12 = alloca i8, i64 %10
store i64 %10, i64* %6
store i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8** %7
%13 = load i32, i32* %2
%14 = load i8*, i8** %7
%15 = load i8*, i8** %7
%16 = call i32 @strlen(i8* %15)
%17 = call i32 @write(i32 %13, i8* %14, i32 %16)
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %1
%21 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %1
%24 = load i32, i32* %4
%25 = call i32 @bzero(i8* %12, i32 %24)
%26 = load i32, i32* %2
%27 = load i32, i32* %4
%28 = call i32 @read(i32 %26, i8* %12, i32 %27)
store i32 %28, i32* %3
%29 = load i32, i32* %3
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %23
%32 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %23
store i8* %12, i8** %8
%35 = load i8*, i8** %8
%36 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %35)
%37 = load i32, i32* %2
%38 = call i32 @close(i32 %37)
%39 = load i8*, i8** %5
ret void
}

LLVM IR (-O2):
@.str = [48 x i8] c"Welcome, you are connected to a C-based server\0A\00"
@.str.1 = [24 x i8] c"ERROR writing to socket\00"
@.str.2 = [26 x i8] c"ERROR reading from socket\00"
@.str.3 = [22 x i8] c"Received message: %s\0A\00"
define void @process(i32 %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @strlen(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @write(i32 %0, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %1
%11 = call i32 @bzero(i8* %3, i32 1024)
%12 = call i32 @read(i32 %0, i8* %3, i32 1024)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %10
%18 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %3)
%19 = call i32 @close(i32 %0)
ret void
}