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

/* Variables and functions */
int accept(int, struct sockaddr *, int *);
int bindServerSocket(char *, int /*<<< orphan*/);
int close(int);
int /*<<< orphan*/ errMsg(char *);
int /*<<< orphan*/ forwardImages(int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ waitForClientAlive();
void acceptDataConnection() {
  struct sockaddr_storage claddr;
  int cfd;
  socklen_t addrlen;
  for (;;) {
    printf("Waiting for the client heartbeat.\n");
    waitForClientAlive();
    int lfd = bindServerSocket("1338", 0);
    addrlen = sizeof(struct sockaddr_storage);
    printf("Waiting for an image receiver to connect.\n");
    cfd = accept(lfd, (struct sockaddr *)&claddr, &addrlen);
    if (cfd == -1) {
      errMsg("accept");
      continue;
    }
    printf("Connection accepted.\n");
    forwardImages(cfd);
    if (close(cfd) == -1) {
      errMsg("close");
    }
    if (close(lfd) == -1) {
      errMsg("close");
    }
  }
}


x86 assembly (clang, -O0):
<acceptDataConnection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe81(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1070 <waitForClientAlive@plt>
lea    0xe91(%rip),%rdi
xor    %esi,%esi
call   1030 <bindServerSocket@plt>
mov    %eax,-0x14(%rbp)
movl   $0x4,-0x10(%rbp)
lea    0xe7e(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x8(%rbp),%rsi
lea    -0x10(%rbp),%rdx
call   1090 <accept@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11dd <acceptDataConnection+0x6d>
lea    0xe80(%rip),%rdi
call   1060 <errMsg@plt>
jmp    1178 <acceptDataConnection+0x8>
lea    0xe76(%rip),%rdi
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
call   1080 <forwardImages@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    120e <acceptDataConnection+0x9e>
lea    0xe67(%rip),%rdi
call   1060 <errMsg@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    122b <acceptDataConnection+0xbb>
lea    0xe4a(%rip),%rdi
call   1060 <errMsg@plt>
jmp    1178 <acceptDataConnection+0x8>

x86 assembly (clang, -O2):
<acceptDataConnection>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xeaf(%rip),%r12
lea    0x10(%rsp),%r13
lea    0xc(%rsp),%rbx
lea    0xe6f(%rip),%r15
jmp    11a5 <acceptDataConnection+0x35>
nopl   0x0(%rax,%rax,1)
call   1060 <errMsg@plt>
lea    0xe66(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1070 <waitForClientAlive@plt>
lea    0xe41(%rip),%rdi
xor    %esi,%esi
call   1040 <bindServerSocket@plt>
mov    %eax,%ebp
movl   $0x4,0xc(%rsp)
mov    %r12,%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1090 <accept@plt>
mov    %r15,%rdi
cmp    $0xffffffff,%eax
je     11a0 <acceptDataConnection+0x30>
mov    %eax,%r14d
lea    0xe67(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
call   1080 <forwardImages@plt>
mov    %r14d,%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    121d <acceptDataConnection+0xad>
lea    0xdf4(%rip),%rdi
call   1060 <errMsg@plt>
mov    %ebp,%edi
call   1050 <close@plt>
lea    0xde1(%rip),%rdi
cmp    $0xffffffff,%eax
jne    11a5 <acceptDataConnection+0x35>
jmp    11a0 <acceptDataConnection+0x30>

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [35 x i8] c"Waiting for the client heartbeat.\0A\00"
@.str.1 = [5 x i8] c"1338\00"
@.str.2 = [43 x i8] c"Waiting for an image receiver to connect.\0A\00"
@.str.3 = [7 x i8] c"accept\00"
@.str.4 = [22 x i8] c"Connection accepted.\0A\00"
@.str.5 = [6 x i8] c"close\00"
define void @acceptDataConnection() {
%1 = alloca %struct.sockaddr_storage
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
br label %5
5: ; preds = %32, %15, %0
%6 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%7 = call i32 (...) @waitForClientAlive()
%8 = call i32 @bindServerSocket(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 0)
store i32 %8, i32* %4
store i32 4, i32* %3
%9 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
%10 = load i32, i32* %4
%11 = bitcast %struct.sockaddr_storage* %1 to %struct.sockaddr*
%12 = call i32 @accept(i32 %10, %struct.sockaddr* %11, i32* %3)
store i32 %12, i32* %2
%13 = load i32, i32* %2
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %5
%16 = call i32 @errMsg(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %5
17: ; preds = %5
%18 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%19 = load i32, i32* %2
%20 = call i32 @forwardImages(i32 %19)
%21 = load i32, i32* %2
%22 = call i32 @close(i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %17
%25 = call i32 @errMsg(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %26
26: ; preds = %24, %17
%27 = load i32, i32* %4
%28 = call i32 @close(i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 @errMsg(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %32
32: ; preds = %30, %26
br label %5
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@.str.1 = [5 x i8] c"1338\00"
@.str.3 = [7 x i8] c"accept\00"
@.str.5 = [6 x i8] c"close\00"
@str = [34 x i8] c"Waiting for the client heartbeat.\00"
@str.6 = [42 x i8] c"Waiting for an image receiver to connect.\00"
@str.7 = [21 x i8] c"Connection accepted.\00"
define void @acceptDataConnection() {
%1 = alloca %struct.sockaddr_storage
%2 = alloca i32
%3 = bitcast %struct.sockaddr_storage* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = bitcast %struct.sockaddr_storage* %1 to %struct.sockaddr*
br label %6
6: ; preds = %26, %0
%7 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
%8 = call i32 (...) @waitForClientAlive()
%9 = call i32 @bindServerSocket(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 0)
store i32 4, i32* %2
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str.6, i64 0, i64 0))
%11 = call i32 @accept(i32 %9, %struct.sockaddr* %5, i32* %2)
%12 = icmp eq i32 %11, -1
br i1 %12, label %23, label %13
13: ; preds = %6
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.7, i64 0, i64 0))
%15 = call i32 @forwardImages(i32 %11)
%16 = call i32 @close(i32 %11)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = call i32 @errMsg(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %20
20: ; preds = %18, %13
%21 = call i32 @close(i32 %9)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %20, %6
%24 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), %6 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), %20 ]
%25 = call i32 @errMsg(i8* %24)
br label %26
26: ; preds = %23, %20
br label %6
}