#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 */

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ pthread_exit(void *);
int read(int, char *, int);
int /*<<< orphan*/ reset_pin(int);
scalar_t__ strncmp(char *, char *, int);
char *strtok(char *, char *);
long strtol(char *, char **, int);
void *connection_handler(void *param) {
  int connection_fd = *(int *)param;
  char buffer[256];
  int nbytes;
  char *token;
  void *thread_status = (void *)0;
  while ((nbytes = read(connection_fd, buffer, 256)) != -1) {
    buffer[nbytes] = 0;
    token = strtok(buffer, " ");
    if (token == NULL) {
      thread_status = (void *)1;
      break;
    }
    if (strncmp(token, "exit", 4) == 0) {
      break;
    }
    if (strncmp(token, "reset", 5) == 0) {
      long value = strtol(strtok(NULL, " "), (char **)NULL, 10);
      reset_pin((int)value);
      continue;
    }
  }
  close(connection_fd);
  pthread_exit(thread_status);
}


x86 assembly (clang, -O0):
<connection_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
movq   $0x0,-0x138(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
call   1060 <read@plt>
mov    %eax,-0x124(%rbp)
cmp    $0xffffffff,%eax
je     1287 <connection_handler+0x117>
movslq -0x124(%rbp),%rax
movb   $0x0,-0x120(%rbp,%rax,1)
lea    -0x120(%rbp),%rdi
lea    0xe2d(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x130(%rbp)
cmpq   $0x0,-0x130(%rbp)
jne    11fe <connection_handler+0x8e>
mov    $0x1,%eax
mov    %rax,-0x138(%rbp)
jmp    1287 <connection_handler+0x117>
mov    -0x130(%rbp),%rdi
lea    0xdf6(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1225 <connection_handler+0xb5>
jmp    1287 <connection_handler+0x117>
mov    -0x130(%rbp),%rdi
lea    0xdd4(%rip),%rsi
mov    $0x5,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1282 <connection_handler+0x112>
xor    %eax,%eax
mov    %eax,%edi
lea    0xdae(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    %rax,-0x140(%rbp)
mov    -0x140(%rbp),%rax
mov    %eax,%edi
call   1050 <reset_pin@plt>
jmp    1193 <connection_handler+0x23>
jmp    1193 <connection_handler+0x23>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x138(%rbp),%rdi
call   1080 <pthread_exit@plt>
mov    -0x8(%rbp),%rax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connection_handler>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    (%rdi),%ebx
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x100,%edx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
je     1239 <connection_handler+0xc9>
lea    0xe5e(%rip),%r15
mov    %rsp,%r12
lea    0xe56(%rip),%r13
lea    0xe54(%rip),%rbp
jmp    11d4 <connection_handler+0x64>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x100,%edx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
je     1239 <connection_handler+0xc9>
cltq
movb   $0x0,(%rsp,%rax,1)
mov    %r12,%rdi
mov    %r15,%rsi
call   1090 <strtok@plt>
test   %rax,%rax
je     123d <connection_handler+0xcd>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r13,%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     1239 <connection_handler+0xc9>
mov    %r14,%rdi
mov    %rbp,%rsi
mov    $0x5,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11c0 <connection_handler+0x50>
xor    %edi,%edi
mov    %r15,%rsi
call   1090 <strtok@plt>
mov    %rax,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    %eax,%edi
call   1050 <reset_pin@plt>
jmp    11c0 <connection_handler+0x50>
xor    %ebp,%ebp
jmp    1242 <connection_handler+0xd2>
mov    $0x1,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
mov    %rbp,%rdi
call   1080 <pthread_exit@plt>
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c" \00"
@.str.1 = [5 x i8] c"exit\00"
@.str.2 = [6 x i8] c"reset\00"
define i8* @connection_handler(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [256 x i8]
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
store i8* %0, i8** %3
%10 = load i8*, i8** %3
%11 = bitcast i8* %10 to i32*
%12 = load i32, i32* %11
store i32 %12, i32* %4
store i8* null, i8** %8
br label %13
13: ; preds = %42, %36, %1
%14 = load i32, i32* %4
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%16 = call i32 @read(i32 %14, i8* %15, i32 256)
store i32 %16, i32* %6
%17 = icmp ne i32 %16, -1
br i1 %17, label %18, label %43
18: ; preds = %13
%19 = load i32, i32* %6
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 %20
store i8 0, i8* %21
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%23 = call i8* @strtok(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %23, i8** %7
%24 = load i8*, i8** %7
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %27
26: ; preds = %18
store i8* inttoptr (i64 1 to i8*), i8** %8
br label %43
27: ; preds = %18
%28 = load i8*, i8** %7
%29 = call i64 @strncmp(i8* %28, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %27
br label %43
32: ; preds = %27
%33 = load i8*, i8** %7
%34 = call i64 @strncmp(i8* %33, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 5)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %42
36: ; preds = %32
%37 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%38 = call i64 @strtol(i8* %37, i8** null, i32 10)
store i64 %38, i64* %9
%39 = load i64, i64* %9
%40 = trunc i64 %39 to i32
%41 = call i32 @reset_pin(i32 %40)
br label %13
42: ; preds = %32
br label %13
43: ; preds = %31, %26, %13
%44 = load i32, i32* %4
%45 = call i32 @close(i32 %44)
%46 = load i8*, i8** %8
%47 = call i32 @pthread_exit(i8* %46)
%48 = load i8*, i8** %2
ret i8* %48
}

LLVM IR (-O2):
@.str = [2 x i8] c" \00"
@.str.1 = [5 x i8] c"exit\00"
@.str.2 = [6 x i8] c"reset\00"
define noalias i8* @connection_handler(i8* nocapture %0) {
%2 = alloca [256 x i8]
%3 = bitcast i8* %0 to i32*
%4 = load i32, i32* %3
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%6 = call i32 @read(i32 %4, i8* %5, i32 256)
%7 = icmp eq i32 %6, -1
br i1 %7, label %28, label %8
8: ; preds = %1, %25
%9 = phi i32 [ %26, %25 ], [ %6, %1 ]
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %10
store i8 0, i8* %11
%12 = call i8* @strtok(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i8* %12, null
br i1 %13, label %28, label %14
14: ; preds = %8
%15 = call i64 @strncmp(i8* %12, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%16 = icmp eq i64 %15, 0
br i1 %16, label %28, label %17
17: ; preds = %14
%18 = call i64 @strncmp(i8* %12, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 5)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %17
%21 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%22 = call i64 @strtol(i8* nocapture %21, i8** null, i32 10)
%23 = trunc i64 %22 to i32
%24 = call i32 @reset_pin(i32 %23)
br label %25
25: ; preds = %20, %17
%26 = call i32 @read(i32 %4, i8* %5, i32 256)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %8
28: ; preds = %25, %14, %8, %1
%29 = phi i8* [ null, %1 ], [ inttoptr (i64 1 to i8*), %8 ], [ null, %14 ], [ null, %25 ]
%30 = call i32 @close(i32 %4)
%31 = call i32 @pthread_exit(i8* %29)
ret i8* undef
}