#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_storage {
  int /*<<< orphan*/ ss_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ recv_addr_str;
typedef int /*<<< orphan*/ recv_addr;

/* Variables and functions */
int INET6_ADDRSTRLEN;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, struct sockaddr *, char *,
                             int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int recv(int, char *, int, int /*<<< orphan*/);
void recv_expected_tcp(int sockfd) {
  struct sockaddr_storage recv_addr;
  socklen_t recv_addr_size = sizeof(recv_addr);
  char recv_addr_str[INET6_ADDRSTRLEN];
  int recv_fd;
  int recv_nbytes;
  char buffer[100];
  recv_fd = accept(sockfd, (struct sockaddr *)&recv_addr, &recv_addr_size);
  if (recv_fd == -1) {
    perror("\taccept");
    return;
  }
  inet_ntop(recv_addr.ss_family, (struct sockaddr *)&recv_addr, recv_addr_str,
            sizeof(recv_addr_str));
  printf("\t[-->x]\tGot connection from %s", recv_addr_str);
  recv_nbytes = recv(recv_fd, buffer, 100, 0);
  if (recv_nbytes <= 0) {
    perror("\trecv");
    return;
  }
  buffer[recv_nbytes] = '\0';
  printf("\tNumber is: %s", buffer);
}


x86 assembly (clang, -O0):
<recv_expected_tcp>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x4,-0xc(%rbp)
mov    0x2e7c(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0xa8(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0xa0(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
lea    -0xc(%rbp),%rdx
call   1070 <accept@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11d3 <recv_expected_tcp+0x83>
lea    0xe41(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x94(%rbp)
jmp    1269 <recv_expected_tcp+0x119>
mov    -0xa0(%rbp),%rdx
mov    -0xa8(%rbp),%rax
mov    -0x8(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
mov    -0xa0(%rbp),%rsi
lea    0xe0b(%rip),%rdi
call   1050 <printf@plt>
mov    -0x24(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jg     1240 <recv_expected_tcp+0xf0>
lea    0xdfb(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x94(%rbp)
jmp    1269 <recv_expected_tcp+0x119>
movslq -0x28(%rbp),%rax
movb   $0x0,-0x90(%rbp,%rax,1)
lea    -0x90(%rbp),%rsi
lea    0xdd3(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x94(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_expected_tcp>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x70,%rsp
mov    %rsp,%r12
movl   $0x4,-0x2c(%rbp)
mov    0x2e78(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%rbx
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1070 <accept@plt>
cmp    $0xffffffff,%eax
je     1201 <recv_expected_tcp+0xb1>
mov    %eax,%r15d
mov    -0x28(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    %rbx,%rdx
mov    %r14d,%ecx
call   1030 <inet_ntop@plt>
lea    0xe56(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
lea    -0x90(%rbp),%rsi
mov    %r15d,%edi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
test   %eax,%eax
jle    120f <recv_expected_tcp+0xbf>
mov    %eax,%eax
movb   $0x0,-0x90(%rbp,%rax,1)
lea    0xe48(%rip),%rdi
lea    -0x90(%rbp),%rsi
call   1050 <printf@plt>
mov    %r12,%rsp
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf8(%rip),%rdi
call   1060 <perror@plt>
jmp    11f1 <recv_expected_tcp+0xa1>
lea    0xe11(%rip),%rdi
call   1060 <perror@plt>
jmp    11f1 <recv_expected_tcp+0xa1>

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
@.str = [8 x i8] c"\09accept\00"
@.str.1 = [31 x i8] c"\09[-->x]\09Got connection from %s\00"
@.str.2 = [6 x i8] c"\09recv\00"
@.str.3 = [15 x i8] c"\09Number is: %s\00"
define void @recv_expected_tcp(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_storage
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca [100 x i8]
%10 = alloca i32
store i32 %0, i32* %2
store i32 4, i32* %4
%11 = load i32, i32* @INET6_ADDRSTRLEN
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %5
%14 = alloca i8, i64 %12
store i64 %12, i64* %6
%15 = load i32, i32* %2
%16 = bitcast %struct.sockaddr_storage* %3 to %struct.sockaddr*
%17 = call i32 @accept(i32 %15, %struct.sockaddr* %16, i32* %4)
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %1
%21 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %10
br label %42
22: ; preds = %1
%23 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %3, i32 0, i32 0
%24 = load i32, i32* %23
%25 = bitcast %struct.sockaddr_storage* %3 to %struct.sockaddr*
%26 = trunc i64 %12 to i32
%27 = call i32 @inet_ntop(i32 %24, %struct.sockaddr* %25, i8* %14, i32 %26)
%28 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%29 = load i32, i32* %7
%30 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%31 = call i32 @recv(i32 %29, i8* %30, i32 100, i32 0)
store i32 %31, i32* %8
%32 = load i32, i32* %8
%33 = icmp sle i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %22
%35 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %10
br label %42
36: ; preds = %22
%37 = load i32, i32* %8
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 %38
store i8 0, i8* %39
%40 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%41 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %40)
store i32 0, i32* %10
br label %42
42: ; preds = %36, %34, %20
%43 = load i8*, i8** %5
%44 = load i32, i32* %10
switch i32 %44, label %46 [
i32 0, label %45
i32 1, label %45
]
45: ; preds = %42, %42
ret void
46: ; preds = %42
unreachable
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
@.str = [8 x i8] c"\09accept\00"
@.str.1 = [31 x i8] c"\09[-->x]\09Got connection from %s\00"
@.str.2 = [6 x i8] c"\09recv\00"
@.str.3 = [15 x i8] c"\09Number is: %s\00"
define void @recv_expected_tcp(i32 %0) {
%2 = alloca %struct.sockaddr_storage
%3 = alloca i32
%4 = alloca [100 x i8]
%5 = bitcast %struct.sockaddr_storage* %2 to i8*
%6 = bitcast i32* %3 to i8*
store i32 4, i32* %3
%7 = load i32, i32* @INET6_ADDRSTRLEN
%8 = zext i32 %7 to i64
%9 = tail call i8* @llvm.stacksave()
%10 = alloca i8, i64 %8
%11 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%12 = bitcast %struct.sockaddr_storage* %2 to %struct.sockaddr*
%13 = call i32 @accept(i32 %0, %struct.sockaddr* %12, i32* %3)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %30
17: ; preds = %1
%18 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %2, i64 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @inet_ntop(i32 %19, %struct.sockaddr* %12, i8* %10, i32 %7)
%21 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%22 = call i32 @recv(i32 %13, i8* %11, i32 100, i32 0)
%23 = icmp slt i32 %22, 1
br i1 %23, label %24, label %26
24: ; preds = %17
%25 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %30
26: ; preds = %17
%27 = zext i32 %22 to i64
%28 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 %27
store i8 0, i8* %28
%29 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %11)
br label %30
30: ; preds = %26, %24, %15
ret void
}