#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *malloc(int);
int /*<<< orphan*/ stderr;
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *construct_pat(int argc, char *argv[]) {
  char *res = malloc(200 * sizeof(char));
  if (res == NULL) {
    fprintf(stderr, "unable to malloc buffer for pat\n");
    exit(1);
  };
  if (argc == 3) {
    return (argv[2]);
  } else {
    char *pos = res;
    int str_size;
    int total_size = 0;
    char *tmp;
    tmp = argv[2];
    for (int i = 2; i < argc - 1; i++) {
      argv[i] = argv[i + 1];
    };
    argv[argc - 1] = tmp;
    for (int i = 2; i < argc; i++) {
      if (i > 2) {
        *pos = '/';
        pos++;
      };
      str_size = strlen(argv[i]);
      total_size = total_size + str_size + 1;
      if (total_size > 200) {
        fprintf(stderr, "pattern too long\n");
        exit(1);
      };
      strncpy(pos, argv[i], str_size);
      pos += str_size;
    };
    *pos = '\0';
    return (res);
  };
}


x86 assembly (clang, -O0):
<construct_pat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0xc8,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1197 <construct_pat+0x47>
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
lea    0xe78(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpl   $0x3,-0xc(%rbp)
jne    11b2 <construct_pat+0x62>
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    12e7 <construct_pat+0x197>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x38(%rbp)
movl   $0x2,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1210 <construct_pat+0xc0>
mov    -0x18(%rbp),%rax
mov    -0x3c(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdx
mov    -0x18(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    11d4 <construct_pat+0x84>
mov    -0x38(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
movl   $0x2,-0x40(%rbp)
mov    -0x40(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    12d8 <construct_pat+0x188>
cmpl   $0x2,-0x40(%rbp)
jle    1255 <construct_pat+0x105>
mov    -0x28(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x40(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x30(%rbp),%eax
add    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
cmpl   $0xc8,-0x30(%rbp)
jle    12a1 <construct_pat+0x151>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
lea    0xd8f(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rax
movslq -0x40(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
mov    -0x2c(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x2c(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    122c <construct_pat+0xdc>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<construct_pat>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%r12d
mov    $0xc8,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1282 <construct_pat+0x122>
mov    0x10(%r15),%rbp
cmp    $0x3,%r12d
jne    1196 <construct_pat+0x36>
mov    %rbp,%rax
jmp    1254 <construct_pat+0xf4>
lea    -0x1(%r12),%ebx
cmp    $0x3,%r12d
mov    %rax,(%rsp)
jle    1246 <construct_pat+0xe6>
lea    0x10(%r15),%r14
lea    0x18(%r15),%rsi
lea    -0x3(%r12),%edx
shl    $0x3,%rdx
mov    %r14,%rdi
call   1070 <memmove@plt>
mov    %ebx,%eax
mov    %rbp,(%r15,%rax,8)
mov    0x10(%r15),%rdi
call   1040 <strlen@plt>
cmp    $0xc7,%eax
jg     1263 <construct_pat+0x103>
mov    %eax,%r13d
mov    %r12d,%ebp
mov    (%r14),%rsi
mov    (%rsp),%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   1030 <strncpy@plt>
movslq %r13d,%rax
add    %rax,%rbx
mov    $0x3,%r14d
nop
movb   $0x2f,(%rbx)
mov    (%r15,%r14,8),%rdi
call   1040 <strlen@plt>
mov    %eax,%r12d
add    %r12d,%r13d
add    $0x1,%r13d
cmp    $0xc8,%r13d
jge    1263 <construct_pat+0x103>
add    $0x1,%rbx
mov    (%r15,%r14,8),%rsi
mov    %rbx,%rdi
mov    %r12d,%edx
call   1030 <strncpy@plt>
movslq %r12d,%rax
add    %rbx,%rax
add    $0x1,%r14
mov    %rax,%rbx
cmp    %r14,%rbp
jne    1200 <construct_pat+0xa0>
jmp    124d <construct_pat+0xed>
movslq %ebx,%rcx
mov    %rbp,(%r15,%rcx,8)
movb   $0x0,(%rax)
mov    (%rsp),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
lea    0xdae(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
lea    0xd6e(%rip),%rsi
jmp    1273 <construct_pat+0x113>

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

LLVM IR (-O2):
@stderr = global i32 0
@.str = [33 x i8] c"unable to malloc buffer for pat\0A\00"
@.str.1 = [18 x i8] c"pattern too long\0A\00"
define i8* @construct_pat(i32 %0, i8** nocapture %1) {
%3 = tail call dereferenceable_or_null(200) i8* @malloc(i32 200)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = icmp eq i32 %0, 3
%11 = getelementptr i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
br i1 %10, label %61, label %13
13: ; preds = %9
%14 = add nsw i32 %0, -1
%15 = icmp sgt i32 %0, 3
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = sext i32 %14 to i64
%18 = getelementptr inbounds i8*, i8** %1, i64 %17
store i8* %12, i8** %18
br label %37
19: ; preds = %13
%20 = bitcast i8** %11 to i8*
%21 = getelementptr i8*, i8** %1, i64 3
%22 = bitcast i8** %21 to i8*
%23 = add nsw i32 %0, -3
%24 = zext i32 %23 to i64
%25 = shl nuw nsw i64 %24, 3
%26 = zext i32 %14 to i64
%27 = getelementptr inbounds i8*, i8** %1, i64 %26
store i8* %12, i8** %27
%28 = zext i32 %0 to i64
%29 = load i8*, i8** %11
%30 = tail call i32 @strlen(i8* %29)
%31 = icmp sgt i32 %30, 199
br i1 %31, label %50, label %32
32: ; preds = %19
%33 = load i8*, i8** %11
%34 = tail call i32 @strncpy(i8* %3, i8* %33, i32 %30)
%35 = sext i32 %30 to i64
%36 = getelementptr inbounds i8, i8* %3, i64 %35
br label %39
37: ; preds = %54, %16
%38 = phi i8* [ %3, %16 ], [ %58, %54 ]
store i8 0, i8* %38
br label %61
39: ; preds = %32, %54
%40 = phi i64 [ 3, %32 ], [ %59, %54 ]
%41 = phi i32 [ %30, %32 ], [ %48, %54 ]
%42 = phi i8* [ %36, %32 ], [ %58, %54 ]
%43 = add nsw i32 %41, 1
store i8 47, i8* %42
%44 = getelementptr inbounds i8, i8* %42, i64 1
%45 = getelementptr inbounds i8*, i8** %1, i64 %40
%46 = load i8*, i8** %45
%47 = tail call i32 @strlen(i8* %46)
%48 = add nsw i32 %47, %43
%49 = icmp sgt i32 %48, 199
br i1 %49, label %50, label %54
50: ; preds = %39, %19
%51 = load i32, i32* @stderr
%52 = tail call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%53 = tail call i32 @exit(i32 1)
unreachable
54: ; preds = %39
%55 = load i8*, i8** %45
%56 = tail call i32 @strncpy(i8* %44, i8* %55, i32 %47)
%57 = sext i32 %47 to i64
%58 = getelementptr inbounds i8, i8* %44, i64 %57
%59 = add nuw nsw i64 %40, 1
%60 = icmp eq i64 %59, %28
br i1 %60, label %37, label %39
61: ; preds = %9, %37
%62 = phi i8* [ %3, %37 ], [ %12, %9 ]
ret i8* %62
}