#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 O_CREAT;
int O_RDWR;
int /*<<< orphan*/ close(int);
int getpid();
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ sscanf(char *, char *, int *);
scalar_t__ strlen(char *);
scalar_t__ write(int, char *, scalar_t__);
int main(int argc, char *argv[]) {
  if (argc < (2 + 1)) {
    printf("test_open <filename> <sleep>\n");
    return 0;
  }
  char *fname = argv[1];
  int sleep_secs;
  int fd;
  sscanf(argv[2], "%d", &sleep_secs);
  printf("my pid = %d sleeping for %d secs\n", getpid(), sleep_secs);
  sleep(sleep_secs);
  fd = open(fname, O_CREAT | O_RDWR, 0755);
  if (fd < 0) {
    perror("open failed");
    return -2;
  }
  if (write(fd, fname, strlen(fname) + 1) < 0) {
    perror("write failed");
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11ca <main+0x3a>
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12a4 <main+0x114>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe39(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1080 <sscanf@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
mov    -0x1c(%rbp),%edx
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x1c(%rbp),%edi
call   10b0 <sleep@plt>
mov    -0x18(%rbp),%rdi
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
mov    0x2dbc(%rip),%rax
or     (%rax),%esi
mov    $0x1ed,%edx
call   1090 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1255 <main+0xc5>
lea    0xdff(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12a4 <main+0x114>
mov    -0x20(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1295 <main+0x105>
lea    0xdbf(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x2,%edi
jg     11b9 <main+0x19>
lea    0xe8f(%rip),%rdi
call   1030 <puts@plt>
jmp    123d <main+0x9d>
mov    0x8(%rsi),%rbx
mov    0x10(%rsi),%rdi
lea    0xe38(%rip),%rsi
lea    0x4(%rsp),%rdx
call   1090 <sscanf@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
mov    0x4(%rsp),%edx
lea    0xe1f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x4(%rsp),%edi
call   10c0 <sleep@plt>
mov    0x2dfb(%rip),%rax
mov    0x2ddc(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x1ed,%edx
call   10a0 <open@plt>
test   %eax,%eax
js     1246 <main+0xa6>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <write@plt>
test   %rax,%rax
js     1259 <main+0xb9>
mov    %ebp,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdd8(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xfffffffe,%eax
jmp    123f <main+0x9f>
lea    0xdd1(%rip),%rdi
call   10b0 <perror@plt>
jmp    1236 <main+0x96>

LLVM IR (-O0):
@.str = [30 x i8] c"test_open <filename> <sleep>\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [34 x i8] c"my pid = %d sleeping for %d secs\0A\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.3 = [12 x i8] c"open failed\00"
@.str.4 = [13 x i8] c"write failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 3
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %48
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
store i8* %16, i8** %6
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 2
%19 = load i8*, i8** %18
%20 = call i32 @sscanf(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %7)
%21 = call i32 (...) @getpid()
%22 = load i32, i32* %7
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %21, i32 %22)
%24 = load i32, i32* %7
%25 = call i32 @sleep(i32 %24)
%26 = load i8*, i8** %6
%27 = load i32, i32* @O_CREAT
%28 = load i32, i32* @O_RDWR
%29 = or i32 %27, %28
%30 = call i32 @open(i8* %26, i32 %29, i32 493)
store i32 %30, i32* %8
%31 = load i32, i32* %8
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %13
%34 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
store i32 -2, i32* %3
br label %48
35: ; preds = %13
%36 = load i32, i32* %8
%37 = load i8*, i8** %6
%38 = load i8*, i8** %6
%39 = call i64 @strlen(i8* %38)
%40 = add nsw i64 %39, 1
%41 = call i64 @write(i32 %36, i8* %37, i64 %40)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %35
%44 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %45
45: ; preds = %43, %35
%46 = load i32, i32* %8
%47 = call i32 @close(i32 %46)
store i32 0, i32* %3
br label %48
48: ; preds = %45, %33, %11
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [34 x i8] c"my pid = %d sleeping for %d secs\0A\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.3 = [12 x i8] c"open failed\00"
@.str.4 = [13 x i8] c"write failed\00"
@str = [29 x i8] c"test_open <filename> <sleep>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = icmp slt i32 %0, 3
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
br label %37
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = bitcast i32* %3 to i8*
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = call i32 @sscanf(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%14 = call i32 (...) @getpid()
%15 = load i32, i32* %3
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %14, i32 %15)
%17 = load i32, i32* %3
%18 = call i32 @sleep(i32 %17)
%19 = load i32, i32* @O_CREAT
%20 = load i32, i32* @O_RDWR
%21 = or i32 %20, %19
%22 = call i32 @open(i8* %9, i32 %21, i32 493)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %7
%25 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %35
26: ; preds = %7
%27 = call i64 @strlen(i8* dereferenceable(1) %9)
%28 = add nsw i64 %27, 1
%29 = call i64 @write(i32 %22, i8* %9, i64 %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %33
33: ; preds = %31, %26
%34 = call i32 @close(i32 %22)
br label %35
35: ; preds = %33, %24
%36 = phi i32 [ -2, %24 ], [ 0, %33 ]
br label %37
37: ; preds = %35, %5
%38 = phi i32 [ 0, %5 ], [ %36, %35 ]
ret i32 %38
}