#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 */
typedef int pid_t;
typedef int /*<<< orphan*/ buff;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ EINTR;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int create_socket4(int);
int create_socket6(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int socket_field(int version) {
  pid_t pid;
  int socket_srv;
  int socket_cli;
  FILE *sockfd;
  char buff[1024];
  if (version == 4)
    socket_srv = create_socket4(8080);
  else if (version == 6)
    socket_srv = create_socket6(8080);
  else
    socket_srv = -1;
  if (socket_srv == -1) {
    perror("create socket error");
    return -1;
  }
  while ((socket_cli = accept(socket_srv, NULL, NULL))) {
    if (socket_cli == -1) {
      if (errno == EINTR)
        continue;
      perror("accept error");
      return -1;
    }
    sockfd = fdopen(socket_cli, "w+");
    sleep(1);
    pid = fork();
    printf("child:%d\n", pid);
    if (pid == 0) {
      while (fgets(buff, sizeof(buff) - 1, sockfd) != NULL) {
        printf("%s\n", buff);
      }
      exit(0);
    }
  }
  close(socket_cli);
  return 0;
}


x86 assembly (clang, -O0):
<socket_field>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x4,-0x8(%rbp)
jne    11da <socket_field+0x2a>
mov    $0x1f90,%edi
call   10a0 <create_socket4@plt>
mov    %eax,-0x10(%rbp)
jmp    1202 <socket_field+0x52>
cmpl   $0x6,-0x8(%rbp)
jne    11f6 <socket_field+0x46>
mov    $0x1f90,%edi
call   10b0 <create_socket6@plt>
mov    %eax,-0x10(%rbp)
jmp    11fd <socket_field+0x4d>
movl   $0xffffffff,-0x10(%rbp)
jmp    1202 <socket_field+0x52>
cmpl   $0xffffffff,-0x10(%rbp)
jne    1224 <socket_field+0x74>
lea    0xded(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1320 <socket_field+0x170>
jmp    1229 <socket_field+0x79>
mov    -0x10(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1080 <accept@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
je     1311 <socket_field+0x161>
cmpl   $0xffffffff,-0x14(%rbp)
jne    1285 <socket_field+0xd5>
mov    0x2d8b(%rip),%rax
mov    (%rax),%rax
mov    0x2d79(%rip),%rcx
cmp    (%rcx),%rax
jne    126d <socket_field+0xbd>
jmp    1229 <socket_field+0x79>
lea    0xda0(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1320 <socket_field+0x170>
mov    -0x14(%rbp),%edi
lea    0xd92(%rip),%rsi
call   1060 <fdopen@plt>
mov    %rax,-0x20(%rbp)
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xd6e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0xc(%rbp)
jne    130c <socket_field+0x15c>
jmp    12cc <socket_field+0x11c>
lea    -0x420(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x3ff,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1305 <socket_field+0x155>
lea    -0x420(%rbp),%rsi
lea    0xd35(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12cc <socket_field+0x11c>
xor    %edi,%edi
call   1090 <exit@plt>
jmp    1229 <socket_field+0x79>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_field>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
cmp    $0x6,%edi
je     11eb <socket_field+0x2b>
cmp    $0x4,%edi
jne    12e1 <socket_field+0x121>
mov    $0x1f90,%edi
call   10b0 <create_socket4@plt>
jmp    11f5 <socket_field+0x35>
mov    $0x1f90,%edi
call   10c0 <create_socket6@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
je     12e1 <socket_field+0x121>
mov    0x2dd0(%rip),%r13
lea    0xe12(%rip),%r14
lea    0xe0e(%rip),%r15
jmp    1234 <socket_field+0x74>
nopl   0x0(%rax,%rax,1)
mov    0x2db9(%rip),%rax
mov    (%rax),%rax
cmp    0x0(%r13),%rax
jne    12ea <socket_field+0x12a>
mov    %r12d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <accept@plt>
cmp    $0xffffffff,%eax
je     1220 <socket_field+0x60>
test   %eax,%eax
je     12c4 <socket_field+0x104>
mov    %eax,%edi
mov    %r14,%rsi
call   1070 <fdopen@plt>
mov    %rax,%rbx
mov    $0x1,%edi
call   10d0 <sleep@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
mov    %eax,%ebp
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
test   %ebp,%ebp
jne    1234 <socket_field+0x74>
mov    %rsp,%rdi
mov    $0x3ff,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     12bd <socket_field+0xfd>
mov    %rsp,%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1030 <puts@plt>
mov    %rbp,%rdi
mov    $0x3ff,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
jne    12a0 <socket_field+0xe0>
xor    %edi,%edi
call   10a0 <exit@plt>
xor    %ebx,%ebx
xor    %edi,%edi
call   1050 <close@plt>
mov    %ebx,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd18(%rip),%rdi
jmp    12f1 <socket_field+0x131>
lea    0xd23(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebx
jmp    12cd <socket_field+0x10d>

LLVM IR (-O0):
@.str = [20 x i8] c"create socket error\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [13 x i8] c"accept error\00"
@.str.2 = [3 x i8] c"w+\00"
@.str.3 = [10 x i8] c"child:%d\0A\00"
@.str.4 = [4 x i8] c"%s\0A\00"
define i32 @socket_field(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca [1024 x i8]
store i32 %0, i32* %3
%9 = load i32, i32* %3
%10 = icmp eq i32 %9, 4
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @create_socket4(i32 8080)
store i32 %12, i32* %5
br label %20
13: ; preds = %1
%14 = load i32, i32* %3
%15 = icmp eq i32 %14, 6
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @create_socket6(i32 8080)
store i32 %17, i32* %5
br label %19
18: ; preds = %13
store i32 -1, i32* %5
br label %19
19: ; preds = %18, %16
br label %20
20: ; preds = %19, %11
%21 = load i32, i32* %5
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %64
25: ; preds = %20
br label %26
26: ; preds = %60, %37, %25
%27 = load i32, i32* %5
%28 = call i32 @accept(i32 %27, i32* null, i32* null)
store i32 %28, i32* %6
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %61
30: ; preds = %26
%31 = load i32, i32* %6
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %40
33: ; preds = %30
%34 = load i64, i64* @errno
%35 = load i64, i64* @EINTR
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %33
br label %26
38: ; preds = %33
%39 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %64
40: ; preds = %30
%41 = load i32, i32* %6
%42 = call i32* @fdopen(i32 %41, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %42, i32** %7
%43 = call i32 @sleep(i32 1)
%44 = call i32 (...) @fork()
store i32 %44, i32* %4
%45 = load i32, i32* %4
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %45)
%47 = load i32, i32* %4
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %60
49: ; preds = %40
br label %50
50: ; preds = %55, %49
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%52 = load i32*, i32** %7
%53 = call i32* @fgets(i8* %51, i32 1023, i32* %52)
%54 = icmp ne i32* %53, null
br i1 %54, label %55, label %58
55: ; preds = %50
%56 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %56)
br label %50
58: ; preds = %50
%59 = call i32 @exit(i32 0)
unreachable
60: ; preds = %40
br label %26
61: ; preds = %26
%62 = load i32, i32* %6
%63 = call i32 @close(i32 %62)
store i32 0, i32* %2
br label %64
64: ; preds = %61, %38, %23
%65 = load i32, i32* %2
ret i32 %65
}

LLVM IR (-O2):
@.str = [20 x i8] c"create socket error\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [13 x i8] c"accept error\00"
@.str.2 = [3 x i8] c"w+\00"
@.str.3 = [10 x i8] c"child:%d\0A\00"
define i32 @socket_field(i32 %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
switch i32 %0, label %11 [
i32 4, label %4
i32 6, label %6
]
4: ; preds = %1
%5 = tail call i32 @create_socket4(i32 8080)
br label %8
6: ; preds = %1
%7 = tail call i32 @create_socket6(i32 8080)
br label %8
8: ; preds = %6, %4
%9 = phi i32 [ %5, %4 ], [ %7, %6 ]
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %1, %8
%12 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %39
13: ; preds = %8, %19
%14 = tail call i32 @accept(i32 %9, i32* null, i32* null)
switch i32 %14, label %22 [
i32 0, label %37
i32 -1, label %15
]
15: ; preds = %13
%16 = load i64, i64* @errno
%17 = load i64, i64* @EINTR
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %20
19: ; preds = %15, %22
br label %13
20: ; preds = %15
%21 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %39
22: ; preds = %13
%23 = tail call i32* @fdopen(i32 %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%24 = tail call i32 @sleep(i32 1)
%25 = tail call i32 (...) @fork()
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %25)
%27 = icmp eq i32 %25, 0
br i1 %27, label %28, label %19
28: ; preds = %22
%29 = call i32* @fgets(i8* %3, i32 1023, i32* %23)
%30 = icmp eq i32* %29, null
br i1 %30, label %35, label %31
31: ; preds = %28, %31
%32 = call i32 @puts(i8* %3)
%33 = call i32* @fgets(i8* %3, i32 1023, i32* %23)
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %31
35: ; preds = %31, %28
%36 = call i32 @exit(i32 0)
unreachable
37: ; preds = %13
%38 = tail call i32 @close(i32 0)
br label %39
39: ; preds = %37, %20, %11
%40 = phi i32 [ -1, %11 ], [ -1, %20 ], [ 0, %37 ]
ret i32 %40
}