#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EXIT_FAILURE;
int atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fwrite(char *, int, int /*<<< orphan*/,
                          int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
char *saferead(int /*<<< orphan*/ *);
int /*<<< orphan*/ *stderr;
int /*<<< orphan*/ *stdin;
int /*<<< orphan*/ strlen(char *);
char *strtok(char *, char *);
int main(int argc, char *argv[]) {
  FILE *f1, *f2;
  char *str1, *str2, *tok;
  int line = 1;
  int target;
  if (argc < 3) {
    printf("Usage: %s dest lineno [src]\n", argv[0]);
    exit(1);
  }
  f1 = fopen(argv[1], "r");
  if (f1 == NULL) {
    fprintf(stderr, "Could not read %s.\n", argv[1]);
    exit(EXIT_FAILURE);
  }
  target = atoi(argv[2]);
  if (argc > 3) {
    f2 = fopen(argv[3], "r");
    if (f2 == NULL) {
      fprintf(stderr, "Could not read %s.\n", argv[3]);
      exit(EXIT_FAILURE);
    }
  } else
    f2 = stdin;
  str1 = saferead(f1);
  str2 = saferead(f2);
  fclose(f1);
  f1 = fopen(argv[1], "w");
  tok = strtok(str1, "\n");
  while (tok) {
    if (line == target) {
      fwrite(str2, sizeof(char), strlen(str2), f1);
    }
    fprintf(f1, "%s\n", tok);
    line++;
    tok = strtok(NULL, "\n");
  }
  free(str1);
  free(str2);
  fclose(f1);
  if (f2 != stdin)
    fclose(f2);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x3c(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11f4 <main+0x44>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe1b(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe1a(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1243 <main+0x93>
mov    0x2dba(%rip),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdef(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x3,-0x8(%rbp)
jle    12b1 <main+0x101>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
lea    0xdb1(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12ac <main+0xfc>
mov    0x2d51(%rip),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdx
lea    0xd86(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d2b(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
jmp    12bf <main+0x10f>
mov    0x2d30(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <saferead@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <saferead@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xd42(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xd2c(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1380 <main+0x1d0>
mov    -0x3c(%rbp),%eax
cmp    -0x40(%rbp),%eax
jne    134a <main+0x19a>
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   10d0 <fwrite@plt>
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rdx
lea    0xcdc(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xcbf(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x38(%rbp)
jmp    130e <main+0x15e>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    0x2c42(%rip),%rcx
cmp    (%rcx),%rax
je     13b8 <main+0x208>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r13
cmp    $0x2,%edi
jle    1323 <main+0x173>
mov    %edi,%ebp
mov    0x8(%r13),%rdi
lea    0xe46(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     133d <main+0x18d>
mov    %rax,%rbx
mov    0x10(%r13),%rdi
call   10b0 <atoi@plt>
mov    %eax,%r14d
cmp    $0x4,%ebp
jb     1224 <main+0x74>
mov    0x18(%r13),%rdi
lea    0xe19(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    122e <main+0x7e>
mov    0x2dc0(%rip),%rax
mov    (%rax),%rdi
mov    0x18(%r13),%rdx
jmp    134b <main+0x19b>
mov    0x2dbd(%rip),%rax
mov    (%rax),%rbp
mov    %rbx,%rdi
call   1080 <saferead@plt>
mov    %rax,%r15
mov    %rbp,0x10(%rsp)
mov    %rbp,%rdi
call   1080 <saferead@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x8(%r13),%rdi
lea    0xdd7(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%rbx
lea    0xdc6(%rip),%rsi
mov    %r15,0x8(%rsp)
mov    %r15,%rdi
call   10a0 <strtok@plt>
test   %rax,%rax
je     12e2 <main+0x132>
mov    %rax,%rbp
add    $0xffffffff,%r14d
lea    0xdaa(%rip),%r13
lea    0xd9f(%rip),%r15
jmp    12c0 <main+0x110>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1070 <fprintf@plt>
xor    %edi,%edi
mov    %r15,%rsi
call   10a0 <strtok@plt>
mov    %rax,%rbp
test   %rax,%rax
je     12e2 <main+0x132>
sub    $0x1,%r14d
jae    12a0 <main+0xf0>
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %r12,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %rbx,%rcx
call   10d0 <fwrite@plt>
jmp    12a0 <main+0xf0>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x2ce5(%rip),%rax
mov    0x10(%rsp),%rdi
cmp    (%rax),%rdi
je     1312 <main+0x162>
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x0(%r13),%rsi
lea    0xcd2(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2c94(%rip),%rax
mov    (%rax),%rdi
mov    0x8(%r13),%rdx
lea    0xccd(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2c72(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [29 x i8] c"Usage: %s dest lineno [src]\0A\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32* null
@.str.2 = [20 x i8] c"Could not read %s.\0A\00"
@EXIT_FAILURE = global i32 0
@stdin = global i32* null
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [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 i32*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %11
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 3
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %25, i32** %6
%26 = load i32*, i32** %6
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %36
28: ; preds = %21
%29 = load i32*, i32** @stderr
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = call i32 @fprintf(i32* %29, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %32)
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %21
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 2
%39 = load i8*, i8** %38
%40 = call i32 @atoi(i8* %39)
store i32 %40, i32* %12
%41 = load i32, i32* %4
%42 = icmp sgt i32 %41, 3
br i1 %42, label %43, label %59
43: ; preds = %36
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 3
%46 = load i8*, i8** %45
%47 = call i32* @fopen(i8* %46, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %47, i32** %7
%48 = load i32*, i32** %7
%49 = icmp eq i32* %48, null
br i1 %49, label %50, label %58
50: ; preds = %43
%51 = load i32*, i32** @stderr
%52 = load i8**, i8*** %5
%53 = getelementptr inbounds i8*, i8** %52, i64 3
%54 = load i8*, i8** %53
%55 = call i32 @fprintf(i32* %51, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %54)
%56 = load i32, i32* @EXIT_FAILURE
%57 = call i32 @exit(i32 %56)
unreachable
58: ; preds = %43
br label %61
59: ; preds = %36
%60 = load i32*, i32** @stdin
store i32* %60, i32** %7
br label %61
61: ; preds = %59, %58
%62 = load i32*, i32** %6
%63 = call i8* @saferead(i32* %62)
store i8* %63, i8** %8
%64 = load i32*, i32** %7
%65 = call i8* @saferead(i32* %64)
store i8* %65, i8** %9
%66 = load i32*, i32** %6
%67 = call i32 @fclose(i32* %66)
%68 = load i8**, i8*** %5
%69 = getelementptr inbounds i8*, i8** %68, i64 1
%70 = load i8*, i8** %69
%71 = call i32* @fopen(i8* %70, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %71, i32** %6
%72 = load i8*, i8** %8
%73 = call i8* @strtok(i8* %72, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i8* %73, i8** %10
br label %74
74: ; preds = %87, %61
%75 = load i8*, i8** %10
%76 = icmp ne i8* %75, null
br i1 %76, label %77, label %94
77: ; preds = %74
%78 = load i32, i32* %11
%79 = load i32, i32* %12
%80 = icmp eq i32 %78, %79
br i1 %80, label %81, label %87
81: ; preds = %77
%82 = load i8*, i8** %9
%83 = load i8*, i8** %9
%84 = call i32 @strlen(i8* %83)
%85 = load i32*, i32** %6
%86 = call i32 @fwrite(i8* %82, i32 1, i32 %84, i32* %85)
br label %87
87: ; preds = %81, %77
%88 = load i32*, i32** %6
%89 = load i8*, i8** %10
%90 = call i32 @fprintf(i32* %88, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %89)
%91 = load i32, i32* %11
%92 = add nsw i32 %91, 1
store i32 %92, i32* %11
%93 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i8* %93, i8** %10
br label %74
94: ; preds = %74
%95 = load i8*, i8** %8
%96 = call i32 @free(i8* %95)
%97 = load i8*, i8** %9
%98 = call i32 @free(i8* %97)
%99 = load i32*, i32** %6
%100 = call i32 @fclose(i32* %99)
%101 = load i32*, i32** %7
%102 = load i32*, i32** @stdin
%103 = icmp ne i32* %101, %102
br i1 %103, label %104, label %107
104: ; preds = %94
%105 = load i32*, i32** %7
%106 = call i32 @fclose(i32* %105)
br label %107
107: ; preds = %104, %94
ret i32 0
}

LLVM IR (-O2):
@.str = [29 x i8] c"Usage: %s dest lineno [src]\0A\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32* null
@.str.2 = [20 x i8] c"Could not read %s.\0A\00"
@EXIT_FAILURE = global i32 0
@stdin = global i32* null
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %5)
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %19
13: ; preds = %8
%14 = load i32*, i32** @stderr
%15 = load i8*, i8** %9
%16 = tail call i32 @fprintf(i32* %14, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %15)
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %8
%20 = getelementptr inbounds i8*, i8** %1, i64 2
%21 = load i8*, i8** %20
%22 = tail call i32 @atoi(i8* %21)
%23 = icmp ugt i32 %0, 3
br i1 %23, label %24, label %35
24: ; preds = %19
%25 = getelementptr inbounds i8*, i8** %1, i64 3
%26 = load i8*, i8** %25
%27 = tail call i32* @fopen(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %37
29: ; preds = %24
%30 = load i32*, i32** @stderr
%31 = load i8*, i8** %25
%32 = tail call i32 @fprintf(i32* %30, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %31)
%33 = load i32, i32* @EXIT_FAILURE
%34 = tail call i32 @exit(i32 %33)
unreachable
35: ; preds = %19
%36 = load i32*, i32** @stdin
br label %37
37: ; preds = %24, %35
%38 = phi i32* [ %27, %24 ], [ %36, %35 ]
%39 = tail call i8* @saferead(i32* %11)
%40 = tail call i8* @saferead(i32* %38)
%41 = tail call i32 @fclose(i32* %11)
%42 = load i8*, i8** %9
%43 = tail call i32* @fopen(i8* %42, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%44 = tail call i8* @strtok(i8* %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%45 = icmp eq i8* %44, null
br i1 %45, label %58, label %46
46: ; preds = %37, %53
%47 = phi i32 [ %55, %53 ], [ 1, %37 ]
%48 = phi i8* [ %56, %53 ], [ %44, %37 ]
%49 = icmp eq i32 %47, %22
br i1 %49, label %50, label %53
50: ; preds = %46
%51 = tail call i32 @strlen(i8* %40)
%52 = tail call i32 @fwrite(i8* %40, i32 1, i32 %51, i32* %43)
br label %53
53: ; preds = %50, %46
%54 = tail call i32 @fprintf(i32* %43, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %48)
%55 = add nuw nsw i32 %47, 1
%56 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%57 = icmp eq i8* %56, null
br i1 %57, label %58, label %46
58: ; preds = %53, %37
%59 = tail call i32 @free(i8* %39)
%60 = tail call i32 @free(i8* %40)
%61 = tail call i32 @fclose(i32* %43)
%62 = load i32*, i32** @stdin
%63 = icmp eq i32* %38, %62
br i1 %63, label %66, label %64
64: ; preds = %58
%65 = tail call i32 @fclose(i32* %38)
br label %66
66: ; preds = %64, %58
ret i32 0
}