#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 */
struct sbuf {
  int dummy;
};

/* Variables and functions */
int sbuf_append(struct sbuf *, char const *, int);
int sbuf_append_string(struct sbuf *, char const *);
char *strchr(char const *, char);
int ssh_command_append(struct sbuf *s, const char *arg) {
  const char *current = arg, *next;
  while ((next = strchr(current, '\'')) != NULL) {
    if (sbuf_append(s, current, next - current) == -1)
      return -1;
    if (sbuf_append_string(s, "\\'") == -1)
      return -1;
    current = next + 1;
  }
  if (sbuf_append_string(s, current) == -1)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<ssh_command_append>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x27,%esi
call   1030 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     11c9 <ssh_command_append+0x99>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1040 <sbuf_append@plt>
cmp    $0xffffffff,%eax
jne    1193 <ssh_command_append+0x63>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f2 <ssh_command_append+0xc2>
mov    -0x10(%rbp),%rdi
lea    0xe62(%rip),%rsi
call   1050 <sbuf_append_string@plt>
cmp    $0xffffffff,%eax
jne    11b8 <ssh_command_append+0x88>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f2 <ssh_command_append+0xc2>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1148 <ssh_command_append+0x18>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <sbuf_append_string@plt>
cmp    $0xffffffff,%eax
jne    11eb <ssh_command_append+0xbb>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f2 <ssh_command_append+0xc2>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ssh_command_append>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
mov    $0x27,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     11a6 <ssh_command_append+0x76>
mov    %rax,%r12
lea    0xea6(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %r12d,%edx
sub    %ebx,%edx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <sbuf_append@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     11bb <ssh_command_append+0x8b>
mov    %r14,%rdi
mov    %r15,%rsi
call   1050 <sbuf_append_string@plt>
cmp    $0xffffffff,%eax
je     11bb <ssh_command_append+0x8b>
mov    %r12,%rbx
add    $0x1,%rbx
mov    %rbx,%rdi
mov    $0x27,%esi
call   1030 <strchr@plt>
mov    %rax,%r12
test   %rax,%rax
jne    1160 <ssh_command_append+0x30>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <sbuf_append_string@plt>
xor    %ebx,%ebx
cmp    $0xffffffff,%eax
sete   %bl
neg    %ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sbuf = type { i32 }
@.str = [3 x i8] c"\\'\00"
define i32 @ssh_command_append(%struct.sbuf* %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.sbuf*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store %struct.sbuf* %0, %struct.sbuf** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
store i8* %8, i8** %6
br label %9
9: ; preds = %30, %2
%10 = load i8*, i8** %6
%11 = call i8* @strchr(i8* %10, i8 signext 39)
store i8* %11, i8** %7
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %33
13: ; preds = %9
%14 = load %struct.sbuf*, %struct.sbuf** %4
%15 = load i8*, i8** %6
%16 = load i8*, i8** %7
%17 = load i8*, i8** %6
%18 = ptrtoint i8* %16 to i64
%19 = ptrtoint i8* %17 to i64
%20 = sub i64 %18, %19
%21 = trunc i64 %20 to i32
%22 = call i32 @sbuf_append(%struct.sbuf* %14, i8* %15, i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %25
24: ; preds = %13
store i32 -1, i32* %3
br label %40
25: ; preds = %13
%26 = load %struct.sbuf*, %struct.sbuf** %4
%27 = call i32 @sbuf_append_string(%struct.sbuf* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %30
29: ; preds = %25
store i32 -1, i32* %3
br label %40
30: ; preds = %25
%31 = load i8*, i8** %7
%32 = getelementptr inbounds i8, i8* %31, i64 1
store i8* %32, i8** %6
br label %9
33: ; preds = %9
%34 = load %struct.sbuf*, %struct.sbuf** %4
%35 = load i8*, i8** %6
%36 = call i32 @sbuf_append_string(%struct.sbuf* %34, i8* %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %39
38: ; preds = %33
store i32 -1, i32* %3
br label %40
39: ; preds = %33
store i32 0, i32* %3
br label %40
40: ; preds = %39, %38, %29, %24
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
%struct.sbuf = type { i32 }
@.str = [3 x i8] c"\\'\00"
define i32 @ssh_command_append(%struct.sbuf* %0, i8* %1) {
%3 = tail call i8* @strchr(i8* dereferenceable(1) %1, i8 signext 39)
%4 = icmp eq i8* %3, null
br i1 %4, label %21, label %8
5: ; preds = %17
%6 = tail call i8* @strchr(i8* dereferenceable(1) %20, i8 signext 39)
%7 = icmp eq i8* %6, null
br i1 %7, label %21, label %8
8: ; preds = %2, %5
%9 = phi i8* [ %6, %5 ], [ %3, %2 ]
%10 = phi i8* [ %20, %5 ], [ %1, %2 ]
%11 = ptrtoint i8* %9 to i64
%12 = ptrtoint i8* %10 to i64
%13 = sub i64 %11, %12
%14 = trunc i64 %13 to i32
%15 = tail call i32 @sbuf_append(%struct.sbuf* %0, i8* %10, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %26, label %17
17: ; preds = %8
%18 = tail call i32 @sbuf_append_string(%struct.sbuf* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%19 = icmp eq i32 %18, -1
%20 = getelementptr inbounds i8, i8* %9, i64 1
br i1 %19, label %26, label %5
21: ; preds = %5, %2
%22 = phi i8* [ %1, %2 ], [ %20, %5 ]
%23 = tail call i32 @sbuf_append_string(%struct.sbuf* %0, i8* %22)
%24 = icmp eq i32 %23, -1
%25 = sext i1 %24 to i32
br label %26
26: ; preds = %17, %8, %21
%27 = phi i32 [ %25, %21 ], [ -1, %8 ], [ -1, %17 ]
ret i32 %27
}