#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  unsigned int s_addr;
};
struct sockaddr_in {
  scalar_t__ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int);
int pptp_gre_bind(unsigned int ip) {
  struct sockaddr_in addr;
  int s;
  s = socket(AF_INET, SOCK_RAW, 47);
  if (s < 0)
    return -1;
  memset(&addr, 0, sizeof addr);
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = ip;
  addr.sin_port = 0;
  if (connect(s, (struct sockaddr *)&addr, sizeof addr) < 0) {
    close(s);
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<pptp_gre_bind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e96(%rip),%rax
mov    (%rax),%edi
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
mov    $0x2f,%edx
call   1060 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1180 <pptp_gre_bind+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    11df <pptp_gre_bind+0x9f>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   1050 <connect@plt>
cmp    $0x0,%rax
jge    11d9 <pptp_gre_bind+0x99>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11df <pptp_gre_bind+0x9f>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pptp_gre_bind>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
mov    0x2e92(%rip),%r12
mov    (%r12),%edi
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
mov    $0x2f,%edx
call   1060 <socket@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     11be <pptp_gre_bind+0x7e>
mov    %eax,%ebp
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
mov    (%r12),%eax
mov    %eax,0xc(%rsp)
mov    %r14d,0x8(%rsp)
movq   $0x0,(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x10,%edx
call   1050 <connect@plt>
test   %rax,%rax
js     11b7 <pptp_gre_bind+0x77>
mov    %ebp,%ebx
jmp    11be <pptp_gre_bind+0x7e>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i64, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_RAW = global i32 0
define i32 @pptp_gre_bind(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_RAW
%8 = call i32 @socket(i32 %6, i32 %7, i32 47)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %29
12: ; preds = %1
%13 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 16)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %14, i32* %15
%16 = load i32, i32* %3
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%18 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %17, i32 0, i32 0
store i32 %16, i32* %18
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i64 0, i64* %19
%20 = load i32, i32* %5
%21 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%22 = call i64 @connect(i32 %20, %struct.sockaddr* %21, i32 16)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %12
%25 = load i32, i32* %5
%26 = call i32 @close(i32 %25)
store i32 -1, i32* %2
br label %29
27: ; preds = %12
%28 = load i32, i32* %5
store i32 %28, i32* %2
br label %29
29: ; preds = %27, %24, %11
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i64, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_RAW = global i32 0
define i32 @pptp_gre_bind(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_RAW
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 47)
%7 = icmp slt i32 %6, 0
br i1 %7, label %19, label %8
8: ; preds = %1
%9 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 16)
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %0, i32* %12
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i64 0, i64* %13
%14 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%15 = call i64 @connect(i32 %6, %struct.sockaddr* %14, i32 16)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %8
%18 = call i32 @close(i32 %6)
br label %19
19: ; preds = %8, %1, %17
%20 = phi i32 [ -1, %17 ], [ -1, %1 ], [ %6, %8 ]
ret i32 %20
}