#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ time_t;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ member_0;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ sock_opt;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
char *ctime(int /*<<< orphan*/ *);
int getpeername(int, struct sockaddr *, int *);
int getsockname(int, struct sockaddr *, int *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, void *, int);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int write(int, char *, int /*<<< orphan*/);
int main() {
  int ret;
  int server_fd;
  struct sockaddr_in server_addr;
  time_t n;
  server_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (server_fd == -1) {
    perror("create socket error");
    return -1;
  }
  memset(&server_addr, 0, sizeof(struct sockaddr_in));
  server_addr.sin_family = AF_INET;
  server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  server_addr.sin_port = htons(8004);
  int sock_opt = 1;
  if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, (void *)&sock_opt,
                 sizeof(sock_opt))) {
    return -1;
  }
  bind(server_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr_in));
  listen(server_fd, 128);
  while (true) {
    struct sockaddr_in recv_addr = {0};
    int connect_fd = accept(server_fd, (struct sockaddr *)NULL, NULL);
    if (connect_fd == -1) {
      perror("accept error");
      return -1;
    }
    printf("connect_fd is %d\n", connect_fd);
    while (true) {
      char buffer[64] = {0};
      ret = read(connect_fd, buffer, 64);
      printf("recv msg %s\n", buffer);
      struct sockaddr_in addr;
      socklen_t len = sizeof(struct sockaddr);
      ret = getsockname(connect_fd, (struct sockaddr *)&addr, &len);
      if (ret != 0) {
        printf("ret is %d\n", ret);
        perror("get socket name error");
        return ret;
      }
      printf("getsockname: ip is %s, port is %d\n", inet_ntoa(addr.sin_addr),
             ntohs(addr.sin_port));
      struct sockaddr_in peer_addr;
      ret = getpeername(connect_fd, (struct sockaddr *)&peer_addr, &len);
      if (ret != 0) {
        printf("ret is %d\n", ret);
        perror("get socket name error");
        return ret;
      }
      printf("getpeername: ip is %s, port is %d\n",
             inet_ntoa(peer_addr.sin_addr), ntohs(peer_addr.sin_port));
      n = time(NULL);
      sleep(2);
      char write_buffer[64] = {0};
      sprintf(write_buffer, "%s", ctime(&n));
      ret = write(connect_fd, write_buffer, strlen(write_buffer));
      if (ret == -1) {
        perror("write error");
      } else {
        printf("write msg len %d\n\n\n", ret);
      }
    }
    close(connect_fd);
  }
  close(server_fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
mov    0x2d56(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1170 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    12a0 <main+0x50>
lea    0xd71(%rip),%rdi
call   1120 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    158d <main+0x33d>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   10c0 <memset@plt>
mov    0x2d29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
call   10b0 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x1f44,%edi
call   1090 <htons@plt>
mov    %eax,-0x20(%rbp)
movl   $0x1,-0x28(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2cfd(%rip),%rax
mov    (%rax),%esi
mov    0x2ccc(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     131b <main+0xcb>
movl   $0xffffffff,-0x4(%rbp)
jmp    158d <main+0x33d>
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1110 <bind@plt>
mov    -0xc(%rbp),%edi
mov    $0x80,%esi
call   10f0 <listen@plt>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   10c0 <memset@plt>
mov    -0xc(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1130 <accept@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    137d <main+0x12d>
lea    0xca8(%rip),%rdi
call   1120 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    158d <main+0x33d>
mov    -0x3c(%rbp),%esi
lea    0xc9a(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
lea    -0x80(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   10c0 <memset@plt>
mov    -0x3c(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x40,%edx
call   10d0 <read@plt>
mov    %eax,-0x8(%rbp)
lea    -0x80(%rbp),%rsi
lea    0xc76(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
movl   $0x4,-0x94(%rbp)
mov    -0x3c(%rbp),%edi
lea    -0x90(%rbp),%rsi
lea    -0x94(%rbp),%rdx
call   1140 <getsockname@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1419 <main+0x1c9>
mov    -0x8(%rbp),%esi
lea    0xc45(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
lea    0xc42(%rip),%rdi
call   1120 <perror@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    158d <main+0x33d>
mov    -0x8c(%rbp),%edi
call   1050 <inet_ntoa@plt>
mov    %rax,-0xf8(%rbp)
mov    -0x90(%rbp),%edi
call   1100 <ntohs@plt>
mov    -0xf8(%rbp),%rsi
mov    %eax,%edx
lea    0xc1b(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
mov    -0x3c(%rbp),%edi
lea    -0xa8(%rbp),%rsi
lea    -0x94(%rbp),%rdx
call   1060 <getpeername@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1498 <main+0x248>
mov    -0x8(%rbp),%esi
lea    0xbc6(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
lea    0xbc3(%rip),%rdi
call   1120 <perror@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    158d <main+0x33d>
mov    -0xa4(%rbp),%edi
call   1050 <inet_ntoa@plt>
mov    %rax,-0x118(%rbp)
mov    -0xa8(%rbp),%edi
call   1100 <ntohs@plt>
mov    -0x118(%rbp),%rsi
mov    %eax,%edx
lea    0xbbf(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10e0 <time@plt>
mov    %eax,-0x24(%rbp)
mov    $0x2,%edi
call   1160 <sleep@plt>
lea    -0xf0(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   10c0 <memset@plt>
lea    -0xf0(%rbp),%rax
mov    %rax,-0x110(%rbp)
lea    -0x24(%rbp),%rdi
call   1070 <ctime@plt>
mov    -0x110(%rbp),%rdi
mov    %rax,%rdx
lea    0xb8a(%rip),%rsi
call   1150 <sprintf@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x104(%rbp)
lea    -0xf0(%rbp),%rax
mov    %rax,-0x100(%rbp)
lea    -0xf0(%rbp),%rdi
call   1080 <strlen@plt>
mov    -0x104(%rbp),%edi
mov    -0x100(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1577 <main+0x327>
lea    0xb3d(%rip),%rdi
call   1120 <perror@plt>
jmp    1588 <main+0x338>
mov    -0x8(%rbp),%esi
lea    0xb35(%rip),%rdi
mov    $0x0,%al
call   10a0 <printf@plt>
jmp    138e <main+0x13e>
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xc8,%rsp
mov    0x2d78(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2d56(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1170 <socket@plt>
cmp    $0xffffffff,%eax
je     130a <main+0xba>
mov    %eax,%ebx
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   10d0 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x1c(%rsp)
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
call   10c0 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    $0x1f44,%edi
call   10a0 <htons@plt>
mov    %eax,0x10(%rsp)
movl   $0x1,0x8(%rsp)
mov    0x2d1b(%rip),%rax
mov    (%rax),%esi
mov    0x2cea(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    $0xffffffff,%r13d
test   %rax,%rax
je     131e <main+0xce>
mov    %r13d,%eax
add    $0xc8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcef(%rip),%rdi
call   1130 <perror@plt>
mov    $0xffffffff,%r13d
jmp    12f5 <main+0xa5>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   1120 <bind@plt>
mov    %ebx,%edi
mov    $0x80,%esi
call   1100 <listen@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1140 <accept@plt>
cmp    $0xffffffff,%eax
jne    1357 <main+0x107>
lea    0xcc2(%rip),%rdi
jmp    1543 <main+0x2f3>
mov    %eax,%ebx
lea    0xcc1(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   10b0 <printf@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
lea    0x40(%rsp),%rbp
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x40,%edx
call   10e0 <read@plt>
lea    0xc98(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   10b0 <printf@plt>
movl   $0x4,0x4(%rsp)
lea    0x20(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   1150 <getsockname@plt>
mov    %eax,%r13d
test   %eax,%eax
jne    152b <main+0x2db>
lea    0x4(%rsp),%r12
lea    0x80(%rsp),%r14
lea    0x40(%rsp),%r15
jmp    1446 <main+0x1f6>
nopl   (%rax)
lea    0xccc(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   10b0 <printf@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x40,%edx
call   10e0 <read@plt>
lea    0xc16(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   10b0 <printf@plt>
movl   $0x4,0x4(%rsp)
mov    %ebx,%edi
lea    0x20(%rsp),%rsi
mov    %r12,%rdx
call   1150 <getsockname@plt>
test   %eax,%eax
jne    1528 <main+0x2d8>
mov    0x24(%rsp),%edi
call   1060 <inet_ntoa@plt>
mov    %rax,%rbp
mov    0x20(%rsp),%edi
call   1110 <ntohs@plt>
lea    0xbff(%rip),%rdi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   10b0 <printf@plt>
mov    %ebx,%edi
lea    0x30(%rsp),%rsi
mov    %r12,%rdx
call   1070 <getpeername@plt>
test   %eax,%eax
jne    1528 <main+0x2d8>
mov    0x34(%rsp),%edi
call   1060 <inet_ntoa@plt>
mov    %rax,%rbp
mov    0x30(%rsp),%edi
call   1110 <ntohs@plt>
lea    0xbe3(%rip),%rdi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   10b0 <printf@plt>
xor    %edi,%edi
call   10f0 <time@plt>
mov    %eax,0xc(%rsp)
mov    $0x2,%edi
call   1160 <sleep@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
lea    0xc(%rsp),%rdi
call   1080 <ctime@plt>
mov    %r14,%rdi
mov    %rax,%rsi
call   1030 <strcpy@plt>
mov    %r14,%rdi
call   1090 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1050 <write@plt>
cmp    $0xffffffff,%eax
jne    13e0 <main+0x190>
lea    0xb89(%rip),%rdi
call   1130 <perror@plt>
jmp    13f0 <main+0x1a0>
mov    %eax,%r13d
lea    0xb0e(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   10b0 <printf@plt>
lea    0xb08(%rip),%rdi
call   1130 <perror@plt>
jmp    12f5 <main+0xa5>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [20 x i8] c"create socket error\00"
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [13 x i8] c"accept error\00"
@.str.2 = [18 x i8] c"connect_fd is %d\0A\00"
@.str.3 = [13 x i8] c"recv msg %s\0A\00"
@.str.4 = [11 x i8] c"ret is %d\0A\00"
@.str.5 = [22 x i8] c"get socket name error\00"
@.str.6 = [35 x i8] c"getsockname: ip is %s, port is %d\0A\00"
@.str.7 = [35 x i8] c"getpeername: ip is %s, port is %d\0A\00"
@.str.8 = [3 x i8] c"%s\00"
@.str.9 = [12 x i8] c"write error\00"
@.str.10 = [20 x i8] c"write msg len %d\0A\0A\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca [64 x i8]
%10 = alloca %struct.sockaddr_in
%11 = alloca i32
%12 = alloca %struct.sockaddr_in
%13 = alloca [64 x i8]
store i32 0, i32* %1
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %3
%17 = load i32, i32* %3
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %0
%20 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %119
21: ; preds = %0
%22 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 16)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 3
store i32 %23, i32* %24
%25 = load i32, i32* @INADDR_ANY
%26 = call i32 @htonl(i32 %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
store i32 %26, i32* %28
%29 = call i32 @htons(i32 8004)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %29, i32* %30
store i32 1, i32* %6
%31 = load i32, i32* %3
%32 = load i32, i32* @SOL_SOCKET
%33 = load i32, i32* @SO_REUSEADDR
%34 = bitcast i32* %6 to i8*
%35 = call i64 @setsockopt(i32 %31, i32 %32, i32 %33, i8* %34, i32 4)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %21
store i32 -1, i32* %1
br label %119
38: ; preds = %21
%39 = load i32, i32* %3
%40 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%41 = call i32 @bind(i32 %39, %struct.sockaddr* %40, i32 16)
%42 = load i32, i32* %3
%43 = call i32 @listen(i32 %42, i32 128)
br label %44
44: ; preds = %38
%45 = bitcast %struct.sockaddr_in* %7 to i8*
%46 = load i32, i32* %3
%47 = call i32 @accept(i32 %46, %struct.sockaddr* null, i32* null)
store i32 %47, i32* %8
%48 = load i32, i32* %8
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %52
50: ; preds = %44
%51 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %119
52: ; preds = %44
%53 = load i32, i32* %8
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %53)
br label %55
55: ; preds = %52, %118
%56 = bitcast [64 x i8]* %9 to i8*
%57 = load i32, i32* %8
%58 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%59 = call i32 @read(i32 %57, i8* %58, i32 64)
store i32 %59, i32* %2
%60 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %60)
store i32 4, i32* %11
%62 = load i32, i32* %8
%63 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%64 = call i32 @getsockname(i32 %62, %struct.sockaddr* %63, i32* %11)
store i32 %64, i32* %2
%65 = load i32, i32* %2
%66 = icmp ne i32 %65, 0
br i1 %66, label %67, label %72
67: ; preds = %55
%68 = load i32, i32* %2
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %68)
%70 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
%71 = load i32, i32* %2
store i32 %71, i32* %1
br label %119
72: ; preds = %55
%73 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%74 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %73, i32 0, i32 0
%75 = load i32, i32* %74
%76 = call i8* @inet_ntoa(i32 %75)
%77 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%78 = load i32, i32* %77
%79 = call i32 @ntohs(i32 %78)
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i8* %76, i32 %79)
%81 = load i32, i32* %8
%82 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%83 = call i32 @getpeername(i32 %81, %struct.sockaddr* %82, i32* %11)
store i32 %83, i32* %2
%84 = load i32, i32* %2
%85 = icmp ne i32 %84, 0
br i1 %85, label %86, label %91
86: ; preds = %72
%87 = load i32, i32* %2
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %87)
%89 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
%90 = load i32, i32* %2
store i32 %90, i32* %1
br label %119
91: ; preds = %72
%92 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 1
%93 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %92, i32 0, i32 0
%94 = load i32, i32* %93
%95 = call i8* @inet_ntoa(i32 %94)
%96 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
%97 = load i32, i32* %96
%98 = call i32 @ntohs(i32 %97)
%99 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0), i8* %95, i32 %98)
%100 = call i32 @time(i32* null)
store i32 %100, i32* %5
%101 = call i32 @sleep(i32 2)
%102 = bitcast [64 x i8]* %13 to i8*
%103 = getelementptr inbounds [64 x i8], [64 x i8]* %13, i64 0, i64 0
%104 = call i8* @ctime(i32* %5)
%105 = call i32 @sprintf(i8* %103, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i8* %104)
%106 = load i32, i32* %8
%107 = getelementptr inbounds [64 x i8], [64 x i8]* %13, i64 0, i64 0
%108 = getelementptr inbounds [64 x i8], [64 x i8]* %13, i64 0, i64 0
%109 = call i32 @strlen(i8* %108)
%110 = call i32 @write(i32 %106, i8* %107, i32 %109)
store i32 %110, i32* %2
%111 = load i32, i32* %2
%112 = icmp eq i32 %111, -1
br i1 %112, label %113, label %115
113: ; preds = %91
%114 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.9, i64 0, i64 0))
br label %118
115: ; preds = %91
%116 = load i32, i32* %2
%117 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.10, i64 0, i64 0), i32 %116)
br label %118
118: ; preds = %115, %113
br label %55
119: ; preds = %86, %67, %50, %37, %19
%120 = load i32, i32* %1
ret i32 %120
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [20 x i8] c"create socket error\00"
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [13 x i8] c"accept error\00"
@.str.2 = [18 x i8] c"connect_fd is %d\0A\00"
@.str.3 = [13 x i8] c"recv msg %s\0A\00"
@.str.4 = [11 x i8] c"ret is %d\0A\00"
@.str.5 = [22 x i8] c"get socket name error\00"
@.str.6 = [35 x i8] c"getsockname: ip is %s, port is %d\0A\00"
@.str.7 = [35 x i8] c"getpeername: ip is %s, port is %d\0A\00"
@.str.9 = [12 x i8] c"write error\00"
@.str.10 = [20 x i8] c"write msg len %d\0A\0A\0A\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = alloca i32
%4 = alloca [64 x i8]
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca [64 x i8]
%9 = bitcast %struct.sockaddr_in* %1 to i8*
%10 = bitcast i32* %2 to i8*
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %96
17: ; preds = %0
%18 = call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 16)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 3
store i32 %19, i32* %20
%21 = load i32, i32* @INADDR_ANY
%22 = call i32 @htonl(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %22, i32* %23
%24 = call i32 @htons(i32 8004)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %24, i32* %25
%26 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%27 = load i32, i32* @SOL_SOCKET
%28 = load i32, i32* @SO_REUSEADDR
%29 = call i64 @setsockopt(i32 %13, i32 %27, i32 %28, i8* %26, i32 4)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %94
31: ; preds = %17
%32 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%33 = call i32 @bind(i32 %13, %struct.sockaddr* %32, i32 16)
%34 = call i32 @listen(i32 %13, i32 128)
%35 = call i32 @accept(i32 %13, %struct.sockaddr* null, i32* null)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %31
%38 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %94
39: ; preds = %31
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %35)
%41 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%42 = call i32 @read(i32 %35, i8* %41, i32 64)
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %41)
%44 = bitcast %struct.sockaddr_in* %5 to i8*
%45 = bitcast i32* %6 to i8*
store i32 4, i32* %6
%46 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%47 = call i32 @getsockname(i32 %35, %struct.sockaddr* %46, i32* %6)
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %57
49: ; preds = %39
%50 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
%52 = bitcast %struct.sockaddr_in* %7 to i8*
%53 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 1, i32 0
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 0
%56 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
br label %61
57: ; preds = %89, %39
%58 = phi i32 [ %47, %39 ], [ %92, %89 ]
%59 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %58)
%60 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
br label %94
61: ; preds = %49, %89
%62 = load i32, i32* %50
%63 = call i8* @inet_ntoa(i32 %62)
%64 = load i32, i32* %51
%65 = call i32 @ntohs(i32 %64)
%66 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i8* %63, i32 %65)
%67 = call i32 @getpeername(i32 %35, %struct.sockaddr* %53, i32* %6)
%68 = icmp eq i32 %67, 0
br i1 %68, label %72, label %69
69: ; preds = %61
%70 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %67)
%71 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
br label %94
72: ; preds = %61
%73 = load i32, i32* %54
%74 = call i8* @inet_ntoa(i32 %73)
%75 = load i32, i32* %55
%76 = call i32 @ntohs(i32 %75)
%77 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0), i8* %74, i32 %76)
%78 = call i32 @time(i32* null)
store i32 %78, i32* %2
%79 = call i32 @sleep(i32 2)
%80 = call i8* @ctime(i32* %2)
%81 = call i8* @strcpy(i8* %56, i8* dereferenceable(1) %80)
%82 = call i32 @strlen(i8* %56)
%83 = call i32 @write(i32 %35, i8* %56, i32 %82)
%84 = icmp eq i32 %83, -1
br i1 %84, label %85, label %87
85: ; preds = %72
%86 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.9, i64 0, i64 0))
br label %89
87: ; preds = %72
%88 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.10, i64 0, i64 0), i32 %83)
br label %89
89: ; preds = %85, %87
%90 = call i32 @read(i32 %35, i8* %41, i32 64)
%91 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %41)
store i32 4, i32* %6
%92 = call i32 @getsockname(i32 %35, %struct.sockaddr* %46, i32* %6)
%93 = icmp eq i32 %92, 0
br i1 %93, label %61, label %57
94: ; preds = %69, %57, %37, %17
%95 = phi i32 [ -1, %17 ], [ -1, %37 ], [ %58, %57 ], [ %67, %69 ]
br label %96
96: ; preds = %94, %15
%97 = phi i32 [ -1, %15 ], [ %95, %94 ]
ret i32 %97
}