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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EAGAIN;
scalar_t__ EWOULDBLOCK;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ bzero(char *, int);
scalar_t__ errno;
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/ *, char *,
                             int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, int);
int /*<<< orphan*/ usleep(int);
int socketServerAccept(int servfd) {
  socklen_t len;
  int clifd;
  struct sockaddr_in cliaddr;
  len = sizeof(cliaddr);
  clifd = accept(servfd, (struct sockaddr *)&cliaddr, &len);
  if (clifd < 0) {
    if (errno == EAGAIN || errno == EWOULDBLOCK) {
      usleep(10000);
    } else {
      perror("call accept error");
    }
    return -1;
  }
  char szIp[17];
  bzero(szIp, 17);
  inet_ntop(AF_INET, &cliaddr.sin_addr, szIp, 16);
  printf("from client IP:%s,Port:%d\n", szIp, ntohs(cliaddr.sin_port));
  return clifd;
}


x86 assembly (clang, -O0):
<socketServerAccept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x8,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0xc(%rbp),%rdx
call   1080 <accept@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11fa <socketServerAccept+0x8a>
mov    0x2e32(%rip),%rax
mov    (%rax),%rax
mov    0x2e10(%rip),%rcx
cmp    (%rcx),%rax
je     11d3 <socketServerAccept+0x63>
mov    0x2e18(%rip),%rax
mov    (%rax),%rax
mov    0x2e06(%rip),%rcx
cmp    (%rcx),%rax
jne    11e2 <socketServerAccept+0x72>
mov    $0x2710,%edi
call   1090 <usleep@plt>
jmp    11ee <socketServerAccept+0x7e>
lea    0xe17(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124f <socketServerAccept+0xdf>
lea    -0x30(%rbp),%rdi
mov    $0x11,%esi
call   1070 <bzero@plt>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
add    $0x4,%rsi
lea    -0x30(%rbp),%rdx
mov    $0x10,%ecx
call   1030 <inet_ntop@plt>
lea    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%edi
call   1050 <ntohs@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
lea    0xdce(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socketServerAccept>:
push   %r14
push   %rbx
sub    $0x28,%rsp
movl   $0x8,0x4(%rsp)
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
call   1080 <accept@plt>
test   %eax,%eax
js     11dd <socketServerAccept+0x6d>
mov    %eax,%ebx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x11,%esi
call   1070 <bzero@plt>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
lea    0xc(%rsp),%rsi
mov    %r14,%rdx
mov    $0x10,%ecx
call   1030 <inet_ntop@plt>
mov    0x8(%rsp),%edi
call   1050 <ntohs@plt>
lea    0xe41(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <printf@plt>
jmp    120e <socketServerAccept+0x9e>
mov    0x2df4(%rip),%rax
mov    (%rax),%rax
mov    0x2dd2(%rip),%rcx
cmp    (%rcx),%rax
je     11ff <socketServerAccept+0x8f>
mov    0x2dd6(%rip),%rcx
cmp    (%rcx),%rax
jne    1218 <socketServerAccept+0xa8>
mov    $0x2710,%edi
call   1090 <usleep@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
lea    0xde1(%rip),%rdi
call   1060 <perror@plt>
jmp    1209 <socketServerAccept+0x99>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@.str = [18 x i8] c"call accept error\00"
@AF_INET = global i32 0
@.str.1 = [27 x i8] c"from client IP:%s,Port:%d\0A\00"
define i32 @socketServerAccept(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca [17 x i8]
store i32 %0, i32* %3
store i32 8, i32* %4
%8 = load i32, i32* %3
%9 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%10 = call i32 @accept(i32 %8, %struct.sockaddr* %9, i32* %4)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %26
13: ; preds = %1
%14 = load i64, i64* @errno
%15 = load i64, i64* @EAGAIN
%16 = icmp eq i64 %14, %15
br i1 %16, label %21, label %17
17: ; preds = %13
%18 = load i64, i64* @errno
%19 = load i64, i64* @EWOULDBLOCK
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %23
21: ; preds = %17, %13
%22 = call i32 @usleep(i32 10000)
br label %25
23: ; preds = %17
%24 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %23, %21
store i32 -1, i32* %2
br label %39
26: ; preds = %1
%27 = getelementptr inbounds [17 x i8], [17 x i8]* %7, i64 0, i64 0
%28 = call i32 @bzero(i8* %27, i32 17)
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%31 = getelementptr inbounds [17 x i8], [17 x i8]* %7, i64 0, i64 0
%32 = call i32 @inet_ntop(i32 %29, i32* %30, i8* %31, i32 16)
%33 = getelementptr inbounds [17 x i8], [17 x i8]* %7, i64 0, i64 0
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @ntohs(i32 %35)
%37 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %33, i32 %36)
%38 = load i32, i32* %5
store i32 %38, i32* %2
br label %39
39: ; preds = %26, %25
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@.str = [18 x i8] c"call accept error\00"
@AF_INET = global i32 0
@.str.1 = [27 x i8] c"from client IP:%s,Port:%d\0A\00"
define i32 @socketServerAccept(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca [17 x i8]
%5 = bitcast i32* %2 to i8*
%6 = bitcast %struct.sockaddr_in* %3 to i8*
store i32 8, i32* %2
%7 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%8 = call i32 @accept(i32 %0, %struct.sockaddr* %7, i32* %2)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %21
10: ; preds = %1
%11 = load i64, i64* @errno
%12 = load i64, i64* @EAGAIN
%13 = icmp eq i64 %11, %12
%14 = load i64, i64* @EWOULDBLOCK
%15 = icmp eq i64 %11, %14
%16 = select i1 %13, i1 true, i1 %15
br i1 %16, label %17, label %19
17: ; preds = %10
%18 = call i32 @usleep(i32 10000)
br label %31
19: ; preds = %10
%20 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %31
21: ; preds = %1
%22 = getelementptr inbounds [17 x i8], [17 x i8]* %4, i64 0, i64 0
%23 = call i32 @bzero(i8* %22, i32 17)
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%26 = call i32 @inet_ntop(i32 %24, i32* %25, i8* %22, i32 16)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @ntohs(i32 %28)
%30 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %22, i32 %29)
br label %31
31: ; preds = %17, %19, %21
%32 = phi i32 [ %8, %21 ], [ -1, %19 ], [ -1, %17 ]
ret i32 %32
}