#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 {
  void *sin_port;
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_family;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int atoi(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getopt(int, char *const *, char *);
void *htons(int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ optarg;
int /*<<< orphan*/ printf(char *, int);
int sscanf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ stderr;
int args_ip(int argc, char *const argv[], struct sockaddr_in *seraddr) {
  char ip[16], port[8];
  int ch, n;
  while ((ch = getopt(argc, argv, "s:")) != -1) {
    switch (ch) {
    case 's':
      if ((n = sscanf(optarg, "%15[0-9.]:%5[0-9]", ip, port)) < 1) {
        fprintf(stderr, "ip error\n");
        break;
      }
      seraddr->sin_family = AF_INET;
      inet_pton(AF_INET, ip, &seraddr->sin_addr);
      printf("%d\n", n);
      if (n == 2)
        seraddr->sin_port = htons(atoi(port));
      else
        seraddr->sin_port = htons(9000);
      return 0;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<args_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe6f(%rip),%rdx
call   1080 <getopt@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0xffffffff,%eax
je     127d <args_ip+0x10d>
mov    -0x3c(%rbp),%eax
sub    $0x73,%eax
jne    1278 <args_ip+0x108>
jmp    11b3 <args_ip+0x43>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
lea    0xe38(%rip),%rsi
call   1070 <sscanf@plt>
mov    %eax,-0x40(%rbp)
cmp    $0x1,%eax
jge    11f6 <args_ip+0x86>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
lea    0xe29(%rip),%rsi
call   1050 <fprintf@plt>
jmp    1278 <args_ip+0x108>
mov    0x2de3(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0xc(%rax)
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rdx
add    $0x8,%rdx
call   1060 <inet_pton@plt>
mov    -0x40(%rbp),%esi
lea    0xdf5(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x2,-0x40(%rbp)
jne    1258 <args_ip+0xe8>
lea    -0x38(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
jmp    126c <args_ip+0xfc>
mov    $0x2328,%edi
call   1030 <htons@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1284 <args_ip+0x114>
jmp    1183 <args_ip+0x13>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<args_ip>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,(%rsp)
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xe72(%rip),%r15
lea    0x8(%rsp),%r14
lea    0xe7b(%rip),%r12
jmp    11a9 <args_ip+0x39>
nopl   0x0(%rax)
cmp    $0xffffffff,%eax
je     1240 <args_ip+0xd0>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1080 <getopt@plt>
cmp    $0x73,%eax
jne    11a0 <args_ip+0x30>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
lea    0xe38(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    %r14,%rcx
call   1070 <sscanf@plt>
test   %eax,%eax
jg     11ef <args_ip+0x7f>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
mov    %r12,%rsi
call   1050 <fprintf@plt>
jmp    11a9 <args_ip+0x39>
mov    %eax,%r13d
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
mov    (%rsp),%rbx
mov    %edi,0xc(%rbx)
lea    0x8(%rbx),%rdx
lea    0x10(%rsp),%rsi
call   1060 <inet_pton@plt>
lea    0xe08(%rip),%rdi
mov    %r13d,%esi
call   1040 <printf@plt>
mov    $0x2328,%edi
cmp    $0x2,%r13d
jne    1236 <args_ip+0xc6>
lea    0x8(%rsp),%rdi
call   1090 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %rax,(%rbx)
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i8*, i32, i32 }
@.str = [3 x i8] c"s:\00"
@optarg = global i32 0
@.str.1 = [18 x i8] c"%15[0-9.]:%5[0-9]\00"
@stderr = global i32 0
@.str.2 = [10 x i8] c"ip error\0A\00"
@AF_INET = global i32 0
@.str.3 = [4 x i8] c"%d\0A\00"
define i32 @args_ip(i32 %0, i8** %1, %struct.sockaddr_in* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca %struct.sockaddr_in*
%8 = alloca [16 x i8]
%9 = alloca [8 x i8]
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %5
store i8** %1, i8*** %6
store %struct.sockaddr_in* %2, %struct.sockaddr_in** %7
br label %12
12: ; preds = %52, %3
%13 = load i32, i32* %5
%14 = load i8**, i8*** %6
%15 = call i32 @getopt(i32 %13, i8** %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32 %15, i32* %10
%16 = icmp ne i32 %15, -1
br i1 %16, label %17, label %53
17: ; preds = %12
%18 = load i32, i32* %10
switch i32 %18, label %52 [
i32 115, label %19
]
19: ; preds = %17
%20 = load i32, i32* @optarg
%21 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%22 = getelementptr inbounds [8 x i8], [8 x i8]* %9, i64 0, i64 0
%23 = call i32 @sscanf(i32 %20, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %21, i8* %22)
store i32 %23, i32* %11
%24 = icmp slt i32 %23, 1
br i1 %24, label %25, label %28
25: ; preds = %19
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %52
28: ; preds = %19
%29 = load i32, i32* @AF_INET
%30 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %30, i32 0, i32 2
store i32 %29, i32* %31
%32 = load i32, i32* @AF_INET
%33 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%34 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %34, i32 0, i32 1
%36 = call i32 @inet_pton(i32 %32, i8* %33, i32* %35)
%37 = load i32, i32* %11
%38 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %37)
%39 = load i32, i32* %11
%40 = icmp eq i32 %39, 2
br i1 %40, label %41, label %47
41: ; preds = %28
%42 = getelementptr inbounds [8 x i8], [8 x i8]* %9, i64 0, i64 0
%43 = call i32 @atoi(i8* %42)
%44 = call i8* @htons(i32 %43)
%45 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %45, i32 0, i32 0
store i8* %44, i8** %46
br label %51
47: ; preds = %28
%48 = call i8* @htons(i32 9000)
%49 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%50 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %49, i32 0, i32 0
store i8* %48, i8** %50
br label %51
51: ; preds = %47, %41
store i32 0, i32* %4
br label %54
52: ; preds = %17, %25
br label %12
53: ; preds = %12
store i32 -1, i32* %4
br label %54
54: ; preds = %53, %51
%55 = load i32, i32* %4
ret i32 %55
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i8*, i32, i32 }
@.str = [3 x i8] c"s:\00"
@optarg = global i32 0
@.str.1 = [18 x i8] c"%15[0-9.]:%5[0-9]\00"
@stderr = global i32 0
@.str.2 = [10 x i8] c"ip error\0A\00"
@AF_INET = global i32 0
@.str.3 = [4 x i8] c"%d\0A\00"
define i32 @args_ip(i32 %0, i8** %1, %struct.sockaddr_in* %2) {
%4 = alloca [16 x i8]
%5 = alloca [8 x i8]
%6 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [8 x i8], [8 x i8]* %5, i64 0, i64 0
br label %8
8: ; preds = %10, %3
%9 = call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
switch i32 %9, label %10 [
i32 -1, label %31
i32 115, label %11
]
10: ; preds = %8, %15
br label %8
11: ; preds = %8
%12 = load i32, i32* @optarg
%13 = call i32 @sscanf(i32 %12, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %6, i8* %7)
%14 = icmp slt i32 %13, 1
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %10
18: ; preds = %11
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
%22 = call i32 @inet_pton(i32 %19, i8* %6, i32* %21)
%23 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%24 = icmp eq i32 %13, 2
br i1 %24, label %25, label %27
25: ; preds = %18
%26 = call i32 @atoi(i8* %7)
br label %27
27: ; preds = %18, %25
%28 = phi i32 [ %26, %25 ], [ 9000, %18 ]
%29 = call i8* @htons(i32 %28)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i8* %29, i8** %30
br label %31
31: ; preds = %8, %27
%32 = phi i32 [ 0, %27 ], [ %9, %8 ]
ret i32 %32
}