#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 uint16_t;
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ clientname;

/* Variables and functions */
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int);
char *inet_ntoa(int /*<<< orphan*/);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int acceptSocket(int sock) {
  int new;
  socklen_t size;
  struct sockaddr_in clientname;
  char *host;
  uint16_t port;
  size = sizeof(clientname);
  new = accept(sock, (struct sockaddr *)&clientname, &size);
  if (new < 0) {
    perror("accept");
    return -1;
  }
  host = inet_ntoa(clientname.sin_addr);
  port = ntohs(clientname.sin_port);
  fprintf(stderr, "Server: connect from host %s, port %u.\n", host, port);
  return new;
}


x86 assembly (clang, -O0):
<acceptSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x8,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x10(%rbp),%rdx
call   1070 <accept@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1197 <acceptSocket+0x47>
lea    0xe7a(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d0 <acceptSocket+0x80>
mov    -0x14(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
call   1050 <ntohs@plt>
mov    %eax,-0x24(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%ecx
lea    0xe42(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<acceptSocket>:
push   %r14
push   %rbx
sub    $0x18,%rsp
movl   $0x8,0xc(%rsp)
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1070 <accept@plt>
test   %eax,%eax
js     11ad <acceptSocket+0x5d>
mov    %eax,%ebx
mov    0x14(%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%r14
mov    0x10(%rsp),%edi
call   1050 <ntohs@plt>
mov    0x2e50(%rip),%rcx
mov    (%rcx),%edi
lea    0xe6e(%rip),%rsi
mov    %r14,%rdx
mov    %eax,%ecx
call   1040 <fprintf@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe4c(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11a3 <acceptSocket+0x53>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [7 x i8] c"accept\00"
@stderr = global i32 0
@.str.1 = [40 x i8] c"Server: connect from host %s, port %u.\0A\00"
define i32 @acceptSocket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i8*
%8 = alloca i32
store i32 %0, i32* %3
store i32 8, i32* %5
%9 = load i32, i32* %3
%10 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%11 = call i32 @accept(i32 %9, %struct.sockaddr* %10, i32* %5)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %28
16: ; preds = %1
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%18 = load i32, i32* %17
%19 = call i8* @inet_ntoa(i32 %18)
store i8* %19, i8** %7
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @ntohs(i32 %21)
store i32 %22, i32* %8
%23 = load i32, i32* @stderr
%24 = load i8*, i8** %7
%25 = load i32, i32* %8
%26 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i8* %24, i32 %25)
%27 = load i32, i32* %4
store i32 %27, i32* %2
br label %28
28: ; preds = %16, %14
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [7 x i8] c"accept\00"
@stderr = global i32 0
@.str.1 = [40 x i8] c"Server: connect from host %s, port %u.\0A\00"
define i32 @acceptSocket(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = bitcast i32* %2 to i8*
%5 = bitcast %struct.sockaddr_in* %3 to i8*
store i32 8, i32* %2
%6 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%7 = call i32 @accept(i32 %0, %struct.sockaddr* %6, i32* %2)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %20
11: ; preds = %1
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%13 = load i32, i32* %12
%14 = call i8* @inet_ntoa(i32 %13)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @ntohs(i32 %16)
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i8* %14, i32 %17)
br label %20
20: ; preds = %11, %9
%21 = phi i32 [ -1, %9 ], [ %7, %11 ]
ret i32 %21
}