#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 */
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ reply;
typedef int /*<<< orphan*/ clientAddr;
typedef int /*<<< orphan*/ buff;

/* Variables and functions */
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ bzero(char **, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int write(int, char *, int);
int AcceptAndRead(socklen_t sockfd) {
  struct sockaddr_in clientAddr;
  socklen_t size = sizeof(clientAddr);
  int ret = 0;
  int connd = accept(sockfd, (struct sockaddr *)&clientAddr, &size);
  if (connd == -1) {
    printf("failed to accept the connection..\n");
  } else {
    printf("Client connected successfully\n");
    for (;;) {
      char buff[256];
      bzero(&buff, sizeof(buff));
      if ((ret = read(connd, buff, sizeof(buff) - 1)) > 0) {
        printf("Client: %s\n", buff);
        char reply[] = "I hear ya fa shizzle!\n";
        if ((ret = write(connd, reply, sizeof(reply) - 1)) < 0)
          printf("write error\n");
      } else
        break;
    }
  }
  close(connd);
  return 0;
}


x86 assembly (clang, -O0):
<AcceptAndRead>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x4,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
lea    -0xc(%rbp),%rdx
call   1080 <accept@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11ac <AcceptAndRead+0x4c>
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1284 <AcceptAndRead+0x124>
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   1070 <bzero@plt>
mov    -0x14(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0xff,%edx
call   1060 <read@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jle    1275 <AcceptAndRead+0x115>
lea    -0x120(%rbp),%rsi
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0xe59(%rip),%rax
mov    %rax,-0x140(%rbp)
mov    0xe53(%rip),%rax
mov    %rax,-0x138(%rbp)
mov    0xe4e(%rip),%eax
mov    %eax,-0x130(%rbp)
mov    0xe45(%rip),%ax
mov    %ax,-0x12c(%rbp)
mov    0xe3a(%rip),%al
mov    %al,-0x12a(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x140(%rbp),%rsi
mov    $0x16,%edx
call   1030 <write@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    1270 <AcceptAndRead+0x110>
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    127a <AcceptAndRead+0x11a>
jmp    127f <AcceptAndRead+0x11f>
jmp    11ba <AcceptAndRead+0x5a>
jmp    1284 <AcceptAndRead+0x124>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<AcceptAndRead>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x138,%rsp
movl   $0x4,0x4(%rsp)
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
call   1090 <accept@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     125b <AcceptAndRead+0xeb>
lea    0xe62(%rip),%rdi
call   1030 <puts@plt>
lea    0x30(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1080 <bzero@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xff,%edx
call   1070 <read@plt>
test   %eax,%eax
jle    1267 <AcceptAndRead+0xf7>
lea    0xe21(%rip),%r14
lea    0x30(%rsp),%rbx
movabs $0xa21656c7a7a69,%r13
lea    0x10(%rsp),%r15
lea    0xe30(%rip),%r12
jmp    1220 <AcceptAndRead+0xb0>
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    $0x100,%esi
call   1080 <bzero@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xff,%edx
call   1070 <read@plt>
test   %eax,%eax
jle    1267 <AcceptAndRead+0xf7>
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
movaps 0xe2c(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
mov    %r13,0x1f(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x16,%edx
call   1040 <write@plt>
test   %eax,%eax
jns    1200 <AcceptAndRead+0x90>
mov    %r12,%rdi
call   1030 <puts@plt>
jmp    1200 <AcceptAndRead+0x90>
lea    0xdd4(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x138,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [35 x i8] c"failed to accept the connection..\0A\00"
@.str.1 = [31 x i8] c"Client connected successfully\0A\00"
@.str.2 = [12 x i8] c"Client: %s\0A\00"
@__const.AcceptAndRead.reply = [23 x i8] c"I hear ya fa shizzle!\0A\00"
@.str.3 = [13 x i8] c"write error\0A\00"
define i32 @AcceptAndRead(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [256 x i8]
%8 = alloca [23 x i8]
store i32 %0, i32* %2
store i32 4, i32* %4
store i32 0, i32* %5
%9 = load i32, i32* %2
%10 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%11 = call i32 @accept(i32 %9, %struct.sockaddr* %10, i32* %4)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %39
16: ; preds = %1
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %37, %16
%19 = bitcast [256 x i8]* %7 to i8**
%20 = call i32 @bzero(i8** %19, i32 256)
%21 = load i32, i32* %6
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%23 = call i32 @read(i32 %21, i8* %22, i32 255)
store i32 %23, i32* %5
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %36
25: ; preds = %18
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %26)
%28 = bitcast [23 x i8]* %8 to i8*
%29 = load i32, i32* %6
%30 = getelementptr inbounds [23 x i8], [23 x i8]* %8, i64 0, i64 0
%31 = call i32 @write(i32 %29, i8* %30, i32 22)
store i32 %31, i32* %5
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %25
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %35
35: ; preds = %33, %25
br label %37
36: ; preds = %18
br label %38
37: ; preds = %35
br label %18
38: ; preds = %36
br label %39
39: ; preds = %38, %14
%40 = load i32, i32* %6
%41 = call i32 @close(i32 %40)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str.2 = [12 x i8] c"Client: %s\0A\00"
@__const.AcceptAndRead.reply = [23 x i8] c"I hear ya fa shizzle!\0A\00"
@str = [30 x i8] c"Client connected successfully\00"
@str.4 = [12 x i8] c"write error\00"
@str.5 = [34 x i8] c"failed to accept the connection..\00"
define i32 @AcceptAndRead(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca [256 x i8]
%5 = alloca [23 x i8]
%6 = bitcast %struct.sockaddr_in* %2 to i8*
%7 = bitcast i32* %3 to i8*
store i32 4, i32* %3
%8 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%9 = call i32 @accept(i32 %0, %struct.sockaddr* %8, i32* %3)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.5, i64 0, i64 0))
br label %33
13: ; preds = %1
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%16 = bitcast [256 x i8]* %4 to i8**
%17 = call i32 @bzero(i8** %16, i32 256)
%18 = call i32 @read(i32 %9, i8* %15, i32 255)
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %13
%21 = getelementptr inbounds [23 x i8], [23 x i8]* %5, i64 0, i64 0
br label %23
22: ; preds = %29, %13
br label %33
23: ; preds = %20, %29
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %15)
%25 = call i32 @write(i32 %9, i8* %21, i32 22)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.4, i64 0, i64 0))
br label %29
29: ; preds = %23, %27
%30 = call i32 @bzero(i8** %16, i32 256)
%31 = call i32 @read(i32 %9, i8* %15, i32 255)
%32 = icmp sgt i32 %31, 0
br i1 %32, label %23, label %22
33: ; preds = %22, %11
%34 = call i32 @close(i32 %9)
ret i32 0
}