#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 /*<<< orphan*/ socklen_t;

/* Variables and functions */
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
scalar_t__ client_connect(int, char *, int);
int /*<<< orphan*/ close(int);
scalar_t__ create_serv(int, int);
int create_socket();
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int fork();
char *inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, char *, int);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ transdata(int, int);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int bind2conn(int port1, char *host, int port2) {
  int sockfd, sockfd1, sockfd2;
  struct sockaddr_in remote;
  int size;
  int pid;
  char buffer[1024];
  memset(buffer, 0, 1024);
  if ((sockfd = create_socket()) == 0)
    exit(0);
  if (create_serv(sockfd, port1) == 0) {
    close(sockfd1);
    exit(0);
  }
  size = sizeof(struct sockaddr);
  while (1) {
    printf("waiting for response.........\n");
    if ((sockfd1 = accept(sockfd, (struct sockaddr *)&remote,
                          (socklen_t *)&size)) < 0) {
      perror("accept error\n");
      continue;
    }
    printf("accept a client from %s:%d\n", inet_ntoa(remote.sin_addr),
           ntohs(remote.sin_port));
    if ((sockfd2 = create_socket()) == 0) {
      close(sockfd1);
      continue;
    }
    printf("make a connection to %s:%d....", host, port2);
    fflush(stdout);
    if (client_connect(sockfd2, host, port2) == 0) {
      close(sockfd2);
      sprintf(buffer, "[SERVER]connection to %s:%d error\r\n", host, port2);
      write(sockfd1, buffer, strlen(buffer));
      memset(buffer, 0, 1024);
      close(sockfd1);
      continue;
    }
    printf("ok\r\n");
    pid = fork();
    if (pid == 0)
      transdata(sockfd1, sockfd2);
    close(sockfd1);
    close(sockfd2);
  }
}


x86 assembly (clang, -O0):
<bind2conn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1090 <memset@plt>
mov    $0x0,%al
call   10d0 <create_socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jne    1252 <bind2conn+0x42>
xor    %edi,%edi
call   1120 <exit@plt>
mov    -0x18(%rbp),%edi
mov    -0x4(%rbp),%esi
call   1030 <create_serv@plt>
cmp    $0x0,%rax
jne    1276 <bind2conn+0x66>
mov    -0x1c(%rbp),%edi
call   10a0 <close@plt>
xor    %edi,%edi
call   1120 <exit@plt>
movl   $0x4,-0x2c(%rbp)
lea    0xd7c(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1100 <accept@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    12b8 <bind2conn+0xa8>
lea    0xd71(%rip),%rdi
call   10f0 <perror@plt>
jmp    127d <bind2conn+0x6d>
mov    -0x24(%rbp),%edi
call   1060 <inet_ntoa@plt>
mov    %rax,-0x438(%rbp)
mov    -0x28(%rbp),%edi
call   10e0 <ntohs@plt>
mov    -0x438(%rbp),%rsi
mov    %eax,%edx
lea    0xd4e(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    $0x0,%al
call   10d0 <create_socket@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jne    1306 <bind2conn+0xf6>
mov    -0x1c(%rbp),%edi
call   10a0 <close@plt>
jmp    127d <bind2conn+0x6d>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xd35(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    0x2cbe(%rip),%rax
mov    (%rax),%edi
call   10c0 <fflush@plt>
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   10b0 <client_connect@plt>
cmp    $0x0,%rax
jne    13bb <bind2conn+0x1ab>
mov    -0x20(%rbp),%edi
call   10a0 <close@plt>
lea    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    0xd09(%rip),%rsi
call   1110 <sprintf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x444(%rbp)
lea    -0x430(%rbp),%rax
mov    %rax,-0x440(%rbp)
lea    -0x430(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x444(%rbp),%edi
mov    -0x440(%rbp),%rsi
mov    %eax,%edx
call   1050 <write@plt>
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1090 <memset@plt>
mov    -0x1c(%rbp),%edi
call   10a0 <close@plt>
jmp    127d <bind2conn+0x6d>
lea    0xcca(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    $0x0,%al
call   1130 <fork@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jne    13e8 <bind2conn+0x1d8>
mov    -0x1c(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1040 <transdata@plt>
mov    -0x1c(%rbp),%edi
call   10a0 <close@plt>
mov    -0x20(%rbp),%edi
call   10a0 <close@plt>
jmp    127d <bind2conn+0x6d>

x86 assembly (clang, -O2):
<bind2conn>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    %edx,%ebx
mov    %rsi,%rbp
mov    %edi,%r15d
lea    0x20(%rsp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   10a0 <memset@plt>
xor    %eax,%eax
call   10e0 <create_socket@plt>
test   %eax,%eax
je     13da <bind2conn+0x1ba>
mov    %eax,%r13d
mov    %rbp,0x18(%rsp)
mov    %ebx,0xc(%rsp)
mov    %eax,%edi
mov    %r15d,%esi
call   1040 <create_serv@plt>
test   %rax,%rax
je     13e1 <bind2conn+0x1c1>
movl   $0x4,0x4(%rsp)
lea    0xde6(%rip),%r14
lea    0x10(%rsp),%rbp
lea    0x4(%rsp),%rbx
mov    %r13d,0x8(%rsp)
jmp    12ac <bind2conn+0x8c>
nopl   0x0(%rax,%rax,1)
lea    0xd59(%rip),%rdi
call   1100 <perror@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1110 <accept@plt>
test   %eax,%eax
js     12a0 <bind2conn+0x80>
mov    %eax,%r15d
mov    0x14(%rsp),%edi
call   1070 <inet_ntoa@plt>
mov    %rax,%rbx
mov    0x10(%rsp),%edi
call   10f0 <ntohs@plt>
lea    0xd29(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1090 <printf@plt>
xor    %eax,%eax
call   10e0 <create_socket@plt>
test   %eax,%eax
jne    1310 <bind2conn+0xf0>
mov    %r15d,%edi
call   10b0 <close@plt>
lea    0x4(%rsp),%rbx
jmp    12ac <bind2conn+0x8c>
nopl   0x0(%rax,%rax,1)
mov    %eax,%r12d
lea    0xd10(%rip),%rdi
mov    0x18(%rsp),%r13
mov    %r13,%rsi
mov    0xc(%rsp),%ebx
mov    %ebx,%edx
xor    %eax,%eax
call   1090 <printf@plt>
mov    0x2caa(%rip),%rax
mov    (%rax),%edi
call   10d0 <fflush@plt>
mov    %r12d,%edi
mov    %r13,%rsi
mov    %ebx,%edx
call   10c0 <client_connect@plt>
test   %rax,%rax
jne    1399 <bind2conn+0x179>
mov    %r12d,%edi
call   10b0 <close@plt>
lea    0x20(%rsp),%r12
mov    %r12,%rdi
lea    0xce3(%rip),%rsi
mov    %r13,%rdx
mov    %ebx,%ecx
call   1120 <sprintf@plt>
mov    %r12,%rdi
call   1080 <strlen@plt>
mov    %r15d,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1060 <write@plt>
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   10a0 <memset@plt>
mov    %r15d,%edi
jmp    13c6 <bind2conn+0x1a6>
lea    0xceb(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1140 <fork@plt>
test   %eax,%eax
jne    13bb <bind2conn+0x19b>
mov    %r15d,%edi
mov    %r12d,%esi
call   1050 <transdata@plt>
mov    %r15d,%edi
call   10b0 <close@plt>
mov    %r12d,%edi
call   10b0 <close@plt>
mov    0x8(%rsp),%r13d
lea    0x4(%rsp),%rbx
jmp    12ac <bind2conn+0x8c>
xor    %edi,%edi
call   1130 <exit@plt>
call   10b0 <close@plt>
xor    %edi,%edi
call   1130 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [31 x i8] c"waiting for response.........\0A\00"
@.str.1 = [14 x i8] c"accept error\0A\00"
@.str.2 = [28 x i8] c"accept a client from %s:%d\0A\00"
@.str.3 = [31 x i8] c"make a connection to %s:%d....\00"
@stdout = global i32 0
@.str.4 = [36 x i8] c"[SERVER]connection to %s:%d error\0D\0A\00"
@.str.5 = [5 x i8] c"ok\0D\0A\00"
define i32 @bind2conn(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
%11 = alloca i32
%12 = alloca i32
%13 = alloca [1024 x i8]
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%15 = call i32 @memset(i8* %14, i32 0, i32 1024)
%16 = call i32 (...) @create_socket()
store i32 %16, i32* %7
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %3
%19 = call i32 @exit(i32 0)
unreachable
20: ; preds = %3
%21 = load i32, i32* %7
%22 = load i32, i32* %4
%23 = call i64 @create_serv(i32 %21, i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %20
%26 = load i32, i32* %8
%27 = call i32 @close(i32 %26)
%28 = call i32 @exit(i32 0)
unreachable
29: ; preds = %20
store i32 4, i32* %11
br label %30
30: ; preds = %29, %36, %48, %62, %87
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%32 = load i32, i32* %7
%33 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%34 = call i32 @accept(i32 %32, %struct.sockaddr* %33, i32* %11)
store i32 %34, i32* %8
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %30
%37 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %30
38: ; preds = %30
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%40 = load i32, i32* %39
%41 = call i8* @inet_ntoa(i32 %40)
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%43 = load i32, i32* %42
%44 = call i32 @ntohs(i32 %43)
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %41, i32 %44)
%46 = call i32 (...) @create_socket()
store i32 %46, i32* %9
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %38
%49 = load i32, i32* %8
%50 = call i32 @close(i32 %49)
br label %30
51: ; preds = %38
%52 = load i8*, i8** %5
%53 = load i32, i32* %6
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %52, i32 %53)
%55 = load i32, i32* @stdout
%56 = call i32 @fflush(i32 %55)
%57 = load i32, i32* %9
%58 = load i8*, i8** %5
%59 = load i32, i32* %6
%60 = call i64 @client_connect(i32 %57, i8* %58, i32 %59)
%61 = icmp eq i64 %60, 0
br i1 %61, label %62, label %78
62: ; preds = %51
%63 = load i32, i32* %9
%64 = call i32 @close(i32 %63)
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%66 = load i8*, i8** %5
%67 = load i32, i32* %6
%68 = call i32 @sprintf(i8* %65, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i8* %66, i32 %67)
%69 = load i32, i32* %8
%70 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%71 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%72 = call i32 @strlen(i8* %71)
%73 = call i32 @write(i32 %69, i8* %70, i32 %72)
%74 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%75 = call i32 @memset(i8* %74, i32 0, i32 1024)
%76 = load i32, i32* %8
%77 = call i32 @close(i32 %76)
br label %30
78: ; preds = %51
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%80 = call i32 (...) @fork()
store i32 %80, i32* %12
%81 = load i32, i32* %12
%82 = icmp eq i32 %81, 0
br i1 %82, label %83, label %87
83: ; preds = %78
%84 = load i32, i32* %8
%85 = load i32, i32* %9
%86 = call i32 @transdata(i32 %84, i32 %85)
br label %87
87: ; preds = %83, %78
%88 = load i32, i32* %8
%89 = call i32 @close(i32 %88)
%90 = load i32, i32* %9
%91 = call i32 @close(i32 %90)
br label %30
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str.1 = [14 x i8] c"accept error\0A\00"
@.str.2 = [28 x i8] c"accept a client from %s:%d\0A\00"
@.str.3 = [31 x i8] c"make a connection to %s:%d....\00"
@stdout = global i32 0
@.str.4 = [36 x i8] c"[SERVER]connection to %s:%d error\0D\0A\00"
@str = [30 x i8] c"waiting for response.........\00"
@str.6 = [4 x i8] c"ok\0D\00"
define i32 @bind2conn(i32 %0, i8* %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%10 = call i32 @memset(i8* %9, i32 0, i32 1024)
%11 = call i32 (...) @create_socket()
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = call i32 @exit(i32 0)
unreachable
15: ; preds = %3
%16 = call i64 @create_serv(i32 %11, i32 %0)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = call i32 @close(i32 undef)
%20 = call i32 @exit(i32 0)
unreachable
21: ; preds = %15
store i32 4, i32* %5
%22 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
br label %25
25: ; preds = %31, %21
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
%27 = call i32 @accept(i32 %11, %struct.sockaddr* %22, i32* %5)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %29, %40, %48, %61
br label %25
32: ; preds = %25
%33 = load i32, i32* %23
%34 = call i8* @inet_ntoa(i32 %33)
%35 = load i32, i32* %24
%36 = call i32 @ntohs(i32 %35)
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %34, i32 %36)
%38 = call i32 (...) @create_socket()
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %32
%41 = call i32 @close(i32 %27)
br label %31
42: ; preds = %32
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %1, i32 %2)
%44 = load i32, i32* @stdout
%45 = call i32 @fflush(i32 %44)
%46 = call i64 @client_connect(i32 %38, i8* %1, i32 %2)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %55
48: ; preds = %42
%49 = call i32 @close(i32 %38)
%50 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i8* %1, i32 %2)
%51 = call i32 @strlen(i8* %9)
%52 = call i32 @write(i32 %27, i8* %9, i32 %51)
%53 = call i32 @memset(i8* %9, i32 0, i32 1024)
%54 = call i32 @close(i32 %27)
br label %31
55: ; preds = %42
%56 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @str.6, i64 0, i64 0))
%57 = call i32 (...) @fork()
%58 = icmp eq i32 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %55
%60 = call i32 @transdata(i32 %27, i32 %38)
br label %61
61: ; preds = %59, %55
%62 = call i32 @close(i32 %27)
%63 = call i32 @close(i32 %38)
br label %31
}