#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 int /*<<< orphan*/ timestr;
typedef int /*<<< orphan*/ time_t;
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ close(int);
char *ctime(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int /*<<< orphan*/, int /*<<< orphan*/, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int snprintf(char *, int, char *, char *);
int /*<<< orphan*/ time(int /*<<< orphan*/);
int write(int, char *, int);
int connect_process(int listenfd, int connfd) {
  pid_t pid;
  time_t tval;
  char timestr[32];
  int wlen = 0;
  pid = fork();
  if (pid < 0) {
    error(0, errno, "fork");
    return -1;
  } else if (pid == 0) {
    close(listenfd);
    tval = time(0);
    wlen = snprintf(timestr, sizeof(timestr), "%.24s\r\n", ctime(&tval));
    if (write(connfd, timestr, wlen) != wlen)
      error(0, errno, "write");
    close(connfd);
    exit(0);
  }
  close(connfd);
  return 0;
}


x86 assembly (clang, -O0):
<connect_process>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x44(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11ce <connect_process+0x4e>
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    0xe43(%rip),%rdx
call   1080 <error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1269 <connect_process+0xe9>
cmpq   $0x0,-0x18(%rbp)
jne    1255 <connect_process+0xd5>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    -0x1c(%rbp),%rdi
call   1040 <ctime@plt>
mov    -0x50(%rbp),%rdi
mov    %rax,%rcx
mov    $0x20,%esi
lea    0xdf6(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x44(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    -0x44(%rbp),%edx
call   1030 <write@plt>
cmp    -0x44(%rbp),%eax
je     1246 <connect_process+0xc6>
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    0xdcc(%rip),%rdx
call   1080 <error@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   1090 <exit@plt>
jmp    125a <connect_process+0xda>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_process>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     11a5 <connect_process+0x25>
je     11ca <connect_process+0x4a>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
jmp    11c1 <connect_process+0x41>
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
lea    0xe4b(%rip),%rdx
xor    %edi,%edi
call   1080 <error@plt>
mov    $0xffffffff,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %ebp,%edi
call   1060 <close@plt>
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,0xc(%rsp)
lea    0xc(%rsp),%rdi
call   1040 <ctime@plt>
lea    0xe18(%rip),%rdx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x20,%esi
mov    %rax,%rcx
call   1050 <snprintf@plt>
mov    %eax,%ebp
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
je     122b <connect_process+0xab>
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
lea    0xde9(%rip),%rdx
xor    %edi,%edi
call   1080 <error@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@errno = global i32 0
@.str = [5 x i8] c"fork\00"
@.str.1 = [8 x i8] c"%.24s\0D\0A\00"
@.str.2 = [6 x i8] c"write\00"
define i32 @connect_process(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
%8 = alloca [32 x i8]
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 0, i32* %9
%10 = call i64 (...) @fork()
store i64 %10, i64* %6
%11 = load i64, i64* %6
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @errno
%15 = call i32 @error(i32 0, i32 %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %43
16: ; preds = %2
%17 = load i64, i64* %6
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %39
19: ; preds = %16
%20 = load i32, i32* %4
%21 = call i32 @close(i32 %20)
%22 = call i32 @time(i32 0)
store i32 %22, i32* %7
%23 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%24 = call i8* @ctime(i32* %7)
%25 = call i32 @snprintf(i8* %23, i32 32, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %24)
store i32 %25, i32* %9
%26 = load i32, i32* %5
%27 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%28 = load i32, i32* %9
%29 = call i32 @write(i32 %26, i8* %27, i32 %28)
%30 = load i32, i32* %9
%31 = icmp ne i32 %29, %30
br i1 %31, label %32, label %35
32: ; preds = %19
%33 = load i32, i32* @errno
%34 = call i32 @error(i32 0, i32 %33, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %32, %19
%36 = load i32, i32* %5
%37 = call i32 @close(i32 %36)
%38 = call i32 @exit(i32 0)
unreachable
39: ; preds = %16
br label %40
40: ; preds = %39
%41 = load i32, i32* %5
%42 = call i32 @close(i32 %41)
store i32 0, i32* %3
br label %43
43: ; preds = %40, %13
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
@errno = global i32 0
@.str = [5 x i8] c"fork\00"
@.str.1 = [8 x i8] c"%.24s\0D\0A\00"
@.str.2 = [6 x i8] c"write\00"
define i32 @connect_process(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca [32 x i8]
%5 = bitcast i32* %3 to i8*
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%7 = tail call i64 (...) @fork()
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* @errno
%11 = tail call i32 @error(i32 0, i32 %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %29
12: ; preds = %2
%13 = icmp eq i64 %7, 0
br i1 %13, label %14, label %27
14: ; preds = %12
%15 = tail call i32 @close(i32 %0)
%16 = tail call i32 @time(i32 0)
store i32 %16, i32* %3
%17 = call i8* @ctime(i32* %3)
%18 = call i32 @snprintf(i8* %6, i32 32, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = call i32 @write(i32 %1, i8* %6, i32 %18)
%20 = icmp eq i32 %19, %18
br i1 %20, label %24, label %21
21: ; preds = %14
%22 = load i32, i32* @errno
%23 = call i32 @error(i32 0, i32 %22, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %21, %14
%25 = call i32 @close(i32 %1)
%26 = call i32 @exit(i32 0)
unreachable
27: ; preds = %12
%28 = tail call i32 @close(i32 %1)
br label %29
29: ; preds = %27, %9
%30 = phi i32 [ -1, %9 ], [ 0, %27 ]
ret i32 %30
}