#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;
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ log1(char *);
int /*<<< orphan*/ perror(char *);
scalar_t__ recv(int, char *, int, int /*<<< orphan*/);
scalar_t__ send_string(int, char *);
scalar_t__ spawn(int, char **);
scalar_t__ strncmp(char *, char *, int);
char run_connection(int fd, char *params[]) {
  assert(fd != 0);
  assert(params != 0);
  assert(*params != 0);
  for (;;) {
    char buffer[10];
    ssize_t received = recv(fd, buffer, 10, 0);
    if (received < 0) {
      perror("recv");
      return 0;
    }
    if (received == 0)
      return 1;
    if (strncmp(buffer, "SPAWN\n", 6) == 0) {
      pid_t spawned = spawn(fd, params);
      if (spawned != 0) {
        log1("Sending NOK");
        if (send_string(fd, "NOK\n") < 0) {
          perror("send");
          return 0;
        }
      }
    } else {
      log1("Sending NACK!");
      if (send_string(fd, "NACK!\n") < 0) {
        perror("send");
        return 0;
      }
    }
  }
}


x86 assembly (clang, -O0):
<run_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x8(%rbp),%edi
lea    -0x1a(%rbp),%rsi
mov    $0xa,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    11ee <run_connection+0x7e>
lea    0xe20(%rip),%rdi
call   1090 <perror@plt>
movb   $0x0,-0x1(%rbp)
jmp    12c4 <run_connection+0x154>
cmpq   $0x0,-0x28(%rbp)
jne    1202 <run_connection+0x92>
movb   $0x1,-0x1(%rbp)
jmp    12c4 <run_connection+0x154>
lea    -0x1a(%rbp),%rdi
lea    0xdf8(%rip),%rsi
mov    $0x6,%edx
call   1050 <strncmp@plt>
cmp    $0x0,%rax
jne    1280 <run_connection+0x110>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1080 <spawn@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     127b <run_connection+0x10b>
lea    0xdc9(%rip),%rdi
call   1070 <log1@plt>
mov    -0x8(%rbp),%edi
lea    0xdc6(%rip),%rsi
call   1040 <send_string@plt>
cmp    $0x0,%rax
jge    1276 <run_connection+0x106>
lea    0xdb5(%rip),%rdi
call   1090 <perror@plt>
movb   $0x0,-0x1(%rbp)
jmp    12c4 <run_connection+0x154>
jmp    127b <run_connection+0x10b>
jmp    12bf <run_connection+0x14f>
lea    0xd9b(%rip),%rdi
call   1070 <log1@plt>
mov    -0x8(%rbp),%edi
lea    0xd9a(%rip),%rsi
call   1040 <send_string@plt>
cmp    $0x0,%rax
jge    12ba <run_connection+0x14a>
lea    0xd71(%rip),%rdi
call   1090 <perror@plt>
movb   $0x0,-0x1(%rbp)
jmp    12c4 <run_connection+0x154>
jmp    12bf <run_connection+0x14f>
jmp    11b7 <run_connection+0x47>
movsbl -0x1(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_connection>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    %edi,%ebp
xor    %edi,%edi
test   %ebp,%ebp
setne  %dil
call   1060 <assert@plt>
xor    %edi,%edi
test   %r15,%r15
setne  %dil
call   1060 <assert@plt>
xor    %edi,%edi
cmpq   $0x0,(%r15)
setne  %dil
call   1060 <assert@plt>
lea    0xe(%rsp),%rsi
mov    %ebp,%edi
mov    $0xa,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
lea    0xe39(%rip),%rdi
test   %rax,%rax
js     127e <run_connection+0x10e>
lea    0xe2e(%rip),%r12
lea    0xe(%rsp),%rbx
lea    0xe3f(%rip),%r13
lea    0xe46(%rip),%r14
jmp    121d <run_connection+0xad>
nopl   0x0(%rax)
mov    %r13,%rdi
call   1070 <log1@plt>
mov    %ebp,%edi
mov    %r14,%rsi
call   1040 <send_string@plt>
test   %rax,%rax
js     1277 <run_connection+0x107>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xa,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
js     126e <run_connection+0xfe>
test   %rax,%rax
je     125d <run_connection+0xed>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x6,%edx
call   1050 <strncmp@plt>
test   %rax,%rax
jne    11f0 <run_connection+0x80>
mov    %ebp,%edi
mov    %r15,%rsi
call   1080 <spawn@plt>
test   %rax,%rax
je     1207 <run_connection+0x97>
lea    0xdbf(%rip),%rdi
call   1070 <log1@plt>
mov    %ebp,%edi
lea    0xdbd(%rip),%rsi
jmp    11fd <run_connection+0x8d>
mov    $0x1,%al
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd8b(%rip),%rdi
jmp    127e <run_connection+0x10e>
lea    0xd9f(%rip),%rdi
call   1090 <perror@plt>
xor    %eax,%eax
jmp    125f <run_connection+0xef>

LLVM IR (-O0):
@.str = [5 x i8] c"recv\00"
@.str.1 = [7 x i8] c"SPAWN\0A\00"
@.str.2 = [12 x i8] c"Sending NOK\00"
@.str.3 = [5 x i8] c"NOK\0A\00"
@.str.4 = [5 x i8] c"send\00"
@.str.5 = [14 x i8] c"Sending NACK!\00"
@.str.6 = [7 x i8] c"NACK!\0A\00"
define signext i8 @run_connection(i32 %0, i8** %1) {
%3 = alloca i8
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [10 x i8]
%7 = alloca i64
%8 = alloca i64
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 0
%11 = zext i1 %10 to i32
%12 = call i32 @assert(i32 %11)
%13 = load i8**, i8*** %5
%14 = icmp ne i8** %13, null
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
%17 = load i8**, i8*** %5
%18 = load i8*, i8** %17
%19 = icmp ne i8* %18, null
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
br label %22
22: ; preds = %61, %2
%23 = load i32, i32* %4
%24 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%25 = call i64 @recv(i32 %23, i8* %24, i32 10, i32 0)
store i64 %25, i64* %7
%26 = load i64, i64* %7
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8 0, i8* %3
br label %62
30: ; preds = %22
%31 = load i64, i64* %7
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %30
store i8 1, i8* %3
br label %62
34: ; preds = %30
%35 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%36 = call i64 @strncmp(i8* %35, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 6)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %53
38: ; preds = %34
%39 = load i32, i32* %4
%40 = load i8**, i8*** %5
%41 = call i64 @spawn(i32 %39, i8** %40)
store i64 %41, i64* %8
%42 = load i64, i64* %8
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %52
44: ; preds = %38
%45 = call i32 @log1(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%46 = load i32, i32* %4
%47 = call i64 @send_string(i32 %46, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %44
%50 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
store i8 0, i8* %3
br label %62
51: ; preds = %44
br label %52
52: ; preds = %51, %38
br label %61
53: ; preds = %34
%54 = call i32 @log1(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%55 = load i32, i32* %4
%56 = call i64 @send_string(i32 %55, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %53
%59 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
store i8 0, i8* %3
br label %62
60: ; preds = %53
br label %61
61: ; preds = %60, %52
br label %22
62: ; preds = %58, %49, %33, %28
%63 = load i8, i8* %3
ret i8 %63
}

LLVM IR (-O2):
@.str = [5 x i8] c"recv\00"
@.str.1 = [7 x i8] c"SPAWN\0A\00"
@.str.2 = [12 x i8] c"Sending NOK\00"
@.str.3 = [5 x i8] c"NOK\0A\00"
@.str.4 = [5 x i8] c"send\00"
@.str.5 = [14 x i8] c"Sending NACK!\00"
@.str.6 = [7 x i8] c"NACK!\0A\00"
define signext i8 @run_connection(i32 %0, i8** %1) {
%3 = alloca [10 x i8]
%4 = icmp ne i32 %0, 0
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = icmp ne i8** %1, null
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = load i8*, i8** %1
%11 = icmp ne i8* %10, null
%12 = zext i1 %11 to i32
%13 = tail call i32 @assert(i32 %12)
%14 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%15 = call i64 @recv(i32 %0, i8* %14, i32 10, i32 0)
%16 = icmp slt i64 %15, 0
br i1 %16, label %37, label %17
17: ; preds = %2, %34
%18 = phi i64 [ %35, %34 ], [ %15, %2 ]
%19 = icmp eq i64 %18, 0
br i1 %19, label %40, label %20
20: ; preds = %17
%21 = call i64 @strncmp(i8* %14, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 6)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %30
23: ; preds = %20
%24 = call i64 @spawn(i32 %0, i8** %1)
%25 = icmp eq i64 %24, 0
br i1 %25, label %34, label %26
26: ; preds = %23
%27 = call i32 @log1(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%28 = call i64 @send_string(i32 %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%29 = icmp slt i64 %28, 0
br i1 %29, label %37, label %34
30: ; preds = %20
%31 = call i32 @log1(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%32 = call i64 @send_string(i32 %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%33 = icmp slt i64 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %23, %26, %30
%35 = call i64 @recv(i32 %0, i8* %14, i32 10, i32 0)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %17
37: ; preds = %30, %26, %34, %2
%38 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %34 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), %26 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), %30 ]
%39 = call i32 @perror(i8* %38)
br label %40
40: ; preds = %17, %37
%41 = phi i8 [ 0, %37 ], [ 1, %17 ]
ret i8 %41
}