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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int PATH_MAX;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int, ...);
scalar_t__ read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int main(int argc, char *argv[]) {
  int tot, j;
  ssize_t nread;
  char buf[1024];
  FILE *fp;
  char cwd[PATH_MAX];
  snprintf(cwd, PATH_MAX, "/proc/%s/cwd", argv[1]);
  chdir(cwd);
  fp = fopen("core.info", "w+");
  if (fp == NULL)
    exit(EXIT_FAILURE);
  fprintf(fp, "argc=%d\n", argc);
  for (j = 0; j < argc; j++)
    fprintf(fp, "argc[%d]=<%s>\n", j, argv[j]);
  tot = 0;
  while ((nread = read(STDIN_FILENO, buf, 1024)) > 0)
    tot += nread;
  fprintf(fp, "Total bytes in core dump: %d\n", tot);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e70(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x430(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x440(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x438(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
lea    0xe3a(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x440(%rbp),%rdi
call   1030 <chdir@plt>
lea    0xe2f(%rip),%rdi
lea    0xe32(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    120d <main+0xad>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x428(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xdfc(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    126a <main+0x10a>
mov    -0x428(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,8),%rcx
lea    0xdce(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    122c <main+0xcc>
movl   $0x0,-0x14(%rbp)
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jle    12ac <main+0x14c>
mov    -0x20(%rbp),%rcx
movslq -0x14(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x14(%rbp)
jmp    1271 <main+0x111>
mov    -0x428(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xd75(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d05(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%r14
mov    %edi,%r12d
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x8(%r14),%rcx
lea    0xe5e(%rip),%rdx
mov    %rbx,%rdi
call   1040 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <chdir@plt>
lea    0xe54(%rip),%rdi
lea    0xe57(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
jne    11d8 <main+0x78>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xe3b(%rip),%rsi
xor    %r13d,%r13d
mov    %rax,%r15
mov    %rax,%rdi
mov    %r12d,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
test   %r12d,%r12d
jle    1230 <main+0xd0>
mov    %r12d,%eax
mov    %r14,%r12
mov    %eax,%r14d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r12,%rbx,8),%rcx
mov    %r15,%rdi
lea    0xe05(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1210 <main+0xb0>
mov    %r15,%r12
mov    0x2d8e(%rip),%r15
lea    -0x430(%rbp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r13d,%ebx
mov    (%r15),%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
lea    (%rbx,%rax,1),%r13d
test   %rax,%rax
jg     1250 <main+0xf0>
lea    0xdbf(%rip),%rsi
mov    %r12,%rdi
mov    %ebx,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [13 x i8] c"/proc/%s/cwd\00"
@.str.1 = [10 x i8] c"core.info\00"
@.str.2 = [3 x i8] c"w+\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [9 x i8] c"argc=%d\0A\00"
@.str.4 = [15 x i8] c"argc[%d]=<%s>\0A\00"
@STDIN_FILENO = global i32 0
@.str.5 = [30 x i8] c"Total bytes in core dump: %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca [1024 x i8]
%10 = alloca i32*
%11 = alloca i8*
%12 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* @PATH_MAX
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %11
%16 = alloca i8, i64 %14
store i64 %14, i64* %12
%17 = load i32, i32* @PATH_MAX
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @snprintf(i8* %16, i32 %17, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = call i32 @chdir(i8* %16)
%23 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %23, i32** %10
%24 = load i32*, i32** %10
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %29
26: ; preds = %2
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %2
%30 = load i32*, i32** %10
%31 = load i32, i32* %4
%32 = call i32 (i32*, i8*, i32, ...) @fprintf(i32* %30, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %31)
store i32 0, i32* %7
br label %33
33: ; preds = %46, %29
%34 = load i32, i32* %7
%35 = load i32, i32* %4
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %49
37: ; preds = %33
%38 = load i32*, i32** %10
%39 = load i32, i32* %7
%40 = load i8**, i8*** %5
%41 = load i32, i32* %7
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8*, i8** %40, i64 %42
%44 = load i8*, i8** %43
%45 = call i32 (i32*, i8*, i32, ...) @fprintf(i32* %38, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %39, i8* %44)
br label %46
46: ; preds = %37
%47 = load i32, i32* %7
%48 = add nsw i32 %47, 1
store i32 %48, i32* %7
br label %33
49: ; preds = %33
store i32 0, i32* %6
br label %50
50: ; preds = %55, %49
%51 = load i32, i32* @STDIN_FILENO
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%53 = call i64 @read(i32 %51, i8* %52, i32 1024)
store i64 %53, i64* %8
%54 = icmp sgt i64 %53, 0
br i1 %54, label %55, label %61
55: ; preds = %50
%56 = load i64, i64* %8
%57 = load i32, i32* %6
%58 = sext i32 %57 to i64
%59 = add nsw i64 %58, %56
%60 = trunc i64 %59 to i32
store i32 %60, i32* %6
br label %50
61: ; preds = %50
%62 = load i32*, i32** %10
%63 = load i32, i32* %6
%64 = call i32 (i32*, i8*, i32, ...) @fprintf(i32* %62, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 %63)
%65 = load i32, i32* @EXIT_SUCCESS
%66 = call i32 @exit(i32 %65)
unreachable
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [13 x i8] c"/proc/%s/cwd\00"
@.str.1 = [10 x i8] c"core.info\00"
@.str.2 = [3 x i8] c"w+\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [9 x i8] c"argc=%d\0A\00"
@.str.4 = [15 x i8] c"argc[%d]=<%s>\0A\00"
@STDIN_FILENO = global i32 0
@.str.5 = [30 x i8] c"Total bytes in core dump: %d\0A\00"
@EXIT_SUCCESS = global i32 0
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 = load i32, i32* @PATH_MAX
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = call i32 @snprintf(i8* %7, i32 %5, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = call i32 @chdir(i8* %7)
%12 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %2
%18 = call i32 (i32*, i8*, i32, ...) @fprintf(i32* %12, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %0)
%19 = icmp sgt i32 %0, 0
br i1 %19, label %20, label %30
20: ; preds = %17
%21 = zext i32 %0 to i64
br label %22
22: ; preds = %20, %22
%23 = phi i64 [ 0, %20 ], [ %28, %22 ]
%24 = getelementptr inbounds i8*, i8** %1, i64 %23
%25 = load i8*, i8** %24
%26 = trunc i64 %23 to i32
%27 = call i32 (i32*, i8*, i32, ...) @fprintf(i32* %12, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %26, i8* %25)
%28 = add nuw nsw i64 %23, 1
%29 = icmp eq i64 %28, %21
br i1 %29, label %30, label %22
30: ; preds = %22, %17
br label %31
31: ; preds = %30, %31
%32 = phi i32 [ %37, %31 ], [ 0, %30 ]
%33 = load i32, i32* @STDIN_FILENO
%34 = call i64 @read(i32 %33, i8* %4, i32 1024)
%35 = icmp sgt i64 %34, 0
%36 = trunc i64 %34 to i32
%37 = add i32 %32, %36
br i1 %35, label %31, label %38
38: ; preds = %31
%39 = call i32 (i32*, i8*, i32, ...) @fprintf(i32* %12, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 %32)
%40 = load i32, i32* @EXIT_SUCCESS
%41 = call i32 @exit(i32 %40)
unreachable
}