#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 protocol {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ check_load();
int /*<<< orphan*/ close(int);
struct protocol *fetch();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(struct protocol *);
int make_connection(char const *);
int process(struct protocol *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
void *thread_main(void *arg) {
  const char *spath = (const char *)arg;
  int fd = make_connection(spath);
  if (fd < 0) {
    fprintf(stderr, "socket connect failed for: %s\n", spath);
    return NULL;
  }
  while (1) {
    struct protocol *cmd;
    check_load();
    if (!(cmd = fetch())) {
      break;
    }
    while (process(cmd, fd) < 1) {
      fprintf(stderr, "connection to renderd lost");
      close(fd);
      fd = -1;
      while (fd < 0) {
        fprintf(stderr, "sleeping for 30 seconds");
        sleep(30);
        fprintf(stderr, "attempting to reconnect");
        fd = make_connection(spath);
      }
    }
    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    -0x18(%rbp),%rdi
call   1090 <make_connection@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11d2 <thread_main+0x52>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe42(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ae <thread_main+0x12e>
jmp    11d7 <thread_main+0x57>
mov    $0x0,%al
call   1040 <check_load@plt>
mov    $0x0,%al
call   1080 <fetch@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11f8 <thread_main+0x78>
jmp    129e <thread_main+0x11e>
jmp    11fd <thread_main+0x7d>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1050 <process@plt>
cmp    $0x1,%eax
jge    1290 <thread_main+0x110>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
lea    0xdfd(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    128b <thread_main+0x10b>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
lea    0xde8(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1e,%edi
call   10a0 <sleep@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
lea    0xddf(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1090 <make_connection@plt>
mov    %eax,-0x1c(%rbp)
jmp    1238 <thread_main+0xb8>
jmp    11fd <thread_main+0x7d>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    11d7 <thread_main+0x57>
mov    -0x1c(%rbp),%edi
call   1060 <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   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1090 <make_connection@plt>
test   %eax,%eax
js     125d <thread_main+0xdd>
mov    %eax,%ebp
xor    %eax,%eax
call   1040 <check_load@plt>
xor    %eax,%eax
call   1080 <fetch@plt>
test   %rax,%rax
je     1254 <thread_main+0xd4>
mov    %rax,%r13
mov    0x2e22(%rip),%r14
lea    0xe75(%rip),%r15
lea    0xe86(%rip),%r12
jmp    11f0 <thread_main+0x70>
mov    %r13,%rdi
call   1030 <free@plt>
xor    %eax,%eax
call   1040 <check_load@plt>
xor    %eax,%eax
call   1080 <fetch@plt>
mov    %rax,%r13
test   %rax,%rax
je     1254 <thread_main+0xd4>
nopl   0x0(%rax)
mov    %r13,%rdi
mov    %ebp,%esi
call   1050 <process@plt>
test   %eax,%eax
jg     11ce <thread_main+0x4e>
mov    (%r14),%edi
lea    0xe17(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
cs nopw 0x0(%rax,%rax,1)
mov    (%r14),%edi
mov    %r15,%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1e,%edi
call   10a0 <sleep@plt>
mov    (%r14),%edi
mov    %r12,%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1090 <make_connection@plt>
test   %eax,%eax
js     1220 <thread_main+0xa0>
mov    %eax,%ebp
jmp    11f0 <thread_main+0x70>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    1277 <thread_main+0xf7>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
lea    0xd93(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.protocol = type { i32 }
@stderr = global i32 0
@.str = [31 x i8] c"socket connect failed for: %s\0A\00"
@.str.1 = [27 x i8] c"connection to renderd lost\00"
@.str.2 = [24 x i8] c"sleeping for 30 seconds\00"
@.str.3 = [24 x i8] c"attempting to reconnect\00"
define noalias i8* @thread_main(i8* %0) {
%2 = tail call i32 @make_connection(i8* %0)
%3 = icmp slt i32 %2, 0
br i1 %3, label %8, label %4
4: ; preds = %1
%5 = tail call i32 (...) @check_load()
%6 = tail call %struct.protocol* (...) @fetch()
%7 = icmp eq %struct.protocol* %6, null
br i1 %7, label %38, label %11
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %41
11: ; preds = %4, %32
%12 = phi %struct.protocol* [ %36, %32 ], [ %6, %4 ]
%13 = phi i32 [ %33, %32 ], [ %2, %4 ]
%14 = tail call i32 @process(%struct.protocol* %12, i32 %13)
%15 = icmp slt i32 %14, 1
br i1 %15, label %19, label %32
16: ; preds = %24
%17 = tail call i32 @process(%struct.protocol* %12, i32 %30)
%18 = icmp slt i32 %17, 1
br i1 %18, label %19, label %32
19: ; preds = %11, %16
%20 = phi i32 [ %30, %16 ], [ %13, %11 ]
%21 = load i32, i32* @stderr
%22 = tail call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%23 = tail call i32 @close(i32 %20)
br label %24
24: ; preds = %19, %24
%25 = load i32, i32* @stderr
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%27 = tail call i32 @sleep(i32 30)
%28 = load i32, i32* @stderr
%29 = tail call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%30 = tail call i32 @make_connection(i8* %0)
%31 = icmp slt i32 %30, 0
br i1 %31, label %24, label %16
32: ; preds = %16, %11
%33 = phi i32 [ %13, %11 ], [ %30, %16 ]
%34 = tail call i32 @free(%struct.protocol* %12)
%35 = tail call i32 (...) @check_load()
%36 = tail call %struct.protocol* (...) @fetch()
%37 = icmp eq %struct.protocol* %36, null
br i1 %37, label %38, label %11
38: ; preds = %32, %4
%39 = phi i32 [ %2, %4 ], [ %33, %32 ]
%40 = tail call i32 @close(i32 %39)
br label %41
41: ; preds = %38, %8
ret i8* null
}