#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 */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ fatalx(char *, ...);
int /*<<< orphan*/ free(int *);
int kodeitb(int *, int, int);
int strlen(char *);
int string_binary(char *sx) {
  int *aa, len, i, t;
  char c;
  len = strlen(sx);
  if ((aa = (int *)calloc((len), sizeof(int))) == NULL)
    fatalx("Unable to allocate %d unit(s) for item \n", len);
  for (i = 0; i < len; i++) {
    c = sx[i];
    if (c == '0')
      continue;
    if (c != '1')
      fatalx("bad string: %s\n", sx);
    aa[i] = 1;
  }
  t = kodeitb(aa, len, 2);
  free(aa);
  return t;
}


x86 assembly (clang, -O0):
<string_binary>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    $0x4,%esi
call   1060 <calloc@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1194 <string_binary+0x44>
mov    -0x14(%rbp),%esi
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1040 <fatalx@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1203 <string_binary+0xb3>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x1d(%rbp)
movsbl -0x1d(%rbp),%eax
cmp    $0x30,%eax
jne    11c7 <string_binary+0x77>
jmp    11f5 <string_binary+0xa5>
movsbl -0x1d(%rbp),%eax
cmp    $0x31,%eax
je     11e6 <string_binary+0x96>
mov    -0x8(%rbp),%rsi
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1040 <fatalx@plt>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    119b <string_binary+0x4b>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    $0x2,%edx
call   1070 <kodeitb@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_binary>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r13
call   1050 <strlen@plt>
mov    %eax,%r14d
mov    %eax,%edi
mov    $0x4,%esi
call   1060 <calloc@plt>
mov    %rax,%r12
test   %rax,%rax
je     1181 <string_binary+0x31>
test   %r14d,%r14d
jg     1197 <string_binary+0x47>
jmp    11de <string_binary+0x8e>
lea    0xe78(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <fatalx@plt>
test   %r14d,%r14d
jle    11de <string_binary+0x8e>
mov    %r14d,%ebp
lea    0xe88(%rip),%r15
xor    %ebx,%ebx
jmp    11c1 <string_binary+0x71>
cs nopw 0x0(%rax,%rax,1)
nop
movl   $0x1,(%r12,%rbx,4)
add    $0x1,%rbx
cmp    %rbx,%rbp
je     11de <string_binary+0x8e>
movzbl 0x0(%r13,%rbx,1),%eax
cmp    $0x30,%al
je     11b8 <string_binary+0x68>
cmp    $0x31,%al
je     11b0 <string_binary+0x60>
mov    %r15,%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1040 <fatalx@plt>
jmp    11b0 <string_binary+0x60>
mov    %r12,%rdi
mov    %r14d,%esi
mov    $0x2,%edx
call   1070 <kodeitb@plt>
mov    %eax,%ebx
mov    %r12,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [41 x i8] c"Unable to allocate %d unit(s) for item \0A\00"
@.str.1 = [16 x i8] c"bad string: %s\0A\00"
define i32 @string_binary(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = call i32 @strlen(i8* %8)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = call i64 @calloc(i32 %10, i32 4)
%12 = inttoptr i64 %11 to i32*
store i32* %12, i32** %3
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = load i32, i32* %4
%16 = call i32 (i8*, ...) @fatalx(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %15)
br label %17
17: ; preds = %14, %1
store i32 0, i32* %5
br label %18
18: ; preds = %44, %17
%19 = load i32, i32* %5
%20 = load i32, i32* %4
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %47
22: ; preds = %18
%23 = load i8*, i8** %2
%24 = load i32, i32* %5
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i8, i8* %26
store i8 %27, i8* %7
%28 = load i8, i8* %7
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 48
br i1 %30, label %31, label %32
31: ; preds = %22
br label %44
32: ; preds = %22
%33 = load i8, i8* %7
%34 = sext i8 %33 to i32
%35 = icmp ne i32 %34, 49
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = load i8*, i8** %2
%38 = call i32 (i8*, ...) @fatalx(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %37)
br label %39
39: ; preds = %36, %32
%40 = load i32*, i32** %3
%41 = load i32, i32* %5
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i32, i32* %40, i64 %42
store i32 1, i32* %43
br label %44
44: ; preds = %39, %31
%45 = load i32, i32* %5
%46 = add nsw i32 %45, 1
store i32 %46, i32* %5
br label %18
47: ; preds = %18
%48 = load i32*, i32** %3
%49 = load i32, i32* %4
%50 = call i32 @kodeitb(i32* %48, i32 %49, i32 2)
store i32 %50, i32* %6
%51 = load i32*, i32** %3
%52 = call i32 @free(i32* %51)
%53 = load i32, i32* %6
ret i32 %53
}

LLVM IR (-O2):
@.str = [41 x i8] c"Unable to allocate %d unit(s) for item \0A\00"
@.str.1 = [16 x i8] c"bad string: %s\0A\00"
define i32 @string_binary(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = tail call i64 @calloc(i32 %2, i32 4)
%4 = inttoptr i64 %3 to i32*
%5 = icmp eq i64 %3, 0
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 (i8*, ...) @fatalx(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %2)
br label %8
8: ; preds = %6, %1
%9 = icmp sgt i32 %2, 0
br i1 %9, label %10, label %23
10: ; preds = %8
%11 = zext i32 %2 to i64
br label %12
12: ; preds = %10, %20
%13 = phi i64 [ 0, %10 ], [ %21, %20 ]
%14 = getelementptr inbounds i8, i8* %0, i64 %13
%15 = load i8, i8* %14
switch i8 %15, label %16 [
i8 48, label %20
i8 49, label %18
]
16: ; preds = %12
%17 = tail call i32 (i8*, ...) @fatalx(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %18
18: ; preds = %12, %16
%19 = getelementptr inbounds i32, i32* %4, i64 %13
store i32 1, i32* %19
br label %20
20: ; preds = %12, %18
%21 = add nuw nsw i64 %13, 1
%22 = icmp eq i64 %21, %11
br i1 %22, label %23, label %12
23: ; preds = %20, %8
%24 = tail call i32 @kodeitb(i32* %4, i32 %2, i32 2)
%25 = tail call i32 @free(i32* %4)
ret i32 %24
}