#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 */

/* Variables and functions */
long UCHAR_MAX;
int /*<<< orphan*/ read_long(long *, char *);
char *strtok(char *, char *);
int /*<<< orphan*/ trim_line(char *);
int parse_binary(char *str, unsigned char *buf, int limit) {
  int pos = 0;
  char *tok;
  trim_line(str);
  for (tok = strtok(str, "."); tok != NULL && pos < limit;
       tok = strtok(NULL, ".")) {
    long v;
    if (!read_long(&v, tok) || v > UCHAR_MAX || v < 0)
      return -1;
    buf[pos++] = (unsigned char)v;
  }
  return pos;
}


x86 assembly (clang, -O0):
<parse_binary>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <trim_line@plt>
mov    -0x10(%rbp),%rdi
lea    0xea2(%rip),%rsi
call   1050 <strtok@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x28(%rbp)
mov    %al,-0x31(%rbp)
je     1183 <parse_binary+0x53>
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
setl   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    1193 <parse_binary+0x63>
jmp    1208 <parse_binary+0xd8>
mov    -0x28(%rbp),%rsi
lea    -0x30(%rbp),%rdi
call   1030 <read_long@plt>
cmp    $0x0,%eax
je     11c8 <parse_binary+0x98>
mov    -0x30(%rbp),%rax
mov    0x2e3c(%rip),%rcx
cmp    (%rcx),%rax
jg     11c8 <parse_binary+0x98>
cmpq   $0x0,-0x30(%rbp)
jge    11d4 <parse_binary+0xa4>
movl   $0xffffffff,-0x4(%rbp)
jmp    120e <parse_binary+0xde>
mov    -0x30(%rbp),%rax
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x20(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe06(%rip),%rsi
call   1050 <strtok@plt>
mov    %rax,-0x28(%rbp)
jmp    1167 <parse_binary+0x37>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_binary>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
call   1040 <trim_line@plt>
lea    0xeb1(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strtok@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     11c1 <parse_binary+0x91>
test   %ebp,%ebp
jle    11c1 <parse_binary+0x91>
mov    %ebp,%ebp
xor    %ebx,%ebx
mov    %rsp,%r15
mov    0x2e80(%rip),%r13
lea    0xe89(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %rax,%rsi
call   1030 <read_long@plt>
test   %eax,%eax
je     11bc <parse_binary+0x8c>
mov    (%rsp),%rax
test   %rax,%rax
js     11bc <parse_binary+0x8c>
cmp    0x0(%r13),%rax
jg     11bc <parse_binary+0x8c>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
xor    %edi,%edi
mov    %r12,%rsi
call   1050 <strtok@plt>
test   %rax,%rax
je     11c1 <parse_binary+0x91>
cmp    %rbp,%rbx
jb     1180 <parse_binary+0x50>
jmp    11c1 <parse_binary+0x91>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
@UCHAR_MAX = global i64 0
define i32 @parse_binary(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
%11 = load i8*, i8** %5
%12 = call i32 @trim_line(i8* %11)
%13 = load i8*, i8** %5
%14 = call i8* @strtok(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %14, i8** %9
br label %15
15: ; preds = %44, %3
%16 = load i8*, i8** %9
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i32, i32* %8
%20 = load i32, i32* %7
%21 = icmp slt i32 %19, %20
br label %22
22: ; preds = %18, %15
%23 = phi i1 [ false, %15 ], [ %21, %18 ]
br i1 %23, label %24, label %46
24: ; preds = %22
%25 = load i8*, i8** %9
%26 = call i32 @read_long(i64* %10, i8* %25)
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %35
28: ; preds = %24
%29 = load i64, i64* %10
%30 = load i64, i64* @UCHAR_MAX
%31 = icmp sgt i64 %29, %30
br i1 %31, label %35, label %32
32: ; preds = %28
%33 = load i64, i64* %10
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %32, %28, %24
store i32 -1, i32* %4
br label %48
36: ; preds = %32
%37 = load i64, i64* %10
%38 = trunc i64 %37 to i8
%39 = load i8*, i8** %6
%40 = load i32, i32* %8
%41 = add nsw i32 %40, 1
store i32 %41, i32* %8
%42 = sext i32 %40 to i64
%43 = getelementptr inbounds i8, i8* %39, i64 %42
store i8 %38, i8* %43
br label %44
44: ; preds = %36
%45 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %45, i8** %9
br label %15
46: ; preds = %22
%47 = load i32, i32* %8
store i32 %47, i32* %4
br label %48
48: ; preds = %46, %35
%49 = load i32, i32* %4
ret i32 %49
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
@UCHAR_MAX = global i64 0
define i32 @parse_binary(i8* %0, i8* nocapture writeonly %1, i32 %2) {
%4 = alloca i64
%5 = tail call i32 @trim_line(i8* %0)
%6 = tail call i8* @strtok(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp ne i8* %6, null
%8 = icmp sgt i32 %2, 0
%9 = and i1 %7, %8
br i1 %9, label %10, label %35
10: ; preds = %3
%11 = bitcast i64* %4 to i8*
%12 = zext i32 %2 to i64
br label %13
13: ; preds = %10, %25
%14 = phi i64 [ 0, %10 ], [ %27, %25 ]
%15 = phi i8* [ %6, %10 ], [ %29, %25 ]
%16 = call i32 @read_long(i64* %4, i8* %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %24, label %18
18: ; preds = %13
%19 = load i64, i64* %4
%20 = load i64, i64* @UCHAR_MAX
%21 = icmp sgt i64 %19, %20
%22 = icmp slt i64 %19, 0
%23 = or i1 %22, %21
br i1 %23, label %24, label %25
24: ; preds = %18, %13
br label %35
25: ; preds = %18
%26 = trunc i64 %19 to i8
%27 = add nuw nsw i64 %14, 1
%28 = getelementptr inbounds i8, i8* %1, i64 %14
store i8 %26, i8* %28
%29 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%30 = icmp ne i8* %29, null
%31 = icmp ult i64 %27, %12
%32 = select i1 %30, i1 %31, i1 false
br i1 %32, label %13, label %33
33: ; preds = %25
%34 = trunc i64 %27 to i32
br label %35
35: ; preds = %33, %3, %24
%36 = phi i32 [ -1, %24 ], [ 0, %3 ], [ %34, %33 ]
ret i32 %36
}