#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 *extractline(char *, int *, int *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sed_append(int, int, int, char *);
int /*<<< orphan*/ sed_delete(int, int, int);
int /*<<< orphan*/ sed_insert(int, int, int, char *);
int /*<<< orphan*/ sed_substitute(int, int, int, char *);
void sed(char *option, int fd) {
  int start, end;
  option = extractline(option, &start, &end);
  switch (*option) {
  case 'd':
    sed_delete(fd, start, end);
    return;
  case 's':
    option++;
    sed_substitute(fd, start, end, option);
    return;
  case 'a':
    option++;
    sed_append(fd, start, end, option);
    return;
  case 'i':
    option++;
    sed_insert(fd, start, end, option);
    return;
  default:
    printf("sed: wrong command\n");
    return;
  }
}


x86 assembly (clang, -O0):
<sed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1080 <extractline@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
add    $0xffffff9f,%eax
mov    %eax,%ecx
mov    %rcx,-0x20(%rbp)
sub    $0x12,%eax
ja     122d <sed+0xcd>
mov    -0x20(%rbp),%rax
lea    0xe58(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
call   1030 <sed_delete@plt>
jmp    1239 <sed+0xd9>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    -0x8(%rbp),%rcx
call   1040 <sed_substitute@plt>
jmp    1239 <sed+0xd9>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    -0x8(%rbp),%rcx
call   1070 <sed_append@plt>
jmp    1239 <sed+0xd9>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    -0x8(%rbp),%rcx
call   1060 <sed_insert@plt>
jmp    1239 <sed+0xd9>
lea    0xe18(%rip),%rdi
call   1050 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sed>:
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
lea    0xc(%rsp),%rsi
lea    0x8(%rsp),%rdx
call   1080 <extractline@plt>
movsbl (%rax),%ecx
add    $0xffffff9f,%ecx
cmp    $0x12,%ecx
ja     11a9 <sed+0x49>
lea    0xe78(%rip),%rdx
movslq (%rdx,%rcx,4),%rcx
add    %rdx,%rcx
jmp    *%rcx
add    $0x1,%rax
mov    0xc(%rsp),%esi
mov    0x8(%rsp),%edx
mov    %ebx,%edi
mov    %rax,%rcx
call   1070 <sed_append@plt>
jmp    11f6 <sed+0x96>
lea    0xe9c(%rip),%rdi
call   1030 <puts@plt>
jmp    11f6 <sed+0x96>
mov    0xc(%rsp),%esi
mov    0x8(%rsp),%edx
mov    %ebx,%edi
call   1040 <sed_delete@plt>
jmp    11f6 <sed+0x96>
add    $0x1,%rax
mov    0xc(%rsp),%esi
mov    0x8(%rsp),%edx
mov    %ebx,%edi
mov    %rax,%rcx
call   1060 <sed_insert@plt>
jmp    11f6 <sed+0x96>
add    $0x1,%rax
mov    0xc(%rsp),%esi
mov    0x8(%rsp),%edx
mov    %ebx,%edi
mov    %rax,%rcx
call   1050 <sed_substitute@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [20 x i8] c"sed: wrong command\0A\00"
define void @sed(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i8*, i8** %3
%8 = call i8* @extractline(i8* %7, i32* %5, i32* %6)
store i8* %8, i8** %3
%9 = load i8*, i8** %3
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
switch i32 %11, label %41 [
i32 100, label %12
i32 115, label %17
i32 97, label %25
i32 105, label %33
]
12: ; preds = %2
%13 = load i32, i32* %4
%14 = load i32, i32* %5
%15 = load i32, i32* %6
%16 = call i32 @sed_delete(i32 %13, i32 %14, i32 %15)
br label %43
17: ; preds = %2
%18 = load i8*, i8** %3
%19 = getelementptr inbounds i8, i8* %18, i32 1
store i8* %19, i8** %3
%20 = load i32, i32* %4
%21 = load i32, i32* %5
%22 = load i32, i32* %6
%23 = load i8*, i8** %3
%24 = call i32 @sed_substitute(i32 %20, i32 %21, i32 %22, i8* %23)
br label %43
25: ; preds = %2
%26 = load i8*, i8** %3
%27 = getelementptr inbounds i8, i8* %26, i32 1
store i8* %27, i8** %3
%28 = load i32, i32* %4
%29 = load i32, i32* %5
%30 = load i32, i32* %6
%31 = load i8*, i8** %3
%32 = call i32 @sed_append(i32 %28, i32 %29, i32 %30, i8* %31)
br label %43
33: ; preds = %2
%34 = load i8*, i8** %3
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %3
%36 = load i32, i32* %4
%37 = load i32, i32* %5
%38 = load i32, i32* %6
%39 = load i8*, i8** %3
%40 = call i32 @sed_insert(i32 %36, i32 %37, i32 %38, i8* %39)
br label %43
41: ; preds = %2
%42 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %43
43: ; preds = %41, %33, %25, %17, %12
ret void
}

LLVM IR (-O2):
@str = [19 x i8] c"sed: wrong command\00"
define void @sed(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = call i8* @extractline(i8* %0, i32* %3, i32* %4)
%8 = load i8, i8* %7
%9 = sext i8 %8 to i32
switch i32 %9, label %29 [
i32 100, label %10
i32 115, label %14
i32 97, label %19
i32 105, label %24
]
10: ; preds = %2
%11 = load i32, i32* %3
%12 = load i32, i32* %4
%13 = call i32 @sed_delete(i32 %1, i32 %11, i32 %12)
br label %31
14: ; preds = %2
%15 = getelementptr inbounds i8, i8* %7, i64 1
%16 = load i32, i32* %3
%17 = load i32, i32* %4
%18 = call i32 @sed_substitute(i32 %1, i32 %16, i32 %17, i8* %15)
br label %31
19: ; preds = %2
%20 = getelementptr inbounds i8, i8* %7, i64 1
%21 = load i32, i32* %3
%22 = load i32, i32* %4
%23 = call i32 @sed_append(i32 %1, i32 %21, i32 %22, i8* %20)
br label %31
24: ; preds = %2
%25 = getelementptr inbounds i8, i8* %7, i64 1
%26 = load i32, i32* %3
%27 = load i32, i32* %4
%28 = call i32 @sed_insert(i32 %1, i32 %26, i32 %27, i8* %25)
br label %31
29: ; preds = %2
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %31
31: ; preds = %29, %24, %19, %14, %10
ret void
}