#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*/ time_t;
struct tm {
  int dummy;
};

/* Variables and functions */
int O_CREAT;
int O_WRONLY;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
struct tm *localtime(int /*<<< orphan*/ *);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ strftime(char *, int, char *, struct tm *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  int fd;
  int bytes_read;
  char buff[8192];
  char fname[256];
  time_t now;
  struct tm datetime;
  now = time(NULL);
  datetime = *(localtime(&now));
  strftime(buff, 8192, "%Y%m%d%H%M%S", &datetime);
  sprintf(fname, "/home/dp130708/capture/%s.raw", buff);
  if ((fd = open(fname, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR)) == -1) {
    perror("Failed to open file for writing");
    return (1);
  }
  while ((bytes_read = read(0, buff, 8192))) {
    write(fd, buff, bytes_read);
  }
  close(fd);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2130,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1070 <time@plt>
mov    %eax,-0x2124(%rbp)
lea    -0x2124(%rbp),%rdi
call   1030 <localtime@plt>
mov    (%rax),%eax
mov    %eax,-0x2128(%rbp)
lea    -0x2020(%rbp),%rdi
mov    $0x2000,%esi
lea    0xe21(%rip),%rdx
lea    -0x2128(%rbp),%rcx
call   1080 <strftime@plt>
lea    -0x2120(%rbp),%rdi
lea    -0x2020(%rbp),%rdx
lea    0xe0d(%rip),%rsi
call   10b0 <sprintf@plt>
lea    -0x2120(%rbp),%rdi
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
mov    0x2da4(%rip),%rax
or     (%rax),%esi
mov    0x2dab(%rip),%rax
mov    (%rax),%edx
mov    0x2db2(%rip),%rax
or     (%rax),%edx
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1259 <main+0xc9>
lea    0xde3(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12a3 <main+0x113>
jmp    125e <main+0xce>
lea    -0x2020(%rbp),%rsi
xor    %edi,%edi
mov    $0x2000,%edx
call   1060 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
je     1294 <main+0x104>
mov    -0x14(%rbp),%edi
lea    -0x2020(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1040 <write@plt>
jmp    125e <main+0xce>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x2110,%rsp
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,0xc(%rsp)
lea    0xc(%rsp),%rdi
call   1030 <localtime@plt>
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0xe43(%rip),%rdx
lea    0x110(%rsp),%rbx
lea    0x8(%rsp),%rcx
mov    %rbx,%rdi
mov    $0x2000,%esi
call   1080 <strftime@plt>
lea    0xe2f(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    %rbx,%rdx
call   10b0 <sprintf@plt>
mov    0x2e03(%rip),%rax
mov    0x2dc4(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dc9(%rip),%rax
mov    0x2dd2(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbp,%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1285 <main+0xf5>
mov    %eax,%ebp
xor    %r14d,%r14d
lea    0x110(%rsp),%rsi
xor    %edi,%edi
mov    $0x2000,%edx
call   1060 <read@plt>
test   %eax,%eax
je     126f <main+0xdf>
lea    0x110(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x2000,%edx
call   1060 <read@plt>
test   %eax,%eax
jne    1250 <main+0xc0>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r14d,%eax
add    $0x2110,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd9f(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%r14d
jmp    1276 <main+0xe6>

LLVM IR (-O0):
%struct.tm = type { i32 }
@.str = [13 x i8] c"%Y%m%d%H%M%S\00"
@.str.1 = [30 x i8] c"/home/dp130708/capture/%s.raw\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.2 = [32 x i8] c"Failed to open file for writing\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 [8192 x i8]
%9 = alloca [256 x i8]
%10 = alloca i32
%11 = alloca %struct.tm
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = call i32 @time(i32* null)
store i32 %12, i32* %10
%13 = call %struct.tm* @localtime(i32* %10)
%14 = bitcast %struct.tm* %11 to i8*
%15 = bitcast %struct.tm* %13 to i8*
%16 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%17 = call i32 @strftime(i8* %16, i32 8192, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), %struct.tm* %11)
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%19 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%20 = call i32 @sprintf(i8* %18, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%22 = load i32, i32* @O_CREAT
%23 = load i32, i32* @O_WRONLY
%24 = or i32 %22, %23
%25 = load i32, i32* @S_IRUSR
%26 = load i32, i32* @S_IWUSR
%27 = or i32 %25, %26
%28 = call i32 @open(i8* %21, i32 %24, i32 %27)
store i32 %28, i32* %6
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %2
%31 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %45
32: ; preds = %2
br label %33
33: ; preds = %37, %32
%34 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%35 = call i32 @read(i32 0, i8* %34, i32 8192)
store i32 %35, i32* %7
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %33
%38 = load i32, i32* %6
%39 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%40 = load i32, i32* %7
%41 = call i32 @write(i32 %38, i8* %39, i32 %40)
br label %33
42: ; preds = %33
%43 = load i32, i32* %6
%44 = call i32 @close(i32 %43)
store i32 0, i32* %3
br label %45
45: ; preds = %42, %30
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
%struct.tm = type { i32 }
@.str = [13 x i8] c"%Y%m%d%H%M%S\00"
@.str.1 = [30 x i8] c"/home/dp130708/capture/%s.raw\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.2 = [32 x i8] c"Failed to open file for writing\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [8192 x i8]
%4 = alloca [256 x i8]
%5 = alloca i32
%6 = alloca %struct.tm
%7 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%9 = bitcast i32* %5 to i8*
%10 = bitcast %struct.tm* %6 to i8*
%11 = tail call i32 @time(i32* null)
store i32 %11, i32* %5
%12 = call %struct.tm* @localtime(i32* %5)
%13 = getelementptr %struct.tm, %struct.tm* %12, i64 0, i32 0
%14 = getelementptr inbounds %struct.tm, %struct.tm* %6, i64 0, i32 0
%15 = load i32, i32* %13
store i32 %15, i32* %14
%16 = call i32 @strftime(i8* %7, i32 8192, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), %struct.tm* %6)
%17 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %7)
%18 = load i32, i32* @O_CREAT
%19 = load i32, i32* @O_WRONLY
%20 = or i32 %19, %18
%21 = load i32, i32* @S_IRUSR
%22 = load i32, i32* @S_IWUSR
%23 = or i32 %22, %21
%24 = call i32 @open(i8* %8, i32 %20, i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %29, label %26
26: ; preds = %2
%27 = call i32 @read(i32 0, i8* %7, i32 8192)
%28 = icmp eq i32 %27, 0
br i1 %28, label %36, label %31
29: ; preds = %2
%30 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
br label %38
31: ; preds = %26, %31
%32 = phi i32 [ %34, %31 ], [ %27, %26 ]
%33 = call i32 @write(i32 %24, i8* %7, i32 %32)
%34 = call i32 @read(i32 0, i8* %7, i32 8192)
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %31
36: ; preds = %31, %26
%37 = call i32 @close(i32 %24)
br label %38
38: ; preds = %36, %29
%39 = phi i32 [ 1, %29 ], [ 0, %36 ]
ret i32 %39
}