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

/* Variables and functions */
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char *inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ socketSetNonBlocking(int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int socketAccept(int sockfd) {
  int new_fd = -1;
  int sin_size;
  struct sockaddr_in client_addr;
  sin_size = sizeof(client_addr);
  socketSetNonBlocking(sockfd);
  if ((new_fd = accept(sockfd, (struct sockaddr *)&client_addr,
                       (socklen_t *)&sin_size)) == -1) {
    fprintf(stderr, "Accept error:%s\n\a", strerror(errno));
    return -1;
  }
  fprintf(stderr, "Server get connection from %s\n",
          inet_ntoa(client_addr.sin_addr));
  return new_fd;
}


x86 assembly (clang, -O0):
<socketAccept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
movl   $0x4,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <socketSetNonBlocking@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x10(%rbp),%rdx
call   1060 <accept@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    11c5 <socketAccept+0x75>
mov    0x2e4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe4c(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f1 <socketAccept+0xa1>
mov    0x2e14(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xe2c(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socketAccept>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
movl   $0x4,0x4(%rsp)
call   1050 <socketSetNonBlocking@plt>
mov    %rsp,%rsi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   1060 <accept@plt>
mov    %eax,%ebx
mov    0x2e66(%rip),%rax
mov    (%rax),%ebp
cmp    $0xffffffff,%ebx
je     1192 <socketAccept+0x42>
mov    (%rsp),%edi
call   1030 <inet_ntoa@plt>
lea    0xe82(%rip),%rsi
jmp    11a7 <socketAccept+0x57>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe59(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1040 <fprintf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [18 x i8] c"Accept error:%s\0A\07\00"
@errno = global i32 0
@.str.1 = [31 x i8] c"Server get connection from %s\0A\00"
define i32 @socketAccept(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
store i32 %0, i32* %3
store i32 -1, i32* %4
store i32 4, i32* %5
%7 = load i32, i32* %3
%8 = call i32 @socketSetNonBlocking(i32 %7)
%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 = icmp eq i32 %11, -1
br i1 %12, label %13, label %18
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 -1, i32* %2
br label %25
18: ; preds = %1
%19 = load i32, i32* @stderr
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i8* @inet_ntoa(i32 %21)
%23 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = load i32, i32* %4
store i32 %24, i32* %2
br label %25
25: ; preds = %18, %13
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [18 x i8] c"Accept error:%s\0A\07\00"
@errno = global i32 0
@.str.1 = [31 x i8] c"Server get connection from %s\0A\00"
define i32 @socketAccept(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 4, i32* %2
%6 = tail call i32 @socketSetNonBlocking(i32 %0)
%7 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%8 = call i32 @accept(i32 %0, %struct.sockaddr* %7, i32* %2)
%9 = icmp eq i32 %8, -1
%10 = load i32, i32* @stderr
br i1 %9, label %11, label %15
11: ; preds = %1
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %13)
br label %20
15: ; preds = %1
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%17 = load i32, i32* %16
%18 = call i8* @inet_ntoa(i32 %17)
%19 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %18)
br label %20
20: ; preds = %15, %11
ret i32 %8
}