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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_aton(char *, int /*<<< orphan*/ *);
char *inet_ntoa(int /*<<< orphan*/);
int listen(int, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int serverfd;
  int cfd;
  struct sockaddr_in saddr;
  struct sockaddr_in caddr;
  socklen_t len;
  int r;
  serverfd = socket(AF_INET, SOCK_STREAM, 0);
  if (serverfd == -1) {
    printf("socket create error:%m\n");
    exit(-1);
  }
  saddr.sin_family = AF_INET;
  saddr.sin_port = htons(8888);
  inet_aton("192.168.137.3", &saddr.sin_addr);
  r = bind(serverfd, (struct sockaddr *)&saddr, sizeof(saddr));
  if (r == -1) {
    printf("socket bind error:%m\n");
    exit(-1);
  }
  printf("socket address bind success...\n");
  r = listen(serverfd, 10);
  if (r == -1) {
    printf("socket listen error:%m\n");
    exit(-1);
  }
  printf("socket server listen success....\n");
  while (1) {
    len = sizeof(caddr);
    cfd = accept(serverfd, (struct sockaddr *)&caddr, &len);
    printf("connect:%d,ip:%s:%u\n", cfd, inet_ntoa(caddr.sin_addr),
           ntohs(caddr.sin_port));
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11ed <main+0x4d>
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x22b8,%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
lea    -0x18(%rbp),%rsi
add    $0x4,%rsi
lea    0xe03(%rip),%rdi
call   1060 <inet_aton@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    1250 <main+0xb0>
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
mov    $0xa,%esi
call   1070 <listen@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    1290 <main+0xf0>
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xc,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   10a0 <accept@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x24(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%edi
call   1080 <ntohs@plt>
mov    -0x3c(%rbp),%esi
mov    -0x38(%rbp),%rdx
mov    %eax,%ecx
lea    0xdb4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    129e <main+0xfe>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2e23(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     12a0 <main+0xf0>
mov    %eax,%r12d
mov    (%rbx),%eax
mov    %eax,0x10(%rsp)
mov    $0x22b8,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0xc(%rsp),%rsi
lea    0xe15(%rip),%rdi
call   1070 <inet_aton@plt>
lea    0x8(%rsp),%rsi
mov    %r12d,%edi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
je     12a9 <main+0xf9>
lea    0xe3f(%rip),%rdi
call   1030 <puts@plt>
mov    %r12d,%edi
mov    $0xa,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
je     12b2 <main+0x102>
lea    0xe40(%rip),%rdi
call   1030 <puts@plt>
lea    0x18(%rsp),%r14
lea    0x4(%rsp),%r15
lea    0xdf6(%rip),%r13
xchg   %ax,%ax
movl   $0xc,0x4(%rsp)
mov    %r12d,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10b0 <accept@plt>
mov    %eax,%ebp
mov    0x1c(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rbx
mov    0x18(%rsp),%edi
call   1090 <ntohs@plt>
mov    %r13,%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1260 <main+0xb0>
lea    0xd59(%rip),%rdi
jmp    12b9 <main+0x109>
lea    0xd76(%rip),%rdi
jmp    12b9 <main+0x109>
lea    0xd83(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [24 x i8] c"socket create error:%m\0A\00"
@.str.1 = [14 x i8] c"192.168.137.3\00"
@.str.2 = [22 x i8] c"socket bind error:%m\0A\00"
@.str.3 = [32 x i8] c"socket address bind success...\0A\00"
@.str.4 = [24 x i8] c"socket listen error:%m\0A\00"
@.str.5 = [34 x i8] c"socket server listen success....\0A\00"
@.str.6 = [21 x i8] c"connect:%d,ip:%s:%u\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 -1)
unreachable
16: ; preds = %0
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %17, i32* %18
%19 = call i32 @htons(i32 8888)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%22 = call i32 @inet_aton(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32* %21)
%23 = load i32, i32* %2
%24 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%25 = call i32 @bind(i32 %23, %struct.sockaddr* %24, i32 12)
store i32 %25, i32* %7
%26 = load i32, i32* %7
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %16
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32 @exit(i32 -1)
unreachable
31: ; preds = %16
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%33 = load i32, i32* %2
%34 = call i32 @listen(i32 %33, i32 10)
store i32 %34, i32* %7
%35 = load i32, i32* %7
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %40
37: ; preds = %31
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
%39 = call i32 @exit(i32 -1)
unreachable
40: ; preds = %31
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0))
br label %42
42: ; preds = %40, %42
store i32 12, i32* %6
%43 = load i32, i32* %2
%44 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%45 = call i32 @accept(i32 %43, %struct.sockaddr* %44, i32* %6)
store i32 %45, i32* %3
%46 = load i32, i32* %3
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%48 = load i32, i32* %47
%49 = call i8* @inet_ntoa(i32 %48)
%50 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%51 = load i32, i32* %50
%52 = call i32 @ntohs(i32 %51)
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i32 %46, i8* %49, i32 %52)
br label %42
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [24 x i8] c"socket create error:%m\0A\00"
@.str.1 = [14 x i8] c"192.168.137.3\00"
@.str.2 = [22 x i8] c"socket bind error:%m\0A\00"
@.str.4 = [24 x i8] c"socket listen error:%m\0A\00"
@.str.6 = [21 x i8] c"connect:%d,ip:%s:%u\0A\00"
@str = [31 x i8] c"socket address bind success...\00"
@str.7 = [33 x i8] c"socket server listen success....\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in* %1 to i8*
%5 = bitcast %struct.sockaddr_in* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 -1)
unreachable
14: ; preds = %0
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %15, i32* %16
%17 = tail call i32 @htons(i32 8888)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
%20 = call i32 @inet_aton(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32* %19)
%21 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%22 = call i32 @bind(i32 %9, %struct.sockaddr* %21, i32 12)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %14
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @exit(i32 -1)
unreachable
27: ; preds = %14
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
%29 = call i32 @listen(i32 %9, i32 10)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
%33 = call i32 @exit(i32 -1)
unreachable
34: ; preds = %27
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.7, i64 0, i64 0))
%36 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
br label %39
39: ; preds = %39, %34
store i32 12, i32* %3
%40 = call i32 @accept(i32 %9, %struct.sockaddr* %36, i32* %3)
%41 = load i32, i32* %37
%42 = call i8* @inet_ntoa(i32 %41)
%43 = load i32, i32* %38
%44 = call i32 @ntohs(i32 %43)
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i32 %40, i8* %42, i32 %44)
br label %39
}