#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 dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef scalar_t__ pid_t;

/* Variables and functions */
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ close(int);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ subprocess(int);
int tcp_create_socket();
int qt_task() {
  int listenfd, sockfd;
  socklen_t len;
  struct sockaddr_in clientaddr;
  pid_t pid;
  len = sizeof(struct sockaddr_in);
  if (-1 == (listenfd = tcp_create_socket())) {
    return -1;
  }
  while (1) {
    printf("server waiting...\n");
    if (0 > (sockfd = accept(listenfd, (struct sockaddr *)&clientaddr, &len))) {
      perror("accept");
      return -1;
    }
    if (0 == (pid = fork())) {
      close(listenfd);
      subprocess(sockfd);
      close(sockfd);
      break;
    } else if (pid > 0) {
      close(sockfd);
      continue;
    } else {
      printf("fork error\n");
      return -1;
    }
  }
  close(listenfd);
  return 0;
}


x86 assembly (clang, -O0):
<qt_task>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x4,-0x10(%rbp)
mov    $0x0,%al
call   1050 <tcp_create_socket@plt>
mov    %eax,%ecx
mov    %ecx,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    11a4 <qt_task+0x34>
movl   $0xffffffff,-0x4(%rbp)
jmp    1261 <qt_task+0xf1>
jmp    11a9 <qt_task+0x39>
lea    0xe50(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x10(%rbp),%rdx
call   1070 <accept@plt>
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jle    11ec <qt_task+0x7c>
lea    0xe38(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1261 <qt_task+0xf1>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,%rcx
mov    %rcx,-0x20(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    1222 <qt_task+0xb2>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0xc(%rbp),%edi
call   1090 <subprocess@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
jmp    1252 <qt_task+0xe2>
cmpq   $0x0,-0x20(%rbp)
jle    123a <qt_task+0xca>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
jmp    11a9 <qt_task+0x39>
lea    0xdd9(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1261 <qt_task+0xf1>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<qt_task>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movl   $0x4,0xc(%rsp)
xor    %eax,%eax
call   1050 <tcp_create_socket@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%eax
je     124b <qt_task+0xdb>
mov    %eax,%ebp
lea    0xe62(%rip),%rdi
call   1030 <puts@plt>
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebp,%edi
call   1070 <accept@plt>
test   %eax,%eax
js     1210 <qt_task+0xa0>
mov    %eax,%ebx
lea    0xe3f(%rip),%r15
lea    0x10(%rsp),%r12
lea    0xc(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
je     121e <qt_task+0xae>
jle    123f <qt_task+0xcf>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %r15,%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %r13,%rdx
call   1070 <accept@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11e0 <qt_task+0x70>
lea    0xde9(%rip),%rdi
call   1060 <perror@plt>
jmp    124b <qt_task+0xdb>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%edi
call   1090 <subprocess@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %r14d,%r14d
jmp    124b <qt_task+0xdb>
lea    0xdd3(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [19 x i8] c"server waiting...\0A\00"
@.str.1 = [7 x i8] c"accept\00"
@.str.2 = [12 x i8] c"fork error\0A\00"
define i32 @qt_task() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i64
store i32 4, i32* %4
%7 = call i32 (...) @tcp_create_socket()
store i32 %7, i32* %2
%8 = icmp eq i32 -1, %7
br i1 %8, label %9, label %10
9: ; preds = %0
store i32 -1, i32* %1
br label %40
10: ; preds = %0
br label %11
11: ; preds = %10, %32
%12 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* %2
%14 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%15 = call i32 @accept(i32 %13, %struct.sockaddr* %14, i32* %4)
store i32 %15, i32* %3
%16 = icmp sgt i32 0, %15
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %40
19: ; preds = %11
%20 = call i64 (...) @fork()
store i64 %20, i64* %6
%21 = icmp eq i64 0, %20
br i1 %21, label %22, label %29
22: ; preds = %19
%23 = load i32, i32* %2
%24 = call i32 @close(i32 %23)
%25 = load i32, i32* %3
%26 = call i32 @subprocess(i32 %25)
%27 = load i32, i32* %3
%28 = call i32 @close(i32 %27)
br label %37
29: ; preds = %19
%30 = load i64, i64* %6
%31 = icmp sgt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i32, i32* %3
%34 = call i32 @close(i32 %33)
br label %11
35: ; preds = %29
%36 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %40
37: ; preds = %22
%38 = load i32, i32* %2
%39 = call i32 @close(i32 %38)
store i32 0, i32* %1
br label %40
40: ; preds = %37, %35, %17, %9
%41 = load i32, i32* %1
ret i32 %41
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str.1 = [7 x i8] c"accept\00"
@str = [18 x i8] c"server waiting...\00"
@str.3 = [11 x i8] c"fork error\00"
define i32 @qt_task() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = bitcast i32* %1 to i8*
%4 = bitcast %struct.sockaddr_in* %2 to i8*
store i32 4, i32* %1
%5 = tail call i32 (...) @tcp_create_socket()
%6 = icmp eq i32 %5, -1
br i1 %6, label %32, label %7
7: ; preds = %0
%8 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%9 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%10 = call i32 @accept(i32 %5, %struct.sockaddr* %9, i32* %1)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %25, %7
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %32
14: ; preds = %7, %25
%15 = phi i32 [ %28, %25 ], [ %10, %7 ]
%16 = call i64 (...) @fork()
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %14
%19 = call i32 @close(i32 %5)
%20 = call i32 @subprocess(i32 %15)
%21 = call i32 @close(i32 %15)
%22 = call i32 @close(i32 %5)
br label %32
23: ; preds = %14
%24 = icmp sgt i64 %16, 0
br i1 %24, label %25, label %30
25: ; preds = %23
%26 = call i32 @close(i32 %15)
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%28 = call i32 @accept(i32 %5, %struct.sockaddr* %9, i32* %1)
%29 = icmp slt i32 %28, 0
br i1 %29, label %12, label %14
30: ; preds = %23
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.3, i64 0, i64 0))
br label %32
32: ; preds = %0, %18, %30, %12
%33 = phi i32 [ -1, %12 ], [ 0, %18 ], [ -1, %30 ], [ -1, %0 ]
ret i32 %33
}