#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;
};

/* Variables and functions */
int /*<<< orphan*/ close(int);
int errno;
int listen(int, int);
int tcp_sock_bind(int *, struct sockaddr_in *, int, int);
int tcp_sock_listen(int *srv_sd, struct sockaddr_in *sin, int qlen,
                    int nonblock, int tuning) {
  int ret, sd;
  ret = tcp_sock_bind(&sd, sin, nonblock, tuning);
  if (ret)
    goto err_ret;
  ret = listen(sd, qlen);
  if (ret == -1) {
    ret = errno;
    goto err_sd;
  }
  *srv_sd = sd;
  return 0;
err_sd:
  close(sd);
err_ret:
  return ret;
}


x86 assembly (clang, -O0):
<tcp_sock_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%edx
mov    -0x24(%rbp),%ecx
lea    -0x2c(%rbp),%rdi
call   1030 <tcp_sock_bind@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     116f <tcp_sock_listen+0x3f>
jmp    11b5 <tcp_sock_listen+0x85>
mov    -0x2c(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1050 <listen@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    1198 <tcp_sock_listen+0x68>
mov    0x2e52(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
jmp    11ad <tcp_sock_listen+0x7d>
mov    -0x2c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11bb <tcp_sock_listen+0x8b>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_sock_listen>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebp
mov    %rdi,%r14
lea    0xc(%rsp),%rdi
mov    %ecx,%edx
mov    %r8d,%ecx
call   1030 <tcp_sock_bind@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    117f <tcp_sock_listen+0x4f>
mov    0xc(%rsp),%edi
mov    %ebp,%esi
call   1050 <listen@plt>
cmp    $0xffffffff,%eax
je     116d <tcp_sock_listen+0x3d>
mov    0xc(%rsp),%eax
mov    %eax,(%r14)
xor    %ebx,%ebx
jmp    117f <tcp_sock_listen+0x4f>
mov    0x2e6c(%rip),%rax
mov    (%rax),%ebx
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
@errno = global i32 0
define i32 @tcp_sock_listen(i32* %0, %struct.sockaddr_in* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca %struct.sockaddr_in*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32* %0, i32** %7
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%14 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%15 = load i32, i32* %10
%16 = load i32, i32* %11
%17 = call i32 @tcp_sock_bind(i32* %13, %struct.sockaddr_in* %14, i32 %15, i32 %16)
store i32 %17, i32* %12
%18 = load i32, i32* %12
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %5
br label %35
21: ; preds = %5
%22 = load i32, i32* %13
%23 = load i32, i32* %9
%24 = call i32 @listen(i32 %22, i32 %23)
store i32 %24, i32* %12
%25 = load i32, i32* %12
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = load i32, i32* @errno
store i32 %28, i32* %12
br label %32
29: ; preds = %21
%30 = load i32, i32* %13
%31 = load i32*, i32** %7
store i32 %30, i32* %31
store i32 0, i32* %6
br label %37
32: ; preds = %27
%33 = load i32, i32* %13
%34 = call i32 @close(i32 %33)
br label %35
35: ; preds = %32, %20
%36 = load i32, i32* %12
store i32 %36, i32* %6
br label %37
37: ; preds = %35, %29
%38 = load i32, i32* %6
ret i32 %38
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
@errno = global i32 0
define i32 @tcp_sock_listen(i32* nocapture writeonly %0, %struct.sockaddr_in* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = bitcast i32* %6 to i8*
%8 = call i32 @tcp_sock_bind(i32* %6, %struct.sockaddr_in* %1, i32 %3, i32 %4)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %20
10: ; preds = %5
%11 = load i32, i32* %6
%12 = call i32 @listen(i32 %11, i32 %2)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = load i32, i32* @errno
%16 = load i32, i32* %6
%17 = call i32 @close(i32 %16)
br label %20
18: ; preds = %10
%19 = load i32, i32* %6
store i32 %19, i32* %0
br label %20
20: ; preds = %14, %5, %18
%21 = phi i32 [ 0, %18 ], [ %8, %5 ], [ %15, %14 ]
ret i32 %21
}