#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 */
char *strchr(char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int getnext(char *src, int separator, char *dest) {
  char *c;
  int len;
  if ((src == NULL) || (dest == NULL)) {
    return -1;
  }
  c = strchr(src, separator);
  if (c == NULL) {
    strcpy(dest, src);
    return -1;
  }
  len = c - src;
  strncpy(dest, src, len);
  dest[len] = '\0';
  return len + 1;
}


x86 assembly (clang, -O0):
<getnext>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1159 <getnext+0x29>
cmpq   $0x0,-0x20(%rbp)
jne    1165 <getnext+0x35>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <getnext+0x9c>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1199 <getnext+0x69>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <getnext+0x9c>
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getnext>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    $0xffffffff,%r15d
test   %rdi,%rdi
je     1185 <getnext+0x55>
mov    %rdx,%r14
test   %rdx,%rdx
je     1185 <getnext+0x55>
mov    %rdi,%rbx
call   1050 <strchr@plt>
test   %rax,%rax
je     117a <getnext+0x4a>
mov    %rax,%r12
sub    %rbx,%r12
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1030 <strncpy@plt>
movslq %r12d,%r15
movb   $0x0,(%r14,%r15,1)
add    $0x1,%r15d
jmp    1185 <getnext+0x55>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @getnext(i8* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%10 = load i8*, i8** %5
%11 = icmp eq i8* %10, null
br i1 %11, label %15, label %12
12: ; preds = %3
%13 = load i8*, i8** %7
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %16
15: ; preds = %12, %3
store i32 -1, i32* %4
br label %43
16: ; preds = %12
%17 = load i8*, i8** %5
%18 = load i32, i32* %6
%19 = call i8* @strchr(i8* %17, i32 %18)
store i8* %19, i8** %8
%20 = load i8*, i8** %8
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %26
22: ; preds = %16
%23 = load i8*, i8** %7
%24 = load i8*, i8** %5
%25 = call i32 @strcpy(i8* %23, i8* %24)
store i32 -1, i32* %4
br label %43
26: ; preds = %16
%27 = load i8*, i8** %8
%28 = load i8*, i8** %5
%29 = ptrtoint i8* %27 to i64
%30 = ptrtoint i8* %28 to i64
%31 = sub i64 %29, %30
%32 = trunc i64 %31 to i32
store i32 %32, i32* %9
%33 = load i8*, i8** %7
%34 = load i8*, i8** %5
%35 = load i32, i32* %9
%36 = call i32 @strncpy(i8* %33, i8* %34, i32 %35)
%37 = load i8*, i8** %7
%38 = load i32, i32* %9
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 0, i8* %40
%41 = load i32, i32* %9
%42 = add nsw i32 %41, 1
store i32 %42, i32* %4
br label %43
43: ; preds = %26, %22, %15
%44 = load i32, i32* %4
ret i32 %44
}

LLVM IR (-O2):
define i32 @getnext(i8* %0, i32 %1, i8* %2) {
%4 = icmp eq i8* %0, null
%5 = icmp eq i8* %2, null
%6 = or i1 %4, %5
br i1 %6, label %22, label %7
7: ; preds = %3
%8 = tail call i8* @strchr(i8* dereferenceable(1) %0, i32 %1)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 @strcpy(i8* %2, i8* %0)
br label %22
12: ; preds = %7
%13 = ptrtoint i8* %8 to i64
%14 = ptrtoint i8* %0 to i64
%15 = sub i64 %13, %14
%16 = trunc i64 %15 to i32
%17 = tail call i32 @strncpy(i8* %2, i8* %0, i32 %16)
%18 = shl i64 %15, 32
%19 = ashr exact i64 %18, 32
%20 = getelementptr inbounds i8, i8* %2, i64 %19
store i8 0, i8* %20
%21 = add nsw i32 %16, 1
br label %22
22: ; preds = %3, %12, %10
%23 = phi i32 [ -1, %10 ], [ %21, %12 ], [ -1, %3 ]
ret i32 %23
}