#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 sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
int genericTcpAccept(char *, int, struct sockaddr *, int *);
scalar_t__ tcpNonBlock(char *, int);
int /*<<< orphan*/ writeLog(int, char *, char *);
int tcpAccept(char *err, int s, int *port) {
  int fd;
  struct sockaddr_in sa;
  socklen_t salen = sizeof(sa);
  if ((fd = genericTcpAccept(err, s, (struct sockaddr *)&sa, &salen)) == -1)
    return -1;
  if ((tcpNonBlock(err, fd)) < 0) {
    writeLog(1, "%s", "set socket nonBlock failed!");
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<tcpAccept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x4,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
lea    -0x28(%rbp),%rdx
lea    -0x2c(%rbp),%rcx
call   1030 <genericTcpAccept@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    1176 <tcpAccept+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b6 <tcpAccept+0x86>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1050 <tcpNonBlock@plt>
cmp    $0x0,%rax
jge    11b0 <tcpAccept+0x80>
mov    $0x1,%edi
lea    0xe68(%rip),%rsi
lea    0xe64(%rip),%rdx
call   1040 <writeLog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b6 <tcpAccept+0x86>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcpAccept>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
movl   $0x4,0xc(%rsp)
lea    0x10(%rsp),%rdx
lea    0xc(%rsp),%rcx
call   1030 <genericTcpAccept@plt>
mov    %eax,%ebp
mov    $0xffffffff,%eax
cmp    $0xffffffff,%ebp
je     118d <tcpAccept+0x5d>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1050 <tcpNonBlock@plt>
mov    %rax,%rcx
mov    %ebp,%eax
test   %rcx,%rcx
jns    118d <tcpAccept+0x5d>
lea    0xe89(%rip),%rsi
lea    0xe85(%rip),%rdx
mov    $0x1,%edi
call   1040 <writeLog@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [3 x i8] c"%s\00"
@.str.1 = [28 x i8] c"set socket nonBlock failed!\00"
define i32 @tcpAccept(i8* %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
store i32 4, i32* %10
%11 = load i8*, i8** %5
%12 = load i32, i32* %6
%13 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%14 = call i32 @genericTcpAccept(i8* %11, i32 %12, %struct.sockaddr* %13, i32* %10)
store i32 %14, i32* %8
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %3
store i32 -1, i32* %4
br label %26
17: ; preds = %3
%18 = load i8*, i8** %5
%19 = load i32, i32* %8
%20 = call i64 @tcpNonBlock(i8* %18, i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = call i32 @writeLog(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %26
24: ; preds = %17
%25 = load i32, i32* %8
store i32 %25, i32* %4
br label %26
26: ; preds = %24, %22, %16
%27 = load i32, i32* %4
ret i32 %27
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [3 x i8] c"%s\00"
@.str.1 = [28 x i8] c"set socket nonBlock failed!\00"
define i32 @tcpAccept(i8* %0, i32 %1, i32* nocapture readnone %2) {
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = bitcast i32* %5 to i8*
store i32 4, i32* %5
%8 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%9 = call i32 @genericTcpAccept(i8* %0, i32 %1, %struct.sockaddr* %8, i32* %5)
%10 = icmp eq i32 %9, -1
br i1 %10, label %16, label %11
11: ; preds = %3
%12 = call i64 @tcpNonBlock(i8* %0, i32 %9)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 @writeLog(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %11, %3, %14
%17 = phi i32 [ -1, %14 ], [ -1, %3 ], [ %9, %11 ]
ret i32 %17
}