#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 */
typedef int ptrdiff_t;

/* Variables and functions */
char *char_replace(char *, char, char);
int /*<<< orphan*/ memmove(char *, char *, int);
char *strstr(char *, char *);
char *parse_ios(char *agent, int tlen) {
  char *p = NULL, *q = NULL;
  ptrdiff_t offset;
  p = agent;
  if ((p = strstr(agent, " OS ")) == NULL)
    goto out;
  if ((offset = p - agent) <= 0)
    goto out;
  if ((q = strstr(p, " like Mac")) == NULL)
    goto out;
  *q = 0;
  memmove(agent + tlen, agent + offset, offset);
  return char_replace(agent, '_', '.');
out:
  agent[tlen] = 0;
  return agent;
}


x86 assembly (clang, -O0):
<parse_ios>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe9e(%rip),%rsi
call   1050 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    117a <parse_ios+0x4a>
jmp    11fa <parse_ios+0xca>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jg     1196 <parse_ios+0x66>
jmp    11fa <parse_ios+0xca>
mov    -0x20(%rbp),%rdi
lea    0xe64(%rip),%rsi
call   1050 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11b9 <parse_ios+0x89>
jmp    11fa <parse_ios+0xca>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
movslq -0x2c(%rbp),%rax
add    %rax,%rsi
mov    -0x2c(%rbp),%edx
call   1040 <memmove@plt>
mov    -0x10(%rbp),%rdi
mov    $0x5f,%esi
mov    $0x2e,%edx
call   1030 <char_replace@plt>
mov    %rax,-0x8(%rbp)
jmp    120e <parse_ios+0xde>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_ios>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbx
lea    0xebf(%rip),%rsi
call   1050 <strstr@plt>
test   %rax,%rax
je     1195 <parse_ios+0x65>
mov    %rax,%rbp
sub    %rbx,%rbp
test   %ebp,%ebp
jle    1195 <parse_ios+0x65>
lea    0xea9(%rip),%rsi
mov    %rax,%rdi
call   1050 <strstr@plt>
test   %rax,%rax
je     1195 <parse_ios+0x65>
movb   $0x0,(%rax)
movslq %r14d,%rdi
add    %rbx,%rdi
movslq %ebp,%rsi
add    %rbx,%rsi
mov    %ebp,%edx
call   1040 <memmove@plt>
mov    %rbx,%rdi
mov    $0x5f,%esi
mov    $0x2e,%edx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <char_replace@plt>
movslq %r14d,%rax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c" OS \00"
@.str.1 = [10 x i8] c" like Mac\00"
define i8* @parse_ios(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* null, i8** %6
store i8* null, i8** %7
%9 = load i8*, i8** %4
store i8* %9, i8** %6
%10 = load i8*, i8** %4
%11 = call i8* @strstr(i8* %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %11, i8** %6
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %14
13: ; preds = %2
br label %42
14: ; preds = %2
%15 = load i8*, i8** %6
%16 = load i8*, i8** %4
%17 = ptrtoint i8* %15 to i64
%18 = ptrtoint i8* %16 to i64
%19 = sub i64 %17, %18
%20 = trunc i64 %19 to i32
store i32 %20, i32* %8
%21 = icmp sle i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %14
br label %42
23: ; preds = %14
%24 = load i8*, i8** %6
%25 = call i8* @strstr(i8* %24, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i8* %25, i8** %7
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %28
27: ; preds = %23
br label %42
28: ; preds = %23
%29 = load i8*, i8** %7
store i8 0, i8* %29
%30 = load i8*, i8** %4
%31 = load i32, i32* %5
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = load i8*, i8** %4
%35 = load i32, i32* %8
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
%38 = load i32, i32* %8
%39 = call i32 @memmove(i8* %33, i8* %37, i32 %38)
%40 = load i8*, i8** %4
%41 = call i8* @char_replace(i8* %40, i8 signext 95, i8 signext 46)
store i8* %41, i8** %3
br label %48
42: ; preds = %27, %22, %13
%43 = load i8*, i8** %4
%44 = load i32, i32* %5
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %43, i64 %45
store i8 0, i8* %46
%47 = load i8*, i8** %4
store i8* %47, i8** %3
br label %48
48: ; preds = %42, %28
%49 = load i8*, i8** %3
ret i8* %49
}

LLVM IR (-O2):
@.str = [5 x i8] c" OS \00"
@.str.1 = [10 x i8] c" like Mac\00"
define i8* @parse_ios(i8* %0, i32 %1) {
%3 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i8* %3, null
br i1 %4, label %22, label %5
5: ; preds = %2
%6 = ptrtoint i8* %3 to i64
%7 = ptrtoint i8* %0 to i64
%8 = sub i64 %6, %7
%9 = trunc i64 %8 to i32
%10 = icmp slt i32 %9, 1
br i1 %10, label %22, label %11
11: ; preds = %5
%12 = tail call i8* @strstr(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i8* %12, null
br i1 %13, label %22, label %14
14: ; preds = %11
store i8 0, i8* %12
%15 = sext i32 %1 to i64
%16 = getelementptr inbounds i8, i8* %0, i64 %15
%17 = shl i64 %8, 32
%18 = ashr exact i64 %17, 32
%19 = getelementptr inbounds i8, i8* %0, i64 %18
%20 = tail call i32 @memmove(i8* %16, i8* %19, i32 %9)
%21 = tail call i8* @char_replace(i8* %0, i8 signext 95, i8 signext 46)
br label %25
22: ; preds = %11, %5, %2
%23 = sext i32 %1 to i64
%24 = getelementptr inbounds i8, i8* %0, i64 %23
store i8 0, i8* %24
br label %25
25: ; preds = %22, %14
%26 = phi i8* [ %0, %22 ], [ %21, %14 ]
ret i8* %26
}