#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*/ EX_OSERR;
int /*<<< orphan*/ LOG_EMERG;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ malloc(size_t);
size_t strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
int /*<<< orphan*/ syslog_syserror(int /*<<< orphan*/, char *);
char *string_chomp_copy(char *source) {
  char *dest = NULL;
  size_t i = 0;
  size_t len = 0;
  while (source[i] == ' ' || source[i] == '\t') {
    i++;
  }
  len = strlen(source + i);
  while (len != 0 &&
         (source[i + len - 1] == '\n' || source[i + len - 1] == ' ' ||
          source[i + len - 1] == '\t')) {
    len--;
  }
  if (len == 0) {
    return NULL;
  }
  dest = (char *)malloc(len + 1);
  if (dest == NULL) {
    syslog_syserror(LOG_EMERG, "Unable to allocate memory");
    exit(EX_OSERR);
  }
  strncpy(dest, source + i, len);
  dest[len] = '\0';
  return dest;
}


x86 assembly (clang, -O0):
<string_chomp_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
mov    $0x1,%al
cmp    $0x20,%ecx
mov    %al,-0x29(%rbp)
je     11a3 <string_chomp_copy+0x53>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x9,%eax
sete   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    11b3 <string_chomp_copy+0x63>
jmp    11c4 <string_chomp_copy+0x74>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1174 <string_chomp_copy+0x24>
mov    -0x10(%rbp),%rdi
add    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x28(%rbp)
mov    %al,-0x2a(%rbp)
je     124c <string_chomp_copy+0xfc>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
add    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%ecx
mov    $0x1,%al
cmp    $0xa,%ecx
mov    %al,-0x2b(%rbp)
je     1246 <string_chomp_copy+0xf6>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
add    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%ecx
mov    $0x1,%al
cmp    $0x20,%ecx
mov    %al,-0x2b(%rbp)
je     1246 <string_chomp_copy+0xf6>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
add    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x9,%eax
sete   %al
mov    %al,-0x2b(%rbp)
mov    -0x2b(%rbp),%al
mov    %al,-0x2a(%rbp)
mov    -0x2a(%rbp),%al
test   $0x1,%al
jne    125c <string_chomp_copy+0x10c>
jmp    126d <string_chomp_copy+0x11d>
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
jmp    11d5 <string_chomp_copy+0x85>
cmpq   $0x0,-0x28(%rbp)
jne    1285 <string_chomp_copy+0x135>
movq   $0x0,-0x8(%rbp)
jmp    12ed <string_chomp_copy+0x19d>
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    12c4 <string_chomp_copy+0x174>
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
lea    0xd4f(%rip),%rsi
call   1060 <syslog_syserror@plt>
mov    0x2d1b(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_chomp_copy>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
jmp    1164 <string_chomp_copy+0x14>
nopw   0x0(%rax,%rax,1)
add    $0x1,%rbx
movzbl (%rbx),%eax
cmp    $0x20,%al
je     1160 <string_chomp_copy+0x10>
cmp    $0x9,%al
je     1160 <string_chomp_copy+0x10>
mov    %rbx,%rdi
call   1040 <strlen@plt>
test   %rax,%rax
je     11aa <string_chomp_copy+0x5a>
mov    %rax,%r14
movabs $0x100000600,%rax
xor    %r15d,%r15d
nopl   0x0(%rax)
movzbl -0x1(%rbx,%r14,1),%ecx
cmp    $0x20,%rcx
ja     11af <string_chomp_copy+0x5f>
bt     %rcx,%rax
jae    11af <string_chomp_copy+0x5f>
add    $0xffffffffffffffff,%r14
jne    1190 <string_chomp_copy+0x40>
jmp    11d3 <string_chomp_copy+0x83>
xor    %r15d,%r15d
jmp    11d3 <string_chomp_copy+0x83>
lea    0x1(%r14),%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     11dc <string_chomp_copy+0x8c>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <strncpy@plt>
movb   $0x0,(%r15,%r14,1)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
lea    0xe14(%rip),%rsi
call   1060 <syslog_syserror@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@LOG_EMERG = global i32 0
@.str = [26 x i8] c"Unable to allocate memory\00"
@EX_OSERR = global i32 0
define i8* @string_chomp_copy(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
store i8* %0, i8** %3
store i8* null, i8** %4
store i64 0, i64* %5
store i64 0, i64* %6
br label %7
7: ; preds = %23, %1
%8 = load i8*, i8** %3
%9 = load i64, i64* %5
%10 = getelementptr inbounds i8, i8* %8, i64 %9
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
%13 = icmp eq i32 %12, 32
br i1 %13, label %21, label %14
14: ; preds = %7
%15 = load i8*, i8** %3
%16 = load i64, i64* %5
%17 = getelementptr inbounds i8, i8* %15, i64 %16
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 9
br label %21
21: ; preds = %14, %7
%22 = phi i1 [ true, %7 ], [ %20, %14 ]
br i1 %22, label %23, label %26
23: ; preds = %21
%24 = load i64, i64* %5
%25 = add i64 %24, 1
store i64 %25, i64* %5
br label %7
26: ; preds = %21
%27 = load i8*, i8** %3
%28 = load i64, i64* %5
%29 = getelementptr inbounds i8, i8* %27, i64 %28
%30 = call i64 @strlen(i8* %29)
store i64 %30, i64* %6
br label %31
31: ; preds = %68, %26
%32 = load i64, i64* %6
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %66
34: ; preds = %31
%35 = load i8*, i8** %3
%36 = load i64, i64* %5
%37 = load i64, i64* %6
%38 = add i64 %36, %37
%39 = sub i64 %38, 1
%40 = getelementptr inbounds i8, i8* %35, i64 %39
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = icmp eq i32 %42, 10
br i1 %43, label %64, label %44
44: ; preds = %34
%45 = load i8*, i8** %3
%46 = load i64, i64* %5
%47 = load i64, i64* %6
%48 = add i64 %46, %47
%49 = sub i64 %48, 1
%50 = getelementptr inbounds i8, i8* %45, i64 %49
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = icmp eq i32 %52, 32
br i1 %53, label %64, label %54
54: ; preds = %44
%55 = load i8*, i8** %3
%56 = load i64, i64* %5
%57 = load i64, i64* %6
%58 = add i64 %56, %57
%59 = sub i64 %58, 1
%60 = getelementptr inbounds i8, i8* %55, i64 %59
%61 = load i8, i8* %60
%62 = sext i8 %61 to i32
%63 = icmp eq i32 %62, 9
br label %64
64: ; preds = %54, %44, %34
%65 = phi i1 [ true, %44 ], [ true, %34 ], [ %63, %54 ]
br label %66
66: ; preds = %64, %31
%67 = phi i1 [ false, %31 ], [ %65, %64 ]
br i1 %67, label %68, label %71
68: ; preds = %66
%69 = load i64, i64* %6
%70 = add i64 %69, -1
store i64 %70, i64* %6
br label %31
71: ; preds = %66
%72 = load i64, i64* %6
%73 = icmp eq i64 %72, 0
br i1 %73, label %74, label %75
74: ; preds = %71
store i8* null, i8** %2
br label %98
75: ; preds = %71
%76 = load i64, i64* %6
%77 = add i64 %76, 1
%78 = call i64 @malloc(i64 %77)
%79 = inttoptr i64 %78 to i8*
store i8* %79, i8** %4
%80 = load i8*, i8** %4
%81 = icmp eq i8* %80, null
br i1 %81, label %82, label %87
82: ; preds = %75
%83 = load i32, i32* @LOG_EMERG
%84 = call i32 @syslog_syserror(i32 %83, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%85 = load i32, i32* @EX_OSERR
%86 = call i32 @exit(i32 %85)
unreachable
87: ; preds = %75
%88 = load i8*, i8** %4
%89 = load i8*, i8** %3
%90 = load i64, i64* %5
%91 = getelementptr inbounds i8, i8* %89, i64 %90
%92 = load i64, i64* %6
%93 = call i32 @strncpy(i8* %88, i8* %91, i64 %92)
%94 = load i8*, i8** %4
%95 = load i64, i64* %6
%96 = getelementptr inbounds i8, i8* %94, i64 %95
store i8 0, i8* %96
%97 = load i8*, i8** %4
store i8* %97, i8** %2
br label %98
98: ; preds = %87, %74
%99 = load i8*, i8** %2
ret i8* %99
}

LLVM IR (-O2):
@LOG_EMERG = global i32 0
@.str = [26 x i8] c"Unable to allocate memory\00"
@EX_OSERR = global i32 0
define i8* @string_chomp_copy(i8* %0) {
br label %2
2: ; preds = %6, %1
%3 = phi i64 [ 0, %1 ], [ %7, %6 ]
%4 = getelementptr inbounds i8, i8* %0, i64 %3
%5 = load i8, i8* %4
switch i8 %5, label %8 [
i8 32, label %6
i8 9, label %6
]
6: ; preds = %2, %2
%7 = add i64 %3, 1
br label %2
8: ; preds = %2
%9 = getelementptr inbounds i8, i8* %0, i64 %3
%10 = tail call i64 @strlen(i8* %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %33, label %12
12: ; preds = %8, %18
%13 = phi i64 [ %14, %18 ], [ %10, %8 ]
%14 = add i64 %13, -1
%15 = add i64 %14, %3
%16 = getelementptr inbounds i8, i8* %0, i64 %15
%17 = load i8, i8* %16
switch i8 %17, label %20 [
i8 10, label %18
i8 32, label %18
i8 9, label %18
]
18: ; preds = %12, %12, %12
%19 = icmp eq i64 %14, 0
br i1 %19, label %33, label %12
20: ; preds = %12
%21 = add i64 %13, 1
%22 = tail call i64 @malloc(i64 %21)
%23 = inttoptr i64 %22 to i8*
%24 = icmp eq i64 %22, 0
br i1 %24, label %25, label %30
25: ; preds = %20
%26 = load i32, i32* @LOG_EMERG
%27 = tail call i32 @syslog_syserror(i32 %26, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%28 = load i32, i32* @EX_OSERR
%29 = tail call i32 @exit(i32 %28)
unreachable
30: ; preds = %20
%31 = tail call i32 @strncpy(i8* %23, i8* %9, i64 %13)
%32 = getelementptr inbounds i8, i8* %23, i64 %13
store i8 0, i8* %32
br label %33
33: ; preds = %18, %8, %30
%34 = phi i8* [ %23, %30 ], [ null, %8 ], [ null, %18 ]
ret i8* %34
}