#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 /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sa;
typedef int /*<<< orphan*/ in_addr_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int EINVAL;
scalar_t__ bind(unsigned int, struct sockaddr *, int);
int errno;
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ ntohs(unsigned short);
int sscanf(char *, char *, unsigned int *, int /*<<< orphan*/ *,
           unsigned short *);
int main(int argc, char **argv) {
  if (1 == argc) {
    unsigned int sock;
    in_addr_t ia;
    unsigned short port;
    if (3 == sscanf(argv[0], "%8x%8x%4hx", &sock, &ia, &port)) {
      struct sockaddr_in sa;
      if (sock >= 16384)
        return EINVAL;
      if (port >= 1024)
        return EINVAL;
      memset(&sa, 0, sizeof(sa));
      memcpy(&sa.sin_addr, &ia, sizeof(sa.sin_addr));
      sa.sin_family = AF_INET;
      sa.sin_port = ntohs(port);
      if (bind(sock, (struct sockaddr *)&sa, sizeof(sa))) {
        return errno;
      }
      return 0;
    }
  }
  return EINVAL;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x1,%eax
cmp    -0x8(%rbp),%eax
jne    125b <main+0x10b>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xe7e(%rip),%rsi
lea    -0x14(%rbp),%rdx
lea    -0x18(%rbp),%rcx
lea    -0x1a(%rbp),%r8
call   1050 <sscanf@plt>
mov    %eax,%ecx
mov    $0x3,%eax
cmp    %ecx,%eax
jne    1256 <main+0x106>
cmpl   $0x4000,-0x14(%rbp)
jb     11c0 <main+0x70>
mov    0x2e1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1267 <main+0x117>
movzwl -0x1a(%rbp),%eax
cmp    $0x400,%eax
jl     11e0 <main+0x90>
mov    0x2dfa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1267 <main+0x117>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1030 <memset@plt>
lea    -0x28(%rbp),%rdi
add    $0x8,%rdi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
movzwl -0x1a(%rbp),%edi
call   1060 <ntohs@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
je     124a <main+0xfa>
mov    0x2d98(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1267 <main+0x117>
movl   $0x0,-0x4(%rbp)
jmp    1267 <main+0x117>
jmp    125b <main+0x10b>
mov    0x2d6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x28,%rsp
cmp    $0x1,%edi
jne    1195 <main+0x45>
mov    (%rsi),%rdi
lea    0xe9a(%rip),%rsi
lea    0x10(%rsp),%rdx
lea    0x14(%rsp),%rcx
lea    0xe(%rsp),%r8
call   1050 <sscanf@plt>
cmp    $0x3,%eax
jne    1195 <main+0x45>
cmpl   $0x4000,0x10(%rsp)
jae    1195 <main+0x45>
movzwl 0xe(%rsp),%eax
cmp    $0x400,%eax
jb     11a8 <main+0x58>
mov    0x2e34(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
lea    0x18(%rsp),%r14
xor    %ebx,%ebx
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1030 <memset@plt>
lea    0x20(%rsp),%rdi
lea    0x14(%rsp),%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
movzwl 0xe(%rsp),%edi
call   1060 <ntohs@plt>
mov    %eax,0x18(%rsp)
mov    0x10(%rsp),%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
je     119e <main+0x4e>
mov    0x2dce(%rip),%rax
jmp    119c <main+0x4c>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [11 x i8] c"%8x%8x%4hx\00"
@EINVAL = global i32 0
@AF_INET = global i32 0
@errno = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i16
%9 = alloca %struct.sockaddr_in
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp eq i32 1, %10
br i1 %11, label %12, label %46
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @sscanf(i8* %15, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32* %6, i32* %7, i16* %8)
%17 = icmp eq i32 3, %16
br i1 %17, label %18, label %45
18: ; preds = %12
%19 = load i32, i32* %6
%20 = icmp uge i32 %19, 16384
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = load i32, i32* @EINVAL
store i32 %22, i32* %3
br label %48
23: ; preds = %18
%24 = load i16, i16* %8
%25 = zext i16 %24 to i32
%26 = icmp sge i32 %25, 1024
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = load i32, i32* @EINVAL
store i32 %28, i32* %3
br label %48
29: ; preds = %23
%30 = call i32 @memset(%struct.sockaddr_in* %9, i32 0, i32 12)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
%32 = call i32 @memcpy(i32* %31, i32* %7, i32 4)
%33 = load i32, i32* @AF_INET
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i32 %33, i32* %34
%35 = load i16, i16* %8
%36 = call i32 @ntohs(i16 zeroext %35)
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %36, i32* %37
%38 = load i32, i32* %6
%39 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%40 = call i64 @bind(i32 %38, %struct.sockaddr* %39, i32 12)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %29
%43 = load i32, i32* @errno
store i32 %43, i32* %3
br label %48
44: ; preds = %29
store i32 0, i32* %3
br label %48
45: ; preds = %12
br label %46
46: ; preds = %45, %2
%47 = load i32, i32* @EINVAL
store i32 %47, i32* %3
br label %48
48: ; preds = %46, %44, %42, %27, %21
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [11 x i8] c"%8x%8x%4hx\00"
@EINVAL = global i32 0
@AF_INET = global i32 0
@errno = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i16
%6 = alloca %struct.sockaddr_in
%7 = icmp eq i32 %0, 1
br i1 %7, label %8, label %44
8: ; preds = %2
%9 = bitcast i32* %3 to i8*
%10 = bitcast i32* %4 to i8*
%11 = bitcast i16* %5 to i8*
%12 = load i8*, i8** %1
%13 = call i32 @sscanf(i8* %12, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32* %3, i32* %4, i16* %5)
%14 = icmp eq i32 %13, 3
br i1 %14, label %16, label %15
15: ; preds = %8
br label %44
16: ; preds = %8
%17 = bitcast %struct.sockaddr_in* %6 to i8*
%18 = load i32, i32* %3
%19 = icmp ugt i32 %18, 16383
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = load i32, i32* @EINVAL
br label %42
22: ; preds = %16
%23 = load i16, i16* %5
%24 = icmp ugt i16 %23, 1023
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = load i32, i32* @EINVAL
br label %42
27: ; preds = %22
%28 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 12)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 2
%30 = call i32 @memcpy(i32* %29, i32* %4, i32 4)
%31 = load i32, i32* @AF_INET
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1
store i32 %31, i32* %32
%33 = load i16, i16* %5
%34 = call i32 @ntohs(i16 zeroext %33)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0
store i32 %34, i32* %35
%36 = load i32, i32* %3
%37 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%38 = call i64 @bind(i32 %36, %struct.sockaddr* %37, i32 12)
%39 = icmp eq i64 %38, 0
%40 = load i32, i32* @errno
%41 = select i1 %39, i32 0, i32 %40
br label %42
42: ; preds = %20, %25, %27
%43 = phi i32 [ %21, %20 ], [ %26, %25 ], [ %41, %27 ]
br label %46
44: ; preds = %15, %2
%45 = load i32, i32* @EINVAL
br label %46
46: ; preds = %42, %44
%47 = phi i32 [ %43, %42 ], [ %45, %44 ]
ret i32 %47
}