#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_un {
  int /*<<< orphan*/ sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};
struct protocol {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_un *, int);
int /*<<< orphan*/ check_load();
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
struct protocol *fetch();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(struct protocol *);
int /*<<< orphan*/ process(struct protocol *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
void *thread_main(void *arg) {
  const char *spath = (const char *)arg;
  int fd;
  struct sockaddr_un addr;
  struct protocol *cmd;
  fd = socket(PF_UNIX, SOCK_STREAM, 0);
  if (fd < 0) {
    fprintf(stderr, "failed to create unix socket\n");
    exit(2);
  }
  bzero(&addr, sizeof(addr));
  addr.sun_family = AF_UNIX;
  strncpy(addr.sun_path, spath, sizeof(addr.sun_path) - 1);
  if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    fprintf(stderr, "socket connect failed for: %s\n", spath);
    close(fd);
    return NULL;
  }
  while (1) {
    check_load();
    if (!(cmd = fetch()))
      break;
    process(cmd, fd);
    free(cmd);
  }
  close(fd);
  return NULL;
}


x86 assembly (clang, -O0):
<thread_main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    120b <thread_main+0x5b>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
lea    0xe06(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
lea    -0x28(%rbp),%rdi
mov    $0x8,%esi
call   10a0 <bzero@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x3,%edx
call   1040 <strncpy@plt>
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   10c0 <connect@plt>
cmp    $0x0,%rax
jge    1281 <thread_main+0xd1>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xdb9(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d1 <thread_main+0x121>
jmp    1286 <thread_main+0xd6>
mov    $0x0,%al
call   1050 <check_load@plt>
mov    $0x0,%al
call   1090 <fetch@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    12a7 <thread_main+0xf7>
jmp    12c1 <thread_main+0x111>
mov    -0x30(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1060 <process@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
jmp    1286 <thread_main+0xd6>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<thread_main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     1296 <thread_main+0xe6>
mov    %eax,%ebp
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x8,%esi
call   10a0 <bzero@plt>
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rsp),%edi
mov    %r14,%rsi
mov    $0x3,%edx
call   1040 <strncpy@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   10c0 <connect@plt>
test   %rax,%rax
js     126a <thread_main+0xba>
xor    %eax,%eax
call   1050 <check_load@plt>
xor    %eax,%eax
call   1090 <fetch@plt>
test   %rax,%rax
je     1284 <thread_main+0xd4>
mov    %rax,%rbx
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %ebp,%esi
call   1060 <process@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
call   1050 <check_load@plt>
xor    %eax,%eax
call   1090 <fetch@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1240 <thread_main+0x90>
jmp    1284 <thread_main+0xd4>
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
lea    0xda4(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
lea    0xd5a(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.protocol = type { i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [30 x i8] c"failed to create unix socket\0A\00"
@AF_UNIX = global i32 0
@.str.1 = [31 x i8] c"socket connect failed for: %s\0A\00"
define i8* @thread_main(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr_un
%7 = alloca %struct.protocol*
store i8* %0, i8** %3
%8 = load i8*, i8** %3
store i8* %8, i8** %4
%9 = load i32, i32* @PF_UNIX
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %1
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 2)
unreachable
18: ; preds = %1
%19 = call i32 @bzero(%struct.sockaddr_un* %6, i32 8)
%20 = load i32, i32* @AF_UNIX
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%23 = load i32, i32* %22
%24 = load i8*, i8** %4
%25 = call i32 @strncpy(i32 %23, i8* %24, i32 3)
%26 = load i32, i32* %5
%27 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%28 = call i64 @connect(i32 %26, %struct.sockaddr* %27, i32 8)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %36
30: ; preds = %18
%31 = load i32, i32* @stderr
%32 = load i8*, i8** %4
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %32)
%34 = load i32, i32* %5
%35 = call i32 @close(i32 %34)
store i8* null, i8** %2
br label %51
36: ; preds = %18
br label %37
37: ; preds = %36, %42
%38 = call i32 (...) @check_load()
%39 = call %struct.protocol* (...) @fetch()
store %struct.protocol* %39, %struct.protocol** %7
%40 = icmp ne %struct.protocol* %39, null
br i1 %40, label %42, label %41
41: ; preds = %37
br label %48
42: ; preds = %37
%43 = load %struct.protocol*, %struct.protocol** %7
%44 = load i32, i32* %5
%45 = call i32 @process(%struct.protocol* %43, i32 %44)
%46 = load %struct.protocol*, %struct.protocol** %7
%47 = call i32 @free(%struct.protocol* %46)
br label %37
48: ; preds = %41
%49 = load i32, i32* %5
%50 = call i32 @close(i32 %49)
store i8* null, i8** %2
br label %51
51: ; preds = %48, %30
%52 = load i8*, i8** %2
ret i8* %52
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
%struct.protocol = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [30 x i8] c"failed to create unix socket\0A\00"
@AF_UNIX = global i32 0
@.str.1 = [31 x i8] c"socket connect failed for: %s\0A\00"
define noalias i8* @thread_main(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = load i32, i32* @PF_UNIX
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 2)
unreachable
12: ; preds = %1
%13 = call i32 @bzero(%struct.sockaddr_un* %2, i32 8)
%14 = load i32, i32* @AF_UNIX
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%17 = load i32, i32* %16
%18 = tail call i32 @strncpy(i32 %17, i8* %0, i32 3)
%19 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%20 = call i64 @connect(i32 %6, %struct.sockaddr* %19, i32 8)
%21 = icmp slt i64 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %12
%23 = call i32 (...) @check_load()
%24 = call %struct.protocol* (...) @fetch()
%25 = icmp eq %struct.protocol* %24, null
br i1 %25, label %36, label %29
26: ; preds = %12
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %36
29: ; preds = %22, %29
%30 = phi %struct.protocol* [ %34, %29 ], [ %24, %22 ]
%31 = call i32 @process(%struct.protocol* %30, i32 %6)
%32 = call i32 @free(%struct.protocol* %30)
%33 = call i32 (...) @check_load()
%34 = call %struct.protocol* (...) @fetch()
%35 = icmp eq %struct.protocol* %34, null
br i1 %35, label %36, label %29
36: ; preds = %29, %22, %26
%37 = call i32 @close(i32 %6)
ret i8* null
}