#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__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ execlp(char *, char *, char *, char *, char *,
                          int /*<<< orphan*/ *);
scalar_t__ fork();
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ wait(int /*<<< orphan*/);
int syn_ack(char *arguments, int syn, int fd) {
  pid_t child_pid;
  int static port;
  child_pid = fork();
  if (child_pid != 0) {
    close(1);
    wait(0);
    return 1;
  } else {
    close(1);
    dup(fd);
    if (arguments == "hearts")
      if (!syn)
        arguments = "diamonds";
      else if (arguments == "port")
        if (syn == 1)
          return 0;
        else
          arguments = "it's the ping of death for you my friend!";
    execlp("/bin/sh", "bash", "-c", "echo", arguments, NULL);
    syslog(LOG_ERR, "%s", strerror(errno));
    abort();
  }
  return 1;
}


x86 assembly (clang, -O0):
<syn_ack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11c5 <syn_ack+0x45>
mov    $0x1,%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1090 <wait@plt>
movl   $0x1,-0x4(%rbp)
jmp    129f <syn_ack+0x11f>
mov    $0x1,%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
call   1040 <dup@plt>
lea    0xe22(%rip),%rax
cmp    %rax,-0x10(%rbp)
jne    1243 <syn_ack+0xc3>
cmpl   $0x0,-0x14(%rbp)
jne    1202 <syn_ack+0x82>
lea    0xe0e(%rip),%rax
mov    %rax,-0x10(%rbp)
jmp    123e <syn_ack+0xbe>
lea    0xe07(%rip),%rax
cmp    %rax,-0x10(%rbp)
jne    1239 <syn_ack+0xb9>
cmpl   $0x1,-0x14(%rbp)
jne    1229 <syn_ack+0xa9>
movl   $0x0,-0x4(%rbp)
jmp    129f <syn_ack+0x11f>
lea    0xde5(%rip),%rax
mov    %rax,-0x10(%rbp)
jmp    1239 <syn_ack+0xb9>
jmp    123e <syn_ack+0xbe>
jmp    1243 <syn_ack+0xc3>
mov    -0x10(%rbp),%r8
lea    0xdf1(%rip),%rdi
lea    0xdf2(%rip),%rsi
lea    0xdf0(%rip),%rdx
lea    0xdec(%rip),%rcx
xor    %eax,%eax
mov    %eax,%r9d
call   1070 <execlp@plt>
mov    0x2d5c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %eax,%edx
lea    0xdc1(%rip),%rsi
call   1060 <syslog@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<syn_ack>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r15d
mov    %esi,%r14d
mov    %rdi,%r12
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %rax,%rbx
mov    $0x1,%ebp
mov    $0x1,%edi
call   1050 <close@plt>
test   %rbx,%rbx
je     11b8 <syn_ack+0x38>
xor    %edi,%edi
call   1090 <wait@plt>
jmp    11f0 <syn_ack+0x70>
mov    %r15d,%edi
call   1040 <dup@plt>
lea    0xe39(%rip),%rax
cmp    %rax,%r12
jne    1202 <syn_ack+0x82>
lea    0xe34(%rip),%r12
test   %r14d,%r14d
cmovne %rax,%r12
je     1202 <syn_ack+0x82>
lea    0xe2d(%rip),%rcx
cmp    %rcx,%rax
jne    1202 <syn_ack+0x82>
xor    %ebp,%ebp
cmp    $0x1,%r14d
jne    11fb <syn_ack+0x7b>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe13(%rip),%r12
lea    0xe36(%rip),%rdi
lea    0xe37(%rip),%rsi
lea    0xe35(%rip),%rdx
lea    0xe31(%rip),%rcx
mov    %r12,%r8
xor    %r9d,%r9d
call   1070 <execlp@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%ebx
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe0d(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   1060 <syslog@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@syn_ack.port = internal global i32 0
@.str = [7 x i8] c"hearts\00"
@.str.1 = [9 x i8] c"diamonds\00"
@.str.2 = [5 x i8] c"port\00"
@.str.3 = [42 x i8] c"it's the ping of death for you my friend!\00"
@.str.4 = [8 x i8] c"/bin/sh\00"
@.str.5 = [5 x i8] c"bash\00"
@.str.6 = [3 x i8] c"-c\00"
@.str.7 = [5 x i8] c"echo\00"
@LOG_ERR = global i32 0
@.str.8 = [3 x i8] c"%s\00"
@errno = global i32 0
define i32 @syn_ack(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%9 = call i64 (...) @fork()
store i64 %9, i64* %8
%10 = load i64, i64* %8
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = call i32 @close(i32 1)
%14 = call i32 @wait(i32 0)
store i32 1, i32* %4
br label %44
15: ; preds = %3
%16 = call i32 @close(i32 1)
%17 = load i32, i32* %7
%18 = call i32 @dup(i32 %17)
%19 = load i8*, i8** %5
%20 = icmp eq i8* %19, getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0)
br i1 %20, label %21, label %36
21: ; preds = %15
%22 = load i32, i32* %6
%23 = icmp ne i32 %22, 0
br i1 %23, label %25, label %24
24: ; preds = %21
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8** %5
br label %35
25: ; preds = %21
%26 = load i8*, i8** %5
%27 = icmp eq i8* %26, getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0)
br i1 %27, label %28, label %34
28: ; preds = %25
%29 = load i32, i32* %6
%30 = icmp eq i32 %29, 1
br i1 %30, label %31, label %32
31: ; preds = %28
store i32 0, i32* %4
br label %44
32: ; preds = %28
store i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i8** %5
br label %33
33: ; preds = %32
br label %34
34: ; preds = %33, %25
br label %35
35: ; preds = %34, %24
br label %36
36: ; preds = %35, %15
%37 = load i8*, i8** %5
%38 = call i32 @execlp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), i8* %37, i32* null)
%39 = load i32, i32* @LOG_ERR
%40 = load i32, i32* @errno
%41 = call i32 @strerror(i32 %40)
%42 = call i32 @syslog(i32 %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i32 %41)
%43 = call i32 (...) @abort()
unreachable
44: ; preds = %31, %12
%45 = load i32, i32* %4
ret i32 %45
}

LLVM IR (-O2):
@.str = [7 x i8] c"hearts\00"
@.str.1 = [9 x i8] c"diamonds\00"
@.str.2 = [5 x i8] c"port\00"
@.str.3 = [42 x i8] c"it's the ping of death for you my friend!\00"
@.str.4 = [8 x i8] c"/bin/sh\00"
@.str.5 = [5 x i8] c"bash\00"
@.str.6 = [3 x i8] c"-c\00"
@.str.7 = [5 x i8] c"echo\00"
@LOG_ERR = global i32 0
@.str.8 = [3 x i8] c"%s\00"
@errno = global i32 0
define i32 @syn_ack(i8* %0, i32 %1, i32 %2) {
%4 = tail call i64 (...) @fork()
%5 = icmp eq i64 %4, 0
%6 = tail call i32 @close(i32 1)
br i1 %5, label %9, label %7
7: ; preds = %3
%8 = tail call i32 @wait(i32 0)
br label %26
9: ; preds = %3
%10 = tail call i32 @dup(i32 %2)
%11 = icmp eq i8* %0, getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0)
br i1 %11, label %12, label %18
12: ; preds = %9
%13 = icmp eq i32 %1, 0
%14 = or i1 %13, icmp ne (i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%15 = select i1 %13, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0)
br i1 %14, label %18, label %16
16: ; preds = %12
%17 = icmp eq i32 %1, 1
br i1 %17, label %26, label %18
18: ; preds = %16, %12, %9
%19 = phi i8* [ %0, %9 ], [ %15, %12 ], [ getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), %16 ]
%20 = tail call i32 @execlp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), i8* %19, i32* null)
%21 = load i32, i32* @LOG_ERR
%22 = load i32, i32* @errno
%23 = tail call i32 @strerror(i32 %22)
%24 = tail call i32 @syslog(i32 %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i32 %23)
%25 = tail call i32 (...) @abort()
unreachable
26: ; preds = %16, %7
%27 = phi i32 [ 1, %7 ], [ 0, %16 ]
ret i32 %27
}