#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*/ O_CREAT;
int /*<<< orphan*/ O_WRONLY;
int open(char *, int /*<<< orphan*/, ...);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
scalar_t__ strncmp(char *, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int create_file(int connection) {
  int fp;
  char buf[100];
  printf("before reading: connection is %d \n", connection);
  int nbytes = read(connection, buf, 100);
  buf[nbytes] = '\0';
  if (strncmp(buf, "header", 6)) {
    printf("ERROR: Filename creation on server side\n");
    write(connection, "Failed", 6);
    return 0;
  }
  printf("before opening \n");
  fp = open(buf + 7, O_CREAT, 0640);
  fp = open(buf + 7, O_WRONLY);
  if (!fp) {
    printf("ERROR: Filename creation on server side\n");
    write(connection, "Failed", 6);
    return 0;
  }
  printf("File created successfully\n");
  write(connection, "Success", 7);
  return fp;
}


x86 assembly (clang, -O0):
<create_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe98(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x64,%edx
call   1060 <read@plt>
mov    %eax,-0x74(%rbp)
movslq -0x74(%rbp),%rax
movb   $0x0,-0x70(%rbp,%rax,1)
lea    -0x70(%rbp),%rdi
lea    0xe8c(%rip),%rsi
mov    $0x6,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     11d9 <create_file+0x89>
lea    0xe78(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
lea    0xe90(%rip),%rsi
mov    $0x6,%edx
call   1040 <write@plt>
movl   $0x0,-0x4(%rbp)
jmp    1282 <create_file+0x132>
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x70(%rbp),%rdi
add    $0x7,%rdi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
mov    $0x1a0,%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
lea    -0x70(%rbp),%rdi
add    $0x7,%rdi
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    125a <create_file+0x10a>
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
lea    0xe0f(%rip),%rsi
mov    $0x6,%edx
call   1040 <write@plt>
movl   $0x0,-0x4(%rbp)
jmp    1282 <create_file+0x132>
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
lea    0xe14(%rip),%rsi
mov    $0x7,%edx
call   1040 <write@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x68,%rsp
mov    %edi,%ebx
lea    0xe8d(%rip),%rdi
xor    %r15d,%r15d
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rsp,%rbp
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x64,%edx
call   1070 <read@plt>
cltq
movb   $0x0,(%rsp,%rax,1)
lea    0xe85(%rip),%rsi
mov    %rbp,%rdi
mov    $0x6,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     11d1 <create_file+0x71>
lea    0xeac(%rip),%rdi
call   1040 <puts@plt>
lea    0xe67(%rip),%rsi
mov    %ebx,%edi
mov    $0x6,%edx
call   1050 <write@plt>
jmp    1239 <create_file+0xd9>
lea    0xe61(%rip),%rdi
call   1040 <puts@plt>
lea    0x7(%rsp),%rbp
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
xor    %r15d,%r15d
mov    %rbp,%rdi
mov    $0x1a0,%edx
xor    %eax,%eax
call   1080 <open@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
xor    %eax,%eax
call   1080 <open@plt>
test   %eax,%eax
je     11b0 <create_file+0x50>
mov    %eax,%r14d
lea    0xe2b(%rip),%rdi
call   1040 <puts@plt>
lea    0xe07(%rip),%rsi
mov    %ebx,%edi
mov    $0x7,%edx
call   1050 <write@plt>
mov    %r14d,%r15d
mov    %r15d,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [35 x i8] c"before reading: connection is %d \0A\00"
@.str.1 = [7 x i8] c"header\00"
@.str.2 = [41 x i8] c"ERROR: Filename creation on server side\0A\00"
@.str.3 = [7 x i8] c"Failed\00"
@.str.4 = [17 x i8] c"before opening \0A\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str.5 = [27 x i8] c"File created successfully\0A\00"
@.str.6 = [8 x i8] c"Success\00"
define i32 @create_file(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [100 x i8]
%6 = alloca i32
store i32 %0, i32* %3
%7 = load i32, i32* %3
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = load i32, i32* %3
%10 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%11 = call i32 @read(i32 %9, i8* %10, i32 100)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 %13
store i8 0, i8* %14
%15 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%16 = call i64 @strncmp(i8* %15, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 6)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %1
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
%20 = load i32, i32* %3
%21 = call i32 @write(i32 %20, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 6)
store i32 0, i32* %2
br label %43
22: ; preds = %1
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%24 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%25 = getelementptr inbounds i8, i8* %24, i64 7
%26 = load i32, i32* @O_CREAT
%27 = call i32 (i8*, i32, ...) @open(i8* %25, i32 %26, i32 416)
store i32 %27, i32* %4
%28 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%29 = getelementptr inbounds i8, i8* %28, i64 7
%30 = load i32, i32* @O_WRONLY
%31 = call i32 (i8*, i32, ...) @open(i8* %29, i32 %30)
store i32 %31, i32* %4
%32 = load i32, i32* %4
%33 = icmp ne i32 %32, 0
br i1 %33, label %38, label %34
34: ; preds = %22
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
%36 = load i32, i32* %3
%37 = call i32 @write(i32 %36, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 6)
store i32 0, i32* %2
br label %43
38: ; preds = %22
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
%40 = load i32, i32* %3
%41 = call i32 @write(i32 %40, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i32 7)
%42 = load i32, i32* %4
store i32 %42, i32* %2
br label %43
43: ; preds = %38, %34, %18
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
@.str = [35 x i8] c"before reading: connection is %d \0A\00"
@.str.1 = [7 x i8] c"header\00"
@.str.3 = [7 x i8] c"Failed\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str.6 = [8 x i8] c"Success\00"
@str = [16 x i8] c"before opening \00"
@str.8 = [26 x i8] c"File created successfully\00"
@str.9 = [40 x i8] c"ERROR: Filename creation on server side\00"
define i32 @create_file(i32 %0) {
%2 = alloca [100 x i8]
%3 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = call i32 @read(i32 %0, i8* %3, i32 100)
%6 = sext i32 %5 to i64
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 %6
store i8 0, i8* %7
%8 = call i64 @strncmp(i8* %3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 6)
%9 = icmp eq i64 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %1
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str.9, i64 0, i64 0))
%12 = call i32 @write(i32 %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 6)
br label %27
13: ; preds = %1
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%15 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 7
%16 = load i32, i32* @O_CREAT
%17 = call i32 (i8*, i32, ...) @open(i8* %15, i32 %16, i32 416)
%18 = load i32, i32* @O_WRONLY
%19 = call i32 (i8*, i32, ...) @open(i8* %15, i32 %18)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %13
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str.9, i64 0, i64 0))
%23 = call i32 @write(i32 %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 6)
br label %27
24: ; preds = %13
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.8, i64 0, i64 0))
%26 = call i32 @write(i32 %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i32 7)
br label %27
27: ; preds = %24, %21, %10
%28 = phi i32 [ 0, %10 ], [ %19, %24 ], [ 0, %21 ]
ret i32 %28
}