#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_RDWR;
int /*<<< orphan*/ O_WRONLY;
char close(int);
int /*<<< orphan*/ getchar();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int snprintf(char *, int, char *, int);
int write(int, char *, int);
int main() {
  int fd, bdev_fd, count, ret;
  char num_buf[156] = {0};
  char temp;
  fd = open("/dev/sdb", O_RDWR);
  if (fd < 0) {
    printf("Open /dev/sdb failed, %d.\n", fd);
    goto out1;
  }
  count = snprintf(num_buf, 156 - 1, "%d", fd);
  if (count < 0) {
    printf("failed to write num_buf\n");
    goto out2;
  }
  bdev_fd = open("/sys/kernel/config/kafka/bdev", O_WRONLY);
  if (bdev_fd < 0) {
    printf("write bdev failed, %d\n", bdev_fd);
    goto out3;
  }
  ret = write(bdev_fd, num_buf, count + 1);
  if (ret - 1 != count) {
    printf("failed to write %d : %s to bdev, because count %d != ret %d", fd,
           num_buf, count, ret);
  }
  getchar();
out3:
  printf("in out3\n");
  close(bdev_fd);
  getchar();
out2:
  printf("in out2\n");
  temp = close(fd);
  getchar();
out1:
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xb0(%rbp),%rdi
xor    %esi,%esi
mov    $0x9c,%edx
call   1060 <memset@plt>
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
lea    0xe5b(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11cd <main+0x5d>
mov    -0x8(%rbp),%esi
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12cd <main+0x15d>
lea    -0xb0(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x9b,%esi
lea    0xec8(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1208 <main+0x98>
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12aa <main+0x13a>
mov    0x2dc1(%rip),%rax
mov    (%rax),%esi
lea    0xe25(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1240 <main+0xd0>
mov    -0xc(%rbp),%esi
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    128d <main+0x11d>
mov    -0xc(%rbp),%edi
lea    -0xb0(%rbp),%rsi
mov    -0x10(%rbp),%edx
add    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
cmp    -0x10(%rbp),%eax
je     1286 <main+0x116>
mov    -0x8(%rbp),%esi
lea    -0xb0(%rbp),%rdx
mov    -0x10(%rbp),%ecx
mov    -0x14(%rbp),%r8d
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1080 <getchar@plt>
lea    0xe1a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    $0x0,%al
call   1080 <getchar@plt>
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    %al,-0xb1(%rbp)
mov    $0x0,%al
call   1080 <getchar@plt>
mov    $0x1,%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0xa0,%rsp
xorps  %xmm0,%xmm0
movups %xmm0,0x8c(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
lea    0xe3b(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     123e <main+0xce>
lea    0xebb(%rip),%rdx
mov    %rsp,%rdi
mov    $0x9b,%esi
mov    %ebx,%ecx
call   1060 <snprintf@plt>
test   %eax,%eax
js     1250 <main+0xe0>
mov    %eax,%ebp
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
lea    0xe28(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%r14d
test   %eax,%eax
js     125e <main+0xee>
lea    0x1(%rbp),%edx
mov    %rsp,%rsi
mov    %r14d,%edi
call   1040 <write@plt>
lea    -0x1(%rax),%ecx
cmp    %ebp,%ecx
je     1235 <main+0xc5>
lea    0xe35(%rip),%rdi
mov    %rsp,%rdx
mov    %ebx,%esi
mov    %ebp,%ecx
mov    %eax,%r8d
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1080 <getchar@plt>
jmp    126f <main+0xff>
lea    0xdc4(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12a4 <main+0x134>
lea    0xe4e(%rip),%rdi
call   1030 <puts@plt>
jmp    128a <main+0x11a>
lea    0xddd(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe1f(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
call   1070 <close@plt>
xor    %eax,%eax
call   1080 <getchar@plt>
lea    0xe0c(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
call   1080 <getchar@plt>
mov    $0x1,%eax
add    $0xa0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/sdb\00"
@O_RDWR = global i32 0
@.str.1 = [27 x i8] c"Open /dev/sdb failed, %d.\0A\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [25 x i8] c"failed to write num_buf\0A\00"
@.str.4 = [30 x i8] c"/sys/kernel/config/kafka/bdev\00"
@O_WRONLY = global i32 0
@.str.5 = [23 x i8] c"write bdev failed, %d\0A\00"
@.str.6 = [60 x i8] c"failed to write %d : %s to bdev, because count %d != ret %d\00"
@.str.7 = [9 x i8] c"in out3\0A\00"
@.str.8 = [9 x i8] c"in out2\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [156 x i8]
%7 = alloca i8
store i32 0, i32* %1
%8 = bitcast [156 x i8]* %6 to i8*
%9 = load i32, i32* @O_RDWR
%10 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = load i32, i32* %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %14)
br label %60
16: ; preds = %0
%17 = getelementptr inbounds [156 x i8], [156 x i8]* %6, i64 0, i64 0
%18 = load i32, i32* %2
%19 = call i32 @snprintf(i8* %17, i32 155, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %18)
store i32 %19, i32* %4
%20 = load i32, i32* %4
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %55
24: ; preds = %16
%25 = load i32, i32* @O_WRONLY
%26 = call i32 @open(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 %25)
store i32 %26, i32* %3
%27 = load i32, i32* %3
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = load i32, i32* %3
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 %30)
br label %50
32: ; preds = %24
%33 = load i32, i32* %3
%34 = getelementptr inbounds [156 x i8], [156 x i8]* %6, i64 0, i64 0
%35 = load i32, i32* %4
%36 = add nsw i32 %35, 1
%37 = call i32 @write(i32 %33, i8* %34, i32 %36)
store i32 %37, i32* %5
%38 = load i32, i32* %5
%39 = sub nsw i32 %38, 1
%40 = load i32, i32* %4
%41 = icmp ne i32 %39, %40
br i1 %41, label %42, label %48
42: ; preds = %32
%43 = load i32, i32* %2
%44 = getelementptr inbounds [156 x i8], [156 x i8]* %6, i64 0, i64 0
%45 = load i32, i32* %4
%46 = load i32, i32* %5
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.6, i64 0, i64 0), i32 %43, i8* %44, i32 %45, i32 %46)
br label %48
48: ; preds = %42, %32
%49 = call i32 (...) @getchar()
br label %50
50: ; preds = %48, %29
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0))
%52 = load i32, i32* %3
%53 = call signext i8 @close(i32 %52)
%54 = call i32 (...) @getchar()
br label %55
55: ; preds = %50, %22
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i64 0, i64 0))
%57 = load i32, i32* %2
%58 = call signext i8 @close(i32 %57)
store i8 %58, i8* %7
%59 = call i32 (...) @getchar()
br label %60
60: ; preds = %55, %13
ret i32 1
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/sdb\00"
@O_RDWR = global i32 0
@.str.1 = [27 x i8] c"Open /dev/sdb failed, %d.\0A\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.4 = [30 x i8] c"/sys/kernel/config/kafka/bdev\00"
@O_WRONLY = global i32 0
@.str.5 = [23 x i8] c"write bdev failed, %d\0A\00"
@.str.6 = [60 x i8] c"failed to write %d : %s to bdev, because count %d != ret %d\00"
@str = [8 x i8] c"in out3\00"
@str.9 = [8 x i8] c"in out2\00"
@str.10 = [24 x i8] c"failed to write num_buf\00"
define i32 @main() {
%1 = alloca [156 x i8]
%2 = getelementptr inbounds [156 x i8], [156 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %4)
br label %36
8: ; preds = %0
%9 = call i32 @snprintf(i8* %2, i32 155, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %4)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.10, i64 0, i64 0))
br label %32
13: ; preds = %8
%14 = load i32, i32* @O_WRONLY
%15 = call i32 @open(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 %15)
br label %28
19: ; preds = %13
%20 = add nuw nsw i32 %9, 1
%21 = call i32 @write(i32 %15, i8* %2, i32 %20)
%22 = add nsw i32 %21, -1
%23 = icmp eq i32 %22, %9
br i1 %23, label %26, label %24
24: ; preds = %19
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([60 x i8], [60 x i8]* @.str.6, i64 0, i64 0), i32 %4, i8* %2, i32 %9, i32 %21)
br label %26
26: ; preds = %24, %19
%27 = call i32 (...) @getchar()
br label %28
28: ; preds = %26, %17
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
%30 = call signext i8 @close(i32 %15)
%31 = call i32 (...) @getchar()
br label %32
32: ; preds = %28, %11
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str.9, i64 0, i64 0))
%34 = call signext i8 @close(i32 %4)
%35 = call i32 (...) @getchar()
br label %36
36: ; preds = %32, %6
ret i32 1
}