#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 scalar_t__ ssize_t;

/* Variables and functions */
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ bridge_bindLocalSocketToName(int, char const *);
int bridge_createSocket();
scalar_t__ bridge_receiveFromSocket(int, char *, int);
int /*<<< orphan*/ close(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ printf(char *);
scalar_t__ strcmp(char *, char *);
int bridge_thread(const char *domain_name, void (*action)(void *, int)) {
  int localSocket = -1;
  char buffer[512];
  int __exit = 0;
  if ((localSocket = bridge_createSocket()) < 0) {
    printf("Local Socket Creation Error\n");
    return -1;
  }
  if (bridge_bindLocalSocketToName(localSocket, domain_name) < 0) {
    close(localSocket);
    return -1;
  }
  if (listen(localSocket, 4) < 0) {
    close(localSocket);
    return -1;
  }
  while (!__exit) {
    int clientSocket = accept(localSocket, NULL, NULL);
    if (clientSocket < 0) {
      printf("Accept Local Socket Error\n");
      __exit = 1;
    }
    ssize_t recvSize = bridge_receiveFromSocket(clientSocket, buffer, 512 - 1);
    close(clientSocket);
    if (recvSize > 0) {
      if (strcmp(buffer, "EXIT") == 0)
        __exit = 1;
      action(buffer, (int)recvSize);
    }
  }
  close(localSocket);
  return 0;
}


x86 assembly (clang, -O0):
<bridge_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movl   $0x0,-0x224(%rbp)
mov    $0x0,%al
call   1080 <bridge_createSocket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11cf <bridge_thread+0x4f>
lea    0xe42(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1302 <bridge_thread+0x182>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   10a0 <bridge_bindLocalSocketToName@plt>
cmp    $0x0,%rax
jge    11f9 <bridge_thread+0x79>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1302 <bridge_thread+0x182>
mov    -0x1c(%rbp),%edi
mov    $0x4,%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
jge    1224 <bridge_thread+0xa4>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1302 <bridge_thread+0x182>
jmp    1229 <bridge_thread+0xa9>
cmpl   $0x0,-0x224(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1242 <bridge_thread+0xc2>
jmp    12f3 <bridge_thread+0x173>
mov    -0x1c(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1090 <accept@plt>
mov    %eax,-0x228(%rbp)
cmpl   $0x0,-0x228(%rbp)
jge    127a <bridge_thread+0xfa>
lea    0xdb2(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x224(%rbp)
mov    -0x228(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x1ff,%edx
call   1050 <bridge_receiveFromSocket@plt>
mov    %rax,-0x230(%rbp)
mov    -0x228(%rbp),%edi
call   1040 <close@plt>
cmpq   $0x0,-0x230(%rbp)
jle    12ee <bridge_thread+0x16e>
lea    -0x220(%rbp),%rdi
lea    0xd79(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    12d8 <bridge_thread+0x158>
movl   $0x1,-0x224(%rbp)
mov    -0x18(%rbp),%rax
lea    -0x220(%rbp),%rdi
mov    -0x230(%rbp),%rcx
mov    %ecx,%esi
call   *%rax
jmp    1229 <bridge_thread+0xa9>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bridge_thread>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %rsi,%r15
mov    %rdi,%rbx
xor    %eax,%eax
call   1080 <bridge_createSocket@plt>
test   %eax,%eax
js     1250 <bridge_thread+0xd0>
mov    %eax,%r13d
mov    %eax,%edi
mov    %rbx,%rsi
call   10a0 <bridge_bindLocalSocketToName@plt>
test   %rax,%rax
js     125e <bridge_thread+0xde>
mov    %r13d,%edi
mov    $0x4,%esi
call   1070 <listen@plt>
test   %rax,%rax
js     125e <bridge_thread+0xde>
mov    %rsp,%rbx
jmp    11e4 <bridge_thread+0x64>
nopw   0x0(%rax,%rax,1)
test   %ebp,%ebp
js     1244 <bridge_thread+0xc4>
mov    %r13d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1202 <bridge_thread+0x82>
lea    0xe08(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1ff,%edx
call   1050 <bridge_receiveFromSocket@plt>
mov    %rax,%r14
mov    %ebp,%edi
call   1040 <close@plt>
test   %r14,%r14
jle    11e0 <bridge_thread+0x60>
mov    %rbx,%rdi
lea    0xdd6(%rip),%rsi
call   1060 <strcmp@plt>
mov    %rax,%r12
mov    %rbx,%rdi
mov    %r14d,%esi
call   *%r15
test   %ebp,%ebp
js     1244 <bridge_thread+0xc4>
test   %r12,%r12
jne    11e4 <bridge_thread+0x64>
mov    %r13d,%edi
call   1040 <close@plt>
xor    %eax,%eax
jmp    126b <bridge_thread+0xeb>
lea    0xdc8(%rip),%rdi
call   1030 <puts@plt>
jmp    1266 <bridge_thread+0xe6>
mov    %r13d,%edi
call   1040 <close@plt>
mov    $0xffffffff,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"Local Socket Creation Error\0A\00"
@.str.1 = [27 x i8] c"Accept Local Socket Error\0A\00"
@.str.2 = [5 x i8] c"EXIT\00"
define i32 @bridge_thread(i8* %0, void (i8*, i32)* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca void (i8*, i32)*
%6 = alloca i32
%7 = alloca [512 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
store i8* %0, i8** %4
store void (i8*, i32)* %1, void (i8*, i32)** %5
store i32 -1, i32* %6
store i32 0, i32* %8
%11 = call i32 (...) @bridge_createSocket()
store i32 %11, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %64
15: ; preds = %2
%16 = load i32, i32* %6
%17 = load i8*, i8** %4
%18 = call i64 @bridge_bindLocalSocketToName(i32 %16, i8* %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load i32, i32* %6
%22 = call i32 @close(i32 %21)
store i32 -1, i32* %3
br label %64
23: ; preds = %15
%24 = load i32, i32* %6
%25 = call i64 @listen(i32 %24, i32 4)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load i32, i32* %6
%29 = call i32 @close(i32 %28)
store i32 -1, i32* %3
br label %64
30: ; preds = %23
br label %31
31: ; preds = %60, %30
%32 = load i32, i32* %8
%33 = icmp ne i32 %32, 0
%34 = xor i1 %33, true
br i1 %34, label %35, label %61
35: ; preds = %31
%36 = load i32, i32* %6
%37 = call i32 @accept(i32 %36, i32* null, i32* null)
store i32 %37, i32* %9
%38 = load i32, i32* %9
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %8
br label %42
42: ; preds = %40, %35
%43 = load i32, i32* %9
%44 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%45 = call i64 @bridge_receiveFromSocket(i32 %43, i8* %44, i32 511)
store i64 %45, i64* %10
%46 = load i32, i32* %9
%47 = call i32 @close(i32 %46)
%48 = load i64, i64* %10
%49 = icmp sgt i64 %48, 0
br i1 %49, label %50, label %60
50: ; preds = %42
%51 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%52 = call i64 @strcmp(i8* %51, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%53 = icmp eq i64 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %50
store i32 1, i32* %8
br label %55
55: ; preds = %54, %50
%56 = load void (i8*, i32)*, void (i8*, i32)** %5
%57 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%58 = load i64, i64* %10
%59 = trunc i64 %58 to i32
br label %60
60: ; preds = %55, %42
br label %31
61: ; preds = %31
%62 = load i32, i32* %6
%63 = call i32 @close(i32 %62)
store i32 0, i32* %3
br label %64
64: ; preds = %61, %27, %20, %13
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
@.str.2 = [5 x i8] c"EXIT\00"
@str = [26 x i8] c"Accept Local Socket Error\00"
@str.3 = [28 x i8] c"Local Socket Creation Error\00"
define i32 @bridge_thread(i8* %0, void (i8*, i32)* nocapture %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = tail call i32 (...) @bridge_createSocket()
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.3, i64 0, i64 0))
br label %37
9: ; preds = %2
%10 = tail call i64 @bridge_bindLocalSocketToName(i32 %5, i8* %0)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @close(i32 %5)
br label %37
14: ; preds = %9
%15 = tail call i64 @listen(i32 %5, i32 4)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = tail call i32 @close(i32 %5)
br label %37
19: ; preds = %14, %34
%20 = call i32 @accept(i32 %5, i32* null, i32* null)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
br label %24
24: ; preds = %22, %19
%25 = call i64 @bridge_receiveFromSocket(i32 %20, i8* %4, i32 511)
%26 = call i32 @close(i32 %20)
%27 = icmp sgt i64 %25, 0
br i1 %27, label %28, label %33
28: ; preds = %24
%29 = call i64 @strcmp(i8* %4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
%31 = trunc i64 %25 to i32
%32 = or i1 %21, %30
br i1 %32, label %35, label %34
33: ; preds = %24
br i1 %21, label %35, label %34
34: ; preds = %33, %28
br label %19
35: ; preds = %28, %33
%36 = call i32 @close(i32 %5)
br label %37
37: ; preds = %35, %17, %12, %7
%38 = phi i32 [ -1, %7 ], [ -1, %12 ], [ -1, %17 ], [ 0, %35 ]
ret i32 %38
}