#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*/ fprintf(int /*<<< orphan*/, char *, int, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int Sfile_str(char target[4096], char *source, int flag) {
  int l;
  l = strlen(source);
  if (flag & 1)
    l += strlen(target);
  if (l >= 4096) {
    fprintf(stderr, "--- Path string overflow (%d > %d). Malicious input ?\n",
            l, 4096 - 1);
    return (0);
  }
  if (flag & 1)
    strcat(target, source);
  else
    strcpy(target, source);
  return (1);
}


x86 assembly (clang, -O0):
<Sfile_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     117d <Sfile_str+0x3d>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x1000,-0x20(%rbp)
jl     11b3 <Sfile_str+0x73>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
lea    0xe63(%rip),%rsi
mov    $0xfff,%ecx
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11e8 <Sfile_str+0xa8>
mov    -0x1c(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     11d4 <Sfile_str+0x94>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strcat@plt>
jmp    11e1 <Sfile_str+0xa1>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Sfile_str>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
test   $0x1,%bl
jne    1173 <Sfile_str+0x33>
cmp    $0xfff,%ebp
jg     1185 <Sfile_str+0x45>
mov    %r14,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
jmp    11b0 <Sfile_str+0x70>
mov    %r14,%rdi
call   1040 <strlen@plt>
add    %eax,%ebp
cmp    $0x1000,%ebp
jl     11a5 <Sfile_str+0x65>
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
lea    0xe6b(%rip),%rsi
mov    %ebp,%edx
mov    $0xfff,%ecx
call   1050 <fprintf@plt>
xor    %eax,%eax
jmp    11b5 <Sfile_str+0x75>
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <strcat@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [55 x i8] c"--- Path string overflow (%d > %d). Malicious input ?\0A\00"
define i32 @Sfile_str(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i8*, i8** %6
%10 = call i32 @strlen(i8* %9)
store i32 %10, i32* %8
%11 = load i32, i32* %7
%12 = and i32 %11, 1
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %3
%15 = load i8*, i8** %5
%16 = call i32 @strlen(i8* %15)
%17 = load i32, i32* %8
%18 = add nsw i32 %17, %16
store i32 %18, i32* %8
br label %19
19: ; preds = %14, %3
%20 = load i32, i32* %8
%21 = icmp sge i32 %20, 4096
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = load i32, i32* @stderr
%24 = load i32, i32* %8
%25 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0), i32 %24, i32 4095)
store i32 0, i32* %4
br label %39
26: ; preds = %19
%27 = load i32, i32* %7
%28 = and i32 %27, 1
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %26
%31 = load i8*, i8** %5
%32 = load i8*, i8** %6
%33 = call i32 @strcat(i8* %31, i8* %32)
br label %38
34: ; preds = %26
%35 = load i8*, i8** %5
%36 = load i8*, i8** %6
%37 = call i32 @strcpy(i8* %35, i8* %36)
br label %38
38: ; preds = %34, %30
store i32 1, i32* %4
br label %39
39: ; preds = %38, %22
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [55 x i8] c"--- Path string overflow (%d > %d). Malicious input ?\0A\00"
define i32 @Sfile_str(i8* %0, i8* %1, i32 %2) {
%4 = tail call i32 @strlen(i8* %1)
%5 = and i32 %2, 1
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = icmp sgt i32 %4, 4095
br i1 %8, label %13, label %19
9: ; preds = %3
%10 = tail call i32 @strlen(i8* %0)
%11 = add nsw i32 %10, %4
%12 = icmp sgt i32 %11, 4095
br i1 %12, label %13, label %17
13: ; preds = %9, %7
%14 = phi i32 [ %11, %9 ], [ %4, %7 ]
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0), i32 %14, i32 4095)
br label %21
17: ; preds = %9
%18 = tail call i32 @strcat(i8* %0, i8* %1)
br label %21
19: ; preds = %7
%20 = tail call i32 @strcpy(i8* %0, i8* %1)
br label %21
21: ; preds = %17, %19, %13
%22 = phi i32 [ 0, %13 ], [ 1, %19 ], [ 1, %17 ]
ret i32 %22
}