#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 */
int /*<<< orphan*/ Error(char *, char *);
scalar_t__ malloc(int);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *copyBeforeChar(char c, char *s) {
  int k = 0;
  int l = strlen(s);
  char *result;
  while (k < l && s[k] != c)
    k++;
  result = (char *)malloc(sizeof(char) * (k + 1));
  if (result != NULL) {
    strncpy(result, s, k);
    result[k] = 0;
    return result;
  } else {
    Error("copyBeforeChar", "Memory allocation error");
  }
}


x86 assembly (clang, -O0):
<copyBeforeChar>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %dil,%al
mov    %al,-0x9(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%ecx
xor    %eax,%eax
cmp    -0x20(%rbp),%ecx
mov    %al,-0x29(%rbp)
jge    118e <copyBeforeChar+0x4e>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
movsbl -0x9(%rbp),%ecx
cmp    %ecx,%eax
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    119e <copyBeforeChar+0x5e>
jmp    11ac <copyBeforeChar+0x6c>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1165 <copyBeforeChar+0x25>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11f7 <copyBeforeChar+0xb7>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    120a <copyBeforeChar+0xca>
lea    0xe02(%rip),%rdi
lea    0xe0a(%rip),%rsi
call   1050 <Error@plt>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copyBeforeChar>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebp
mov    %rsi,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    1173 <copyBeforeChar+0x33>
mov    %eax,%eax
xor    %ebx,%ebx
nopl   0x0(%rax)
cmp    %bpl,(%r14,%rbx,1)
je     1175 <copyBeforeChar+0x35>
add    $0x1,%rbx
cmp    %rbx,%rax
jne    1160 <copyBeforeChar+0x20>
mov    %eax,%ebx
jmp    1175 <copyBeforeChar+0x35>
xor    %ebx,%ebx
lea    0x1(%rbx),%edi
call   1060 <malloc@plt>
mov    %rax,%rbp
test   %rax,%rax
je     119b <copyBeforeChar+0x5b>
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
mov    %ebx,%eax
movb   $0x0,0x0(%rbp,%rax,1)
jmp    11ae <copyBeforeChar+0x6e>
lea    0xe5e(%rip),%rdi
lea    0xe66(%rip),%rsi
call   1050 <Error@plt>
mov    %rbp,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [15 x i8] c"copyBeforeChar\00"
@.str.1 = [24 x i8] c"Memory allocation error\00"
define i8* @copyBeforeChar(i8 signext %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = icmp sgt i32 %3, 0
br i1 %4, label %5, label %17
5: ; preds = %2
%6 = zext i32 %3 to i64
br label %7
7: ; preds = %5, %12
%8 = phi i64 [ 0, %5 ], [ %13, %12 ]
%9 = getelementptr inbounds i8, i8* %1, i64 %8
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, %0
br i1 %11, label %15, label %12
12: ; preds = %7
%13 = add nuw nsw i64 %8, 1
%14 = icmp eq i64 %13, %6
br i1 %14, label %17, label %7
15: ; preds = %7
%16 = trunc i64 %8 to i32
br label %17
17: ; preds = %12, %15, %2
%18 = phi i32 [ 0, %2 ], [ %16, %15 ], [ %3, %12 ]
%19 = add nuw nsw i32 %18, 1
%20 = tail call i64 @malloc(i32 %19)
%21 = inttoptr i64 %20 to i8*
%22 = icmp eq i64 %20, 0
br i1 %22, label %27, label %23
23: ; preds = %17
%24 = tail call i32 @strncpy(i8* %21, i8* %1, i32 %18)
%25 = zext i32 %18 to i64
%26 = getelementptr inbounds i8, i8* %21, i64 %25
store i8 0, i8* %26
br label %29
27: ; preds = %17
%28 = tail call i32 @Error(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %27, %23
ret i8* %21
}