#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 in_addr {
  int /*<<< orphan*/ s_addr;
};

/* Variables and functions */
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int, int, int, int);
int sscanf(char const *, char *, int *, int *, int *, int *, char *);
int safe_inet_addr(const char *buf, struct in_addr *addr) {
  static char addrbuf[32];
  int a1 = 0, a2 = 0, a3 = 0, a4 = 0;
  struct in_addr A;
  char x;
  if (sscanf(buf, "%d.%d.%d.%d%c", &a1, &a2, &a3, &a4, &x) != 4)
    return 0;
  if (a1 < 0 || a1 > 255)
    return 0;
  if (a2 < 0 || a2 > 255)
    return 0;
  if (a3 < 0 || a3 > 255)
    return 0;
  if (a4 < 0 || a4 > 255)
    return 0;
  snprintf(addrbuf, 32, "%d.%d.%d.%d", a1, a2, a3, a4);
  A.s_addr = inet_addr(addrbuf);
  if (addr)
    addr->s_addr = A.s_addr;
  return 1;
}


x86 assembly (clang, -O0):
<safe_inet_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe99(%rip),%rsi
lea    -0x1c(%rbp),%rdx
lea    -0x20(%rbp),%rcx
lea    -0x24(%rbp),%r8
lea    -0x28(%rbp),%r9
lea    -0x31(%rbp),%rax
mov    %rax,(%rsp)
call   1050 <sscanf@plt>
cmp    $0x4,%eax
je     1199 <safe_inet_addr+0x69>
movl   $0x0,-0x4(%rbp)
jmp    1278 <safe_inet_addr+0x148>
cmpl   $0x0,-0x1c(%rbp)
jl     11b0 <safe_inet_addr+0x80>
cmpl   $0xff,-0x1c(%rbp)
jle    11bc <safe_inet_addr+0x8c>
movl   $0x0,-0x4(%rbp)
jmp    1278 <safe_inet_addr+0x148>
cmpl   $0x0,-0x20(%rbp)
jl     11d3 <safe_inet_addr+0xa3>
cmpl   $0xff,-0x20(%rbp)
jle    11df <safe_inet_addr+0xaf>
movl   $0x0,-0x4(%rbp)
jmp    1278 <safe_inet_addr+0x148>
cmpl   $0x0,-0x24(%rbp)
jl     11f6 <safe_inet_addr+0xc6>
cmpl   $0xff,-0x24(%rbp)
jle    1202 <safe_inet_addr+0xd2>
movl   $0x0,-0x4(%rbp)
jmp    1278 <safe_inet_addr+0x148>
cmpl   $0x0,-0x28(%rbp)
jl     1219 <safe_inet_addr+0xe9>
cmpl   $0xff,-0x28(%rbp)
jle    1225 <safe_inet_addr+0xf5>
movl   $0x0,-0x4(%rbp)
jmp    1278 <safe_inet_addr+0x148>
mov    -0x1c(%rbp),%ecx
mov    -0x20(%rbp),%r8d
mov    -0x24(%rbp),%r9d
mov    -0x28(%rbp),%eax
lea    0x2e16(%rip),%rdi
mov    $0x20,%esi
lea    0xdc8(%rip),%rdx
mov    %eax,(%rsp)
call   1030 <snprintf@plt>
lea    0x2dfb(%rip),%rdi
call   1040 <inet_addr@plt>
mov    %eax,-0x30(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1271 <safe_inet_addr+0x141>
mov    -0x30(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_inet_addr>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
movl   $0x0,0x24(%rsp)
movl   $0x0,0x20(%rsp)
movl   $0x0,0x1c(%rsp)
movl   $0x0,0x18(%rsp)
lea    0x17(%rsp),%rax
mov    %rax,(%rsp)
lea    0xe96(%rip),%rsi
lea    0x24(%rsp),%rdx
lea    0x20(%rsp),%rcx
lea    0x1c(%rsp),%r8
lea    0x18(%rsp),%r9
call   1050 <sscanf@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x4,%ecx
jne    11f4 <safe_inet_addr+0xc4>
mov    0x24(%rsp),%ecx
cmp    $0xff,%ecx
ja     11f4 <safe_inet_addr+0xc4>
mov    0x20(%rsp),%r8d
cmp    $0xff,%r8d
ja     11f4 <safe_inet_addr+0xc4>
mov    0x1c(%rsp),%r9d
cmp    $0xff,%r9d
ja     11f4 <safe_inet_addr+0xc4>
mov    0x18(%rsp),%edx
cmp    $0xff,%edx
ja     11f4 <safe_inet_addr+0xc4>
mov    %edx,(%rsp)
lea    0x2e86(%rip),%r14
lea    0xe3d(%rip),%rdx
mov    %r14,%rdi
mov    $0x20,%esi
call   1030 <snprintf@plt>
mov    %r14,%rdi
call   1040 <inet_addr@plt>
mov    %eax,%ecx
mov    $0x1,%eax
test   %rbx,%rbx
je     11f4 <safe_inet_addr+0xc4>
mov    %ecx,(%rbx)
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.in_addr = type { i32 }
@safe_inet_addr.addrbuf = internal global [32 x i8] zeroinitializer
@.str = [14 x i8] c"%d.%d.%d.%d%c\00"
@.str.1 = [12 x i8] c"%d.%d.%d.%d\00"
define i32 @safe_inet_addr(i8* %0, %struct.in_addr* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.in_addr*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.in_addr
%11 = alloca i8
store i8* %0, i8** %4
store %struct.in_addr* %1, %struct.in_addr** %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
%12 = load i8*, i8** %4
%13 = call i32 @sscanf(i8* %12, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32* %6, i32* %7, i32* %8, i32* %9, i8* %11)
%14 = icmp ne i32 %13, 4
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 0, i32* %3
br label %60
16: ; preds = %2
%17 = load i32, i32* %6
%18 = icmp slt i32 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %16
%20 = load i32, i32* %6
%21 = icmp sgt i32 %20, 255
br i1 %21, label %22, label %23
22: ; preds = %19, %16
store i32 0, i32* %3
br label %60
23: ; preds = %19
%24 = load i32, i32* %7
%25 = icmp slt i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %23
%27 = load i32, i32* %7
%28 = icmp sgt i32 %27, 255
br i1 %28, label %29, label %30
29: ; preds = %26, %23
store i32 0, i32* %3
br label %60
30: ; preds = %26
%31 = load i32, i32* %8
%32 = icmp slt i32 %31, 0
br i1 %32, label %36, label %33
33: ; preds = %30
%34 = load i32, i32* %8
%35 = icmp sgt i32 %34, 255
br i1 %35, label %36, label %37
36: ; preds = %33, %30
store i32 0, i32* %3
br label %60
37: ; preds = %33
%38 = load i32, i32* %9
%39 = icmp slt i32 %38, 0
br i1 %39, label %43, label %40
40: ; preds = %37
%41 = load i32, i32* %9
%42 = icmp sgt i32 %41, 255
br i1 %42, label %43, label %44
43: ; preds = %40, %37
store i32 0, i32* %3
br label %60
44: ; preds = %40
%45 = load i32, i32* %6
%46 = load i32, i32* %7
%47 = load i32, i32* %8
%48 = load i32, i32* %9
%49 = call i32 @snprintf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @safe_inet_addr.addrbuf, i64 0, i64 0), i32 32, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %45, i32 %46, i32 %47, i32 %48)
%50 = call i32 @inet_addr(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @safe_inet_addr.addrbuf, i64 0, i64 0))
%51 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %10, i32 0, i32 0
store i32 %50, i32* %51
%52 = load %struct.in_addr*, %struct.in_addr** %5
%53 = icmp ne %struct.in_addr* %52, null
br i1 %53, label %54, label %59
54: ; preds = %44
%55 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %10, i32 0, i32 0
%56 = load i32, i32* %55
%57 = load %struct.in_addr*, %struct.in_addr** %5
%58 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %57, i32 0, i32 0
store i32 %56, i32* %58
br label %59
59: ; preds = %54, %44
store i32 1, i32* %3
br label %60
60: ; preds = %59, %43, %36, %29, %22, %15
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
@safe_inet_addr.addrbuf = internal global [32 x i8] zeroinitializer
@.str = [14 x i8] c"%d.%d.%d.%d%c\00"
@.str.1 = [12 x i8] c"%d.%d.%d.%d\00"
define i32 @safe_inet_addr(i8* nocapture %0, %struct.in_addr* writeonly %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
%8 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%9 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%10 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%11 = bitcast i32* %6 to i8*
store i32 0, i32* %6
%12 = call i32 @sscanf(i8* %0, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32* %3, i32* %4, i32* %5, i32* %6, i8* %7)
%13 = icmp eq i32 %12, 4
br i1 %13, label %14, label %32
14: ; preds = %2
%15 = load i32, i32* %3
%16 = icmp ugt i32 %15, 255
br i1 %16, label %32, label %17
17: ; preds = %14
%18 = load i32, i32* %4
%19 = icmp ugt i32 %18, 255
br i1 %19, label %32, label %20
20: ; preds = %17
%21 = load i32, i32* %5
%22 = icmp ugt i32 %21, 255
br i1 %22, label %32, label %23
23: ; preds = %20
%24 = load i32, i32* %6
%25 = icmp ugt i32 %24, 255
br i1 %25, label %32, label %26
26: ; preds = %23
%27 = call i32 @snprintf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @safe_inet_addr.addrbuf, i64 0, i64 0), i32 32, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 %18, i32 %21, i32 %24)
%28 = call i32 @inet_addr(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @safe_inet_addr.addrbuf, i64 0, i64 0))
%29 = icmp eq %struct.in_addr* %1, null
br i1 %29, label %32, label %30
30: ; preds = %26
%31 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %1, i64 0, i32 0
store i32 %28, i32* %31
br label %32
32: ; preds = %26, %30, %23, %20, %17, %14, %2
%33 = phi i32 [ 0, %2 ], [ 0, %14 ], [ 0, %17 ], [ 0, %20 ], [ 0, %23 ], [ 1, %30 ], [ 1, %26 ]
ret i32 %33
}