#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 atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ get_line(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int strlen(char *);
int main(int argc, char **argv) {
  char in_buffer[1024];
  char *end_ptr;
  char character;
  int num2trim;
  int current_field;
  if (argc < 2) {
    fprintf(stderr, "Usage: %s character [num2trim]\n", argv[0]);
    exit(1);
  }
  character = *argv[1];
  if (argc == 3)
    num2trim = atoi(argv[2]);
  else
    num2trim = 1;
  while (get_line(in_buffer, stdin)) {
    end_ptr = in_buffer + strlen(in_buffer);
    current_field = 1;
    while (end_ptr != in_buffer) {
      if (num2trim == 0) {
        printf("%s\n", in_buffer);
        break;
      }
      if (*end_ptr == character) {
        if (current_field == num2trim) {
          *end_ptr = '\0';
          printf("%s\n", in_buffer);
          break;
        }
        current_field++;
      }
      end_ptr--;
    }
    if (end_ptr == in_buffer)
      printf("%s\n", in_buffer);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11a9 <main+0x49>
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe66(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%al
mov    %al,-0x419(%rbp)
cmpl   $0x3,-0x8(%rbp)
jne    11db <main+0x7b>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x420(%rbp)
jmp    11e5 <main+0x85>
movl   $0x1,-0x420(%rbp)
jmp    11ea <main+0x8a>
lea    -0x410(%rbp),%rdi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
call   1030 <get_line@plt>
cmp    $0x0,%rax
je     1321 <main+0x1c1>
lea    -0x410(%rbp),%rax
mov    %rax,-0x430(%rbp)
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x430(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x418(%rbp)
movl   $0x1,-0x424(%rbp)
mov    -0x418(%rbp),%rax
lea    -0x410(%rbp),%rcx
cmp    %rcx,%rax
je     12f2 <main+0x192>
cmpl   $0x0,-0x420(%rbp)
jne    127f <main+0x11f>
lea    -0x410(%rbp),%rsi
lea    0xdab(%rip),%rdi
call   1050 <printf@plt>
jmp    12f2 <main+0x192>
mov    -0x418(%rbp),%rax
movsbl (%rax),%eax
movsbl -0x419(%rbp),%ecx
cmp    %ecx,%eax
jne    12db <main+0x17b>
mov    -0x424(%rbp),%eax
cmp    -0x420(%rbp),%eax
jne    12cc <main+0x16c>
mov    -0x418(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x410(%rbp),%rsi
lea    0xd5e(%rip),%rdi
call   1050 <printf@plt>
jmp    12f2 <main+0x192>
mov    -0x424(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x424(%rbp)
mov    -0x418(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x418(%rbp)
jmp    1243 <main+0xe3>
mov    -0x418(%rbp),%rax
lea    -0x410(%rbp),%rcx
cmp    %rcx,%rax
jne    131c <main+0x1bc>
lea    -0x410(%rbp),%rsi
lea    0xd09(%rip),%rdi
call   1050 <printf@plt>
jmp    11ea <main+0x8a>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
cmp    $0x1,%edi
jle    1226 <main+0xc6>
mov    0x8(%rsi),%rax
mov    (%rax),%bl
mov    $0x1,%ebp
cmp    $0x3,%edi
jne    1193 <main+0x33>
mov    0x10(%rsi),%rdi
call   1070 <atoi@plt>
mov    %eax,%ebp
mov    0x2e4e(%rip),%r12
mov    (%r12),%esi
mov    %rsp,%rdi
call   1030 <get_line@plt>
test   %rax,%rax
je     1214 <main+0xb4>
mov    %rsp,%r14
lea    0xe6b(%rip),%r15
jmp    11dc <main+0x7c>
movb   $0x0,(%rsp,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
mov    (%r12),%esi
mov    %r14,%rdi
call   1030 <get_line@plt>
test   %rax,%rax
je     1214 <main+0xb4>
mov    %r14,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
je     11c0 <main+0x60>
test   %ebp,%ebp
je     11c0 <main+0x60>
cltq
mov    $0x1,%ecx
jmp    1206 <main+0xa6>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0xffffffffffffffff,%rax
je     11c0 <main+0x60>
cmp    %bl,(%rsp,%rax,1)
jne    1200 <main+0xa0>
cmp    %ebp,%ecx
je     11b7 <main+0x57>
add    $0x1,%ecx
jmp    1200 <main+0xa0>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xdc7(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [32 x i8] c"Usage: %s character [num2trim]\0A\00"
@stdin = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1024 x i8]
%7 = alloca i8*
%8 = alloca i8
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %20
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = load i8, i8* %23
store i8 %24, i8* %8
%25 = load i32, i32* %4
%26 = icmp eq i32 %25, 3
br i1 %26, label %27, label %32
27: ; preds = %20
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 2
%30 = load i8*, i8** %29
%31 = call i32 @atoi(i8* %30)
store i32 %31, i32* %9
br label %33
32: ; preds = %20
store i32 1, i32* %9
br label %33
33: ; preds = %32, %27
br label %34
34: ; preds = %83, %33
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%36 = load i32, i32* @stdin
%37 = call i64 @get_line(i8* %35, i32 %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %84
39: ; preds = %34
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%42 = call i32 @strlen(i8* %41)
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %40, i64 %43
store i8* %44, i8** %7
store i32 1, i32* %10
br label %45
45: ; preds = %73, %39
%46 = load i8*, i8** %7
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%48 = icmp ne i8* %46, %47
br i1 %48, label %49, label %76
49: ; preds = %45
%50 = load i32, i32* %9
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %49
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%54 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %53)
br label %76
55: ; preds = %49
%56 = load i8*, i8** %7
%57 = load i8, i8* %56
%58 = sext i8 %57 to i32
%59 = load i8, i8* %8
%60 = sext i8 %59 to i32
%61 = icmp eq i32 %58, %60
br i1 %61, label %62, label %73
62: ; preds = %55
%63 = load i32, i32* %10
%64 = load i32, i32* %9
%65 = icmp eq i32 %63, %64
br i1 %65, label %66, label %70
66: ; preds = %62
%67 = load i8*, i8** %7
store i8 0, i8* %67
%68 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%69 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %68)
br label %76
70: ; preds = %62
%71 = load i32, i32* %10
%72 = add nsw i32 %71, 1
store i32 %72, i32* %10
br label %73
73: ; preds = %70, %55
%74 = load i8*, i8** %7
%75 = getelementptr inbounds i8, i8* %74, i32 -1
store i8* %75, i8** %7
br label %45
76: ; preds = %66, %52, %45
%77 = load i8*, i8** %7
%78 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%79 = icmp eq i8* %77, %78
br i1 %79, label %80, label %83
80: ; preds = %76
%81 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%82 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %81)
br label %83
83: ; preds = %80, %76
br label %34
84: ; preds = %34
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [32 x i8] c"Usage: %s character [num2trim]\0A\00"
@stdin = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %11
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = load i8, i8* %13
%15 = icmp eq i32 %0, 3
br i1 %15, label %16, label %20
16: ; preds = %11
%17 = getelementptr inbounds i8*, i8** %1, i64 2
%18 = load i8*, i8** %17
%19 = tail call i32 @atoi(i8* %18)
br label %20
20: ; preds = %11, %16
%21 = phi i32 [ %19, %16 ], [ 1, %11 ]
%22 = load i32, i32* @stdin
%23 = call i64 @get_line(i8* %4, i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %53, label %25
25: ; preds = %20
%26 = icmp eq i32 %21, 0
br label %27
27: ; preds = %25, %48
%28 = call i32 @strlen(i8* %4)
%29 = icmp eq i32 %28, 0
%30 = select i1 %29, i1 true, i1 %26
br i1 %30, label %48, label %31
31: ; preds = %27
%32 = sext i32 %28 to i64
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %32
br label %34
34: ; preds = %31, %44
%35 = phi i32 [ %45, %44 ], [ 1, %31 ]
%36 = phi i8* [ %46, %44 ], [ %33, %31 ]
%37 = load i8, i8* %36
%38 = icmp eq i8 %37, %14
br i1 %38, label %39, label %44
39: ; preds = %34
%40 = icmp eq i32 %35, %21
br i1 %40, label %41, label %42
41: ; preds = %39
store i8 0, i8* %36
br label %48
42: ; preds = %39
%43 = add nsw i32 %35, 1
br label %44
44: ; preds = %42, %34
%45 = phi i32 [ %43, %42 ], [ %35, %34 ]
%46 = getelementptr inbounds i8, i8* %36, i64 -1
%47 = icmp eq i8* %46, %4
br i1 %47, label %48, label %34
48: ; preds = %44, %27, %41
%49 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%50 = load i32, i32* @stdin
%51 = call i64 @get_line(i8* %4, i32 %50)
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %27
53: ; preds = %48, %20
ret i32 0
}