#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;

/* Variables and functions */
int INET6_ADDRSTRLEN;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ get_in_addr(struct sockaddr *);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/, char *,
                             int);
scalar_t__ malloc(int);
int recv(int, char *, int, int /*<<< orphan*/);
char *receive_msg(int sockfd) {
  char *buf = (char *)malloc(sizeof(char) * 1000);
  int numbytes, new_fd;
  struct sockaddr_storage their_addr;
  socklen_t sin_size;
  char s[INET6_ADDRSTRLEN];
  sin_size = sizeof their_addr;
  new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
  inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr),
            s, sizeof s);
  close(sockfd);
  numbytes = recv(new_fd, buf, 100 - 1, 0);
  buf[numbytes] = '\0';
  close(new_fd);
  return buf;
}


x86 assembly (clang, -O0):
<receive_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x3e8,%edi
call   1070 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    0x2e68(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x40(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x30(%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,-0x38(%rbp)
movl   $0x4,-0x24(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   1080 <accept@plt>
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4c(%rbp)
lea    -0x20(%rbp),%rdi
call   1050 <get_in_addr@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %eax,%esi
mov    -0x40(%rbp),%rax
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x63,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receive_msg>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r14d
mov    %edi,-0x34(%rbp)
mov    $0x3e8,%edi
call   1070 <malloc@plt>
mov    %rax,%r15
mov    %rsp,-0x40(%rbp)
mov    0x2e59(%rip),%rax
mov    (%rax),%r13d
mov    %rsp,%rbx
mov    %r13,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
movl   $0x4,-0x38(%rbp)
lea    -0x30(%rbp),%rsi
lea    -0x38(%rbp),%rdx
mov    %r14d,%edi
call   1080 <accept@plt>
mov    %eax,%r12d
mov    -0x30(%rbp),%r14d
lea    -0x30(%rbp),%rdi
call   1050 <get_in_addr@plt>
mov    %r14d,%edi
mov    %eax,%esi
mov    %rbx,%rdx
mov    %r13d,%ecx
call   1030 <inet_ntop@plt>
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
mov    %r12d,%edi
mov    %r15,%rsi
mov    $0x63,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
cltq
movb   $0x0,(%r15,%rax,1)
mov    %r12d,%edi
call   1060 <close@plt>
mov    -0x40(%rbp),%rsp
mov    %r15,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
define i8* @receive_msg(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_storage
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
store i32 %0, i32* %2
%10 = call i64 @malloc(i32 1000)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %3
%12 = load i32, i32* @INET6_ADDRSTRLEN
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %8
%15 = alloca i8, i64 %13
store i64 %13, i64* %9
store i32 4, i32* %7
%16 = load i32, i32* %2
%17 = bitcast %struct.sockaddr_storage* %6 to %struct.sockaddr*
%18 = call i32 @accept(i32 %16, %struct.sockaddr* %17, i32* %7)
store i32 %18, i32* %5
%19 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %6, i32 0, i32 0
%20 = load i32, i32* %19
%21 = bitcast %struct.sockaddr_storage* %6 to %struct.sockaddr*
%22 = call i32 @get_in_addr(%struct.sockaddr* %21)
%23 = trunc i64 %13 to i32
%24 = call i32 @inet_ntop(i32 %20, i32 %22, i8* %15, i32 %23)
%25 = load i32, i32* %2
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* %5
%28 = load i8*, i8** %3
%29 = call i32 @recv(i32 %27, i8* %28, i32 99, i32 0)
store i32 %29, i32* %4
%30 = load i8*, i8** %3
%31 = load i32, i32* %4
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
store i8 0, i8* %33
%34 = load i32, i32* %5
%35 = call i32 @close(i32 %34)
%36 = load i8*, i8** %3
%37 = load i8*, i8** %8
ret i8* %36
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
define i8* @receive_msg(i32 %0) {
%2 = alloca %struct.sockaddr_storage
%3 = alloca i32
%4 = tail call i64 @malloc(i32 1000)
%5 = inttoptr i64 %4 to i8*
%6 = bitcast %struct.sockaddr_storage* %2 to i8*
%7 = bitcast i32* %3 to i8*
%8 = load i32, i32* @INET6_ADDRSTRLEN
%9 = zext i32 %8 to i64
%10 = tail call i8* @llvm.stacksave()
%11 = alloca i8, i64 %9
store i32 4, i32* %3
%12 = bitcast %struct.sockaddr_storage* %2 to %struct.sockaddr*
%13 = call i32 @accept(i32 %0, %struct.sockaddr* %12, i32* %3)
%14 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %2, i64 0, i32 0
%15 = load i32, i32* %14
%16 = call i32 @get_in_addr(%struct.sockaddr* %12)
%17 = call i32 @inet_ntop(i32 %15, i32 %16, i8* %11, i32 %8)
%18 = call i32 @close(i32 %0)
%19 = call i32 @recv(i32 %13, i8* %5, i32 99, i32 0)
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %5, i64 %20
store i8 0, i8* %21
%22 = call i32 @close(i32 %13)
ret i8* %5
}